front_idill/extern/fajran-npTuioClient/TuioClient/oscpack/osc/OscReceivedElements.cpp
changeset 28 9ccef81f02ab
parent 21 e4e5f02787a1
equal deleted inserted replaced
27:6c08d4d7219e 28:9ccef81f02ab
     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 
    40 // return the first 4 byte boundary after the end of a str4
    40 // return the first 4 byte boundary after the end of a str4
    41 // be careful about calling this version if you don't know whether
    41 // be careful about calling this version if you don't know whether
    42 // the string is terminated correctly.
    42 // the string is terminated correctly.
    43 static inline const char* FindStr4End( const char *p )
    43 static inline const char* FindStr4End( const char *p )
    44 {
    44 {
    45 	if( p[0] == '\0' )    // special case for SuperCollider integer address pattern
    45     if( p[0] == '\0' )    // special case for SuperCollider integer address pattern
    46 		return p + 4;
    46         return p + 4;
    47 
    47 
    48     p += 3;
    48     p += 3;
    49 
    49 
    50     while( *p )
    50     while( *p )
    51         p += 4;
    51         p += 4;
    59 static inline const char* FindStr4End( const char *p, const char *end )
    59 static inline const char* FindStr4End( const char *p, const char *end )
    60 {
    60 {
    61     if( p >= end )
    61     if( p >= end )
    62         return 0;
    62         return 0;
    63 
    63 
    64 	if( p[0] == '\0' )    // special case for SuperCollider integer address pattern
    64     if( p[0] == '\0' )    // special case for SuperCollider integer address pattern
    65 		return p + 4;
    65         return p + 4;
    66 
    66 
    67     p += 3;
    67     p += 3;
    68     end -= 1;
    68     end -= 1;
    69 
    69 
    70     while( p < end && *p )
    70     while( p < end && *p )
   100     u.c[2] = p[1];
   100     u.c[2] = p[1];
   101     u.c[3] = p[0];
   101     u.c[3] = p[0];
   102 
   102 
   103     return u.i;
   103     return u.i;
   104 #else
   104 #else
   105 	return *(int32*)p;
   105     return *(int32*)p;
   106 #endif
   106 #endif
   107 }
   107 }
   108 
   108 
   109 
   109 
   110 static inline uint32 ToUInt32( const char *p )
   110 static inline uint32 ToUInt32( const char *p )
   120     u.c[2] = p[1];
   120     u.c[2] = p[1];
   121     u.c[3] = p[0];
   121     u.c[3] = p[0];
   122 
   122 
   123     return u.i;
   123     return u.i;
   124 #else
   124 #else
   125 	return *(uint32*)p;
   125     return *(uint32*)p;
   126 #endif
   126 #endif
   127 }
   127 }
   128 
   128 
   129 
   129 
   130 int64 ToInt64( const char *p )
   130 int64 ToInt64( const char *p )
   144     u.c[6] = p[1];
   144     u.c[6] = p[1];
   145     u.c[7] = p[0];
   145     u.c[7] = p[0];
   146 
   146 
   147     return u.i;
   147     return u.i;
   148 #else
   148 #else
   149 	return *(int64*)p;
   149     return *(int64*)p;
   150 #endif
   150 #endif
   151 }
   151 }
   152 
   152 
   153 
   153 
   154 uint64 ToUInt64( const char *p )
   154 uint64 ToUInt64( const char *p )
   168     u.c[6] = p[1];
   168     u.c[6] = p[1];
   169     u.c[7] = p[0];
   169     u.c[7] = p[0];
   170 
   170 
   171     return u.i;
   171     return u.i;
   172 #else
   172 #else
   173 	return *(uint64*)p;
   173     return *(uint64*)p;
   174 #endif
   174 #endif
   175 }
   175 }
   176 
   176 
   177 //------------------------------------------------------------------------------
   177 //------------------------------------------------------------------------------
   178 
   178 
   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 {
   352 
   352 
   353 uint64 ReceivedMessageArgument::AsTimeTag() const
   353 uint64 ReceivedMessageArgument::AsTimeTag() const
   354 {
   354 {
   355     if( !typeTag_ )
   355     if( !typeTag_ )
   356         throw MissingArgumentException();
   356         throw MissingArgumentException();
   357 	else if( *typeTag_ == TIME_TAG_TYPE_TAG )
   357     else if( *typeTag_ == TIME_TAG_TYPE_TAG )
   358 		return AsTimeTagUnchecked();
   358         return AsTimeTagUnchecked();
   359 	else
   359     else
   360 		throw WrongArgumentTypeException();
   360         throw WrongArgumentTypeException();
   361 }
   361 }
   362 
   362 
   363 
   363 
   364 uint64 ReceivedMessageArgument::AsTimeTagUnchecked() const
   364 uint64 ReceivedMessageArgument::AsTimeTagUnchecked() const
   365 {
   365 {
   369 
   369 
   370 double ReceivedMessageArgument::AsDouble() const
   370 double ReceivedMessageArgument::AsDouble() const
   371 {
   371 {
   372     if( !typeTag_ )
   372     if( !typeTag_ )
   373         throw MissingArgumentException();
   373         throw MissingArgumentException();
   374 	else if( *typeTag_ == DOUBLE_TYPE_TAG )
   374     else if( *typeTag_ == DOUBLE_TYPE_TAG )
   375 		return AsDoubleUnchecked();
   375         return AsDoubleUnchecked();
   376 	else
   376     else
   377 		throw WrongArgumentTypeException();
   377         throw WrongArgumentTypeException();
   378 }
   378 }
   379 
   379 
   380 
   380 
   381 double ReceivedMessageArgument::AsDoubleUnchecked() const
   381 double ReceivedMessageArgument::AsDoubleUnchecked() const
   382 {
   382 {
   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()
   461 
   461 
   462             // zero length
   462             // zero length
   463             break;
   463             break;
   464 
   464 
   465         case INT32_TYPE_TAG:
   465         case INT32_TYPE_TAG:
   466         case FLOAT_TYPE_TAG: 					
   466         case FLOAT_TYPE_TAG:                     
   467         case CHAR_TYPE_TAG:
   467         case CHAR_TYPE_TAG:
   468         case RGBA_COLOR_TYPE_TAG:
   468         case RGBA_COLOR_TYPE_TAG:
   469         case MIDI_MESSAGE_TYPE_TAG:
   469         case MIDI_MESSAGE_TYPE_TAG:
   470 
   470 
   471             value_.argument_ += 4;
   471             value_.argument_ += 4;
   472             break;
   472             break;
   473 
   473 
   474         case INT64_TYPE_TAG:
   474         case INT64_TYPE_TAG:
   475         case TIME_TAG_TYPE_TAG:
   475         case TIME_TAG_TYPE_TAG:
   476         case DOUBLE_TYPE_TAG:
   476         case DOUBLE_TYPE_TAG:
   477 				
   477                 
   478             value_.argument_ += 8;
   478             value_.argument_ += 8;
   479             break;
   479             break;
   480 
   480 
   481         case STRING_TYPE_TAG: 
   481         case STRING_TYPE_TAG: 
   482         case SYMBOL_TYPE_TAG:
   482         case SYMBOL_TYPE_TAG:
   524 }
   524 }
   525 
   525 
   526 
   526 
   527 bool ReceivedMessage::AddressPatternIsUInt32() const
   527 bool ReceivedMessage::AddressPatternIsUInt32() const
   528 {
   528 {
   529 	return (addressPattern_[0] == '\0');
   529     return (addressPattern_[0] == '\0');
   530 }
   530 }
   531 
   531 
   532 
   532 
   533 uint32 ReceivedMessage::AddressPatternAsUInt32() const
   533 uint32 ReceivedMessage::AddressPatternAsUInt32() const
   534 {
   534 {