annotate addons/ofxOsc/src/ofxOscMessage.cpp @ 52:13194a9dca77 tip

Added exporting of image and text data
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Tue, 17 Jul 2012 22:13:10 +0100
parents b299a65a3ad0
children
rev   line source
andrew@0 1 /*
andrew@0 2
andrew@0 3 Copyright (c) 2007-2009, Damian Stewart
andrew@0 4 All rights reserved.
andrew@0 5
andrew@0 6 Redistribution and use in source and binary forms, with or without
andrew@0 7 modification, are permitted provided that the following conditions are met:
andrew@0 8 * Redistributions of source code must retain the above copyright
andrew@0 9 notice, this list of conditions and the following disclaimer.
andrew@0 10 * Redistributions in binary form must reproduce the above copyright
andrew@0 11 notice, this list of conditions and the following disclaimer in the
andrew@0 12 documentation and/or other materials provided with the distribution.
andrew@0 13 * Neither the name of the developer nor the
andrew@0 14 names of its contributors may be used to endorse or promote products
andrew@0 15 derived from this software without specific prior written permission.
andrew@0 16
andrew@0 17 THIS SOFTWARE IS PROVIDED BY DAMIAN STEWART ''AS IS'' AND ANY
andrew@0 18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
andrew@0 19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
andrew@0 20 DISCLAIMED. IN NO EVENT SHALL DAMIAN STEWART BE LIABLE FOR ANY
andrew@0 21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
andrew@0 22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
andrew@0 23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
andrew@0 24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
andrew@0 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
andrew@0 26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
andrew@0 27 */
andrew@0 28
andrew@0 29 #include "ofxOscMessage.h"
andrew@0 30 #include <iostream>
andrew@0 31 #include <assert.h>
andrew@0 32
andrew@0 33 ofxOscMessage::ofxOscMessage()
andrew@0 34
andrew@0 35 {
andrew@0 36 }
andrew@0 37
andrew@0 38 ofxOscMessage::~ofxOscMessage()
andrew@0 39 {
andrew@0 40 clear();
andrew@0 41 }
andrew@0 42
andrew@0 43 void ofxOscMessage::clear()
andrew@0 44 {
andrew@0 45 for ( unsigned int i=0; i<args.size(); ++i )
andrew@0 46 delete args[i];
andrew@0 47 args.clear();
andrew@0 48 address = "";
andrew@0 49 }
andrew@0 50
andrew@0 51 /*
andrew@0 52
andrew@0 53 get methods
andrew@0 54
andrew@0 55 */
andrew@0 56
andrew@0 57 int ofxOscMessage::getNumArgs() const
andrew@0 58 {
andrew@0 59 return (int)args.size();
andrew@0 60 }
andrew@0 61
andrew@0 62 ofxOscArgType ofxOscMessage::getArgType( int index ) const
andrew@0 63 {
andrew@0 64 if ( index >= (int)args.size() )
andrew@0 65 {
andrew@0 66 fprintf(stderr,"ofxOscMessage::getArgType: index %d out of bounds\n", index );
andrew@0 67 return OFXOSC_TYPE_INDEXOUTOFBOUNDS;
andrew@0 68 }
andrew@0 69 else
andrew@0 70 return args[index]->getType();
andrew@0 71 }
andrew@0 72
andrew@0 73 string ofxOscMessage::getArgTypeName( int index ) const
andrew@0 74 {
andrew@0 75 if ( index >= (int)args.size() )
andrew@0 76 {
andrew@0 77 fprintf(stderr,"ofxOscMessage::getArgTypeName: index %d out of bounds\n", index );
andrew@0 78 return "INDEX OUT OF BOUNDS";
andrew@0 79 }
andrew@0 80 else
andrew@0 81 return args[index]->getTypeName();
andrew@0 82 }
andrew@0 83
andrew@0 84
andrew@0 85 int32_t ofxOscMessage::getArgAsInt32( int index ) const
andrew@0 86 {
andrew@0 87 if ( getArgType(index) != OFXOSC_TYPE_INT32 )
andrew@0 88 {
andrew@0 89 if ( getArgType( index ) == OFXOSC_TYPE_FLOAT )
andrew@0 90 {
andrew@0 91 fprintf(stderr, "ofxOscMessage:getArgAsInt32: warning: converting int32 to float for argument %i\n", index );
andrew@0 92 return ((ofxOscArgFloat*)args[index])->get();
andrew@0 93 }
andrew@0 94 else
andrew@0 95 {
andrew@0 96 fprintf(stderr, "ofxOscMessage:getArgAsInt32: error: argument %i is not a number\n", index );
andrew@0 97 return 0;
andrew@0 98 }
andrew@0 99 }
andrew@0 100 else
andrew@0 101 return ((ofxOscArgInt32*)args[index])->get();
andrew@0 102 }
andrew@0 103
andrew@0 104
andrew@0 105 float ofxOscMessage::getArgAsFloat( int index ) const
andrew@0 106 {
andrew@0 107 if ( getArgType(index) != OFXOSC_TYPE_FLOAT )
andrew@0 108 {
andrew@0 109 if ( getArgType( index ) == OFXOSC_TYPE_INT32 )
andrew@0 110 {
andrew@0 111 fprintf(stderr, "ofxOscMessage:getArgAsFloat: warning: converting float to int32 for argument %i\n", index );
andrew@0 112 return ((ofxOscArgInt32*)args[index])->get();
andrew@0 113 }
andrew@0 114 else
andrew@0 115 {
andrew@0 116 fprintf(stderr, "ofxOscMessage:getArgAsFloat: error: argument %i is not a number\n", index );
andrew@0 117 return 0;
andrew@0 118 }
andrew@0 119 }
andrew@0 120 else
andrew@0 121 return ((ofxOscArgFloat*)args[index])->get();
andrew@0 122 }
andrew@0 123
andrew@0 124
andrew@0 125 string ofxOscMessage::getArgAsString( int index ) const
andrew@0 126 {
andrew@0 127 if ( getArgType(index) != OFXOSC_TYPE_STRING )
andrew@0 128 {
andrew@0 129 if ( getArgType( index ) == OFXOSC_TYPE_FLOAT )
andrew@0 130 {
andrew@0 131 char buf[1024];
andrew@0 132 sprintf(buf,"%f",((ofxOscArgFloat*)args[index])->get() );
andrew@0 133 fprintf(stderr, "ofxOscMessage:getArgAsString: warning: converting float to string for argument %i\n", index );
andrew@0 134 return buf;
andrew@0 135 }
andrew@0 136 else if ( getArgType( index ) == OFXOSC_TYPE_INT32 )
andrew@0 137 {
andrew@0 138 char buf[1024];
andrew@0 139 sprintf(buf,"%i",((ofxOscArgInt32*)args[index])->get() );
andrew@0 140 fprintf(stderr, "ofxOscMessage:getArgAsString: warning: converting int32 to string for argument %i\n", index );
andrew@0 141 return buf;
andrew@0 142 }
andrew@0 143 else
andrew@0 144 {
andrew@0 145 fprintf(stderr, "ofxOscMessage:getArgAsString: error: argument %i is not a string\n", index );
andrew@0 146 return "";
andrew@0 147 }
andrew@0 148 }
andrew@0 149 else
andrew@0 150 return ((ofxOscArgString*)args[index])->get();
andrew@0 151 }
andrew@0 152
andrew@0 153
andrew@0 154
andrew@0 155 /*
andrew@0 156
andrew@0 157 set methods
andrew@0 158
andrew@0 159 */
andrew@0 160
andrew@0 161
andrew@0 162 void ofxOscMessage::addIntArg( int32_t argument )
andrew@0 163 {
andrew@0 164
andrew@0 165 args.push_back( new ofxOscArgInt32( argument ) );
andrew@0 166 }
andrew@0 167
andrew@0 168 void ofxOscMessage::addFloatArg( float argument )
andrew@0 169 {
andrew@0 170 args.push_back( new ofxOscArgFloat( argument ) );
andrew@0 171 }
andrew@0 172
andrew@0 173 void ofxOscMessage::addStringArg( string argument )
andrew@0 174 {
andrew@0 175 args.push_back( new ofxOscArgString( argument ) );
andrew@0 176 }
andrew@0 177
andrew@0 178
andrew@0 179 /*
andrew@0 180
andrew@0 181 housekeeping
andrew@0 182
andrew@0 183 */
andrew@0 184
andrew@0 185 ofxOscMessage& ofxOscMessage::copy( const ofxOscMessage& other )
andrew@0 186 {
andrew@0 187 // copy address
andrew@0 188 address = other.address;
andrew@0 189
andrew@0 190 remote_host = other.remote_host;
andrew@0 191 remote_port = other.remote_port;
andrew@0 192
andrew@0 193 // copy arguments
andrew@0 194 for ( int i=0; i<(int)other.args.size(); ++i )
andrew@0 195 {
andrew@0 196 ofxOscArgType argType = other.getArgType( i );
andrew@0 197 if ( argType == OFXOSC_TYPE_INT32 )
andrew@0 198 args.push_back( new ofxOscArgInt32( other.getArgAsInt32( i ) ) );
andrew@0 199 else if ( argType == OFXOSC_TYPE_FLOAT )
andrew@0 200 args.push_back( new ofxOscArgFloat( other.getArgAsFloat( i ) ) );
andrew@0 201 else if ( argType == OFXOSC_TYPE_STRING )
andrew@0 202 args.push_back( new ofxOscArgString( other.getArgAsString( i ) ) );
andrew@0 203 else
andrew@0 204 {
andrew@0 205 assert( false && "bad argument type" );
andrew@0 206 }
andrew@0 207 }
andrew@0 208
andrew@0 209 return *this;
andrew@0 210 }