XmlRpc::XmlRpcValue Class Reference

A class to represent RPC arguments and results. More...

#include <XmlRpcValue.h>

List of all members.

Public Types

enum  Type {
  TypeInvalid, TypeBoolean, TypeInt, TypeDouble,
  TypeString, TypeDateTime, TypeBase64, TypeArray,
  TypeStruct
}
 

XmlRpcValue types.

More...
typedef std::vector< char > BinaryData
typedef std::vector< XmlRpcValueValueArray
typedef std::map< std::string,
XmlRpcValue
ValueStruct

Public Member Functions

 XmlRpcValue ()
 Construct an empty XmlRpcValue.
 XmlRpcValue (bool value)
 Construct an XmlRpcValue with a bool value.
 XmlRpcValue (int value)
 Construct an XmlRpcValue with an int value.
 XmlRpcValue (double value)
 Construct an XmlRpcValue with a double value.
 XmlRpcValue (std::string const &value)
 Construct an XmlRpcValue with a string value.
 XmlRpcValue (const char *value)
 Construct an XmlRpcValue with a string value.
 XmlRpcValue (struct tm *value)
 Construct an XmlRpcValue with a date/time value.
 XmlRpcValue (void *value, int nBytes)
 Construct an XmlRpcValue with a binary data value.
 XmlRpcValue (std::string const &xml, int *offset)
 Construct from xml, beginning at *offset chars into the string, updates offset.
 XmlRpcValue (XmlRpcValue const &rhs)
 Copy constructor.
 ~XmlRpcValue ()
 Destructor (make virtual if you want to subclass).
void clear ()
 Erase the current value.
XmlRpcValueoperator= (XmlRpcValue const &rhs)
 Assignment from one XmlRpcValue to this one.
XmlRpcValueoperator= (int const &rhs)
 Assign an int to this XmlRpcValue.
XmlRpcValueoperator= (double const &rhs)
 Assign a double to this XmlRpcValue.
XmlRpcValueoperator= (const char *rhs)
 Assign a string to this XmlRpcValue.
bool operator== (XmlRpcValue const &other) const
 Tests two XmlRpcValues for equality.
bool operator!= (XmlRpcValue const &other) const
 Tests two XmlRpcValues for inequality.
 operator bool & ()
 Treat an XmlRpcValue as a bool.
 operator int & ()
 Treat an XmlRpcValue as an int.
 operator double & ()
 Treat an XmlRpcValue as a double.
 operator std::string & ()
 Treat an XmlRpcValue as a string.
 operator BinaryData & ()
 Access the BinaryData value.
 operator struct tm & ()
 Access the DateTime value.
XmlRpcValue const & operator[] (int i) const
 Const array value accessor.
XmlRpcValueoperator[] (int i)
 Array value accessor.
XmlRpcValueoperator[] (std::string const &k)
 Struct entry accessor.
XmlRpcValueoperator[] (const char *k)
 Struct entry accessor.
 operator ValueStruct const & ()
 Access the struct value map.
bool isValid () const
 Return true if the value has been set to something.
Type const & getType () const
 Return the type of the value stored.
int size () const
 Return the size for string, base64, array, and struct values.
void setSize (int size)
 Specify the size for array values. Array values will grow beyond this size if needed.
bool hasMember (const std::string &name) const
 Check for the existence of a struct member by name.
bool fromXml (std::string const &valueXml, int *offset)
 Decode xml. Destroys any existing value.
std::string toXml () const
 Encode the Value in xml.
std::ostream & write (std::ostream &os) const
 Write the value (no xml encoding).

Static Public Member Functions

static std::string const & getDoubleFormat ()
 Return the format used to write double values.
static void setDoubleFormat (const char *f)
 Specify the format used to write double values.

Protected Member Functions

void invalidate ()
void assertTypeOrInvalid (Type t)
void assertArray (int size) const
void assertArray (int size)
void assertStruct ()
bool boolFromXml (std::string const &valueXml, int *offset)
bool intFromXml (std::string const &valueXml, int *offset)
bool doubleFromXml (std::string const &valueXml, int *offset)
bool stringFromXml (std::string const &valueXml, int *offset)
bool timeFromXml (std::string const &valueXml, int *offset)
bool binaryFromXml (std::string const &valueXml, int *offset)
bool arrayFromXml (std::string const &valueXml, int *offset)
bool structFromXml (std::string const &valueXml, int *offset)
std::string boolToXml () const
std::string intToXml () const
std::string doubleToXml () const
std::string stringToXml () const
std::string timeToXml () const
std::string binaryToXml () const
std::string arrayToXml () const
std::string structToXml () const

Protected Attributes

