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

  • Home
  • SEARCH
  • 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 8874623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:46:07+00:00 2026-06-14T18:46:07+00:00

I have a Message class that is able to pack its payload to binary

  • 0

I have a Message class that is able to pack its payload to binary and unpack it back. Like:

PayloadA p;
msg->Unpack(&p);

where PayloadA is a class.

The problem is that I have a bunch of payloads, so I need giant if or switch statement:

if (msg->PayloadType() == kPayloadTypeA)
{
    PayloadA p;
    msg->Unpack(&p); // void Unpack(IPayload *);

    // do something with payload ...
}
else if ...

I want to write a helper function that unpacks payloads. But what would be the type of this function? Something like:

PayloadType UnpackPayload(IMessage *msg) { ... }

where PayloadType is a typedef of a proper payload class. I know it is impossible but I looking for solutions like this. Any ideas?

Thanks.

  • 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-14T18:46:09+00:00Added an answer on June 14, 2026 at 6:46 pm

    I would split one level higher to avoid the problem entirely:

    #include <map>
    #include <functional>
    
    ...
    std::map<int, std::function<void()> _actions;
    ...
    
    // In some init section
    _actions[kPayloadA] = [](IMessage* msg) {
        PayloadA p;
        msg->Unpack(&p);
    
        // do something with payload ...
    };
    // repeat for all payloads
    
    ...
    
    // decoding function
    DecodeMsg(IMessage* msg) {
        _actions[id](msg);
    }
    

    To further reduce the code size, try to make Unpack a function template (possible easily only if it’s not virtual, if it is you can try to add one level of indirection so that it isn’t ;):

    class Message {
       template <class Payload>
       Payload Unpack() { ... }
    };
    
    auto p = msg->Unpack<PayloadA>();
    
    // do something with payload ...
    

    EDIT

    Now let’s see how we can avoid writing the long list of _actions[kPayloadN]. This is highly non trivial.

    First you need a helper to run code during the static initialization (i.e. before main):

    template <class T>
    class Registrable
    {
        struct Registrar
        {
            Registrar()
            {
                T::Init();
            }
        };
    
        static Registrar R;
    
        template <Registrar& r>
        struct Force{ };
        static Force<R> F; // Required to force all compilers to instantiate R
                           // it won't be done without this
    };
    
    template <class T>
    typename Registrable<T>::Registrar Registrable<T>::R;
    

    Now we need to define our actual registration logic:

    typedef map<int, function<void()> PayloadActionsT;
    inline PayloadActionsT& GetActions() // you may move this to a CPP
    {
        static PayloadActionsT all;
        return all;
    }
    

    Then we factor in the parsing code:

    template <class Payload>
    struct BasePayload : Registrable<BasePayload>
    {
        static void Init()
        {
            GetActions()[Payload::Id] = [](IMessage* msg) {
                 auto p = msg->Unpack<Payload>();
                 p.Action();
            }
        }
    };
    

    Then we define all the payloads one by one

    struct PayloadA : BasePayload<PayloadA>
    {
        static const int Id = /* something unique */;
        void Action()
        { /* what to do with this payload */ }
    }
    

    Finally we parse the incoming messages:

    void DecodeMessage(IMessage* msg)
    {
        static const auto& actions = GetActions();
        actions[msg->GetPayloadType]();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a serializable Message class that has a Data As Object property that
I have a class that raises an event with an error message. In some
I have a specialized object class that sends messages to its components so that
I have a class that maps incoming messages to matching readers based on the
I have a class in c# in that store some user messages, and return
I have a web service , i add some extra class which have message
I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
I have an object of class Message, which can be written and subsequently updated.
In my homework, I have to design a class Message; among other attributes, it
I have the following generic class: public class MessageProcesser<T> where T : Message Inside

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.