[MPL] How to use a mpl::map

Hi, I'm new to MPL and I have difficulties to use the map container. I need to map (and then retrieve) some types based on they ID: I try the following code but it does not work.. sturct MSG_METHOD_CONNECT { .... } template<class MESSAGE> class PBMessage { public: PBMessage() {}; /* ... other methods ... */ private: MESSAGE m_Msg; }; // Map of types. The key is the Message opCode typedef typename mpl::map< mpl::pair<mpl::int_[100], PBMessage<MSG_METHOD_CONNECT> >, mpl::pair<mpl::int_[101], PBMessage<MSG_EVENT_CONNECT> >, > TMessageMap; // The Message type template < typename MessageMap, int opCode > typedef typename mpl::at<MessageMap, mpl::int_<opCode> >::type::value TMessage; And to retrieve the type: TMessage<TMessageMap, opCode> msg; Could someone give me some suggestions? Regards, Daniele.

Am 15.03.2013 08:52, schrieb Daniele Barzotti:
// Map of types. The key is the Message opCode typedef typename mpl::map< mpl::pair<mpl::int_[100], PBMessage<MSG_METHOD_CONNECT> >, mpl::pair<mpl::int_[101], PBMessage<MSG_EVENT_CONNECT> >, > TMessageMap;
typedef mpl::map< mpl::pair<mpl::int_<100>, PBMessage<MSG_METHOD_CONNECT> >, mpl::pair<mpl::int_<101>, PBMessage<MSG_EVENT_CONNECT> > > TMessageMap;
// The Message type template < typename MessageMap, int opCode > typedef typename mpl::at<MessageMap, mpl::int_<opCode> >::type::value TMessage;
there are no templated typedefs, just use mpl::at<TMessageMap,mpl::int_<100> >::type to look up the type for an "opcode". However, IIUC what you're trying to do, you won't know the op code at compile time, as you're reading it at runtime from some kind of message stream. Maybe the following code is helpful to you. it does something very similar (read log entries of different types from a file). http://pastebin.com/Q2XGz2R2 it uses a MPL vector instead of a map though and assigns the Ids ("op codes") continuously, so the first entry has id 1, the second Id 2, ...

Hi Stefan, thanks a lot for your answer!
there are no templated typedefs
Yes, this is my type mistake over my tests! :-)
However, IIUC what you're trying to do, you won't know the op code at compile time, as you're reading it at runtime from some kind of message stream.
Yes, it's what I need...unfortunately I'm a newbie in Metaprogramming!
Maybe the following code is helpful to you. it does something very similar (read log entries of different types from a file).
Thanks! I will try to figure out how to understand for my need! Regards, Daniele.
participants (2)
-
Daniele Barzotti
-
Stefan Strasser