Effective message dispatching [on hold]Threadsafe network messageEffective way to remove white spaces from...

A function which translates a sentence to title-case

Email Account under attack (really) - anything I can do?

Schwarzchild Radius of the Universe

What are these boxed doors outside store fronts in New York?

Are there any consumables that function as addictive (psychedelic) drugs?

How to type dʒ symbol (IPA) on Mac?

The use of multiple foreign keys on same column in SQL Server

Why is the design of haulage companies so “special”?

Is there really no realistic way for a skeleton monster to move around without magic?

What do you call a Matrix-like slowdown and camera movement effect?

Are tax years 2016 & 2017 back taxes deductible for tax year 2018?

Do airline pilots ever risk not hearing communication directed to them specifically, from traffic controllers?

Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?

whey we use polarized capacitor?

declaring a variable twice in IIFE

Are white and non-white police officers equally likely to kill black suspects?

What is the command to reset a PC without deleting any files

How is this relation reflexive?

Can I make popcorn with any corn?

What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?

Could a US political party gain complete control over the government by removing checks & balances?

Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?

Is it tax fraud for an individual to declare non-taxable revenue as taxable income? (US tax laws)

Why are 150k or 200k jobs considered good when there are 300k+ births a month?



Effective message dispatching [on hold]


Threadsafe network messageEffective way to remove white spaces from stringCross-platform message box libraryEffective STL Item 24: add_or_update for std::map with perfect forwarding and emplaceSimple and effective port checker in C++Effective way to find two string are anagramsGeneric message system using variadic templatesCalculating effective shop inventories from CSV filesC++ Generic Action Dispatching & Handling SystemEffective 3d array transposition with R/C++ (Rcpp)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







3












$begingroup$


I have some code, that works fine. However, I know there is a lot of code duplication and C-style programming. In the code example there only is a DeviceOneDispatcher, in reality there are more child classes and case options.



The question is how to optimize/refactor this code best following c++11-c++17.
I believe a big part of the duplication, could be solved by using a template. But I am not sure how implement this.



