initial load of upstream version 1.06.32
[xmlrpc-c] / include / xmlrpc-c / base.hpp
1 #ifndef XMLRPC_BASE_HPP_INCLUDED
2 #define XMLRPC_BASE_HPP_INCLUDED
3
4 #include <climits>
5 #include <cfloat>
6 #include <ctime>
7 #include <vector>
8 #include <map>
9 #include <string>
10
11 #include <xmlrpc-c/base.h>
12
13 namespace xmlrpc_c {
14
15
16 class value {
17     // This is a handle.  You don't want to create a pointer to this;
18     // it is in fact a pointer itself.
19 public:
20     value();
21         // This creates a placeholder.  It can't be used for anything, but
22         // holds memory.  instantiate() can turn it into a real object.
23
24     value(xmlrpc_c::value const &value);  // copy constructor
25
26     ~value();
27
28     enum type_t {
29         TYPE_INT        = 0,
30         TYPE_BOOLEAN    = 1,
31         TYPE_DOUBLE     = 2,
32         TYPE_DATETIME   = 3,
33         TYPE_STRING     = 4,
34         TYPE_BYTESTRING = 5,
35         TYPE_ARRAY      = 6,
36         TYPE_STRUCT     = 7,
37         TYPE_C_PTR      = 8,
38         TYPE_NIL        = 9,
39         TYPE_DEAD       = 0xDEAD
40     };
41
42     type_t type() const;
43
44     xmlrpc_c::value&
45     operator=(xmlrpc_c::value const&);
46
47     // The following are not meant to be public to users, but just to
48     // other Xmlrpc-c library modules.  If we ever go to a pure C++
49     // implementation, not based on C xmlrpc_value objects, this shouldn't
50     // be necessary.
51
52     void
53     appendToCArray(xmlrpc_value * const arrayP) const;
54
55     void
56     addToCStruct(xmlrpc_value * const structP,
57                     std::string const key) const;
58
59     xmlrpc_value *
60     cValue() const;
61
62     value(xmlrpc_value * const valueP);
63
64     void
65     instantiate(xmlrpc_value * const valueP);
66         // Work only on a placeholder object created by the no-argument
67         // constructor.
68
69     xmlrpc_value * cValueP;
70         // NULL means this is merely a placeholder object.
71 };
72
73
74
75 class value_int : public value {
76 public:
77     value_int(int const cvalue);
78
79     value_int(xmlrpc_c::value const baseValue);
80
81     operator int() const;
82 };
83
84
85
86 class value_boolean : public value {
87 public:
88     value_boolean(bool const cvalue);
89
90     value_boolean(xmlrpc_c::value const baseValue);
91
92     operator bool() const;
93 };
94
95
96
97 class value_string : public value {
98 public:
99     value_string(std::string const& cvalue);
100
101     value_string(xmlrpc_c::value const baseValue);
102
103     operator std::string() const;
104 };
105
106
107
108 class value_double : public value {
109 public:
110     value_double(double const cvalue);
111
112     value_double(xmlrpc_c::value const baseValue);
113
114     operator double() const;
115 };
116
117
118
119 class value_datetime : public value {
120 public:
121     value_datetime(std::string const cvalue);
122     value_datetime(time_t const cvalue);
123     value_datetime(struct timeval const& cvalue);
124     value_datetime(struct timespec const& cvalue);
125
126     value_datetime(xmlrpc_c::value const baseValue);
127
128     operator time_t() const;
129 };
130
131
132
133 class value_bytestring : public value {
134 public:
135     value_bytestring(std::vector<unsigned char> const& cvalue);
136
137     value_bytestring(xmlrpc_c::value const baseValue);
138
139     // You can't cast to a vector because the compiler can't tell which
140     // constructor to use (complains about ambiguity).  So we have this:
141     std::vector<unsigned char>
142     vectorUcharValue() const;
143
144     size_t
145     length() const;
146 };
147
148
149
150 class value_nil : public value {
151 public:
152     value_nil();
153
154     value_nil(xmlrpc_c::value const baseValue);
155 };
156
157
158
159 class value_struct : public value {
160 public:
161     value_struct(std::map<std::string, xmlrpc_c::value> const& cvalue);
162
163     value_struct(xmlrpc_c::value const baseValue);
164
165     operator std::map<std::string, xmlrpc_c::value>() const;
166 };
167
168
169
170 class value_array : public value {
171 public:
172     value_array(std::vector<xmlrpc_c::value> const& cvalue);
173
174     value_array(xmlrpc_c::value const baseValue);
175
176     std::vector<xmlrpc_c::value>
177     vectorValueValue() const;
178
179     size_t
180     size() const;
181 };
182
183
184
185 class fault {
186 /*----------------------------------------------------------------------------
187    This is an XML-RPC fault.
188
189    This object is not intended to be used to represent a fault in the
190    execution of XML-RPC client/server software -- just a fault in an
191    XML-RPC RPC as described by the XML-RPC spec.
192
193    There is no way to represent "no fault" with this object.  The object is
194    meaningful only in the context of some fault.
195 -----------------------------------------------------------------------------*/
196 public:
197     enum code_t {
198         CODE_UNSPECIFIED            =    0,
199         CODE_INTERNAL               = -500,
200         CODE_TYPE                   = -501,
201         CODE_INDEX                  = -502,
202         CODE_PARSE                  = -503,
203         CODE_NETWORK                = -504,
204         CODE_TIMEOUT                = -505,
205         CODE_NO_SUCH_METHOD         = -506,
206         CODE_REQUEST_REFUSED        = -507,
207         CODE_INTROSPECTION_DISABLED = -508,
208         CODE_LIMIT_EXCEEDED         = -509,
209         CODE_INVALID_UTF8           = -510
210     };
211
212     fault();
213
214     fault(std::string             const _faultString,
215           xmlrpc_c::fault::code_t const _faultCode 
216               = xmlrpc_c::fault::CODE_UNSPECIFIED
217         );
218     
219     xmlrpc_c::fault::code_t getCode() const;
220
221     std::string getDescription() const;
222
223 private:
224     bool                    valid;
225     xmlrpc_c::fault::code_t code;
226     std::string             description;
227 };
228
229 class rpcOutcome {
230 /*----------------------------------------------------------------------------
231   The outcome of a validly executed RPC -- either an XML-RPC fault
232   or an XML-RPC value of the result.
233 -----------------------------------------------------------------------------*/
234 public:
235     rpcOutcome();
236     rpcOutcome(xmlrpc_c::value const result);
237     rpcOutcome(xmlrpc_c::fault const fault);
238     bool succeeded() const;
239     xmlrpc_c::fault getFault() const;
240     xmlrpc_c::value getResult() const;
241 private:
242     bool valid;
243         // This is false in a placeholder variable -- i.e. an object you
244         // create with the no-argument constructor, which is waiting to be
245         // assigned a value.  When false, nothing below is valid.
246     bool _succeeded;
247     xmlrpc_c::value result;  // valid if 'succeeded'
248     xmlrpc_c::fault fault;   // valid if not 'succeeded'
249 };
250
251 class paramList {
252 /*----------------------------------------------------------------------------
253    A parameter list of an XML-RPC call.
254 -----------------------------------------------------------------------------*/
255 public:
256     paramList(unsigned int const paramCount = 0);
257
258     void
259     add(xmlrpc_c::value const param);
260
261     unsigned int
262     size() const;
263
264     xmlrpc_c::value operator[](unsigned int const subscript) const;
265
266     int
267     getInt(unsigned int const paramNumber,
268            int          const minimum = INT_MIN,
269            int          const maximum = INT_MAX) const;
270
271     bool
272     getBoolean(unsigned int const paramNumber) const;
273
274     double
275     getDouble(unsigned int const paramNumber,
276               double       const minimum = -DBL_MAX,
277               double       const maximum = DBL_MAX) const;
278
279     enum timeConstraint {TC_ANY, TC_NO_PAST, TC_NO_FUTURE};
280
281     time_t
282     getDatetime_sec(unsigned int   const paramNumber,
283                     timeConstraint const constraint
284                         = paramList::TC_ANY) const;
285
286     std::string
287     getString(unsigned int const paramNumber) const;
288
289     std::vector<unsigned char>
290     getBytestring(unsigned int const paramNumber) const;
291
292     std::vector<xmlrpc_c::value>
293     getArray(unsigned int const paramNumber,
294              unsigned int const minSize = 0,
295              unsigned int const maxSize = UINT_MAX) const;
296
297     std::map<std::string, xmlrpc_c::value>
298     getStruct(unsigned int const paramNumber) const;
299
300     void
301     getNil(unsigned int const paramNumber) const;
302
303     void
304     verifyEnd(unsigned int const paramNumber) const;
305
306 private:
307     std::vector<xmlrpc_c::value> paramVector;
308 };
309
310 } // namespace
311
312 #endif