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


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley
autogenerated on Tue Mar 7 2017 03:44:43