The usage of typelist in the advanced usage of the C ++ Template

Source: Internet
Author: User

In modern c ++ design, I mentioned a lot about the template usage. I was most impressed by typelist, and I didn't know how to use it. So I won't talk about it here.

But for typelist, the book describes how to implement it, but there is no usage introduction. Here is an example:

#include <iostream>#include <assert.h>#include "typelist.h"using namespace std;struct MsgRegisterRequest{  int container_type;  int  codec_id;  MsgRegisterRequest() :    container_type(0),    codec_id(1)  {}};struct MsgRegisterReply{  bool result;  MsgRegisterReply() :    result(false)  {}  static void echoStatic() { cout << __PRETTY_FUNCTION__ << endl; };  void echo() { cout << __PRETTY_FUNCTION__ << endl; };};struct MsgInviteRequest{  int container_type;  int  codec_id;  MsgInviteRequest() :    container_type(0),    codec_id(1)  {}};struct MsgInviteReply{  bool result;  MsgInviteReply() :    result(false)  {}  static void echoStatic() { cout << __PRETTY_FUNCTION__ << endl; };  void echo() { cout << __PRETTY_FUNCTION__ << endl; };};typedef ly::MakeTypelist< MsgRegisterRequest,                           MsgInviteRequest>::Result MyRequestMessages;typedef ly::MakeTypelist< MsgRegisterReply,                           MsgInviteReply>::Result MyReplyMessages;typedef ly::Append<  MyRequestMessages,  MyReplyMessages  >::Result MyMessage;template<class TList>class MsgQueueServer{public:  explicit MsgQueueServer(const char* name) {}  virtual ~MsgQueueServer() {}  template<typename M>  int postReply(const M& msg)  {    const int id = ly::IndexOf<TList, M>::value;    const int num = ly::Length<TList>::value;    std::cout << "size " << num << " id " << id << endl;    assert(id >= 0);    typedef typename ly::TypeAt<MyMessage, id>::Result MsgType;    MsgType m;    m.echo();    M::echoStatic();    return 0;  }  template<typename M>  void printId(const M& msg)  {    const int id = ly::IndexOf<TList, M>::value;    std::cout << id << std::endl;    return;  }};typedef MsgQueueServer<MyMessage> MyMQServer;class MessageHandlerObject{public:  MessageHandlerObject(MyMQServer& mq):mq_(mq)  {}  void handleRequest(const MsgRegisterRequest& request)   {    MsgRegisterReply reply;    mq_.postReply(reply);  }  void handleRequest(const MsgInviteRequest& request)   {    MsgInviteReply reply;    mq_.postReply(reply);  }  template<typename M>  void printid(const M& msg)  {    mq_.printId(msg);  }private:  MyMQServer& mq_;};int main(){  MyMQServer mq("luoyu");  MessageHandlerObject msgHandler(mq);  MsgRegisterRequest regist;  msgHandler.handleRequest(regist);  MsgInviteRequest invite;  msgHandler.handleRequest(invite);   msgHandler.printid(regist);  msgHandler.printid(invite);  std::cout << "end" << std::endl;  return 0;}/**************** OUTPUT from Console *******************[luo.yu@ly template]$ ./test size 4 id 2void MsgRegisterReply::echo()static void MsgRegisterReply::echoStatic()size 4 id 3void MsgInviteReply::echo()static void MsgInviteReply::echoStatic()01end*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.