class BaseDispatcher
{
public:
BaseDispatcher() = default;
virtual ~BaseDispatcher() = default;

result_t Init(uint8_t idx)
{
msgHandler.Init(idx);
};

void Execute(MainControl &ctrl)
{
MsgInfo msg = { 0 };
if (msgHandler.RxMsg(msg))
{
switch (msg.msgID)
{
case REQ_PING:
HandlePing(msg);
break;
case REQ_VERSION:
HandleVersion(msg);
break;
default:
SpecificExecute(msg, ctrl);
}
};

protected:
void HandlePing(const MsgInfo &msg)
{
txMsg = msgHandler.ReserveMsg(msg.msgID, sizeof(DefaultMsg_t));

if (nullptr != txMsg.msgBuf)
{
auto *response = (DefaultMsg_t *)txMsg.msgBuf;
response->ack = htons(static_cast<uint16_t>(Reply::ACCEPTED));
msgHandler.SendMsg(txMsg);
mainCtrl->GetConsole().OutputEnable();
}
};
void HandleVersion(const MsgInfo &msg)
{
txMsg = msgHandler.ReserveMsg(msg.msgID, sizeof(VersionInfoMsg_t));

if (nullptr != txMsg.msgBuf)
{
auto *response = (VersionInfoMsg_t *)txMsg.msgBuf;
memset(response->name, 0, 32);
response->svnRevision = htons(svnRevision);
uint8_t sl = (uint8_t)strlen(sw);
memcpy(response->name, sw, sl <= 32 ? sl : 32);
msgHandler.SendMsg(txMsg);
}
};
void HandleUnknown(const MsgInfo &msg)
{
txMsg = msgHandler.ReserveMsg(msg.msgID, sizeof(DefaultMsg_t));

if (nullptr != txMsg.msgBuf)
{
auto *response = (DefaultMsg_t *)txMsg.msgBuf;
response->ack = htons(static_cast<uint16_t>(Reply::UNKNOWN_REQ));
msgHandler.SendMsg(txMsg);
}
};
virtual void SpecificExecute(const MsgInfo &msg, MainControl &ctrl);

MsgHandler &GetMsgHandler() { return msgHandler; }

protected:
MsgInfo txMsg;
MsgHandler msgHandler;
static const char* sw = "Embedded Test Program";
};


class DeviceOneDispatcher : public BaseDispatcher
{
public:
DeviceOneDispatcher () = default;
virtual ~DeviceOneDispatcher () = default;

void SpecificExecute(const MsgInfo &msg, MainControl &ctrl) override
{
switch (msg.msgID)
{
case INFO_TEMP:
HandleTemperature(msg, ctrl);
break;
default:
HandleUnknown(msg);
}
};

protected:
void HandleTemperature(const MsgInfo &msg, MainControl &ctrl)
{
float temperature = (float)ntohl(*(uint16_t*)msg.msgBuf) / 10;
ctrl.GetRFMonitor().SetTemperature(temperature);
};
};

class DeviceTwoDispatcher : public BaseDispatcher
{
public:
DeviceTwoDispatcher () = default;
virtual ~DeviceTwoDispatcher () = default;

void SpecificExecute(const MsgInfo &msg, MainControl &ctrl) override
{
switch (msg.msgID)
{
case INFO_TEMP:
HandleMotor(msg, ctrl);
break;
default:
HandleUnknown(msg);
}
};

protected:
void HandleMotor(const MsgInfo &msg, MainControl &ctrl)
{
//do something
};
};
Class MainController()
{
public:
Execute()
{
dispatcherOne.Execute(*this);
dispatcherTwo.Execute(*this);
}
DeviceOneDispatcher &GetDispatcherOne() { return dispatcherOne; }
DeviceTwoDispatcher &GetDispatcherTwo() { return dispatcherTwo; }
protected:
DeviceOneDispatcher dispatcherOne;
DeviceTwoDispatcher dispatcherTwo;
}
```









share|improve this question









New contributor




RForce is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



put on hold as off-topic by pacmaninbw, Toby Speight, Edward, Graipher, Mast 9 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Edward, Graipher, Mast

If this question can be reworded to fit the rules in the help center, please edit the question.
















  • $begingroup$
    This code seems to be incomplete, there is no function HandleConsoleVerbosity()
    $endgroup$
    – pacmaninbw
    Apr 1 at 12:44










  • $begingroup$
    This code can't compile, the function HandleVersion() uses the variable sw which is never defined. Suggestion turn all of your C style casts into static casts.
    $endgroup$
    – pacmaninbw
    Apr 1 at 13:04










  • $begingroup$
    The code does compile, however as this is only a snapshot of the program not everything is defined here.
    $endgroup$
    – RForce
    Apr 1 at 13:13












  • $begingroup$
    @RForce Please include the rest as well to avoid such confusion. We need to see code in it's context, otherwise a review would be filled with guesswork. That helps nobody.
    $endgroup$
    – Mast
    9 hours ago


















3












$begingroup$


I have some code, that works fine. However, I know there is a lot of code duplication and C-style programming. In the code example there only is a DeviceOneDispatcher, in reality there are more child classes and case options.



The question is how to optimize/refactor this code best following c++11-c++17.
I believe a big part of the duplication, could be solved by using a template. But I am not sure how implement this.



class BaseDispatcher
{
public:
BaseDispatcher() = default;
virtual ~BaseDispatcher() = default;

result_t Init(uint8_t idx)
{
msgHandler.Init(idx);
};

void Execute(MainControl &ctrl)
{
MsgInfo msg = { 0 };
if (msgHandler.RxMsg(msg))
{
switch (msg.msgID)
{
case REQ_PING:
HandlePing(msg);
break;
case REQ_VERSION:
HandleVersion(msg);
break;
default:
SpecificExecute(msg, ctrl);
}
};

protected:
void HandlePing(const MsgInfo &msg)
{
txMsg = msgHandler.ReserveMsg(msg.msgID, sizeof(DefaultMsg_t));

if (nullptr != txMsg.msgBuf)
{
auto *response = (DefaultMsg_t *)txMsg.msgBuf;
response->ack = htons(static_cast<uint16_t>(Reply::ACCEPTED));
msgHandler.SendMsg(txMsg);
mainCtrl->GetConsole().OutputEnable();
}
};
void HandleVersion(const MsgInfo &msg)
{
txMsg = msgHandler.ReserveMsg(msg.msgID, sizeof(VersionInfoMsg_t));

if (nullptr != txMsg.msgBuf)
{
auto *response = (VersionInfoMsg_t *)txMsg.msgBuf;
memset(response->name, 0, 32);
response->svnRevision = htons(svnRevision);
uint8_t sl = (uint8_t)strlen(sw);
memcpy(response->name, sw, sl <= 32 ? sl : 32);
msgHandler.SendMsg(txMsg);
}
};
void HandleUnknown(const MsgInfo &msg)
{
txMsg = msgHandler.ReserveMsg(msg.msgID, sizeof(DefaultMsg_t));

if (nullptr != txMsg.msgBuf)
{
auto *response = (DefaultMsg_t *)txMsg.msgBuf;
response->ack = htons(static_cast<uint16_t>(Reply::UNKNOWN_REQ));
msgHandler.SendMsg(txMsg);
}
};
virtual void SpecificExecute(const MsgInfo &msg, MainControl &ctrl);

MsgHandler &GetMsgHandler() { return msgHandler; }

protected:
MsgInfo txMsg;
MsgHandler msgHandler;
static const char* sw = "Embedded Test Program";
};


class DeviceOneDispatcher : public BaseDispatcher
{
public:
DeviceOneDispatcher () = default;
virtual ~DeviceOneDispatcher () = default;

void SpecificExecute(const MsgInfo &msg, MainControl &ctrl) override
{
switch (msg.msgID)
{
case INFO_TEMP:
HandleTemperature(msg, ctrl);
break;
default:
HandleUnknown(msg);
}
};

protected:
void HandleTemperature(const MsgInfo &msg, MainControl &ctrl)
{
float temperature = (float)ntohl(*(uint16_t*)msg.msgBuf) / 10;
ctrl.GetRFMonitor().SetTemperature(temperature);
};
};

class DeviceTwoDispatcher : public BaseDispatcher
{
public:
DeviceTwoDispatcher () = default;
virtual ~DeviceTwoDispatcher () = default;

void SpecificExecute(const MsgInfo &msg, MainControl &ctrl) override
{
switch (msg.msgID)
{
case INFO_TEMP:
HandleMotor(msg, ctrl);
break;
default:
HandleUnknown(msg);
}
};

protected:
void HandleMotor(const MsgInfo &msg, MainControl &ctrl)
{
//do something
};
};
Class MainController()
{
public:
Execute()
{
dispatcherOne.Execute(*this);
dispatcherTwo.Execute(*this);
}
DeviceOneDispatcher &GetDispatcherOne() { return dispatcherOne; }
DeviceTwoDispatcher &GetDispatcherTwo() { return dispatcherTwo; }
protected:
DeviceOneDispatcher dispatcherOne;
DeviceTwoDispatcher dispatcherTwo;
}
```









share|improve this question









New contributor




RForce is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



put on hold as off-topic by pacmaninbw, Toby Speight, Edward, Graipher, Mast 9 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Edward, Graipher, Mast

If this question can be reworded to fit the rules in the help center, please edit the question.
















