5 #include "xmlrpc-c/oldcppwrapper.hpp"
6 #include "DataType.hpp"
12 //=========================================================================
13 // abstract class DataType
14 //=========================================================================
15 // Instances of DataType know how generate code fragments for manipulating
16 // a specific XML-RPC data type.
18 string DataType::defaultParameterBaseName (int position) const {
19 ostringstream name_stream;
20 name_stream << typeName() << position << ends;
21 string name(name_stream.str());
26 //=========================================================================
28 //=========================================================================
29 // We want to manipulate some XML-RPC data types as XmlRpcValue objects.
31 class RawDataType : public DataType {
33 RawDataType (const string& type_name) : DataType(type_name) {}
35 virtual string parameterFragment (const string& base_name) const;
36 virtual string inputConversionFragment (const string& base_name) const;
37 virtual string returnTypeFragment () const;
38 virtual string outputConversionFragment (const string& var_name) const;
41 string RawDataType::parameterFragment (const string& base_name) const {
42 return "XmlRpcValue /*" + typeName() + "*/ " + base_name;
45 string RawDataType::inputConversionFragment (const string& base_name) const {
49 string RawDataType::returnTypeFragment () const {
50 return "XmlRpcValue /*" + typeName() + "*/";
53 string RawDataType::outputConversionFragment (const string& var_name) const {
58 //=========================================================================
59 // class SimpleDataType
60 //=========================================================================
61 // Other types can be easily converted to and from a single native type.
63 class SimpleDataType : public DataType {
69 SimpleDataType (const string& type_name,
70 const string& native_type,
71 const string& maker_func,
72 const string& getter_func);
74 virtual string parameterFragment (const string& base_name) const;
75 virtual string inputConversionFragment (const string& base_name) const;
76 virtual string returnTypeFragment () const;
77 virtual string outputConversionFragment (const string& var_name) const;
80 SimpleDataType::SimpleDataType (const string& type_name,
81 const string& native_type,
82 const string& maker_func,
83 const string& getter_func)
84 : DataType(type_name),
85 mNativeType(native_type),
86 mMakerFunc(maker_func),
87 mGetterFunc(getter_func)
91 string SimpleDataType::parameterFragment (const string& base_name) const {
92 return mNativeType + " " + base_name;
95 string SimpleDataType::inputConversionFragment (const string& base_name) const
97 return mMakerFunc + "(" + base_name + ")";
100 string SimpleDataType::returnTypeFragment () const {
104 string SimpleDataType::outputConversionFragment (const string& var_name) const
106 return var_name + "." + mGetterFunc + "()";
110 //=========================================================================
111 // class VoidDataType
112 //=========================================================================
113 // Some XML-RPC servers declare functions as void. Such functions have
114 // an arbitrary return value which we should ignore.
116 class VoidDataType : public DataType {
118 VoidDataType () : DataType("void") {}
120 virtual string parameterFragment (const string& base_name) const;
121 virtual string inputConversionFragment (const string& base_name) const;
122 virtual string returnTypeFragment () const;
123 virtual string outputConversionFragment (const string& var_name) const;
126 string VoidDataType::parameterFragment (const string&) const {
127 throw domain_error("Can't handle functions with 'void' arguments'");
131 string VoidDataType::inputConversionFragment (const string&) const {
132 throw domain_error("Can't handle functions with 'void' arguments'");
135 string VoidDataType::returnTypeFragment () const {
139 string VoidDataType::outputConversionFragment (const string&) const {
140 return "/* Return value ignored. */";
144 //=========================================================================
145 // function findDataType
146 //=========================================================================
147 // Given the name of an XML-RPC data type, try to find a corresponding
150 SimpleDataType intType ("int", "XmlRpcValue::int32",
151 "XmlRpcValue::makeInt",
153 SimpleDataType boolType ("bool", "bool",
154 "XmlRpcValue::makeBool",
156 SimpleDataType doubleType ("double", "double",
157 "XmlRpcValue::makeDouble",
159 SimpleDataType stringType ("string", "string",
160 "XmlRpcValue::makeString",
163 RawDataType dateTimeType ("dateTime");
164 RawDataType base64Type ("base64");
165 RawDataType structType ("struct");
166 RawDataType arrayType ("array");
168 VoidDataType voidType;
170 const DataType& findDataType (const string& name) {
171 if (name == "int" || name == "i4")
173 else if (name == "boolean")
175 else if (name == "double")
177 else if (name == "string")
179 else if (name == "dateTime.iso8601")
181 else if (name == "base64")
183 else if (name == "struct")
185 else if (name == "array")
187 else if (name == "void")
190 throw domain_error("Unknown XML-RPC type " + name);
192 // This code should never be executed.