libalvalue  2.0.6.8
/home/opennao/work/master/sdk/libnaoqi/libalvalue/alvalue/alvalue.h
Go to the documentation of this file.
00001 
00010 #pragma once
00011 #ifndef _LIBALVALUE_ALVALUE_ALVALUE_H_
00012 #define _LIBALVALUE_ALVALUE_ALVALUE_H_
00013 
00014 # include <cstdio>
00015 # include <string>
00016 # include <vector>
00017 
00018 # include <alvalue/config.h>
00019 
00020 namespace AL
00021 {
00022 
00027 #ifndef ALVALUE_VERBOSITY
00028 # define ALVALUE_VERBOSITY
00029 
00035   typedef enum ALVALUE_API _TVerbosity
00036   {
00037     VerbosityNone=0,   
00038     VerbosityMini,   
00039     VerbosityMedium, 
00040     VerbosityFull    
00041   } Verbosity;
00042 #endif
00043 
00052   class ALVALUE_API ALValue
00053   {
00054     friend void ConvertALValueToSoapXml(const ALValue *pAlValue,
00055                                         std::string &strToFill,
00056                                         bool bInAnArray);
00057     friend class ALNetwork;
00058 
00059   public:
00064     typedef int                                TALValueInt;
00069     typedef float                              TALValueFloat;
00074     typedef double                             TALValueDouble;
00079     typedef bool                               TALValueBool;
00084     typedef std::string                        TALValueString;
00085 
00090     typedef std::vector<unsigned char>         TAlValueBinaryData;
00095     typedef TAlValueBinaryData::iterator       ITAlValueBinaryData;
00100     typedef TAlValueBinaryData::const_iterator CITAlValueBinaryData;
00101 
00106     typedef std::vector<ALValue>               TAlValueArray;
00111     typedef TAlValueArray::iterator            ITAlValueArray;
00116     typedef TAlValueArray::const_iterator      CITAlValueArray;
00117 
00122     union unionValue {
00123       TAlValueArray      *asArray;  
00124       TALValueBool       asBool;    
00125       TALValueInt        asInt;     
00126       TALValueFloat      asFloat;   
00127       TALValueString     *asString; 
00128       TAlValueBinaryData *asObject; 
00129       TAlValueBinaryData *asBinary; 
00130     };
00131 
00132   public:
00145     enum Type {
00146       TypeInvalid = 0, 
00147       TypeArray,       
00148       TypeBool,        
00149       TypeInt,         
00150       TypeFloat,       
00151       TypeString,      
00152       TypeObject,      
00153       TypeBinary       
00154     };
00155 
00156   public:
00158     ALValue();
00163     ALValue(const bool &value);
00168     ALValue(const int &value);
00173     ALValue(const double &value);
00178     ALValue(const float &value);
00183     ALValue(const std::string &value);
00188     ALValue(const char *value);
00189 
00194     ALValue(const std::vector<std::string> &pListString);
00199     ALValue(const std::vector<float> &pListFloat);
00204     ALValue(const std::vector<int> &pListInt);
00205 
00211     ALValue(const void *value, int nBytes);
00217     ALValue(const char **pArrayOfString, int nNbrString);
00223     ALValue(const float *pFloat, int nNbrElement);
00229     ALValue(const int *pInt, int nNbrElement);
00230 
00235     ALValue(const TAlValueBinaryData &rhs);
00240     ALValue(ALValue const &rhs);
00241 
00243     virtual ~ALValue();
00244 
00246     void clear();
00247 
00254     ALValue& operator=(const ALValue &rhs);
00256     ALValue& operator=(const bool &rhs);
00258     ALValue& operator=(const int &rhs);
00260     ALValue& operator=(const double &rhs);
00262     ALValue& operator=(const float &rhs);
00264     ALValue& operator=(const char *rhs);
00266     ALValue& operator=(const TAlValueBinaryData &rhs);
00267 
00273     bool operator==(ALValue const &other) const;
00279     bool operator!=(ALValue const &other) const;
00280 
00288     operator       bool&();
00296     operator const bool() const;
00304     operator       int&();
00312     operator const int() const;
00320     operator       float&();
00328     operator const float() const;
00329 
00337     operator const double() const;
00338 
00346     operator       std::string&();
00354     operator const std::string&() const;
00355 
00363     operator       TAlValueBinaryData&();
00371     operator const TAlValueBinaryData&() const;
00379     operator const void*() const;
00380 
00385     typedef std::vector<std::string> TStringArray;
00390     typedef std::vector<float>       TFloatArray;
00395     typedef std::vector<int>         TIntArray;
00396 
00404     operator TStringArray() const;
00412     operator TFloatArray() const;
00420     operator TIntArray() const;
00421 
00428     ALValue& setObject(const void* pData, int nDataSizeInBytes);
00429 
00435     void     SetBinary(const void *rhs, int nDataSizeInBytes);
00442     ALValue& SetBinaryNoCopy(const void *rhs, int nDataSizeInBytes);
00443 
00448     const void* GetBinary() const;
00453     const void* getObject() const;
00454 
00459     void *getPtrValue();
00460 
00470     void ToStringArray(TStringArray &pArrayToFill,
00471                        bool bInsertDefaultOnError = false) const;
00481     void ToFloatArray(TFloatArray &pArrayToFill,
00482                       bool bInsertDefaultOnError = false) const;
00492     void ToIntArray(TIntArray &pArrayToFill,
00493                     bool bInsertDefaultOnError = false) const;
00494 
00495 
00502     ALValue& operator[](int i);
00503 
00510     const ALValue& operator[](int i) const;
00511 
00516     enum Type getType() const;
00517 
00522     bool isValid () const;
00527     bool isArray () const;
00532     bool isBool  () const;
00537     bool isInt   () const;
00542     bool isFloat () const;
00547     bool isString() const;
00552     bool isObject() const;
00557     bool isBinary() const;
00562     std::vector<unsigned char> asRaw() const;
00563 
00569     unsigned int getSize() const;
00570 
00571 
00581     void arrayReserve(int size);
00582 
00588     void arraySetSize(int size);
00589 
00599     void arrayPush(const ALValue &pSrcToCopyNotOwned);
00600 
00608     void arrayPopFront();
00609 
00620     std::string toString(Verbosity pnVerbosity = VerbosityMini) const;
00621 
00631     std::string toPythonBuffer() const;
00632 
00642     bool decodeB64(const char *pszB64);
00643 
00653     bool decodeB64Object(const char *pszB64);
00654 
00659     void encodeB64(std::string &strOutput) const;
00660 
00673     static enum Type deduceType(const char *szInput,
00674                                 int nLimitToLen = 0x7FFFFFFF);
00675 
00684     void unSerializeFromText(const char *szInput,
00685                              int nLimitToLen = 0x7FFFFFFF);
00686 
00692     std::string serializeToText(void) const;
00693 
00699     static bool xUnSerializeFromText_InnerTest(void);
00700 
00701     template <typename T0>
00702       static ALValue array(const T0 &a0);
00703     template <typename T0, typename T1>
00704       static ALValue array(const T0 &a0, const T1 &a1);
00705     template <typename T0, typename T1, typename T2>
00706       static ALValue array(const T0 &a0, const T1 &a1, const T2 &a2);
00707     template <typename T0, typename T1, typename T2, typename T3>
00708       static ALValue array(const T0 &a0, const T1 &a1, const T2 &a2, const T3 &a3);
00709     template <typename T0, typename T1, typename T2, typename T3, typename T4>
00710       static ALValue array(const T0 &a0, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4);
00711     template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
00712       static ALValue array(const T0 &a0, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5);
00713     template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
00714       static ALValue array(const T0 &a0, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5, const T6 &a6);
00715     template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
00716       static ALValue array(const T0 &a0, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5, const T6 &a6, const T7 &a7);
00717     template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
00718       static ALValue array(const T0 &a0, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5, const T6 &a6, const T7 &a7, const T8 &a8);
00719     template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
00720       static ALValue array(const T0 &a0, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5, const T6 &a6, const T7 &a7, const T8 &a8, const T9 &a9);
00721 
00722   protected:
00724     void xInvalidate();
00725     void xAssertCompatibleType(enum Type pnWantedType);
00726     void xAssertType(enum Type pnWantedType) const;
00727     void xAssertArraySize(int pnSize) const;
00732     void assertStruct();
00736     int              _type;
00738     union unionValue _value;
00739 
00740   public:
00745     const unionValue getUnionValue();
00746 
00751     TAlValueArray *getArrayPtr() const;
00752 
00758     static std::string TypeToString(enum Type pnType);
00759 
00760   };
00761 
00762   template <typename T0>
00763   ALValue ALValue::array(const T0 &a0)
00764   {
00765     ALValue result;
00766     result.arraySetSize(1);
00767     result[0] = a0;
00768     return result;
00769   }
00770 
00771   template <typename T0, typename T1>
00772   ALValue ALValue::array(const T0 &a0, const T1 &a1)
00773   {
00774     ALValue result;
00775     result.arraySetSize(2);
00776     result[0] = a0;
00777     result[1] = a1;
00778     return result;
00779   }
00780 
00781   template <typename T0, typename T1, typename T2>
00782   ALValue ALValue::array(const T0 &a0, const T1 &a1, const T2 &a2)
00783   {
00784     ALValue result;
00785     result.arraySetSize(3);
00786     result[0] = a0;
00787     result[1] = a1;
00788     result[2] = a2;
00789     return result;
00790   }
00791 
00792   template <typename T0, typename T1, typename T2, typename T3>
00793   ALValue ALValue::array(const T0 &a0, const T1 &a1, const T2 &a2, const T3 &a3)
00794   {
00795     ALValue result;
00796     result.arraySetSize(4);
00797     result[0] = a0;
00798     result[1] = a1;
00799     result[2] = a2;
00800     result[3] = a3;
00801     return result;
00802   }
00803 
00804   template <typename T0, typename T1, typename T2, typename T3, typename T4>
00805   ALValue ALValue::array(const T0 &a0, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4)
00806   {
00807     ALValue result;
00808     result.arraySetSize(5);
00809     result[0] = a0;
00810     result[1] = a1;
00811     result[2] = a2;
00812     result[3] = a3;
00813     result[4] = a4;
00814     return result;
00815   }
00816 
00817   template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
00818   ALValue ALValue::array(const T0 &a0, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5)
00819   {
00820     ALValue result;
00821     result.arraySetSize(6);
00822     result[0] = a0;
00823     result[1] = a1;
00824     result[2] = a2;
00825     result[3] = a3;
00826     result[4] = a4;
00827     result[5] = a5;
00828     return result;
00829   }
00830 
00831   template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
00832   ALValue ALValue::array(const T0 &a0, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5, const T6 &a6)
00833   {
00834     ALValue result;
00835     result.arraySetSize(7);
00836     result[0] = a0;
00837     result[1] = a1;
00838     result[2] = a2;
00839     result[3] = a3;
00840     result[4] = a4;
00841     result[5] = a5;
00842     result[6] = a6;
00843     return result;
00844   }
00845 
00846   template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
00847   ALValue ALValue::array(const T0 &a0, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5, const T6 &a6, const T7 &a7)
00848   {
00849     ALValue result;
00850     result.arraySetSize(8);
00851     result[0] = a0;
00852     result[1] = a1;
00853     result[2] = a2;
00854     result[3] = a3;
00855     result[4] = a4;
00856     result[5] = a5;
00857     result[6] = a6;
00858     result[7] = a7;
00859     return result;
00860   }
00861 
00862   template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
00863   ALValue ALValue::array(const T0 &a0, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5, const T6 &a6, const T7 &a7, const T8 &a8)
00864   {
00865     ALValue result;
00866     result.arraySetSize(9);
00867     result[0] = a0;
00868     result[1] = a1;
00869     result[2] = a2;
00870     result[3] = a3;
00871     result[4] = a4;
00872     result[5] = a5;
00873     result[6] = a6;
00874     result[7] = a7;
00875     result[8] = a8;
00876     return result;
00877   }
00878 
00879   template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
00880   ALValue ALValue::array(const T0 &a0, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5, const T6 &a6, const T7 &a7, const T8 &a8, const T9 &a9)
00881   {
00882     ALValue result;
00883     result.arraySetSize(10);
00884     result[0] = a0;
00885     result[1] = a1;
00886     result[2] = a2;
00887     result[3] = a3;
00888     result[4] = a4;
00889     result[5] = a5;
00890     result[6] = a6;
00891     result[7] = a7;
00892     result[8] = a8;
00893     result[9] = a9;
00894     return result;
00895   }
00896   ALVALUE_API bool operator <(const ALValue& a, const ALValue& b);
00897 } // namespace AL
00898 
00904 std::ostream &operator<<(std::ostream &os, const AL::ALValue &a);
00905 
00906 
00907 #endif  // _LIBALVALUE_ALVALUE_ALVALUE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines