comparison addons/ofxOsc/src/ofxOscMessage.cpp @ 0:b299a65a3ad0

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