  • $begingroup$
    This code seems to be incomplete, there is no function HandleConsoleVerbosity()
    $endgroup$
    – pacmaninbw
    Apr 1 at 12:44










  • $begingroup$
    This code can't compile, the function HandleVersion() uses the variable sw which is never defined. Suggestion turn all of your C style casts into static casts.
    $endgroup$
    – pacmaninbw
    Apr 1 at 13:04










  • $begingroup$
    The code does compile, however as this is only a snapshot of the program not everything is defined here.
    $endgroup$
    – RForce
    Apr 1 at 13:13












  • $begingroup$
    @RForce Please include the rest as well to avoid such confusion. We need to see code in it's context, otherwise a review would be filled with guesswork. That helps nobody.
    $endgroup$
    – Mast
    9 hours ago














3












3








3





$begingroup$


I have some code, that works fine. However, I know there is a lot of code duplication and C-style programming. In the code example there only is a DeviceOneDispatcher, in reality there are more child classes and case options.



The question is how to optimize/refactor this code best following c++11-c++17.
I believe a big part of the duplication, could be solved by using a template. But I am not sure how implement this.



class BaseDispatcher
{
public:
BaseDispatcher() = default;
virtual ~BaseDispatcher() = default;

result_t Init(uint8_t idx)
{
msgHandler.Init(idx);
};

void Execute(MainControl &ctrl)
{
MsgInfo msg = { 0 };
if (msgHandler.RxMsg(msg))
{
switch (msg.msgID)
{
case REQ_PING:
HandlePing(msg);
break;
case REQ_VERSION:
HandleVersion(msg);
break;
default:
SpecificExecute(msg, ctrl);
}
};

protected:
void HandlePing(const MsgInfo &msg)
{
txMsg = msgHandler.ReserveMsg(msg.msgID, sizeof(DefaultMsg_t));

if (nullptr != txMsg.msgBuf)
{
auto *response = (DefaultMsg_t *)txMsg.msgBuf;
response->ack = htons(static_cast<uint16_t>(Reply::ACCEPTED));
msgHandler.SendMsg(txMsg);
mainCtrl->GetConsole().OutputEnable();
}
};
void HandleVersion(const MsgInfo &msg)
{
txMsg = msgHandler.ReserveMsg(msg.msgID, sizeof(VersionInfoMsg_t));

if (nullptr != txMsg.msgBuf)
{
auto *response = (VersionInfoMsg_t *)txMsg.msgBuf;
memset(response->name, 0, 32);
response->svnRevision = htons(svnRevision);
uint8_t sl = (uint8_t)strlen(sw);
memcpy(response->name, sw, sl <= 32 ? sl : 32);
msgHandler.SendMsg(txMsg);
}
};
void HandleUnknown(const MsgInfo &msg)
{
txMsg = msgHandler.ReserveMsg(msg.msgID, sizeof(DefaultMsg_t));

if (nullptr != txMsg.msgBuf)
{
auto *response = (DefaultMsg_t *)txMsg.msgBuf;
response->ack = htons(static_cast<uint16_t>(Reply::UNKNOWN_REQ));
msgHandler.SendMsg(txMsg);
}
};
virtual void SpecificExecute(const MsgInfo &msg, MainControl &ctrl);

MsgHandler &GetMsgHandler() { return msgHandler; }

protected:
MsgInfo txMsg;
MsgHandler msgHandler;
static const char* sw = "Embedded Test Program";
};


class DeviceOneDispatcher : public BaseDispatcher
{
public:
DeviceOneDispatcher () = default;
virtual ~DeviceOneDispatcher () = default;

void SpecificExecute(const MsgInfo &msg, MainControl &ctrl) override
{
switch (msg.msgID)
{
case INFO_TEMP:
HandleTemperature(msg, ctrl);
break;
default:
HandleUnknown(msg);
}
};

protected:
void HandleTemperature(const MsgInfo &msg, MainControl &ctrl)
{
float temperature = (float)ntohl(*(uint16_t*)msg.msgBuf) / 10;
ctrl.GetRFMonitor().SetTemperature(temperature);
};
};

class DeviceTwoDispatcher : public BaseDispatcher
{
public:
DeviceTwoDispatcher () = default;
virtual ~DeviceTwoDispatcher () = default;

void SpecificExecute(const MsgInfo &msg, MainControl &ctrl) override
{
switch (msg.msgID)
{
case INFO_TEMP:
HandleMotor(msg, ctrl);
break;
default:
HandleUnknown(msg);
}
};

protected:
void HandleMotor(const MsgInfo &msg, MainControl &ctrl)
{
//do something
};
};
Class MainController()
{
public:
Execute()
{
dispatcherOne.Execute(*this);
dispatcherTwo.Execute(*this);
}
DeviceOneDispatcher &GetDispatcherOne() { return dispatcherOne; }
DeviceTwoDispatcher &GetDispatcherTwo() { return dispatcherTwo; }
protected:
DeviceOneDispatcher dispatcherOne;
DeviceTwoDispatcher dispatcherTwo;
}
```









share|improve this question









New contributor




RForce is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I have some code, that works fine. However, I know there is a lot of code duplication and C-style programming. In the code example there only is a DeviceOneDispatcher, in reality there are more child classes and case options.



The question is how to optimize/refactor this code best following c++11-c++17.
I believe a big part of the duplication, could be solved by using a template. But I am not sure how implement this.



class BaseDispatcher
{
public:
BaseDispatcher() = default;
virtual ~BaseDispatcher() = default;

result_t Init(uint8_t idx)
{
msgHandler.Init(idx);
};

void Execute(MainControl &ctrl)
{
MsgInfo msg = { 0 };
if (msgHandler.RxMsg(msg))
{
switch (msg.msgID)
{
case REQ_PING:
HandlePing(msg);
break;
case REQ_VERSION:
HandleVersion(msg);
break;
default:
SpecificExecute(msg, ctrl);
}
};

protected:
void HandlePing(const MsgInfo &msg)
{
txMsg = msgHandler.ReserveMsg(msg.msgID, sizeof(DefaultMsg_t));

if (nullptr != txMsg.msgBuf)
{
auto *response = (DefaultMsg_t *)txMsg.msgBuf;
response->ack = htons(static_cast<uint16_t>(Reply::ACCEPTED));
msgHandler.SendMsg(txMsg);
mainCtrl->GetConsole().OutputEnable();
}
};
void HandleVersion(const MsgInfo &msg)
{
txMsg = msgHandler.ReserveMsg(msg.msgID, sizeof(VersionInfoMsg_t));

if (nullptr != txMsg.msgBuf)
{
auto *response = (VersionInfoMsg_t *)txMsg.msgBuf;
memset(response->name, 0, 32);
response->svnRevision = htons(svnRevision);
uint8_t sl = (uint8_t)strlen(sw);
memcpy(response->name, sw, sl <= 32 ? sl : 32);
msgHandler.SendMsg(txMsg);
}
};
void HandleUnknown(const MsgInfo &msg)
{
txMsg = msgHandler.ReserveMsg(msg.msgID, sizeof(DefaultMsg_t));

if (nullptr != txMsg.msgBuf)
{
auto *response = (DefaultMsg_t *)txMsg.msgBuf;
response->ack = htons(static_cast<uint16_t>(Reply::UNKNOWN_REQ));
msgHandler.SendMsg(txMsg);
}
};
virtual void SpecificExecute(const MsgInfo &msg, MainControl &ctrl);

MsgHandler &GetMsgHandler() { return msgHandler; }

protected:
MsgInfo txMsg;
MsgHandler msgHandler;
static const char* sw = "Embedded Test Program";
};


class DeviceOneDispatcher : public BaseDispatcher
{
public:
DeviceOneDispatcher () = default;
virtual ~DeviceOneDispatcher () = default;

void SpecificExecute(const MsgInfo &msg, MainControl &ctrl) override
{
switch (msg.msgID)
{
case INFO_TEMP:
HandleTemperature(msg, ctrl);
break;
default:
HandleUnknown(msg);
}
};

protected:
void HandleTemperature(const MsgInfo &msg, MainControl &ctrl)
{
float temperature = (float)ntohl(*(uint16_t*)msg.msgBuf) / 10;
ctrl.GetRFMonitor().SetTemperature(temperature);
};
};

class DeviceTwoDispatcher : public BaseDispatcher
{
public:
DeviceTwoDispatcher () = default;
virtual ~DeviceTwoDispatcher () = default;

void SpecificExecute(const MsgInfo &msg, MainControl &ctrl) override
{
switch (msg.msgID)
{
case INFO_TEMP:
HandleMotor(msg, ctrl);
break;
default:
HandleUnknown(msg);
}
};

protected:
void HandleMotor(const MsgInfo &msg, MainControl &ctrl)
{
//do something
};
};
Class MainController()
{
public:
Execute()
{
dispatcherOne.Execute(*this);
dispatcherTwo.Execute(*this);
}
DeviceOneDispatcher &GetDispatcherOne() { return dispatcherOne; }
DeviceTwoDispatcher &GetDispatcherTwo() { return dispatcherTwo; }
protected:
DeviceOneDispatcher dispatcherOne;
DeviceTwoDispatcher dispatcherTwo;
}
```






c++ c++11






share|improve this question









New contributor




RForce is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




RForce is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Apr 2 at 6:44







RForce













New contributor




RForce is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Apr 1 at 12:22









RForceRForce

192




192




New contributor




RForce is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





RForce is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






RForce is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as off-topic by pacmaninbw, Toby Speight, Edward, Graipher, Mast 9 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Edward, Graipher, Mast

If this question can be reworded to fit the rules in the help center, please edit the question.







put on hold as off-topic by pacmaninbw, Toby Speight, Edward, Graipher, Mast 9 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Edward, Graipher, Mast

If this question can be reworded to fit the rules in the help center, please edit the question.












