File size: 6,216 Bytes
fd49381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <iostream>
#include <sstream>
#include <stdexcept>

#include "xmlrpcType.hpp"

#include "xmlrpcMethod.hpp"

using namespace std;


xmlrpcMethod::xmlrpcMethod(string                const& functionName,
                           string                const& methodName,
                           string                const& help,
                           xmlrpc_c::value_array const& signatureList) :
    mFunctionName(functionName),
    mMethodName(methodName),
    mHelp(help),
    mSynopsis(signatureList) {}



xmlrpcMethod::xmlrpcMethod(xmlrpcMethod const& f) :
    mFunctionName(f.mFunctionName),
    mMethodName(f.mMethodName),
    mHelp(f.mHelp),
    mSynopsis(f.mSynopsis) {}



xmlrpcMethod&
xmlrpcMethod::operator= (xmlrpcMethod const& f) {

    if (this != &f) {
        this->mFunctionName = f.mFunctionName;
        this->mMethodName   = f.mMethodName;
        this->mHelp         = f.mHelp;
        this->mSynopsis     = f.mSynopsis;
    }
    return *this;
}






size_t
xmlrpcMethod::parameterCount(size_t const synopsisIndex) const {

    xmlrpc_c::value_array const funcSynop(
        mSynopsis.vectorValueValue()[synopsisIndex]);
    size_t const size(funcSynop.size());

    if (size < 1)
        throw domain_error("Synopsis contains no items");

    return size - 1;
}



xmlrpcType const&
xmlrpcMethod::parameterType(size_t const synopsisIndex,
                            size_t const parameterIndex) const {

    xmlrpc_c::value_array const funcSynop(
        mSynopsis.vectorValueValue()[synopsisIndex]);
    xmlrpc_c::value_string const param(
        funcSynop.vectorValueValue()[parameterIndex + 1]);

    return findXmlrpcType(static_cast<string>(param));
}



const xmlrpcType&
xmlrpcMethod::returnType(size_t const synopsisIndex) const {

    xmlrpc_c::value_array const funcSynop(
        mSynopsis.vectorValueValue()[synopsisIndex]);

    xmlrpc_c::value_string datatype(funcSynop.vectorValueValue()[0]);
    return findXmlrpcType(static_cast<string>(datatype));
}



void
xmlrpcMethod::printParameters(ostream      & out,
                              size_t  const  synopsisIndex) const {
/*----------------------------------------------------------------------------
  Print the parameter declarations.
-----------------------------------------------------------------------------*/
    size_t const end(parameterCount(synopsisIndex));

    bool first;

    first = true;

    for (size_t i = 0; i < end; ++i) {
        if (!first)
            out << ", ";

        xmlrpcType const& ptype(parameterType(synopsisIndex, i));

        string const localName(ptype.defaultParameterBaseName(i + 1));
        out << ptype.parameterFragment(localName);

        first = false;
    }
}



void
xmlrpcMethod::printDeclaration(ostream      & out,
                               size_t  const  synopsisIndex) const {

    try {
        xmlrpcType const& rtype(returnType(synopsisIndex));

        out << "    " << rtype.returnTypeFragment() << " "
            << mFunctionName << " (";

        printParameters(out, synopsisIndex);
        
        out << ");" << endl;
    } catch (xmlrpc_c::fault const& f) {
        ostringstream msg;

        msg << "Failed to generate header for signature "
            << synopsisIndex
            << " .  "
            << f.getDescription();

        throw(logic_error(msg.str()));
    }
}



void
xmlrpcMethod::printDeclarations(ostream & out) const {

    try {
        // Print the method help as a comment

        out << endl << "    /* " << mHelp << " */" << endl;

        size_t end;

        try {
            end = mSynopsis.size();
        } catch (xmlrpc_c::fault const& f) {
            throw(logic_error("Failed to get size of signature array for "
                              "method " + this->mFunctionName + ".  " +
                              f.getDescription()));
        }
        // Print the declarations for all the signatures of this
        // XML-RPC method.
                                      
        for (size_t i = 0; i < end; ++i)
            printDeclaration(out, i);
        
    } catch (exception const& e) {
        throw(logic_error("Failed to generate declarations for method " +
                          this->mFunctionName + ".  " + e.what()));
    }
}



void
xmlrpcMethod::printDefinition(ostream     & out,
                              string const& className,
                              size_t const  synopsisIndex) const {

    xmlrpcType const& rtype(returnType(synopsisIndex));

    out << rtype.returnTypeFragment() << " "
        << className << "::" << mFunctionName << " (";

    printParameters(out, synopsisIndex);

    out << ") {" << endl;

    size_t const end(parameterCount(synopsisIndex));
    if (end > 0){
        // Emit code to generate the parameter list object
        out << "    xmlrpc_c::paramList params;" << endl;
        for (size_t i = 0; i < end; ++i) {
            xmlrpcType const& ptype(parameterType(synopsisIndex, i));
            string const basename(ptype.defaultParameterBaseName(i + 1));
            out << "    params.add("
                << ptype.inputConversionFragment(basename) << ");" << endl;
        }
    }

    // Emit result holder declaration.
    out << "    xmlrpc_c::value result;" << endl;

    // Emit the call to the XML-RPC call method
    out << "    this->client.call("
        << "this->serverUrl, "
        << "\"" << mMethodName << "\", ";

    if (end > 0)
        out << "params, ";

    out << "&result);" << endl;    

    // Emit the return statement.
    out << "    return " << rtype.outputConversionFragment("result")
        << ";" << endl;
    out << "}" << endl;
}



void
xmlrpcMethod::printDefinitions(ostream      & out,
                               string  const& className) const {

    try {
        size_t const end(mSynopsis.size());
    
        for (size_t i = 0; i < end; ++i) {
            out << endl;
            printDefinition(out, className, i);
        }
    } catch (xmlrpc_c::fault const& f) {
        throw(logic_error("Failed to generate definitions for class " +
                          this->mFunctionName + ".  " +
                          f.getDescription()));
    }
}