Thread: Multiple types in lists, vectors or arrays.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    28
    Quote 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..
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Vectors versus normal arrays
    By _izua_ in forum C++ Programming
    Replies: 10
    Last Post: 08-12-2007, 01:59 AM
  2. Stl lists and user defined types
    By figa in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2005, 12:09 PM
  3. multiple inputs of multiple types
    By frontz in forum C Programming
    Replies: 8
    Last Post: 01-19-2004, 02:57 PM
  4. Can multiple linked lists share the same structure?
    By passy in forum C Programming
    Replies: 10
    Last Post: 08-28-2003, 04:38 PM
  5. Parallel Arrays with Multiple Arrays
    By Billye Scott in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 11:14 PM