  • $begingroup$
    This code seems to be incomplete, there is no function HandleConsoleVerbosity()
    $endgroup$
    – pacmaninbw
    Apr 1 at 12:44










  • $begingroup$
    This code can't compile, the function HandleVersion() uses the variable sw which is never defined. Suggestion turn all of your C style casts into static casts.
    $endgroup$
    – pacmaninbw
    Apr 1 at 13:04










  • $begingroup$
    The code does compile, however as this is only a snapshot of the program not everything is defined here.
    $endgroup$
    – RForce
    Apr 1 at 13:13












  • $begingroup$
    @RForce Please include the rest as well to avoid such confusion. We need to see code in it's context, otherwise a review would be filled with guesswork. That helps nobody.
    $endgroup$
    – Mast
    9 hours ago


















  • $begingroup$
    This code seems to be incomplete, there is no function HandleConsoleVerbosity()
    $endgroup$
    – pacmaninbw
    Apr 1 at 12:44










  • $begingroup$
    This code can't compile, the function HandleVersion() uses the variable sw which is never defined. Suggestion turn all of your C style casts into static casts.
    $endgroup$
    – pacmaninbw
    Apr 1 at 13:04










  • $begingroup$
    The code does compile, however as this is only a snapshot of the program not everything is defined here.
    $endgroup$
    – RForce
    Apr 1 at 13:13












