Ibiltari Nora / OSC
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OSCData.h Source File

OSCData.h

00001 /*
00002  Written by Yotam Mann, The Center for New Music and Audio Technologies,
00003  University of California, Berkeley.  Copyright (c) 2013, The Regents of
00004  the University of California (Regents).
00005 
00006  Permission to use, copy, modify, distribute, and distribute modified versions
00007  of this software and its documentation without fee and without a signed
00008  licensing agreement, is hereby granted, provided that the above copyright
00009  notice, this paragraph and the following two paragraphs appear in all copies,
00010  modifications, and distributions.
00011 
00012  IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
00013  SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
00014  OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
00015  BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00016 
00017  REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00018  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00019  PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
00020  HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
00021  MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
00022 
00023  For bug reports and feature requests please email me at yotam@cnmat.berkeley.edu
00024  */
00025 
00026 #ifndef OSCDATA_h
00027 #define OSCDATA_h
00028 
00029 
00030 
00031 #include <stdlib.h>
00032 #include <stdint.h>
00033 #include <inttypes.h>
00034 #include <string.h>
00035 
00036 #include "OSCTiming.h"
00037 
00038 
00039 //ERRORS/////////////////////////////////////////////////
00040 typedef enum { OSC_OK = 0,
00041     BUFFER_FULL, INVALID_OSC, ALLOCFAILED, INDEX_OUT_OF_BOUNDS
00042 } OSCErrorCode;
00043 
00044 class OSCData
00045 {
00046 
00047 private:
00048 
00049     //friends
00050     friend class OSCMessage;
00051 
00052     //should only be used while decoding
00053     //leaves an invalid OSCMessage with a type, but no data
00054     OSCData(char t);
00055 
00056 public:
00057 
00058     //an error flag
00059     OSCErrorCode error;
00060 
00061     //the size (in bytes) of the data
00062     int bytes;
00063 
00064     //the type of the data
00065     int type;
00066 
00067     //the data
00068     union {
00069         char * s; //string
00070         int32_t i; //int
00071         float f; //float
00072         double d; //double
00073         uint64_t l; //long
00074         uint8_t * b; //blob
00075         osctime_t time;
00076     } data;
00077 
00078     //overload the constructor to account for all the types and sizes
00079     OSCData(const char * s);
00080     OSCData (int);
00081     OSCData (long int);
00082     OSCData (unsigned int);
00083     OSCData (float);
00084     OSCData (double);
00085     OSCData (uint8_t *, int);
00086     //accepts another OSCData objects and clones it
00087     OSCData (OSCData *);
00088     OSCData  (bool);
00089 
00090     //destructor
00091     ~OSCData();
00092 
00093     //GETTERS
00094     int32_t getInt();
00095     float getFloat();
00096     double getDouble();
00097     int getString(char *);
00098     int getString(char *, int);
00099     int getString(char *, int, int, int);
00100     int getBlob(uint8_t *);
00101     int getBlob(uint8_t *, int);
00102     int getBlob(uint8_t *, int, int, int);
00103     int getBlobLength();
00104     bool getBoolean();
00105     osctime_t getTime();
00106 
00107     //constructor from byte array with type and length
00108     OSCData(char, uint8_t *, int);
00109     //fill the passed in buffer with the data
00110     //uint8_t * asByteArray();
00111 
00112 };
00113 
00114 /*
00115  based on http://stackoverflow.com/questions/809902/64-bit-ntohl-in-c
00116 
00117  if the system is little endian, it will flip the bits
00118  if the system is big endian, it'll do nothing
00119  */
00120 template<typename T>
00121 static inline T BigEndian(const T& x)
00122 {
00123     const int one = 1;
00124     const char sig = *(char*)&one;
00125     if (sig == 0) return x; // for big endian machine just return the input
00126     T ret;
00127     int size = sizeof(T);
00128     char* src = (char*)&x + sizeof(T) - 1;
00129     char* dst = (char*)&ret;
00130     while (size-- > 0){
00131         *dst++ = *src--;
00132     }
00133     return ret;
00134 }
00135 
00136 #endif