1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <map>
#include <vector>
#include "xmlrpc-c/oldcppwrapper.hpp"
#include "DataType.hpp"
using namespace std;
//=========================================================================
// abstract class DataType
//=========================================================================
// Instances of DataType know how generate code fragments for manipulating
// a specific XML-RPC data type.
string
DataType::defaultParameterBaseName(unsigned int const position) const {
ostringstream nameStream;
nameStream << typeName() << position;
return nameStream.str();
}
class RawDataType : public DataType {
public:
RawDataType(string const& typeName) : DataType(typeName) {}
virtual string
parameterFragment(string const& baseName) const;
virtual string
inputConversionFragment(string const& baseName) const;
virtual string
returnTypeFragment() const;
virtual string
outputConversionFragment(string const& varName) const;
};
string
RawDataType::parameterFragment(string const& baseName) const {
return "XmlRpcValue /*" + typeName() + "*/ " + baseName;
}
string
RawDataType::inputConversionFragment(string const& baseName) const {
return baseName;
}
string
RawDataType::returnTypeFragment() const {
return "XmlRpcValue /*" + typeName() + "*/";
}
string
RawDataType::outputConversionFragment(string const& varName) const {
return varName;
}
class SimpleDataType : public DataType {
string mNativeType;
string mMakerFunc;
string mGetterFunc;
public:
SimpleDataType(string const& typeName,
string const& nativeType,
string const& makerFunc,
string const& getterFunc);
virtual string
parameterFragment(string const& baseName) const;
virtual string
inputConversionFragment(string const& baseName) const;
virtual string
returnTypeFragment() const;
virtual string
outputConversionFragment(string const& varName) const;
};
SimpleDataType::SimpleDataType(string const& typeName,
string const& nativeType,
string const& makerFunc,
string const& getterFunc)
: DataType(typeName),
mNativeType(nativeType),
mMakerFunc(makerFunc),
mGetterFunc(getterFunc) {
}
string
SimpleDataType::parameterFragment(string const& baseName) const {
return mNativeType + " const " + baseName;
}
string
SimpleDataType::inputConversionFragment(string const& baseName) const {
return mMakerFunc + "(" + baseName + ")";
}
string
SimpleDataType::returnTypeFragment() const {
return mNativeType;
}
string
SimpleDataType::outputConversionFragment(string const& varName) const {
return varName + "." + mGetterFunc + "()";
}
class VoidDataType : public DataType {
public:
VoidDataType() : DataType("void") {}
virtual string
parameterFragment(string const& baseName) const;
virtual string
inputConversionFragment(string const& baseName) const;
virtual string
returnTypeFragment() const;
virtual string
outputConversionFragment(string const& varName) const;
};
string
VoidDataType::parameterFragment(string const&) const {
throw domain_error("Can't handle functions with 'void' arguments'");
}
string
VoidDataType::inputConversionFragment(string const&) const {
throw domain_error("Can't handle functions with 'void' arguments'");
}
string
VoidDataType::returnTypeFragment () const {
return "void";
}
string
VoidDataType::outputConversionFragment(string const&) const {
return "/* Return value ignored. */";
}
static SimpleDataType const intType ("int", "XmlRpcValue::int32",
"XmlRpcValue::makeInt",
"getInt");
static SimpleDataType const boolType ("bool", "bool",
"XmlRpcValue::makeBool",
"getBool");
static SimpleDataType const doubleType ("double", "double",
"XmlRpcValue::makeDouble",
"getDouble");
static SimpleDataType const stringType ("string", "std::string",
"XmlRpcValue::makeString",
"getString");
static RawDataType const dateTimeType ("dateTime");
static RawDataType const base64Type ("base64");
static RawDataType const structType ("struct");
static RawDataType const arrayType ("array");
static VoidDataType const voidType;
const DataType&
findDataType(string const& name) {
/*----------------------------------------------------------------------------
Given the name of an XML-RPC data type, try to find a corresponding
DataType object.
-----------------------------------------------------------------------------*/
if (name == "int" || name == "i4")
return intType;
else if (name == "boolean")
return boolType;
else if (name == "double")
return doubleType;
else if (name == "string")
return stringType;
else if (name == "dateTime.iso8601")
return dateTimeType;
else if (name == "base64")
return base64Type;
else if (name == "struct")
return structType;
else if (name == "array")
return arrayType;
else if (name == "void")
return voidType;
else if (name == "INT")
return intType;
else if (name == "BOOLEAN")
return boolType;
else if (name == "DOUBLE")
return doubleType;
else if (name == "STRING")
return stringType;
else if (name == "DATETIME.ISO8601")
return dateTimeType;
else if (name == "BASE64")
return base64Type;
else if (name == "STRUCT")
return structType;
else if (name == "ARRAY")
return arrayType;
else if (name == "VOID")
return voidType;
else if (name == "NIL")
return voidType;
else
throw domain_error("Unknown XML-RPC type name '" + name + "'");
}
|