  • $begingroup$
    @RForce Please include the rest as well to avoid such confusion. We need to see code in it's context, otherwise a review would be filled with guesswork. That helps nobody.
    $endgroup$
    – Mast
    9 hours ago
















$begingroup$
This code seems to be incomplete, there is no function HandleConsoleVerbosity()
$endgroup$
– pacmaninbw
Apr 1 at 12:44




$begingroup$
This code seems to be incomplete, there is no function HandleConsoleVerbosity()
$endgroup$
– pacmaninbw
Apr 1 at 12:44












$begingroup$
This code can't compile, the function HandleVersion() uses the variable sw which is never defined. Suggestion turn all of your C style casts into static casts.
$endgroup$
– pacmaninbw
Apr 1 at 13:04




$begingroup$
This code can't compile, the function HandleVersion() uses the variable sw which is never defined. Suggestion turn all of your C style casts into static casts.
$endgroup$
– pacmaninbw
Apr 1 at 13:04












$begingroup$
The code does compile, however as this is only a snapshot of the program not everything is defined here.
$endgroup$
– RForce
Apr 1 at 13:13






$begingroup$
The code does compile, however as this is only a snapshot of the program not everything is defined here.
$endgroup$
– RForce
Apr 1 at 13:13














$begingroup$
@RForce Please include the rest as well to avoid such confusion. We need to see code in it's context, otherwise a review would be filled with guesswork. That helps nobody.
$endgroup$
– Mast
9 hours ago




$begingroup$
@RForce Please include the rest as well to avoid such confusion. We need to see code in it's context, otherwise a review would be filled with guesswork. That helps nobody.
$endgroup$
– Mast
9 hours ago










1 Answer
1






active

oldest

votes


















0












$begingroup$

it seems you just need a dispatcher and some msghanlders., not many dispatchers.
the codes can modify like this:



class BaseDispatcher
{
public:
BaseDispatcher() = default;
virtual ~BaseDispatcher() = default;

result_t Init(uint8_t idx)
{
//msgHandler.Init(idx);
//to init pmsgHandlerVec
};

void Execute(MainControl &ctrl)
{
for (int i = 0; i < pmsgHandlerVec.size(); ++i)
{
BaseHandler* phandler = pmsgHandlerVec[i];
bool res = phandler->handle_msg(txMsg, ctrl);
if (res)
{
break;
}
}
};

protected:
MsgInfo txMsg;
std::vector<BaseHandler*> pmsgHandlerVec;
static const char* sw = "Embedded Test Program";
};

class BaseHandler
{
public:
virtual bol handle_msg(const MsgInfo &msg, MainControl &ctrl) = 0;
}

class Handler1 : public BaseHandler
{
public:
virtual bool handle_msg(const MsgInfo &msg, MainControl &ctrl)
{
switch (msg.msgID)
{
case REQ_PING:
HandlePing(msg);
break;
case REQ_VERSION:
HandleVersion(msg);
break;
default:
return false;
}
return true;
}
}

class Handler2 : public BaseHandler
{
public:
virtual bool handle_msg(const MsgInfo &msg, MainControl &ctrl)
{
switch (msg.msgID)
{
case INFO_TEMP:
HandleTemperature(msg, ctrl);
break;
default:
return false;
}
return true;
}
}

Class MainController()
{
public:
Execute()
{
dispatcher.Execute(*this);
}
protected:
BaseDispatcher dispatcher;
}





share|improve this answer








New contributor




allen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$













