You're right to be concerned about the current implementation relying on a manually maintained enum and assuming a specific "last component" (OWN = TEACHIN) as the upper bound in:
for (UInt32 i = 0; i <= Datamanagement::Component::OWN; i++)
This is fragile because:
- Adding or reordering enum values requires remembering to update this loop.
- It depends on numeric values rather than semantic grouping.
✅ Goal
Refactor to:
- Avoid using enum values as loop bounds.
- Make the list of components extensible and declarative.
- Avoid magic numbers and implicit assumptions.
✅ Recommended Solution: Use a std::vector or std::array of enum values
You can declare a centralized registry of all known components explicitly — either as a global/static array/vector, or better yet, as a constexpr static member if using modern C++.
🔁 Step 1: Keep the enum, but don't rely on its last value
Keep your enum as is (or even better, use a strongly typed enum):
namespace TeachIn::Datamanagement::Component
{
enum class 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,
OWN = TEACHIN
// New items can be added here safely
};
}
⚠️ Note: Prefer enum class over plain enum to avoid polluting the namespace and enable stronger type safety.
📋 Step 2: Declare a constexpr list of all valid components
We'll define a list we can iterate over:
Option A: Using std::array (C++11 and later)
#include <array>
namespace TeachIn::Datamanagement::Component
{
constexpr std::array<Type, 14> AllComponents = {
Type::BROKER,
Type::GUI,
Type::SHARED_MEMORY_MANAGER,
Type::FORMAT_MANAGER,
Type::IMAGE_MANAGER,
Type::MASK_MANAGER,
Type::CLUT_MANAGER,
Type::EVAL_MANAGER,
Type::GRAB_IO,
Type::IO_MANAGER,
Type::RESULT_PRESENTER,
Type::SENSOR_MANAGER,
Type::TEACHIN,
Type::OWN
};
}
If you ever add a new component, just append it to this list.
🔁 Step 3: Update your loop
Now your loop becomes clean and safe: