initial load of upstream version 1.06.32
[xmlrpc-c] / tools / xml-rpc-api2cpp / DataType.cpp
1 #include <iostream>
2 #include <sstream>
3 #include <stdexcept>
4
5 #include "xmlrpc-c/oldcppwrapper.hpp"
6 #include "DataType.hpp"
7
8 using namespace std;
9
10
11
12 //=========================================================================
13 //  abstract class DataType
14 //=========================================================================
15 //  Instances of DataType know how generate code fragments for manipulating
16 //  a specific XML-RPC data type.
17
18 string DataType::defaultParameterBaseName (int position) const {
19     ostringstream name_stream;
20     name_stream << typeName() << position << ends;
21     string name(name_stream.str());
22     return name;
23 }
24
25
26 //=========================================================================
27 //  class RawDataType
28 //=========================================================================
29 //  We want to manipulate some XML-RPC data types as XmlRpcValue objects.
30
31 class RawDataType : public DataType {
32 public:
33     RawDataType (const string& type_name) : DataType(type_name) {}
34     
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;
39 };
40
41 string RawDataType::parameterFragment (const string& base_name) const {
42     return "XmlRpcValue /*" + typeName() + "*/ " + base_name;
43 }
44
45 string RawDataType::inputConversionFragment (const string& base_name) const {
46     return base_name;
47 }
48
49 string RawDataType::returnTypeFragment () const {
50     return "XmlRpcValue /*" + typeName() + "*/";
51 }
52
53 string RawDataType::outputConversionFragment (const string& var_name) const {
54     return var_name;
55 }
56
57
58 //=========================================================================
59 //  class SimpleDataType
60 //=========================================================================
61 //  Other types can be easily converted to and from a single native type.
62
63 class SimpleDataType : public DataType {
64     string mNativeType;
65     string mMakerFunc;
66     string mGetterFunc;
67
68 public:
69     SimpleDataType (const string& type_name,
70                     const string& native_type,
71                     const string& maker_func,
72                     const string& getter_func);
73
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;
78 };
79
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)
88 {
89 }
90
91 string SimpleDataType::parameterFragment (const string& base_name) const {
92     return mNativeType + " " + base_name;
93 }
94
95 string SimpleDataType::inputConversionFragment (const string& base_name) const
96 {
97     return mMakerFunc + "(" + base_name + ")";
98 }
99
100 string SimpleDataType::returnTypeFragment () const {
101     return mNativeType; 
102 }
103
104 string SimpleDataType::outputConversionFragment (const string& var_name) const
105 {
106     return var_name + "." + mGetterFunc + "()";
107 }
108
109
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.
115
116 class VoidDataType : public DataType {
117 public:
118     VoidDataType () : DataType("void") {}
119     
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;
124 };
125
126 string VoidDataType::parameterFragment (const string&) const {
127     throw domain_error("Can't handle functions with 'void' arguments'");
128     
129 }
130
131 string VoidDataType::inputConversionFragment (const string&) const {
132     throw domain_error("Can't handle functions with 'void' arguments'");
133 }
134
135 string VoidDataType::returnTypeFragment () const {
136     return "void";
137 }
138
139 string VoidDataType::outputConversionFragment (const string&) const {
140     return "/* Return value ignored. */";
141 }
142
143
144 //=========================================================================
145 //  function findDataType
146 //=========================================================================
147 //  Given the name of an XML-RPC data type, try to find a corresponding
148 //  DataType object.
149
150 SimpleDataType intType    ("int", "XmlRpcValue::int32",
151                            "XmlRpcValue::makeInt",
152                            "getInt");
153 SimpleDataType boolType   ("bool", "bool",
154                            "XmlRpcValue::makeBool",
155                            "getBool");
156 SimpleDataType doubleType ("double", "double",
157                            "XmlRpcValue::makeDouble",
158                            "getDouble");
159 SimpleDataType stringType ("string", "string",
160                            "XmlRpcValue::makeString",
161                            "getString");
162
163 RawDataType dateTimeType  ("dateTime");
164 RawDataType base64Type    ("base64");
165 RawDataType structType    ("struct");
166 RawDataType arrayType     ("array");
167
168 VoidDataType voidType;
169
170 const DataType& findDataType (const string& name) {
171     if (name == "int" || name == "i4")
172         return intType;
173     else if (name == "boolean")
174         return boolType;
175     else if (name == "double")
176         return doubleType;
177     else if (name == "string")
178         return stringType;
179     else if (name == "dateTime.iso8601")
180         return dateTimeType;
181     else if (name == "base64")
182         return base64Type;
183     else if (name == "struct")
184         return structType;
185     else if (name == "array")
186         return arrayType;
187     else if (name == "void")
188         return voidType;
189     else
190         throw domain_error("Unknown XML-RPC type " + name);
191     
192     // This code should never be executed.
193     XMLRPC_ASSERT(0);
194     return intType;
195 }