~ubuntu-branches/ubuntu/natty/xmlrpc-c/natty

« back to all changes in this revision

Viewing changes to tools/xml-rpc-api2cpp/XmlRpcFunction.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-01-06 18:56:02 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20110106185602-09og2x3suqlzbf6s
Tags: 1.16.32-0ubuntu1
* New upstream version (stable release). LP: #659591.
  - No unresolved symbols in the shared libraries. LP: #690779.
  - Builds with --no-add-needed and --as-needed.
* Rename shared library packages.
* Add symbols files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include <iostream>
 
2
#include <sstream>
2
3
#include <stdexcept>
3
4
 
4
5
#include "xmlrpc-c/oldcppwrapper.hpp"
5
6
#include "DataType.hpp"
6
7
#include "XmlRpcFunction.hpp"
7
8
 
8
 
using std::domain_error;
9
 
using std::endl;
10
 
 
11
 
 
12
 
//=========================================================================
13
 
//  class XmlRpcFunction
14
 
//=========================================================================
15
 
//  Contains everything we know about a given server function, and knows
16
 
//  how to print local bindings.
17
 
 
18
 
XmlRpcFunction::XmlRpcFunction(const string& function_name,
19
 
                               const string& method_name,
20
 
                               const string& help,
21
 
                               XmlRpcValue synopsis)
22
 
    : mFunctionName(function_name), mMethodName(method_name),
23
 
      mHelp(help), mSynopsis(synopsis)
24
 
{
25
 
}
26
 
 
27
 
XmlRpcFunction::XmlRpcFunction (const XmlRpcFunction& f)
28
 
    : mFunctionName(f.mFunctionName), mMethodName(f.mMethodName),
29
 
      mHelp(f.mHelp), mSynopsis(f.mSynopsis)
30
 
{
31
 
}
32
 
 
33
 
