1 /* |
1 /* |
2 oscpack -- Open Sound Control packet manipulation library |
2 oscpack -- Open Sound Control packet manipulation library |
3 http://www.audiomulch.com/~rossb/oscpack |
3 http://www.audiomulch.com/~rossb/oscpack |
4 |
4 |
5 Copyright (c) 2004-2005 Ross Bencina <rossb@audiomulch.com> |
5 Copyright (c) 2004-2005 Ross Bencina <rossb@audiomulch.com> |
6 |
6 |
7 Permission is hereby granted, free of charge, to any person obtaining |
7 Permission is hereby granted, free of charge, to any person obtaining |
8 a copy of this software and associated documentation files |
8 a copy of this software and associated documentation files |
9 (the "Software"), to deal in the Software without restriction, |
9 (the "Software"), to deal in the Software without restriction, |
10 including without limitation the rights to use, copy, modify, merge, |
10 including without limitation the rights to use, copy, modify, merge, |
11 publish, distribute, sublicense, and/or sell copies of the Software, |
11 publish, distribute, sublicense, and/or sell copies of the Software, |
12 and to permit persons to whom the Software is furnished to do so, |
12 and to permit persons to whom the Software is furnished to do so, |
13 subject to the following conditions: |
13 subject to the following conditions: |
14 |
14 |
15 The above copyright notice and this permission notice shall be |
15 The above copyright notice and this permission notice shall be |
16 included in all copies or substantial portions of the Software. |
16 included in all copies or substantial portions of the Software. |
17 |
17 |
18 Any person wishing to distribute modifications to the Software is |
18 Any person wishing to distribute modifications to the Software is |
19 requested to send the modifications to the original developer so that |
19 requested to send the modifications to the original developer so that |
20 they can be incorporated into the canonical version. |
20 they can be incorporated into the canonical version. |
21 |
21 |
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
25 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
25 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
29 */ |
29 */ |
30 #include "OscReceivedElements.h" |
30 #include "OscReceivedElements.h" |
31 |
31 |
32 #include <cassert> |
32 #include <cassert> |
33 |
33 |
198 |
198 |
199 bool ReceivedMessageArgument::AsBool() const |
199 bool ReceivedMessageArgument::AsBool() const |
200 { |
200 { |
201 if( !typeTag_ ) |
201 if( !typeTag_ ) |
202 throw MissingArgumentException(); |
202 throw MissingArgumentException(); |
203 else if( *typeTag_ == TRUE_TYPE_TAG ) |
203 else if( *typeTag_ == TRUE_TYPE_TAG ) |
204 return true; |
204 return true; |
205 else if( *typeTag_ == FALSE_TYPE_TAG ) |
205 else if( *typeTag_ == FALSE_TYPE_TAG ) |
206 return false; |
206 return false; |
207 else |
207 else |
208 throw WrongArgumentTypeException(); |
208 throw WrongArgumentTypeException(); |
209 } |
209 } |
210 |
210 |
211 |
211 |
212 bool ReceivedMessageArgument::AsBoolUnchecked() const |
212 bool ReceivedMessageArgument::AsBoolUnchecked() const |
213 { |
213 { |
214 if( !typeTag_ ) |
214 if( !typeTag_ ) |
215 throw MissingArgumentException(); |
215 throw MissingArgumentException(); |
216 else if( *typeTag_ == TRUE_TYPE_TAG ) |
216 else if( *typeTag_ == TRUE_TYPE_TAG ) |
217 return true; |
217 return true; |
218 else |
218 else |
219 return false; |
219 return false; |
220 } |
220 } |
221 |
221 |
222 |
222 |
223 int32 ReceivedMessageArgument::AsInt32() const |
223 int32 ReceivedMessageArgument::AsInt32() const |
224 { |
224 { |
225 if( !typeTag_ ) |
225 if( !typeTag_ ) |
226 throw MissingArgumentException(); |
226 throw MissingArgumentException(); |
227 else if( *typeTag_ == INT32_TYPE_TAG ) |
227 else if( *typeTag_ == INT32_TYPE_TAG ) |
228 return AsInt32Unchecked(); |
228 return AsInt32Unchecked(); |
229 else |
229 else |
230 throw WrongArgumentTypeException(); |
230 throw WrongArgumentTypeException(); |
231 } |
231 } |
232 |
232 |
233 |
233 |
234 int32 ReceivedMessageArgument::AsInt32Unchecked() const |
234 int32 ReceivedMessageArgument::AsInt32Unchecked() const |
235 { |
235 { |
244 u.c[2] = argument_[1]; |
244 u.c[2] = argument_[1]; |
245 u.c[3] = argument_[0]; |
245 u.c[3] = argument_[0]; |
246 |
246 |
247 return u.i; |
247 return u.i; |
248 #else |
248 #else |
249 return *(int32*)argument_; |
249 return *(int32*)argument_; |
250 #endif |
250 #endif |
251 } |
251 } |
252 |
252 |
253 |
253 |
254 float ReceivedMessageArgument::AsFloat() const |
254 float ReceivedMessageArgument::AsFloat() const |
255 { |
255 { |
256 if( !typeTag_ ) |
256 if( !typeTag_ ) |
257 throw MissingArgumentException(); |
257 throw MissingArgumentException(); |
258 else if( *typeTag_ == FLOAT_TYPE_TAG ) |
258 else if( *typeTag_ == FLOAT_TYPE_TAG ) |
259 return AsFloatUnchecked(); |
259 return AsFloatUnchecked(); |
260 else |
260 else |
261 throw WrongArgumentTypeException(); |
261 throw WrongArgumentTypeException(); |
262 } |
262 } |
263 |
263 |
264 |
264 |
265 float ReceivedMessageArgument::AsFloatUnchecked() const |
265 float ReceivedMessageArgument::AsFloatUnchecked() const |
266 { |
266 { |
275 u.c[2] = argument_[1]; |
275 u.c[2] = argument_[1]; |
276 u.c[3] = argument_[0]; |
276 u.c[3] = argument_[0]; |
277 |
277 |
278 return u.f; |
278 return u.f; |
279 #else |
279 #else |
280 return *(float*)argument_; |
280 return *(float*)argument_; |
281 #endif |
281 #endif |
282 } |
282 } |
283 |
283 |
284 |
284 |
285 char ReceivedMessageArgument::AsChar() const |
285 char ReceivedMessageArgument::AsChar() const |
286 { |
286 { |
287 if( !typeTag_ ) |
287 if( !typeTag_ ) |
288 throw MissingArgumentException(); |
288 throw MissingArgumentException(); |
289 else if( *typeTag_ == CHAR_TYPE_TAG ) |
289 else if( *typeTag_ == CHAR_TYPE_TAG ) |
290 return AsCharUnchecked(); |
290 return AsCharUnchecked(); |
291 else |
291 else |
292 throw WrongArgumentTypeException(); |
292 throw WrongArgumentTypeException(); |
293 } |
293 } |
294 |
294 |
295 |
295 |
296 char ReceivedMessageArgument::AsCharUnchecked() const |
296 char ReceivedMessageArgument::AsCharUnchecked() const |
297 { |
297 { |
301 |
301 |
302 uint32 ReceivedMessageArgument::AsRgbaColor() const |
302 uint32 ReceivedMessageArgument::AsRgbaColor() const |
303 { |
303 { |
304 if( !typeTag_ ) |
304 if( !typeTag_ ) |
305 throw MissingArgumentException(); |
305 throw MissingArgumentException(); |
306 else if( *typeTag_ == RGBA_COLOR_TYPE_TAG ) |
306 else if( *typeTag_ == RGBA_COLOR_TYPE_TAG ) |
307 return AsRgbaColorUnchecked(); |
307 return AsRgbaColorUnchecked(); |
308 else |
308 else |
309 throw WrongArgumentTypeException(); |
309 throw WrongArgumentTypeException(); |
310 } |
310 } |
311 |
311 |
312 |
312 |
313 uint32 ReceivedMessageArgument::AsRgbaColorUnchecked() const |
313 uint32 ReceivedMessageArgument::AsRgbaColorUnchecked() const |
314 { |
314 { |
315 return ToUInt32( argument_ ); |
315 return ToUInt32( argument_ ); |
316 } |
316 } |
317 |
317 |
318 |
318 |
319 uint32 ReceivedMessageArgument::AsMidiMessage() const |
319 uint32 ReceivedMessageArgument::AsMidiMessage() const |
320 { |
320 { |
321 if( !typeTag_ ) |
321 if( !typeTag_ ) |
322 throw MissingArgumentException(); |
322 throw MissingArgumentException(); |
323 else if( *typeTag_ == MIDI_MESSAGE_TYPE_TAG ) |
323 else if( *typeTag_ == MIDI_MESSAGE_TYPE_TAG ) |
324 return AsMidiMessageUnchecked(); |
324 return AsMidiMessageUnchecked(); |
325 else |
325 else |
326 throw WrongArgumentTypeException(); |
326 throw WrongArgumentTypeException(); |
327 } |
327 } |
328 |
328 |
329 |
329 |
330 uint32 ReceivedMessageArgument::AsMidiMessageUnchecked() const |
330 uint32 ReceivedMessageArgument::AsMidiMessageUnchecked() const |
331 { |
331 { |
332 return ToUInt32( argument_ ); |
332 return ToUInt32( argument_ ); |
333 } |
333 } |
334 |
334 |
335 |
335 |
336 int64 ReceivedMessageArgument::AsInt64() const |
336 int64 ReceivedMessageArgument::AsInt64() const |
337 { |
337 { |
338 if( !typeTag_ ) |
338 if( !typeTag_ ) |
339 throw MissingArgumentException(); |
339 throw MissingArgumentException(); |
340 else if( *typeTag_ == INT64_TYPE_TAG ) |
340 else if( *typeTag_ == INT64_TYPE_TAG ) |
341 return AsInt64Unchecked(); |
341 return AsInt64Unchecked(); |
342 else |
342 else |
343 throw WrongArgumentTypeException(); |
343 throw WrongArgumentTypeException(); |
344 } |
344 } |
345 |
345 |
346 |
346 |
347 int64 ReceivedMessageArgument::AsInt64Unchecked() const |
347 int64 ReceivedMessageArgument::AsInt64Unchecked() const |
348 { |
348 { |
395 u.c[6] = argument_[1]; |
395 u.c[6] = argument_[1]; |
396 u.c[7] = argument_[0]; |
396 u.c[7] = argument_[0]; |
397 |
397 |
398 return u.d; |
398 return u.d; |
399 #else |
399 #else |
400 return *(double*)argument_; |
400 return *(double*)argument_; |
401 #endif |
401 #endif |
402 } |
402 } |
403 |
403 |
404 |
404 |
405 const char* ReceivedMessageArgument::AsString() const |
405 const char* ReceivedMessageArgument::AsString() const |
406 { |
406 { |
407 if( !typeTag_ ) |
407 if( !typeTag_ ) |
408 throw MissingArgumentException(); |
408 throw MissingArgumentException(); |
409 else if( *typeTag_ == STRING_TYPE_TAG ) |
409 else if( *typeTag_ == STRING_TYPE_TAG ) |
410 return argument_; |
410 return argument_; |
411 else |
411 else |
412 throw WrongArgumentTypeException(); |
412 throw WrongArgumentTypeException(); |
413 } |
413 } |
414 |
414 |
415 |
415 |
416 const char* ReceivedMessageArgument::AsSymbol() const |
416 const char* ReceivedMessageArgument::AsSymbol() const |
417 { |
417 { |
418 if( !typeTag_ ) |
418 if( !typeTag_ ) |
419 throw MissingArgumentException(); |
419 throw MissingArgumentException(); |
420 else if( *typeTag_ == SYMBOL_TYPE_TAG ) |
420 else if( *typeTag_ == SYMBOL_TYPE_TAG ) |
421 return argument_; |
421 return argument_; |
422 else |
422 else |
423 throw WrongArgumentTypeException(); |
423 throw WrongArgumentTypeException(); |
424 } |
424 } |
425 |
425 |
426 |
426 |
427 void ReceivedMessageArgument::AsBlob( const void*& data, unsigned long& size ) const |
427 void ReceivedMessageArgument::AsBlob( const void*& data, unsigned long& size ) const |
428 { |
428 { |
429 if( !typeTag_ ) |
429 if( !typeTag_ ) |
430 throw MissingArgumentException(); |
430 throw MissingArgumentException(); |
431 else if( *typeTag_ == BLOB_TYPE_TAG ) |
431 else if( *typeTag_ == BLOB_TYPE_TAG ) |
432 AsBlobUnchecked( data, size ); |
432 AsBlobUnchecked( data, size ); |
433 else |
433 else |
434 throw WrongArgumentTypeException(); |
434 throw WrongArgumentTypeException(); |
435 } |
435 } |
436 |
436 |
437 |
437 |
438 void ReceivedMessageArgument::AsBlobUnchecked( const void*& data, unsigned long& size ) const |
438 void ReceivedMessageArgument::AsBlobUnchecked( const void*& data, unsigned long& size ) const |
439 { |
439 { |
440 size = ToUInt32( argument_ ); |
440 size = ToUInt32( argument_ ); |
441 data = (void*)(argument_+4); |
441 data = (void*)(argument_+4); |
442 } |
442 } |
443 |
443 |
444 //------------------------------------------------------------------------------ |
444 //------------------------------------------------------------------------------ |
445 |
445 |
446 void ReceivedMessageArgumentIterator::Advance() |
446 void ReceivedMessageArgumentIterator::Advance() |