To make this less error-prone and independent of the last component in the enum, I recommend using a few different approaches:
- Use a std::vector or std::array of valid components:
class Component {
public:
enum Type {
NONE = -1,
BROKER,
GUI,
SHARED_MEMORY_MANAGER,
FORMAT_MANAGER,
IMAGE_MANAGER,
MASK_MANAGER,
CLUT_MANAGER,
EVAL_MANAGER,
GRAB_IO,
IO_MANAGER,
RESULT_PRESENTER,
SENSOR_MANAGER,
TEACHIN,
// Remove OWN = TEACHIN
};
// Static method to get all valid components
static const std::vector<Type>& getAllComponents() {
static const std::vector<Type> components = {
BROKER,
GUI,
SHARED_MEMORY_MANAGER,
FORMAT_MANAGER,
IMAGE_MANAGER,
MASK_MANAGER,
CLUT_MANAGER,
EVAL_MANAGER,
GRAB_IO,
IO_MANAGER,
RESULT_PRESENTER,
SENSOR_MANAGER,
TEACHIN
};
return components;
}
};
// Modified parsing method
void CallbackConfig::ConfigurationParser::operator()(MessageProcessor* const poMessageProcessor,
Connection* const poConnection,
const MessageHandle& roMessage) const
{
LOG_FUNCTION_ENTER();
LOG_GATE_MESSAGE(poMessageProcessor, roMessage);
const MIParser::Message oParser = MIParser::oParseMessage(roMessage);
// Iterate through predefined vector of components
for (const auto& componentType : Component::getAllComponents())
{
vReadInComponentInformation(oParser, static_cast<UInt32>(componentType));
}
LOG_FUNCTION_LEAVE();
}
- Alternative approach using a more robust iteration method:
class Component {
public:
enum Type {
NONE = -1,
FIRST = 0, // Add an explicit first component
BROKER = FIRST,
GUI,
SHARED_MEMORY_MANAGER,
FORMAT_MANAGER,
IMAGE_MANAGER,
MASK_MANAGER,
CLUT_MANAGER,
EVAL_MANAGER,
GRAB_IO,
IO_MANAGER,
RESULT_PRESENTER,
SENSOR_MANAGER,
TEACHIN,
LAST = TEACHIN // Add an explicit last component
};
// Helper method to get the last valid component
static constexpr Type getLastComponent() {
return LAST;
}
// Helper method to get the first valid component
static constexpr Type getFirstComponent() {
return FIRST;
}
};
// Modified parsing method
void CallbackConfig::ConfigurationParser::operator()(MessageProcessor* const poMessageProcessor,
Connection* const poConnection,
const MessageHandle& roMessage) const
{
LOG_FUNCTION_ENTER();
LOG_GATE_MESSAGE(poMessageProcessor, roMessage);
const MIParser::Message oParser = MIParser::oParseMessage(roMessage);
// Iterate from first to last component explicitly
for (UInt32 i = Component::getFirstComponent(); i <= Component::getLastComponent(); i++)
{
vReadInComponentInformation(oParser, i);
}
LOG_FUNCTION_LEAVE();
}