XmlRpcFunction& XmlRpcFunction::operator= (const XmlRpcFunction& f) {
34
 
    if (this == &f)
35
 
        return *this;
36
 
    mFunctionName = f.mFunctionName;
37
 
    mMethodName = f.mMethodName;
38
 
    mHelp = f.mHelp;
39
 
    mSynopsis = f.mSynopsis;
 
9
using namespace std;
 
10
 
 
11
 
 
12
XmlRpcFunction::XmlRpcFunction(string      const& functionName,
 
13
                               string      const& methodName,
 
14
                               string      const& help,
 
15
                               XmlRpcValue const  signatureList) :
 
16
    mFunctionName(functionName),
 
17
    mMethodName(methodName),
 
18
    mHelp(help),
 
19
    mSynopsis(signatureList) {}
 
20
 
 
21
 
 
22
 
 
23
XmlRpcFunction::XmlRpcFunction(XmlRpcFunction const& f) :
 
24
    mFunctionName(f.mFunctionName),
 
25
    mMethodName(f.mMethodName),
 
26
    mHelp(f.mHelp),
 
27
    mSynopsis(f.mSynopsis) {}
 
28
 
 
29
 
 
30
 
 
31
XmlRpcFunction&
 
32
XmlRpcFunction::operator= (XmlRpcFunction const& f) {
 
33
 
 
34
    if (this != &f) {
 
35
        this->mFunctionName = f.mFunctionName;
 
36
        this->mMethodName   = f.mMethodName;
 
37
        this->mHelp         = f.mHelp;
 
38
        this->mSynopsis     = f.mSynopsis;
 
39
    }
40
40
    return *this;
41
41
}
42
42
 
43
 
void XmlRpcFunction::printDeclarations (ostream& out) {
44
 
 
45
 
    // XXX - Do a sloppy job of printing documentation.
46
 
    out << endl << "    /* " << mHelp << " */" << endl;
47
 
 
48
 
    // Print each declaration.
49
 
    size_t end = mSynopsis.arraySize();
50
 
    for (size_t i = 0; i < end; i++)
51
 
        printDeclaration(out, i);
52
 
}
53
 
 
54
 
void XmlRpcFunction::printDefinitions (ostream& out, const string& className) {
55
 
    size_t end = mSynopsis.arraySize();
56
 
    for (size_t i = 0; i < end; i++) {
57
 
        out << endl;
58
 
        printDefinition(out, className, i);
59
 
    }
60
 
}
61
 
 
62
 
// Print the parameter declarations.
63
 
void XmlRpcFunction::printParameters (ostream& out, size_t synopsis_index) {
64
 
    size_t end = parameterCount(synopsis_index);
65
 
    bool first = true;
66
 
    for (size_t i = 0; i < end; i++) {
67
 
        if (first)
68
 
            first = false;
69
 
        else
70
 
            out << ", ";
71
 
 
72
 
        const DataType& ptype (parameterType(synopsis_index, i));
73
 
        string basename = ptype.defaultParameterBaseName(i + 1);
74
 
        out << ptype.parameterFragment(basename);
75
 
    }
76
 
}
77
 
 
78
 
void XmlRpcFunction::printDeclaration (ostream& out, size_t synopsis_index) {
79
 
    const DataType& rtype (returnType(synopsis_index));
80
 
    out << "    " << rtype.returnTypeFragment() << " "
81
 
        << mFunctionName << " (";
82
 
    printParameters(out, synopsis_index);
83
 
    out << ");" << endl;
84
 
}
85
 
 
86
 
void XmlRpcFunction::printDefinition (ostream& out,
87
 
                                      const string& className,
88
 
                                      size_t synopsis_index)
89
 
{
90
 
    const DataType& rtype (returnType(synopsis_index));
 
43
 
 
44
 
 
45
void
 
46
XmlRpcFunction::printDeclarations(ostream & out) const {
 
47
 
 
48
    try {
 
49
        // Print the method help as a comment
 
50
 
 
51
        out << endl << "    /* " << mHelp << " */" << endl;
 
52
 
 
53
        size_t end;
 
54
 
 
55
        try {
 
56
            end = mSynopsis.arraySize();
 
57
        } catch (XmlRpcFault const& fault) {
 
58
            throw(logic_error("Failed to get size of signature array for "
 
59
                              "method " + this->mFunctionName + ".  " +
 
60
                              fault.getFaultString()));
 
61
        }
 
62
        // Print the declarations for all the signatures of this
 
63
        // XML-RPC method.
 
64
                                      
 
65
        for (size_t i = 0; i < end; ++i)
 
66
            printDeclaration(out, i);
 
67
        
 
68
    } catch (exception const& e) {
 
69
        throw(logic_error("Failed to generate declarations for method " +
 
70
                          this->mFunctionName + ".  " + e.what()));
 
71
    }
 
72
}
 
73
 
 
74
 
 
75
 
 
76
void
 
77
XmlRpcFunction::printDefinitions(ostream      & out,
 
78
                                 string  const& className) const {
 
79
 
 
80
    try {
 
81
        size_t const end(mSynopsis.arraySize());
 
82
    
 
83
        for (size_t i = 0; i < end; ++i) {
 
84
            out << endl;
 
85
            printDefinition(out, className, i);
 
86
        }
 
87
    } catch (XmlRpcFault const& fault) {
 
88
        throw(logic_error("Failed to generate definitions for class " +
 
89
                          this->mFunctionName + ".  " +
 
90
                          fault.getFaultString()));
 
91
    }
 
92
}
 
93
 
 
94
 
 
95
 
 
96
void
 
97
XmlRpcFunction::printParameters(ostream      & out,
 
98
                                size_t  const  synopsisIndex) const {
 
99
/*----------------------------------------------------------------------------
 
100
  Print the parameter declarations.
 
101
-----------------------------------------------------------------------------*/
 
102
    size_t const end(parameterCount(synopsisIndex));
 
103
 
 
104
    bool first;
 
105
 
 
106
    first = true;
 
107
 
 
108
    for (size_t i = 0; i < end; ++i) {
 
109
        if (!first)
 
110
            out << ", ";
 
111
 
 
112
        DataType const& ptype(parameterType(synopsisIndex, i));
 
113
        string const basename(ptype.defaultParameterBaseName(i + 1));
 
114
        out << ptype.parameterFragment(basename);
 
115
 
 
116
        first = false;
 
117
    }
 
118
}
 
119
 
 
120
 
 
121
 
 
122
void
 
123
XmlRpcFunction::printDeclaration(ostream      & out,
 
124
                                 size_t  const  synopsisIndex) const {
 
125
 
 
126
    try {
 
127
        DataType const& rtype(returnType(synopsisIndex));
 
128
 
 
129
        out << "    " << rtype.returnTypeFragment() << " "
 
130
            << mFunctionName << " (";
 
131
 
 
132
        printParameters(out, synopsisIndex);
 
133
        
 
134
        out << ");" << endl;
 
135
    } catch (XmlRpcFault const& fault) {
 
136
        ostringstream msg;
 
137
 
 
138
        msg << "Failed to generate header for signature "
 
139
            << synopsisIndex
 
140
            << " .  "
 
141
            << fault.getFaultString();
 
142
 
 
143
        throw(logic_error(msg.str()));
 
144
    }
 
145
}
 
146
 
 
147
 
 
148
 
 
149
void
 
150
XmlRpcFunction::printDefinition(ostream     & out,
 
151
                                string const& className,
 
152
                                size_t const  synopsisIndex) const {
 
153
 
 
154
    DataType const& rtype(returnType(synopsisIndex));
 
155
 
91
156
    out << rtype.returnTypeFragment() << " "
92
 
        << className << "::" << mFunctionName << " (";
93
 
    printParameters(out, synopsis_index);
 
157
        << className << "::" << mFunctionName << " (";
 
158
 
 
159
    printParameters(out, synopsisIndex);
 
160
 
94
161
    out << ") {" << endl;    
95
 
    out << "    XmlRpcValue params = XmlRpcValue::makeArray();" << endl;
 
162
    out << "    XmlRpcValue params(XmlRpcValue::makeArray());" << endl;
96
163
 
97
164
    /* Emit code to convert the parameters into an array of XML-RPC objects. */
98
 
    size_t end = parameterCount(synopsis_index);
99
 
    for (size_t i = 0; i < end; i++) {
100
 
        const DataType& ptype (parameterType(synopsis_index, i));
101
 
        string basename = ptype.defaultParameterBaseName(i + 1);
102
 
        out << "    params.arrayAppendItem("
103
 
            << ptype.inputConversionFragment(basename) << ");" << endl;
 
165
    size_t const end(parameterCount(synopsisIndex));
 
166
    for (size_t i = 0; i < end; ++i) {
 
167
        DataType const& ptype(parameterType(synopsisIndex, i));
 
168
        string const basename(ptype.defaultParameterBaseName(i + 1));
 
169
        out << "    params.arrayAppendItem("
 
170
            << ptype.inputConversionFragment(basename) << ");" << endl;
104
171
    }
105
172
 
106
173
    /* Emit the function call.*/
107
 
    out << "    XmlRpcValue result = this->mClient.call(\""
108
 
        << mMethodName << "\", params);" << endl;    
 
174
    out << "    XmlRpcValue result(this->mClient.call(\""
 
175
        << mMethodName << "\", params));" << endl;    
109
176
 
110
177
    /* Emit the return statement. */
111
178
    out << "    return " << rtype.outputConversionFragment("result")
112
 
        << ";" << endl;
 
179
        << ";" << endl;
113
180
    out << "}" << endl;
114
181
}
115
182
 
116
 
const DataType& XmlRpcFunction::returnType (size_t synopsis_index) {
117
 
    XmlRpcValue func_synop = mSynopsis.arrayGetItem(synopsis_index);
118
 
    return findDataType(func_synop.arrayGetItem(0).getString());
 
183
 
 
184
 
 
185
const DataType&
 
186
XmlRpcFunction::returnType(size_t const synopsisIndex) const {
 
187
 
 
188
    XmlRpcValue const funcSynop(mSynopsis.arrayGetItem(synopsisIndex));
 
189
 
 
190
    return findDataType(funcSynop.arrayGetItem(0).getString());
119
191
}
120
192
 
121
 
size_t XmlRpcFunction::parameterCount (size_t synopsis_index) {
122
 
    XmlRpcValue func_synop = mSynopsis.arrayGetItem(synopsis_index);
123
 
    size_t size = func_synop.arraySize();
 
193
 
 
194
 
 
195
size_t
 
196
XmlRpcFunction::parameterCount(size_t const synopsisIndex) const {
 
197
 
 
198
    XmlRpcValue const funcSynop(mSynopsis.arrayGetItem(synopsisIndex));
 
199
    size_t const size(funcSynop.arraySize());
 
200
 
124
201
    if (size < 1)
125
 
        throw domain_error("Synopsis contained no items");
 
202
        throw domain_error("Synopsis contained no items");
126
203
    return size - 1;
127
204
}
128
205
 
129
 
const DataType& XmlRpcFunction::parameterType (size_t synopsis_index,
130
 
                                               size_t parameter_index)
131
 
{
132
 
    XmlRpcValue func_synop = mSynopsis.arrayGetItem(synopsis_index);
133
 
    XmlRpcValue param = func_synop.arrayGetItem(parameter_index + 1);
 
206
 
 
207
 
 
208
DataType const&
 
209
XmlRpcFunction::parameterType(size_t const synopsisIndex,
 
210
                              size_t const parameterIndex) const {
 
211
 
 
212
    XmlRpcValue const funcSynop(mSynopsis.arrayGetItem(synopsisIndex));
 
213
    XmlRpcValue const param(funcSynop.arrayGetItem(parameterIndex + 1));
 
214
 
134
215
    return findDataType(param.getString());
135
216
}
136
 
 
137