Type _type
union {
   bool   asBool
   int   asInt
   double   asDouble
   struct tm *   asTime
   std::string *   asString
   BinaryData *   asBinary
   ValueArray *   asArray
   ValueStruct *   asStruct
_value

Static Protected Attributes

static std::string _doubleFormat

Detailed Description

A class to represent RPC arguments and results.

Each XmlRpcValue object contains a typed value, where the type is determined by the initial value assigned to the object.

Definition at line 31 of file XmlRpcValue.h.


Member Typedef Documentation

typedef std::vector<char> XmlRpc::XmlRpcValue::BinaryData

Definition at line 48 of file XmlRpcValue.h.

Definition at line 49 of file XmlRpcValue.h.

typedef std::map<std::string, XmlRpcValue> XmlRpc::XmlRpcValue::ValueStruct

Definition at line 50 of file XmlRpcValue.h.


Member Enumeration Documentation

XmlRpcValue types.

Enumerator:
TypeInvalid 
TypeBoolean 
TypeInt 
TypeDouble 
TypeString 
TypeDateTime 
TypeBase64 
TypeArray 
TypeStruct 

Definition at line 35 of file XmlRpcValue.h.

00035               {
00036       TypeInvalid,
00037       TypeBoolean,
00038       TypeInt,
00039       TypeDouble,
00040       TypeString,
00041       TypeDateTime,
00042       TypeBase64,
00043       TypeArray,
00044       TypeStruct
00045     };


Constructor & Destructor Documentation

XmlRpc::XmlRpcValue::XmlRpcValue (  )  [inline]

Construct an empty XmlRpcValue.

Definition at line 55 of file XmlRpcValue.h.

Referenced by operator=().

00055 : _type(TypeInvalid) { _value.asBinary = 0; }

XmlRpc::XmlRpcValue::XmlRpcValue ( bool  value  )  [inline]

Construct an XmlRpcValue with a bool value.

Definition at line 58 of file XmlRpcValue.h.

00058 : _type(TypeBoolean) { _value.asBool = value; }

XmlRpc::XmlRpcValue::XmlRpcValue ( int  value  )  [inline]

Construct an XmlRpcValue with an int value.

Definition at line 61 of file XmlRpcValue.h.

00061 : _type(TypeInt) { _value.asInt = value; }

XmlRpc::XmlRpcValue::XmlRpcValue ( double  value  )  [inline]

Construct an XmlRpcValue with a double value.

Definition at line 64 of file XmlRpcValue.h.

00064 : _type(TypeDouble) { _value.asDouble = value; }

XmlRpc::XmlRpcValue::XmlRpcValue ( std::string const &  value  )  [inline]

Construct an XmlRpcValue with a string value.

Definition at line 67 of file XmlRpcValue.h.

00067                                         : _type(TypeString) 
00068     { _value.asString = new std::string(value); }

XmlRpc::XmlRpcValue::XmlRpcValue ( const char *  value  )  [inline]

Construct an XmlRpcValue with a string value.

Parameters:
value A null-terminated (C) string.

Definition at line 72 of file XmlRpcValue.h.

00072                                     : _type(TypeString)
00073     { _value.asString = new std::string(value); }

XmlRpc::XmlRpcValue::XmlRpcValue ( struct tm *  value  )  [inline]

Construct an XmlRpcValue with a date/time value.

Parameters:
value A pointer to a struct tm (see localtime)

Definition at line 77 of file XmlRpcValue.h.

00077                                    : _type(TypeDateTime) 
00078     { _value.asTime = new struct tm(*value); }

XmlRpc::XmlRpcValue::XmlRpcValue ( void *  value,
int  nBytes 
) [inline]

Construct an XmlRpcValue with a binary data value.

Parameters:
value A pointer to data
nBytes The length of the data pointed to, in bytes

Definition at line 83 of file XmlRpcValue.h.

00083                                           : _type(TypeBase64)
00084     {
00085       _value.asBinary = new BinaryData((char*)value, ((char*)value)+nBytes);
00086     }

XmlRpc::XmlRpcValue::XmlRpcValue ( std::string const &  xml,
int *  offset 
) [inline]

Construct from xml, beginning at *offset chars into the string, updates offset.

Definition at line 89 of file XmlRpcValue.h.

00089                                                    : _type(TypeInvalid)
00090     { if ( ! fromXml(xml,offset)) _type = TypeInvalid; }

XmlRpc::XmlRpcValue::XmlRpcValue ( XmlRpcValue const &  rhs  )  [inline]

Copy constructor.

Definition at line 93 of file XmlRpcValue.h.

00093 : _type(TypeInvalid) { *this = rhs; }

XmlRpc::XmlRpcValue::~XmlRpcValue (  )  [inline]

Destructor (make virtual if you want to subclass).

Definition at line 96 of file XmlRpcValue.h.

00096 { invalidate(); }


Member Function Documentation

bool XmlRpc::XmlRpcValue::arrayFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

Definition at line 465 of file XmlRpcValue.cc.

Referenced by fromXml().

00466   {
00467     if ( ! XmlRpcUtil::nextTagIs(DATA_TAG, valueXml, offset))
00468       return false;
00469 
00470     _type = TypeArray;
00471     _value.asArray = new ValueArray;
00472     XmlRpcValue v;
00473     while (v.fromXml(valueXml, offset))
00474       _value.asArray->push_back(v);       // copy...
00475 
00476     // Skip the trailing </data>
00477     (void) XmlRpcUtil::nextTagIs(DATA_ETAG, valueXml, offset);
00478     return true;
00479   }

std::string XmlRpc::XmlRpcValue::arrayToXml (  )  const [protected]

Definition at line 484 of file XmlRpcValue.cc.

Referenced by toXml().

00485   {
00486     std::string xml = VALUE_TAG;
00487     xml += ARRAY_TAG;
00488     xml += DATA_TAG;
00489 
00490     int s = int(_value.asArray->size());
00491     for (int i=0; i<s; ++i)
00492        xml += _value.asArray->at(i).toXml();
00493 
00494     xml += DATA_ETAG;
00495     xml += ARRAY_ETAG;
00496     xml += VALUE_ETAG;
00497     return xml;
00498   }

void XmlRpc::XmlRpcValue::assertArray ( int  size  )  [protected]

Definition at line 101 of file XmlRpcValue.cc.

00102   {
00103     if (_type == TypeInvalid) {
00104       _type = TypeArray;
00105       _value.asArray = new ValueArray(size);
00106     } else if (_type == TypeArray) {
00107       if (int(_value.asArray->size()) < size)
00108         _value.asArray->resize(size);
00109     } else
00110       throw XmlRpcException("type error: expected an array");
00111   }

void XmlRpc::XmlRpcValue::assertArray ( int  size  )  const [protected]

Definition at line 92 of file XmlRpcValue.cc.

00093   {
00094     if (_type != TypeArray)
00095       throw XmlRpcException("type error: expected an array");
00096     else if (int(_value.asArray->size()) < size)
00097       throw XmlRpcException("range error: array index too large");
00098   }

void XmlRpc::XmlRpcValue::assertStruct (  )  [protected]

Definition at line 113 of file XmlRpcValue.cc.

00114   {
00115     if (_type == TypeInvalid) {
00116       _type = TypeStruct;
00117       _value.asStruct = new ValueStruct();
00118     } else if (_type != TypeStruct)
00119       throw XmlRpcException("type error: expected a struct");
00120   }

void XmlRpc::XmlRpcValue::assertTypeOrInvalid ( Type  t  )  [protected]

Definition at line 74 of file XmlRpcValue.cc.

Referenced by operator bool &(), operator double &(), operator int &(), and operator std::string &().

00075   {
00076     if (_type == TypeInvalid)
00077     {
00078       _type = t;
00079       switch (_type) {    // Ensure there is a valid value for the type
00080         case TypeString:   _value.asString = new std::string(); break;
00081         case TypeDateTime: _value.asTime = new struct tm();     break;
00082         case TypeBase64:   _value.asBinary = new BinaryData();  break;
00083         case TypeArray:    _value.asArray = new ValueArray();   break;
00084         case TypeStruct:   _value.asStruct = new ValueStruct(); break;
00085         default:           _value.asBinary = 0; break;
00086       }
00087     }
00088     else if (_type != t)
00089       throw XmlRpcException("type error");
00090   }

bool XmlRpc::XmlRpcValue::binaryFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

Definition at line 423 of file XmlRpcValue.cc.

Referenced by fromXml().

00424   {
00425     size_t valueEnd = valueXml.find('<', *offset);
00426     if (valueEnd == std::string::npos)
00427       return false;     // No end tag;
00428 
00429     _type = TypeBase64;
00430     std::string asString = valueXml.substr(*offset, valueEnd-*offset);
00431     _value.asBinary = new BinaryData();
00432     // check whether base64 encodings can contain chars xml encodes...
00433 
00434     // convert from base64 to binary
00435     int iostatus = 0;
00436           base64<char> decoder;
00437     std::back_insert_iterator<BinaryData> ins = std::back_inserter(*(_value.asBinary));
00438                 decoder.get(asString.begin(), asString.end(), ins, iostatus);
00439 
00440     *offset += int(asString.length());
00441     return true;
00442   }

std::string XmlRpc::XmlRpcValue::binaryToXml (  )  const [protected]

Definition at line 445 of file XmlRpcValue.cc.

Referenced by toXml().

00446   {
00447     // convert to base64
00448     std::vector<char> base64data;
00449     int iostatus = 0;
00450           base64<char> encoder;
00451     std::back_insert_iterator<std::vector<char> > ins = std::back_inserter(base64data);
00452                 encoder.put(_value.asBinary->begin(), _value.asBinary->end(), ins, iostatus, base64<>::crlf());
00453 
00454     // Wrap with xml
00455     std::string xml = VALUE_TAG;
00456     xml += BASE64_TAG;
00457     xml.append(base64data.begin(), base64data.end());
00458     xml += BASE64_ETAG;
00459     xml += VALUE_ETAG;
00460     return xml;
00461   }

bool XmlRpc::XmlRpcValue::boolFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

Definition at line 280 of file XmlRpcValue.cc.

Referenced by fromXml().

00281   {
00282     const char* valueStart = valueXml.c_str() + *offset;
00283     char* valueEnd;
00284     long ivalue = strtol(valueStart, &valueEnd, 10);
00285     if (valueEnd == valueStart || (ivalue != 0 && ivalue != 1))
00286       return false;
00287 
00288     _type = TypeBoolean;
00289     _value.asBool = (ivalue == 1);
00290     *offset += int(valueEnd - valueStart);
00291     return true;
00292   }

std::string XmlRpc::XmlRpcValue::boolToXml (  )  const [protected]

Definition at line 294 of file XmlRpcValue.cc.

Referenced by toXml().

00295   {
00296     std::string xml = VALUE_TAG;
00297     xml += BOOLEAN_TAG;
00298     xml += (_value.asBool ? "1" : "0");
00299     xml += BOOLEAN_ETAG;
00300     xml += VALUE_ETAG;
00301     return xml;
00302   }

void XmlRpc::XmlRpcValue::clear (  )  [inline]

Erase the current value.

Definition at line 99 of file XmlRpcValue.h.

Referenced by XmlRpc::XmlRpcClient::execute().

00099 { invalidate(); }

bool XmlRpc::XmlRpcValue::doubleFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

Definition at line 333 of file XmlRpcValue.cc.

Referenced by fromXml().

00334   {
00335     const char* valueStart = valueXml.c_str() + *offset;
00336     char* valueEnd;
00337     double dvalue = strtod(valueStart, &valueEnd);
00338     if (valueEnd == valueStart)
00339       return false;
00340 
00341     _type = TypeDouble;
00342     _value.asDouble = dvalue;
00343     *offset += int(valueEnd - valueStart);
00344     return true;
00345   }

std::string XmlRpc::XmlRpcValue::doubleToXml (  )  const [protected]

Definition at line 347 of file XmlRpcValue.cc.

Referenced by toXml().

00348   {
00349     char buf[256];
00350     snprintf(buf, sizeof(buf)-1, getDoubleFormat().c_str(), _value.asDouble);
00351     buf[sizeof(buf)-1] = 0;
00352 
00353     std::string xml = VALUE_TAG;
00354     xml += DOUBLE_TAG;
00355     xml += buf;
00356     xml += DOUBLE_ETAG;
00357     xml += VALUE_ETAG;
00358     return xml;
00359   }

bool XmlRpc::XmlRpcValue::fromXml ( std::string const &  valueXml,
int *  offset 
)

Decode xml. Destroys any existing value.

Definition at line 219 of file XmlRpcValue.cc.

Referenced by arrayFromXml(), XmlRpc::XmlRpcClient::parseResponse(), and XmlRpcValue().

00220   {
00221     int savedOffset = *offset;
00222 
00223     invalidate();
00224     if ( ! XmlRpcUtil::nextTagIs(VALUE_TAG, valueXml, offset))
00225       return false;       // Not a value, offset not updated
00226 
00227         int afterValueOffset = *offset;
00228     std::string typeTag = XmlRpcUtil::getNextTag(valueXml, offset);
00229     bool result = false;
00230     if (typeTag == BOOLEAN_TAG)
00231       result = boolFromXml(valueXml, offset);
00232     else if (typeTag == I4_TAG || typeTag == INT_TAG)
00233       result = intFromXml(valueXml, offset);
00234     else if (typeTag == DOUBLE_TAG)
00235       result = doubleFromXml(valueXml, offset);
00236     else if (typeTag.empty() || typeTag == STRING_TAG)
00237       result = stringFromXml(valueXml, offset);
00238     else if (typeTag == DATETIME_TAG)
00239       result = timeFromXml(valueXml, offset);
00240     else if (typeTag == BASE64_TAG)
00241       result = binaryFromXml(valueXml, offset);
00242     else if (typeTag == ARRAY_TAG)
00243       result = arrayFromXml(valueXml, offset);
00244     else if (typeTag == STRUCT_TAG)
00245       result = structFromXml(valueXml, offset);
00246     // Watch for empty/blank strings with no <string>tag
00247     else if (typeTag == VALUE_ETAG)
00248     {
00249       *offset = afterValueOffset;   // back up & try again
00250       result = stringFromXml(valueXml, offset);
00251     }
00252 
00253     if (result)  // Skip over the </value> tag
00254       XmlRpcUtil::findTag(VALUE_ETAG, valueXml, offset);
00255     else        // Unrecognized tag after <value>
00256       *offset = savedOffset;
00257 
00258     return result;
00259   }

static std::string const& XmlRpc::XmlRpcValue::getDoubleFormat (  )  [inline, static]

Return the format used to write double values.

Definition at line 202 of file XmlRpcValue.h.

Referenced by doubleToXml().

00202 { return _doubleFormat; }

Type const& XmlRpc::XmlRpcValue::getType (  )  const [inline]

Return the type of the value stored.

See also:
Type.

Definition at line 180 of file XmlRpcValue.h.

Referenced by XmlRpc::XmlRpcServer::executeMulticall(), and XmlRpc::XmlRpcClient::generateRequest().

00180 { return _type; }

bool XmlRpc::XmlRpcValue::hasMember ( const std::string &  name  )  const

Check for the existence of a struct member by name.

Definition at line 212 of file XmlRpcValue.cc.

00213   {
00214     return _type == TypeStruct && _value.asStruct->find(name) != _value.asStruct->end();
00215   }

bool XmlRpc::XmlRpcValue::intFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

Definition at line 305 of file XmlRpcValue.cc.

Referenced by fromXml().

00306   {
00307     const char* valueStart = valueXml.c_str() + *offset;
00308     char* valueEnd;
00309     long ivalue = strtol(valueStart, &valueEnd, 10);
00310     if (valueEnd == valueStart)
00311       return false;
00312 
00313     _type = TypeInt;
00314     _value.asInt = int(ivalue);
00315     *offset += int(valueEnd - valueStart);
00316     return true;
00317   }

std::string XmlRpc::XmlRpcValue::intToXml (  )  const [protected]

Definition at line 319 of file XmlRpcValue.cc.

Referenced by toXml().

00320   {
00321     char buf[256];
00322     snprintf(buf, sizeof(buf)-1, "%d", _value.asInt);
00323     buf[sizeof(buf)-1] = 0;
00324     std::string xml = VALUE_TAG;
00325     xml += I4_TAG;
00326     xml += buf;
00327     xml += I4_ETAG;
00328     xml += VALUE_ETAG;
00329     return xml;
00330   }

void XmlRpc::XmlRpcValue::invalidate (  )  [protected]

Definition at line 58 of file XmlRpcValue.cc.

Referenced by clear(), fromXml(), operator=(), structFromXml(), and ~XmlRpcValue().

00059   {
00060     switch (_type) {
00061       case TypeString:    delete _value.asString; break;
00062       case TypeDateTime:  delete _value.asTime;   break;
00063       case TypeBase64:    delete _value.asBinary; break;
00064       case TypeArray:     delete _value.asArray;  break;
00065       case TypeStruct:    delete _value.asStruct; break;
00066       default: break;
00067     }
00068     _type = TypeInvalid;
00069     _value.asBinary = 0;
00070   }

bool XmlRpc::XmlRpcValue::isValid ( void   )  const [inline]

Return true if the value has been set to something.

Definition at line 177 of file XmlRpcValue.h.

Referenced by XmlRpc::XmlRpcServer::executeMethod(), XmlRpc::XmlRpcClient::generateRequest(), XmlRpc::XmlRpcClient::parseResponse(), and structFromXml().

00177 { return _type != TypeInvalid; }

XmlRpc::XmlRpcValue::operator BinaryData & (  )  [inline]

Access the BinaryData value.

Throws XmlRpcException if the value is initialized to a type that is not TypeBase64.

Definition at line 144 of file XmlRpcValue.h.

00144 { assertTypeOrInvalid(TypeBase64); return *_value.asBinary; }

XmlRpc::XmlRpcValue::operator bool & (  )  [inline]

Treat an XmlRpcValue as a bool.

Throws XmlRpcException if the value is initialized to a type that is not TypeBoolean.

Definition at line 124 of file XmlRpcValue.h.

00124 { assertTypeOrInvalid(TypeBoolean); return _value.asBool; }

XmlRpc::XmlRpcValue::operator double & (  )  [inline]

Treat an XmlRpcValue as a double.

Throws XmlRpcException if the value is initialized to a type that is not TypeDouble.

Definition at line 134 of file XmlRpcValue.h.

00134 { assertTypeOrInvalid(TypeDouble); return _value.asDouble; }

XmlRpc::XmlRpcValue::operator int & (  )  [inline]

Treat an XmlRpcValue as an int.

Throws XmlRpcException if the value is initialized to a type that is not TypeInt.

Definition at line 129 of file XmlRpcValue.h.

00129 { assertTypeOrInvalid(TypeInt); return _value.asInt; }

XmlRpc::XmlRpcValue::operator std::string & (  )  [inline]

Treat an XmlRpcValue as a string.

Throws XmlRpcException if the value is initialized to a type that is not TypeString.

Definition at line 139 of file XmlRpcValue.h.

00139 { assertTypeOrInvalid(TypeString); return *_value.asString; }

XmlRpc::XmlRpcValue::operator struct tm & (  )  [inline]

Access the DateTime value.

Throws XmlRpcException if the value is initialized to a type that is not TypeDateTime.

Definition at line 149 of file XmlRpcValue.h.

00149 { assertTypeOrInvalid(TypeDateTime); return *_value.asTime; }

XmlRpc::XmlRpcValue::operator ValueStruct const & (  )  [inline]

Access the struct value map.

Can be used to iterate over the entries in the map to find all defined entries.

Definition at line 173 of file XmlRpcValue.h.

00173 { assertStruct(); return *_value.asStruct; } 

bool XmlRpc::XmlRpcValue::operator!= ( XmlRpcValue const &  other  )  const

Tests two XmlRpcValues for inequality.

Definition at line 191 of file XmlRpcValue.cc.

00192   {
00193     return !(*this == other);
00194   }

XmlRpcValue& XmlRpc::XmlRpcValue::operator= ( const char *  rhs  )  [inline]

Assign a string to this XmlRpcValue.

Definition at line 113 of file XmlRpcValue.h.

Referenced by operator=().

00113 { return operator=(XmlRpcValue(std::string(rhs))); }

XmlRpcValue& XmlRpc::XmlRpcValue::operator= ( double const &  rhs  )  [inline]

Assign a double to this XmlRpcValue.

Definition at line 110 of file XmlRpcValue.h.

Referenced by operator=().

00110 { return operator=(XmlRpcValue(rhs)); }

XmlRpcValue& XmlRpc::XmlRpcValue::operator= ( int const &  rhs  )  [inline]

Assign an int to this XmlRpcValue.

Definition at line 107 of file XmlRpcValue.h.

Referenced by operator=().

00107 { return operator=(XmlRpcValue(rhs)); }

XmlRpcValue & XmlRpc::XmlRpcValue::operator= ( XmlRpcValue const &  rhs  ) 

Assignment from one XmlRpcValue to this one.

Parameters:
rhs The value in rhs is copied to this value.

Definition at line 124 of file XmlRpcValue.cc.

00125   {
00126     if (this != &rhs)
00127     {
00128       invalidate();
00129       _type = rhs._type;
00130       switch (_type) {
00131         case TypeBoolean:  _value.asBool = rhs._value.asBool; break;
00132         case TypeInt:      _value.asInt = rhs._value.asInt; break;
00133         case TypeDouble:   _value.asDouble = rhs._value.asDouble; break;
00134         case TypeDateTime: _value.asTime = new struct tm(*rhs._value.asTime); break;
00135         case TypeString:   _value.asString = new std::string(*rhs._value.asString); break;
00136         case TypeBase64:   _value.asBinary = new BinaryData(*rhs._value.asBinary); break;
00137         case TypeArray:    _value.asArray = new ValueArray(*rhs._value.asArray); break;
00138         case TypeStruct:   _value.asStruct = new ValueStruct(*rhs._value.asStruct); break;
00139         default:           _value.asBinary = 0; break;
00140       }
00141     }
00142     return *this;
00143   }

bool XmlRpc::XmlRpcValue::operator== ( XmlRpcValue const &  other  )  const

Tests two XmlRpcValues for equality.

Definition at line 153 of file XmlRpcValue.cc.

00154   {
00155     if (_type != other._type)
00156       return false;
00157 
00158     switch (_type) {
00159       case TypeBoolean:  return ( !_value.asBool && !other._value.asBool) ||
00160                                 ( _value.asBool && other._value.asBool);
00161       case TypeInt:      return _value.asInt == other._value.asInt;
00162       case TypeDouble:   return _value.asDouble == other._value.asDouble;
00163       case TypeDateTime: return tmEq(*_value.asTime, *other._value.asTime);
00164       case TypeString:   return *_value.asString == *other._value.asString;
00165       case TypeBase64:   return *_value.asBinary == *other._value.asBinary;
00166       case TypeArray:    return *_value.asArray == *other._value.asArray;
00167 
00168       // The map<>::operator== requires the definition of value< for kcc
00169       case TypeStruct:   //return *_value.asStruct == *other._value.asStruct;
00170         {
00171           if (_value.asStruct->size() != other._value.asStruct->size())
00172             return false;
00173           
00174           ValueStruct::const_iterator it1=_value.asStruct->begin();
00175           ValueStruct::const_iterator it2=other._value.asStruct->begin();
00176           while (it1 != _value.asStruct->end()) {
00177             const XmlRpcValue& v1 = it1->second;
00178             const XmlRpcValue& v2 = it2->second;
00179             if ( ! (v1 == v2))
00180               return false;
00181             it1++;
00182             it2++;
00183           }
00184           return true;
00185         }
00186       default: break;
00187     }
00188     return true;    // Both invalid values ...
00189   }

XmlRpcValue& XmlRpc::XmlRpcValue::operator[] ( const char *  k  )  [inline]

Struct entry accessor.

Returns the value associated with the given entry, creating one if necessary.

Definition at line 169 of file XmlRpcValue.h.

00169 { assertStruct(); std::string s(k); return (*_value.asStruct)[s]; }

XmlRpcValue& XmlRpc::XmlRpcValue::operator[] ( std::string const &  k  )  [inline]

Struct entry accessor.

Returns the value associated with the given entry, creating one if necessary.

Definition at line 165 of file XmlRpcValue.h.

00165 { assertStruct(); return (*_value.asStruct)[k]; }

XmlRpcValue& XmlRpc::XmlRpcValue::operator[] ( int  i  )  [inline]

Array value accessor.

Access the ith value of the array, growing the array if necessary. Throws XmlRpcException if the value is not an array.

Definition at line 161 of file XmlRpcValue.h.

00161 { assertArray(i+1); return _value.asArray->at(i); }

XmlRpcValue const& XmlRpc::XmlRpcValue::operator[] ( int  i  )  const [inline]

Const array value accessor.

Access the ith value of the array. Throws XmlRpcException if the value is not an array or if the index i is not a valid index for the array.

Definition at line 156 of file XmlRpcValue.h.

00156 { assertArray(i+1); return _value.asArray->at(i); }

static void XmlRpc::XmlRpcValue::setDoubleFormat ( const char *  f  )  [inline, static]

Specify the format used to write double values.

Definition at line 205 of file XmlRpcValue.h.

00205 { _doubleFormat = f; }

void XmlRpc::XmlRpcValue::setSize ( int  size  )  [inline]

Specify the size for array values. Array values will grow beyond this size if needed.

Definition at line 186 of file XmlRpcValue.h.

Referenced by XmlRpc::XmlRpcServer::executeMulticall(), XmlRpcInterface::handleRpcResponse(), and XmlRpc::XmlRpcServer::listMethods().

00186 { assertArray(size); }

int XmlRpc::XmlRpcValue::size (  )  const

Return the size for string, base64, array, and struct values.

Definition at line 198 of file XmlRpcValue.cc.

Referenced by XmlRpcInterface::dumpDht(), XmlRpc::XmlRpcServer::executeMulticall(), XmlRpc::XmlRpcClient::generateRequest(), XmlRpcInterface::get(), XmlRpcInterface::joinOverlay(), XmlRpcInterface::localLookup(), XmlRpcInterface::lookup(), XmlRpcInterface::p2pnsRegister(), XmlRpcInterface::p2pnsResolve(), and XmlRpcInterface::put().

00199   {
00200     switch (_type) {
00201       case TypeString: return int(_value.asString->size());
00202       case TypeBase64: return int(_value.asBinary->size());
00203       case TypeArray:  return int(_value.asArray->size());
00204       case TypeStruct: return int(_value.asStruct->size());
00205       default: break;
00206     }
00207 
00208     throw XmlRpcException("type error");
00209   }

bool XmlRpc::XmlRpcValue::stringFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

Definition at line 362 of file XmlRpcValue.cc.

Referenced by fromXml().

00363   {
00364     size_t valueEnd = valueXml.find('<', *offset);
00365     if (valueEnd == std::string::npos)
00366       return false;     // No end tag;
00367 
00368     _type = TypeString;
00369     _value.asString = new std::string(XmlRpcUtil::xmlDecode(valueXml.substr(*offset, valueEnd-*offset)));
00370     *offset += int(_value.asString->length());
00371     return true;
00372   }

std::string XmlRpc::XmlRpcValue::stringToXml (  )  const [protected]

Definition at line 374 of file XmlRpcValue.cc.

Referenced by toXml().

00375   {
00376     std::string xml = VALUE_TAG;
00377     //xml += STRING_TAG; optional
00378     xml += XmlRpcUtil::xmlEncode(*_value.asString);
00379     //xml += STRING_ETAG;
00380     xml += VALUE_ETAG;
00381     return xml;
00382   }

bool XmlRpc::XmlRpcValue::structFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

Definition at line 502 of file XmlRpcValue.cc.

Referenced by fromXml().

00503   {
00504     _type = TypeStruct;
00505     _value.asStruct = new ValueStruct;
00506 
00507     while (XmlRpcUtil::nextTagIs(MEMBER_TAG, valueXml, offset)) {
00508       // name
00509       const std::string name = XmlRpcUtil::parseTag(NAME_TAG, valueXml, offset);
00510       // value
00511       XmlRpcValue val(valueXml, offset);
00512       if ( ! val.isValid()) {
00513         invalidate();
00514         return false;
00515       }
00516       const std::pair<const std::string, XmlRpcValue> p(name, val);
00517       _value.asStruct->insert(p);
00518 
00519       (void) XmlRpcUtil::nextTagIs(MEMBER_ETAG, valueXml, offset);
00520     }
00521     return true;
00522   }

std::string XmlRpc::XmlRpcValue::structToXml (  )  const [protected]

Definition at line 527 of file XmlRpcValue.cc.

Referenced by toXml().

00528   {
00529     std::string xml = VALUE_TAG;
00530     xml += STRUCT_TAG;
00531 
00532     ValueStruct::const_iterator it;
00533     for (it=_value.asStruct->begin(); it!=_value.asStruct->end(); ++it) {
00534       xml += MEMBER_TAG;
00535       xml += NAME_TAG;
00536       xml += XmlRpcUtil::xmlEncode(it->first);
00537       xml += NAME_ETAG;
00538       xml += it->second.toXml();
00539       xml += MEMBER_ETAG;
00540     }
00541 
00542     xml += STRUCT_ETAG;
00543     xml += VALUE_ETAG;
00544     return xml;
00545   }

bool XmlRpc::XmlRpcValue::timeFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

Definition at line 385 of file XmlRpcValue.cc.

Referenced by fromXml().

00386   {
00387     size_t valueEnd = valueXml.find('<', *offset);
00388     if (valueEnd == std::string::npos)
00389       return false;     // No end tag;
00390 
00391     std::string stime = valueXml.substr(*offset, valueEnd-*offset);
00392 
00393     struct tm t;
00394     if (sscanf(stime.c_str(),"%4d%2d%2dT%2d:%2d:%2d",&t.tm_year,&t.tm_mon,&t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec) != 6)
00395       return false;
00396 
00397     t.tm_year -= 1900;
00398     t.tm_isdst = -1;
00399     _type = TypeDateTime;
00400     _value.asTime = new struct tm(t);
00401     *offset += int(stime.length());
00402     return true;
00403   }

std::string XmlRpc::XmlRpcValue::timeToXml (  )  const [protected]

Definition at line 405 of file XmlRpcValue.cc.

Referenced by toXml().

00406   {
00407     struct tm* t = _value.asTime;
00408     char buf[20];
00409     snprintf(buf, sizeof(buf)-1, "%04d%02d%02dT%02d:%02d:%02d", 
00410       1900+t->tm_year,t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
00411     buf[sizeof(buf)-1] = 0;
00412 
00413     std::string xml = VALUE_TAG;
00414     xml += DATETIME_TAG;
00415     xml += buf;
00416     xml += DATETIME_ETAG;
00417     xml += VALUE_ETAG;
00418     return xml;
00419   }

std::string XmlRpc::XmlRpcValue::toXml (  )  const

Encode the Value in xml.

Definition at line 262 of file XmlRpcValue.cc.

Referenced by XmlRpc::XmlRpcServer::executeRequest(), XmlRpc::XmlRpcServer::generateFaultResponse(), XmlRpc::XmlRpcClient::generateRequest(), and XmlRpcInterface::handleRpcResponse().

00263   {
00264     switch (_type) {
00265       case TypeBoolean:  return boolToXml();
00266       case TypeInt:      return intToXml();
00267       case TypeDouble:   return doubleToXml();
00268       case TypeString:   return stringToXml();
00269       case TypeDateTime: return timeToXml();
00270       case TypeBase64:   return binaryToXml();
00271       case TypeArray:    return arrayToXml();
00272       case TypeStruct:   return structToXml();
00273       default: break;
00274     }
00275     return std::string();   // Invalid value
00276   }

std::ostream & XmlRpc::XmlRpcValue::write ( std::ostream &  os  )  const

Write the value (no xml encoding).

Definition at line 550 of file XmlRpcValue.cc.

Referenced by operator<<().

00550                                                      {
00551     switch (_type) {
00552       default:           break;
00553       case TypeBoolean:  os << _value.asBool; break;
00554       case TypeInt:      os << _value.asInt; break;
00555       case TypeDouble:   os << _value.asDouble; break;
00556       case TypeString:   os << *_value.asString; break;
00557       case TypeDateTime:
00558         {
00559           struct tm* t = _value.asTime;
00560           char buf[20];
00561           snprintf(buf, sizeof(buf)-1, "%4d%02d%02dT%02d:%02d:%02d", 
00562             t->tm_year,t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
00563           buf[sizeof(buf)-1] = 0;
00564           os << buf;
00565           break;
00566         }
00567       case TypeBase64:
00568         {
00569           int iostatus = 0;
00570           std::ostreambuf_iterator<char> out(os);
00571           base64<char> encoder;
00572           encoder.put(_value.asBinary->begin(), _value.asBinary->end(), out, iostatus, base64<>::crlf());
00573           break;
00574         }
00575       case TypeArray:
00576         {
00577           int s = int(_value.asArray->size());
00578           os << '{';
00579           for (int i=0; i<s; ++i)
00580           {
00581             if (i > 0) os << ',';
00582             _value.asArray->at(i).write(os);
00583           }
00584           os << '}';
00585           break;
00586         }
00587       case TypeStruct:
00588         {
00589           os << '[';
00590           ValueStruct::const_iterator it;
00591           for (it=_value.asStruct->begin(); it!=_value.asStruct->end(); ++it)
00592           {
00593             if (it!=_value.asStruct->begin()) os << ',';
00594             os << it->first << ':';
00595             it->second.write(os);
00596           }
00597           os << ']';
00598           break;
00599         }
00600       
00601     }
00602     
00603     return os;
00604   }


Member Data Documentation

std::string XmlRpc::XmlRpcValue::_doubleFormat [static, protected]

Definition at line 239 of file XmlRpcValue.h.

union { ... } XmlRpc::XmlRpcValue::_value [protected]

Definition at line 253 of file XmlRpcValue.h.

Referenced by operator=(), and operator==().

Definition at line 252 of file XmlRpcValue.h.

Referenced by operator=(), and operator==().

Definition at line 247 of file XmlRpcValue.h.

Referenced by operator=(), and operator==().

Definition at line 249 of file XmlRpcValue.h.

Referenced by operator=(), and operator==().

Definition at line 248 of file XmlRpcValue.h.

Referenced by operator=(), and operator==().

Definition at line 251 of file XmlRpcValue.h.

Referenced by binaryFromXml(), operator=(), and operator==().

Definition at line 254 of file XmlRpcValue.h.

Referenced by operator=(), and operator==().

Definition at line 250 of file XmlRpcValue.h.

Referenced by operator=(), and operator==().


The documentation for this class was generated from the following files:
Generated on Wed May 26 16:21:20 2010 for OverSim by  doxygen 1.6.3