Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:08:30+00:00 2026-06-12T11:08:30+00:00

I’ve got a list of types which can be send over the network, take

  • 0

I’ve got a list of types which can be send over the network, take this example:

enum types {
    E_T1,
    E_T2,
    E_T3,
    E_T4
};

Now I have a list of classes which correspond to each of the types, let’s say each is declared as class E_T1 {...}, class E_T2 {...}, etc.

They are not derived from a common base class and it’s not possible to do so. Each of the classes has a verification method I need to invoke with the data send over the network. The client sends the data D and a id correspointing to the message type. I need to get hold of the object corresponding to the type. I can use C++0x features if needed.

What I’ve tried so far is using specialized templates for the types, holding a typedef for the object related to it. This was obviously a stupid idea as templates parameters need to be compile time constant so doing something along getType<data.id()>::type is not possible.

Then I tried using Boost.Variant to get a common returnable type like this (used mpl vector to iterate over the registered types at runntime for debbuging):

template <typename C>
struct getType() {
    typedef C type;
}

typedef boost::mpl::vector<
    getType<E_T1>,
    getType<E_T2>,
    getType<E_TX>...
> _types;

typedef boost::make_variant_over<_types>::type _type;

//use a map to store each type <-> id
boost::unorderd_map<types, _type> m;
m[E_T1] = getType<E_T1>();

m[data.id()]::type x; //<- access type, can now call x.validate(data)

The problem with this is that it’s limited to 20 entries per variant per default. This can be overwritten but from what I understood the overhead per type should be considered and we are talking about a few thousand types here.

Also tried boost.any but it doesn’t hold any type information so that’s out of the question again. Has anyone any good ideas how this can be solved elegantly?
Looking for something where I don’t have to write a 1k switch statement anytime I handle a type.

All types are nown at compile type, same goes for their corresponding IDs.
Id -> Type resolving needs to happen at runtime though.

Thanks in advance,
Robin.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:08:31+00:00Added an answer on June 12, 2026 at 11:08 am

    External Polymorphism (*)

    It’s a widely known idiom, however it’s widely used: I first encountered it in the shared_ptr implementation and it’s been quite useful in my toolbox.

    The idea is to actually create a base class for all those types. But not having them derive from it directly.

    class Holder {
    public:
        virtual ~Holder() {}
    
        virtual void verify(unsigned char const* bytes, size_t size) const = 0;
    
    }; // class Holder
    
    template <typename T>
    class HolderT: public Holder {
    public:
         HolderT(): _t() {}
    
         virtual void verify(unsigned char const* bytes, size_t size) const {
             _t.verify();
         }
    
    private:
        T _t;
    }; // class HolderT
    
    template <typename T>
    std::unique_ptr<Holder> make_holder() {
        return std::unique_ptr<Holder>(new HolderT<T>());
    }
    

    So, it’s the classic strategy of adding a new level of indirection.

    Now, you obviously do need a switch to move from value to class. Or perhaps… a map ?

    using maker = std::unique_ptr<Holder> (&)();
    using maker_map = std::unordered_map<types, maker>;
    
    std::unique_ptr<Holder> select(types const E) {
        static maker_map mm;
        if (mm.empty()) {
            mm.insert(std::make_pair(E_T1, make_holder<EC_T1>));
            // ...
        }
    
        maker_map::const_iterator it = mm.find(E);
    
        if (it == mm.end()) { return std::unique_ptr<Holder>(); }
    
        return (*it->second)();
     }
    

    And now you can handle them polymorphically:

    void verify(types const E, unsigned char const* bytes, size_t size) {
        std::unique_ptr<Holder> holder = select(E);
        if (not holder) { std::cerr << "Unknown type " << (int)E << "\n"; return; }
    
        holder->verify(bytes, size);
    }
    

    Of course, you’re welcome to make the strategy vary according to your needs. For example moving the map out of select so that you can register your types dynamically (like for plugins).

    (*) At least that’s the name I have for it, I would quite happy to find out it’s already been named.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
Does anyone know how can I replace this 2 symbol below from the string
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.