File: DataType.cpp

package info (click to toggle)
xmlrpc-c 1.06.27-1.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,308 kB
  • ctags: 5,111
  • sloc: ansic: 39,324; sh: 8,284; cpp: 6,051; makefile: 1,339; perl: 494; xml: 134
file content (195 lines) | stat: -rw-r--r-- 5,930 bytes parent folder | download | duplicates (2)
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
#include <iostream>
#include <sstream>
#include <stdexcept>

#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 (int position) const {
    ostringstream name_stream;
    name_stream << typeName() << position << ends;
    string name(name_stream.str());
    return name;
}


//=========================================================================
//  class RawDataType
//=========================================================================
//  We want to manipulate some XML-RPC data types as XmlRpcValue objects.

class RawDataType : public DataType {
public:
    RawDataType (const string& type_name) : DataType(type_name) {}
    
    virtual string parameterFragment (const string& base_name) const;
    virtual string inputConversionFragment (const string& base_name) const;
    virtual string returnTypeFragment () const;
    virtual string outputConversionFragment (const string& var_name) const;
};

string RawDataType::parameterFragment (const string& base_name) const {
    return "XmlRpcValue /*" + typeName() + "*/ " + base_name;
}

string RawDataType::inputConversionFragment (const string& base_name) const {
    return base_name;
}

string RawDataType::returnTypeFragment () const {
    return "XmlRpcValue /*" + typeName() + "*/";
}

string RawDataType::outputConversionFragment (const string& var_name) const {
    return var_name;
}


//=========================================================================
//  class SimpleDataType
//=========================================================================
//  Other types can be easily converted to and from a single native type.

class SimpleDataType : public DataType {
    string mNativeType;
    string mMakerFunc;
    string mGetterFunc;

public:
    SimpleDataType (const string& type_name,
		    const string& native_type,
		    const string& maker_func,
		    const string& getter_func);

    virtual string parameterFragment (const string& base_name) const;
    virtual string inputConversionFragment (const string& base_name) const;
    virtual string returnTypeFragment () const;
    virtual string outputConversionFragment (const string& var_name) const;
};

SimpleDataType::SimpleDataType (const string& type_name,
				const string& native_type,
				const string& maker_func,
				const string& getter_func)
    : DataType(type_name),
      mNativeType(native_type),
      mMakerFunc(maker_func),
      mGetterFunc(getter_func)
{
}

string SimpleDataType::parameterFragment (const string& base_name) const {
    return mNativeType + " " + base_name;
}

string SimpleDataType::inputConversionFragment (const string& base_name) const
{
    return mMakerFunc + "(" + base_name + ")";
}

string SimpleDataType::returnTypeFragment () const {
    return mNativeType; 
}

string SimpleDataType::outputConversionFragment (const string& var_name) const
{
    return var_name + "." + mGetterFunc + "()";
}


//=========================================================================
//  class VoidDataType
//=========================================================================
//  Some XML-RPC servers declare functions as void.  Such functions have
//  an arbitrary return value which we should ignore.

class VoidDataType : public DataType {
public:
    VoidDataType () : DataType("void") {}
    
    virtual string parameterFragment (const string& base_name) const;
    virtual string inputConversionFragment (const string& base_name) const;
    virtual string returnTypeFragment () const;
    virtual string outputConversionFragment (const string& var_name) const;
};

string VoidDataType::parameterFragment (const string&) const {
    throw domain_error("Can't handle functions with 'void' arguments'");
    
}

string VoidDataType::inputConversionFragment (const string&) const {
    throw domain_error("Can't handle functions with 'void' arguments'");
}

string VoidDataType::returnTypeFragment () const {
    return "void";
}

string VoidDataType::outputConversionFragment (const string&) const {
    return "/* Return value ignored. */";
}


//=========================================================================
//  function findDataType
//=========================================================================
//  Given the name of an XML-RPC data type, try to find a corresponding
//  DataType object.

SimpleDataType intType    ("int", "XmlRpcValue::int32",
			   "XmlRpcValue::makeInt",
			   "getInt");
SimpleDataType boolType   ("bool", "bool",
			   "XmlRpcValue::makeBool",
			   "getBool");
SimpleDataType doubleType ("double", "double",
			   "XmlRpcValue::makeDouble",
			   "getDouble");
SimpleDataType stringType ("string", "string",
			   "XmlRpcValue::makeString",
			   "getString");

RawDataType dateTimeType  ("dateTime");
RawDataType base64Type    ("base64");
RawDataType structType    ("struct");
RawDataType arrayType     ("array");

VoidDataType voidType;

const DataType& findDataType (const string& name) {
    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
	throw domain_error("Unknown XML-RPC type " + name);
    
    // This code should never be executed.
    XMLRPC_ASSERT(0);
    return intType;
}