  • $begingroup$
    I dont think this is the best solution in this case. I would like to know which handle I am using. For example DeviceOneDispatcher.Execute(). With a vector you dont know this. Besides I need to be able to use the device handle from other classes declared in MainControl as well. The switch case from Handler1 should be common for all the devices handlers and supplemented with the specific switch cases per device. I edited my code example for more clerification
    $endgroup$
    – RForce
    Apr 2 at 5:45












  • $begingroup$
    (Welcome to finally posting on stack exchange!)
    $endgroup$
    – greybeard
    Apr 2 at 6:05


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0












$begingroup$

it seems you just need a dispatcher and some msghanlders., not many dispatchers.
the codes can modify like this:



class BaseDispatcher
{
public:
BaseDispatcher() = default;
virtual ~BaseDispatcher() = default;

result_t Init(uint8_t idx)
{
//msgHandler.Init(idx);
//to init pmsgHandlerVec
};

void Execute(MainControl &ctrl)
{
for (int i = 0; i < pmsgHandlerVec.size(); ++i)
{
BaseHandler* phandler = pmsgHandlerVec[i];
bool res = phandler->handle_msg(txMsg, ctrl);
if (res)
{
break;
}
}
};

protected:
MsgInfo txMsg;
std::vector<BaseHandler*> pmsgHandlerVec;
static const char* sw = "Embedded Test Program";
};

class BaseHandler
{
public:
virtual bol handle_msg(const MsgInfo &msg, MainControl &ctrl) = 0;
}

class Handler1 : public BaseHandler
{
public:
virtual bool handle_msg(const MsgInfo &msg, MainControl &ctrl)
{
switch (msg.msgID)
{
case REQ_PING:
HandlePing(msg);
break;
case REQ_VERSION:
HandleVersion(msg);
break;
default:
return false;
}
return true;
}
}

class Handler2 : public BaseHandler
{
public:
virtual bool handle_msg(const MsgInfo &msg, MainControl &ctrl)
{
switch (msg.msgID)
{
case INFO_TEMP:
HandleTemperature(msg, ctrl);
break;
default:
return false;
}
return true;
}
}

Class MainController()
{
public:
Execute()
{
dispatcher.Execute(*this);
}
protected:
BaseDispatcher dispatcher;
}





share|improve this answer








New contributor




allen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$













