Suppose you’re writing a library, providing a class with some kind of “type” parameter. For this one might use an enum:
namespace MyLib {
class Event {
public:
enum EventType { TYPE1, TYPE2, ... };
Event(EventType _type) : type(_type) { }
private:
EventType type;
}
}
Then to instantiate:
new MyLib::Event(Event::TYPE1);
Fine so far. But what if you want the user to be able to extend the list of event types? This is not possible if the type property is an enum.
Low-quality possibilities include asking them to #define custom event names, or simply using strings, though those both seem suboptimal.
Is there a general solution to this?
One suggestion has been to use a struct EventType to return values within a specified range in an enum. However, while this solves the compiler-safety issue, it doesn’t address the problem of adding named types – it would require the user to add these to the global scope.
One possibility which addresses the latter but not the former issue is to typedef EventType to an integral type, and leave it to the user to add custom types to the library’s namespace or their own. A factory method to provide unique values could be provided as part of Event:
#include <iostream>
#include <vector>
namespace MyLib {
namespace EventType {
typedef int T;
enum { TYPE1, TYPE2, Count};
}
class Event {
public:
Event(EventType::T _type) : type(_type) { }
EventType::T type;
static EventType::T registerType() { return _typeid++; }
private:
static int _typeid;
};
}
MyLib::EventType::T MyLib::Event::_typeid = EventType::Count;
// The user can then add types, including to the library's namespace
// (which may or may not be a good idea)
namespace MyLib { namespace EventType {
MyLib::EventType::T MYTYPE = MyLib::Event::registerType();
} }
int main() {
MyLib::Event ev1(MyLib::EventType::TYPE2);
MyLib::Event ev2(MyLib::EventType::MYTYPE);
std::cout << ev1.type << std::endl;
std::cout << ev2.type << std::endl;
return 0;
}
Outputs:
1
2
While this doesn’t technically restrict the parameter to a registered set of types, the typedef & namespacing provides a helpful syntactic cue in the constructor definition and auto-suggestion in IDEs, and it prevents user types from having to pollute the global scope, which is perhaps the greater concern.
Is there a better way to specify a finite, compiler-checked but user-extensible set of values as a type parameter to a class, or generally to a function/method?
I think your best bet is to simply take the event type as a normal integral type, use an enumeration to define the built-in types, and provide user-defined min and max values between which the user creates their own integer values with a separate enumerator. Then you’ll just use assertion to make sure that the base event doesn’t try to process an unknown event type.
EDIT: Do note that a base event class with some types that understands and some that it doesn’t may be fragile, unless it’s just holding the data for the child class to pick out again later. Even in that case it might be better to consider an alternate design. Can you elaborate on how the event type is used after it’s set?
EDIT2: I think I understand better now, the event stores the event type and an event consumer can retrieve that from the event. If the consumer understands the extended event type it can do appropriate processing, else delegate, assert, or simply no-op. Given that, I think using an integral event type seems fine.