/*
 *  ofOscMessage.cpp
 *  openFrameworks OSC addon
 *
 *  damian@frey.co.nz
 *
 */

#include "ofOscMessage.h"
#include <iostream>
#include <assert.h>

ofOscMessage::ofOscMessage()
{
}

ofOscMessage::~ofOscMessage()
{
	for ( unsigned int i=0; i<args.size(); ++i )
		delete args[i];
}



/* 

get methods 

*/

int ofOscMessage::getNumArgs() const
{
	return (int)args.size();
}

ofOscArgType ofOscMessage::getArgType( int index ) const
{
	assert( index < (int)args.size() && "index out of bounds" );
	return args[index]->getType();
}

const char* ofOscMessage::getArgTypeName( int index ) const
{
	assert( index >=0 && index < (int)args.size() );
	return args[index]->getTypeName();		
}


int32_t ofOscMessage::getArgAsInt32( int index ) const
{ 
	assert( index >=0 && index < (int)args.size() );
	assert( getArgType(index) == OFOSC_TYPE_INT32 && "wrong argument type" );
	return ((ofOscArgInt32*)args[index])->get(); 
}


float ofOscMessage::getArgAsFloat( int index ) const
{ 
	assert( index >=0 && index < (int)args.size() );
	assert( getArgType(index) == OFOSC_TYPE_FLOAT && "wrong argument type" );
	return ((ofOscArgFloat*)args[index])->get(); 
}


const char* ofOscMessage::getArgAsString( int index ) const
{ 
	assert( index >=0 && index < (int)args.size() );
	assert( getArgType(index) == OFOSC_TYPE_STRING && "wrong argument type" );
	return ((ofOscArgString*)args[index])->get(); 
}



/* 

set methods 

*/


void ofOscMessage::addIntArg( int32_t argument )
{
	
	args.push_back( new ofOscArgInt32( argument ) );
}

void ofOscMessage::addFloatArg( float argument )
{
	args.push_back( new ofOscArgFloat( argument ) );
}

void ofOscMessage::addStringArg( const char* argument )
{
	args.push_back( new ofOscArgString( argument ) );
}


/*
 
 housekeeping
 
 */

ofOscMessage& ofOscMessage::copy( const ofOscMessage& other )
{
	// copy address
	address = other.address;

	// copy arguments
	for ( int i=0; i<(int)other.args.size(); ++i )
	{
		ofOscArgType argType = other.getArgType( i );
		if ( argType == OFOSC_TYPE_INT32 )
			args.push_back( new ofOscArgInt32( other.getArgAsInt32( i ) ) );
		else if ( argType == OFOSC_TYPE_FLOAT )
			args.push_back( new ofOscArgFloat( other.getArgAsFloat( i ) ) );
		else if ( argType == OFOSC_TYPE_STRING )
			args.push_back( new ofOscArgString( other.getArgAsString( i ) ) );
		else
		{
			assert( false && "bad argument type" );
		}
	}
	
	return *this;
}