  • $begingroup$
    I dont think this is the best solution in this case. I would like to know which handle I am using. For example DeviceOneDispatcher.Execute(). With a vector you dont know this. Besides I need to be able to use the device handle from other classes declared in MainControl as well. The switch case from Handler1 should be common for all the devices handlers and supplemented with the specific switch cases per device. I edited my code example for more clerification
    $endgroup$
    – RForce
    Apr 2 at 5:45












  • $begingroup$
    (Welcome to finally posting on stack exchange!)
    $endgroup$
    – greybeard
    Apr 2 at 6:05
















0












$begingroup$

it seems you just need a dispatcher and some msghanlders., not many dispatchers.
the codes can modify like this:



class BaseDispatcher
{
public:
BaseDispatcher() = default;
virtual ~BaseDispatcher() = default;

result_t Init(uint8_t idx)
{
//msgHandler.Init(idx);
//to init pmsgHandlerVec
};

void Execute(MainControl &ctrl)
{
for (int i = 0; i < pmsgHandlerVec.size(); ++i)
{
BaseHandler* phandler = pmsgHandlerVec[i];
bool res = phandler->handle_msg(txMsg, ctrl);
if (res)
{
break;
}
}
};

protected:
MsgInfo txMsg;
std::vector<BaseHandler*> pmsgHandlerVec;
static const char* sw = "Embedded Test Program";
};

class BaseHandler
{
public:
virtual bol handle_msg(const MsgInfo &msg, MainControl &ctrl) = 0;
}

class Handler1 : public BaseHandler
{
public:
virtual bool handle_msg(const MsgInfo &msg, MainControl &ctrl)
{
switch (msg.msgID)
{
case REQ_PING:
HandlePing(msg);
break;
case REQ_VERSION:
HandleVersion(msg);
break;
default:
return false;
}
return true;
}
}

class Handler2 : public BaseHandler
{
public:
virtual bool handle_msg(const MsgInfo &msg, MainControl &ctrl)
{
switch (msg.msgID)
{
case INFO_TEMP:
HandleTemperature(msg, ctrl);
break;
default:
return false;
}
return true;
}
}

Class MainController()
{
public:
Execute()
{
dispatcher.Execute(*this);
}
protected:
BaseDispatcher dispatcher;
}





share|improve this answer








New contributor




allen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$













  • $begingroup$
    I dont think this is the best solution in this case. I would like to know which handle I am using. For example DeviceOneDispatcher.Execute(). With a vector you dont know this. Besides I need to be able to use the device handle from other classes declared in MainControl as well. The switch case from Handler1 should be common for all the devices handlers and supplemented with the specific switch cases per device. I edited my code example for more clerification
    $endgroup$
    – RForce
    Apr 2 at 5:45












