Main Page   Class Hierarchy   Compound List   File List   Compound Members  

XmlRpcValue.h

00001 
00002 #ifndef _XMLRPCVALUE_H_
00003 #define _XMLRPCVALUE_H_
00004 //
00005 // XmlRpc++ Copyright (c) 2002-2003 by Chris Morley
00006 //
00007 #if defined(_MSC_VER)
00008 # pragma warning(disable:4786)    // identifier was truncated in debug info
00009 #endif
00010 
00011 #ifndef MAKEDEPEND
00012 # include <map>
00013 # include <string>
00014 # include <vector>
00015 # include <time.h>
00016 #endif
00017 
00018 namespace XmlRpc {
00019 
00021   //   should probably refcount them...
00022   class XmlRpcValue {
00023   public:
00024 
00025 
00026     enum Type {
00027       TypeInvalid,
00028       TypeBoolean,
00029       TypeInt,
00030       TypeDouble,
00031       TypeString,
00032       TypeDateTime,
00033       TypeBase64,
00034       TypeArray,
00035       TypeStruct
00036     };
00037 
00038     // Non-primitive types
00039     typedef std::vector<char> BinaryData;
00040     typedef std::vector<XmlRpcValue> ValueArray;
00041     typedef std::map<std::string, XmlRpcValue> ValueStruct;
00042 
00043 
00045     XmlRpcValue() : _type(TypeInvalid) { _value.asBinary = 0; }
00046     XmlRpcValue(bool value) : _type(TypeBoolean) { _value.asBool = value; }
00047     XmlRpcValue(int value)  : _type(TypeInt) { _value.asInt = value; }
00048     XmlRpcValue(double value)  : _type(TypeDouble) { _value.asDouble = value; }
00049 
00050     XmlRpcValue(std::string const& value) : _type(TypeString) 
00051     { _value.asString = new std::string(value); }
00052 
00053     XmlRpcValue(const char* value)  : _type(TypeString)
00054     { _value.asString = new std::string(value); }
00055 
00056     XmlRpcValue(struct tm* value)  : _type(TypeDateTime) 
00057     { _value.asTime = new struct tm(*value); }
00058 
00059 
00060     XmlRpcValue(void* value, int nBytes)  : _type(TypeBase64)
00061     {
00062       _value.asBinary = new BinaryData((char*)value, ((char*)value)+nBytes);
00063     }
00064 
00066     XmlRpcValue(std::string const& xml, int* offset) : _type(TypeInvalid)
00067     { if ( ! fromXml(xml,offset)) _type = TypeInvalid; }
00068 
00070     XmlRpcValue(XmlRpcValue const& rhs) : _type(TypeInvalid) { *this = rhs; }
00071 
00073     /*virtual*/ ~XmlRpcValue() { invalidate(); }
00074 
00076     void clear() { invalidate(); }
00077 
00078     // Operators
00079     XmlRpcValue& operator=(XmlRpcValue const& rhs);
00080     XmlRpcValue& operator=(int const& rhs) { return operator=(XmlRpcValue(rhs)); }
00081     XmlRpcValue& operator=(double const& rhs) { return operator=(XmlRpcValue(rhs)); }
00082     XmlRpcValue& operator=(const char* rhs) { return operator=(XmlRpcValue(std::string(rhs))); }
00083 
00084     bool operator==(XmlRpcValue const& other) const;
00085     bool operator!=(XmlRpcValue const& other) const;
00086 
00087     operator bool&()          { assertTypeOrInvalid(TypeBoolean); return _value.asBool; }
00088     operator int&()           { assertTypeOrInvalid(TypeInt); return _value.asInt; }
00089     operator double&()        { assertTypeOrInvalid(TypeDouble); return _value.asDouble; }
00090     operator std::string&()   { assertTypeOrInvalid(TypeString); return *_value.asString; }
00091     operator BinaryData&()    { assertTypeOrInvalid(TypeBase64); return *_value.asBinary; }
00092     operator struct tm&()     { assertTypeOrInvalid(TypeDateTime); return *_value.asTime; }
00093 
00094     XmlRpcValue const& operator[](int i) const { assertArray(i+1); return _value.asArray->at(i); }
00095     XmlRpcValue& operator[](int i)             { assertArray(i+1); return _value.asArray->at(i); }
00096 
00097     XmlRpcValue& operator[](std::string const& k) { assertStruct(); return (*_value.asStruct)[k]; }
00098     XmlRpcValue& operator[](const char* k) { assertStruct(); std::string s(k); return (*_value.asStruct)[s]; }
00099 
00100     // Accessors
00102     bool valid() const { return _type != TypeInvalid; }
00103 
00105     Type const &getType() const { return _type; }
00106 
00108     int size() const;
00109 
00111     void setSize(int size)    { assertArray(size); }
00112 
00114     bool hasMember(const std::string& name) const;
00115 
00117     bool fromXml(std::string const& valueXml, int* offset);
00118 
00120     std::string toXml() const;
00121 
00123     std::ostream& write(std::ostream& os) const;
00124 
00125     // Formatting
00127     static std::string const& getDoubleFormat() { return _doubleFormat; }
00128 
00130     static void setDoubleFormat(const char* f) { _doubleFormat = f; }
00131 
00132 
00133   protected:
00134     // Clean up
00135     void invalidate();
00136 
00137     // Type checking
00138     void assertTypeOrInvalid(Type t);
00139     void assertArray(int size) const;
00140     void assertArray(int size);
00141     void assertStruct();
00142 
00143     // XML decoding
00144     bool boolFromXml(std::string const& valueXml, int* offset);
00145     bool intFromXml(std::string const& valueXml, int* offset);
00146     bool doubleFromXml(std::string const& valueXml, int* offset);
00147     bool stringFromXml(std::string const& valueXml, int* offset);
00148     bool timeFromXml(std::string const& valueXml, int* offset);
00149     bool binaryFromXml(std::string const& valueXml, int* offset);
00150     bool arrayFromXml(std::string const& valueXml, int* offset);
00151     bool structFromXml(std::string const& valueXml, int* offset);
00152 
00153     // XML encoding
00154     std::string boolToXml() const;
00155     std::string intToXml() const;
00156     std::string doubleToXml() const;
00157     std::string stringToXml() const;
00158     std::string timeToXml() const;
00159     std::string binaryToXml() const;
00160     std::string arrayToXml() const;
00161     std::string structToXml() const;
00162 
00163     // Format strings
00164     static std::string _doubleFormat;
00165 
00166     // Type tag and values
00167     Type _type;
00168 
00169     // At some point I will split off Arrays and Structs into
00170     // separate ref-counted objects for more efficient copying.
00171     union {
00172       bool          asBool;
00173       int           asInt;
00174       double        asDouble;
00175       struct tm*    asTime;
00176       std::string*  asString;
00177       BinaryData*   asBinary;
00178       ValueArray*   asArray;
00179       ValueStruct*  asStruct;
00180     } _value;
00181     
00182   };
00183 } // namespace XmlRpc
00184 
00185 
00186 std::ostream& operator<<(std::ostream& os, XmlRpc::XmlRpcValue& v);
00187 
00188 
00189 #endif // _XMLRPCVALUE_H_

Generated on Thu Mar 6 11:32:57 2003 for XmlRpc++ by doxygen1.3-rc3