1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
typedef msg_base_class *(*maker_func_type)(const msg_data&)
typedef std::map<unsigned int id, maker_func_type> maker_map_type;
maker_map_type maker_map;
void add_maker(unsigned int id, maker_func_type func)
{
// add only if the id is not associated with an existing maker
make_map_type::iterator_type it = maker_map.find(id);
if (it == maker_map.end()) {
maker_map[id] = func;
} else {
std::stringstream s;
s << "id " << id << " is already bound to a message maker";
throw std::runtime_error(s.str());
}
}
msg_base_class *make(unsigned int id, const msg_data& data)
{
make_map_type::iterator_type it = maker_map.find(id);
if (it != maker_map.end()) {
return it->second(data);
}
return NULL; // or nullptr in C++11
} |
Partager