Originally Posted by
CornedBee
Hmm ... still not a good idea, I think. XML-RPC follows a pre-defined schema. In other words, it should be possible to know, at the time of writing the code, what types are expected. You would typically write a tool that generates a wrapper class for the web service. This class then presents a fully type-safe interface to the outside.
The core library, on the other hand, has no need for variants either: it has to convert the values it gets passed in to XML at some point, so why not do it the moment you get the value? This way, you just have a setParameter template (or overloaded) function which takes any datatype, converts it to its wire representation, and stores the resulting string in a homogeneous string container. For the response, you again store the XML representation, until the library user queries the value. Then you require him to specify the suspected type, and try to convert the representation you have into that type. On failing, you could throw an exception, or return an empty Boost.Optional.
The point is this: at some point, the type decision has to be made, and using variants or any only delays it, transfers it from the interface of the library to the user code, where it does not belong.
I've just read up on overloaded template functions and I think i'll do it this way. The only problem is I need a way to tell what type i'm handling when the the argument is processed, so it can be passed to the right function for serialization into an xml-useable string.
Just to clarify, would the following code give me what i'm after (the server could take up to 5 params of different types per call)..
Code:
template <typename T1>
string sendCall(T1 param1) {
//code which checks param types and processes to xml string..
}
template <typename T1, typename T2>
string sendCall(T1 param1, T2 param2) {
//code which checks param types and processes to xml string..
}
template <typename T1, typename T2, typename T3>
string sendCall(T1 param1, T2 param2, T3 param3) {
//code which checks param types and processes to xml string..
}
template <typename T1, typename T2, typename T3, typename T4>
string sendCall(T1 param1, T2 param2, T3 param3, T4 param4) {
//code which checks param types and processes to xml string..
}
template <typename T1, typename T2, typename T3, typename T4, typename T5>
string sendCall(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5) {
//code which checks param types and processes to xml string..
}