  • $begingroup$
    (Welcome to finally posting on stack exchange!)
    $endgroup$
    – greybeard
    Apr 2 at 6:05














0












0








0





$begingroup$

it seems you just need a dispatcher and some msghanlders., not many dispatchers.
the codes can modify like this:



class BaseDispatcher
{
public:
BaseDispatcher() = default;
virtual ~BaseDispatcher() = default;

result_t Init(uint8_t idx)
{
//msgHandler.Init(idx);
//to init pmsgHandlerVec
};

void Execute(MainControl &ctrl)
{
for (int i = 0; i < pmsgHandlerVec.size(); ++i)
{
BaseHandler* phandler = pmsgHandlerVec[i];
bool res = phandler->handle_msg(txMsg, ctrl);
if (res)
{
break;
}
}
};

protected:
MsgInfo txMsg;
std::vector<BaseHandler*> pmsgHandlerVec;
static const char* sw = "Embedded Test Program";
};

class BaseHandler
{
public:
virtual bol handle_msg(const MsgInfo &msg, MainControl &ctrl) = 0;
}

class Handler1 : public BaseHandler
{
public:
virtual bool handle_msg(const MsgInfo &msg, MainControl &ctrl)
{
switch (msg.msgID)
{
case REQ_PING:
HandlePing(msg);
break;
case REQ_VERSION:
HandleVersion(msg);
break;
default:
return false;
}
return true;
}
}

class Handler2 : public BaseHandler
{
public:
virtual bool handle_msg(const MsgInfo &msg, MainControl &ctrl)
{
switch (msg.msgID)
{
case INFO_TEMP:
HandleTemperature(msg, ctrl);
break;
default:
return false;
}
return true;
}
}

Class MainController()
{
public:
Execute()
{
dispatcher.Execute(*this);
}
protected:
BaseDispatcher dispatcher;
}





share|improve this answer








New contributor




allen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$



it seems you just need a dispatcher and some msghanlders., not many dispatchers.
the codes can modify like this:



class BaseDispatcher
{
public:
BaseDispatcher() = default;
virtual ~BaseDispatcher() = default;

result_t Init(uint8_t idx)
{
//msgHandler.Init(idx);
//to init pmsgHandlerVec
};

void Execute(MainControl &ctrl)
{
for (int i = 0; i < pmsgHandlerVec.size(); ++i)
{
BaseHandler* phandler = pmsgHandlerVec[i];
bool res = phandler->handle_msg(txMsg, ctrl);
if (res)
{
break;
}
}
};

protected:
MsgInfo txMsg;
std::vector<BaseHandler*> pmsgHandlerVec;
static const char* sw = "Embedded Test Program";
};

class BaseHandler
{
public:
virtual bol handle_msg(const MsgInfo &msg, MainControl &ctrl) = 0;
}

class Handler1 : public BaseHandler
{
public:
virtual bool handle_msg(const MsgInfo &msg, MainControl &ctrl)
{
switch (msg.msgID)
{
case REQ_PING:
HandlePing(msg);
break;
case REQ_VERSION:
HandleVersion(msg);
break;
default:
return false;
}
return true;
}
}

class Handler2 : public BaseHandler
{
public:
virtual bool handle_msg(const MsgInfo &msg, MainControl &ctrl)
{
switch (msg.msgID)
{
case INFO_TEMP:
HandleTemperature(msg, ctrl);
break;
default:
return false;
}
return true;
}
}

Class MainController()
{
public:
Execute()
{
dispatcher.Execute(*this);
}
protected:
BaseDispatcher dispatcher;
}






share|improve this answer








New contributor




allen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer






New contributor




allen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered Apr 2 at 4:04









allenallen

1




1




New contributor




allen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





allen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






allen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $begingroup$
    I dont think this is the best solution in this case. I would like to know which handle I am using. For example DeviceOneDispatcher.Execute(). With a vector you dont know this. Besides I need to be able to use the device handle from other classes declared in MainControl as well. The switch case from Handler1 should be common for all the devices handlers and supplemented with the specific switch cases per device. I edited my code example for more clerification
    $endgroup$
    – RForce
    Apr 2 at 5:45












  • $begingroup$
    (Welcome to finally posting on stack exchange!)
    $endgroup$
    – greybeard
    Apr 2 at 6:05


















  • $begingroup$
    I dont think this is the best solution in this case. I would like to know which handle I am using. For example DeviceOneDispatcher.Execute(). With a vector you dont know this. Besides I need to be able to use the device handle from other classes declared in MainControl as well. The switch case from Handler1 should be common for all the devices handlers and supplemented with the specific switch cases per device. I edited my code example for more clerification
    $endgroup$
    – RForce
    Apr 2 at 5:45












  • $begingroup$
    (Welcome to finally posting on stack exchange!)
    $endgroup$
    – greybeard
    Apr 2 at 6:05
















$begingroup$
I dont think this is the best solution in this case. I would like to know which handle I am using. For example DeviceOneDispatcher.Execute(). With a vector you dont know this. Besides I need to be able to use the device handle from other classes declared in MainControl as well. The switch case from Handler1 should be common for all the devices handlers and supplemented with the specific switch cases per device. I edited my code example for more clerification
$endgroup$
– RForce
Apr 2 at 5:45






$begingroup$
I dont think this is the best solution in this case. I would like to know which handle I am using. For example DeviceOneDispatcher.Execute(). With a vector you dont know this. Besides I need to be able to use the device handle from other classes declared in MainControl as well. The switch case from Handler1 should be common for all the devices handlers and supplemented with the specific switch cases per device. I edited my code example for more clerification
$endgroup$
– RForce
Apr 2 at 5:45














$begingroup$
(Welcome to finally posting on stack exchange!)
$endgroup$
– greybeard
Apr 2 at 6:05




$begingroup$
(Welcome to finally posting on stack exchange!)
$endgroup$
– greybeard
Apr 2 at 6:05



Popular posts from this blog

Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...