12 #ifndef SPROCKIT_COMMON_FACTORIES_FACTORY_H_INCLUDED 13 #define SPROCKIT_COMMON_FACTORIES_FACTORY_H_INCLUDED 39 template <
class T,
typename... Args>
44 build(
const Args&... args) = 0;
48 template<
class Child,
class Factory>
53 template<
class T,
typename... Args>
63 typedef std::map<std::string, std::list<std::string> >
alias_map;
70 builder_map_ =
new builder_map;
73 alias_map_ =
new alias_map;
76 builder_t* base = (*builder_map_)[oldname];
78 (*alias_map_)[oldname].push_back(newname);
80 (*builder_map_)[newname] = base;
89 if (builder_map_)
delete builder_map_;
90 if (alias_map_)
delete alias_map_;
99 builder_map_ =
new builder_map;
102 alias_map_ =
new alias_map;
104 add_to_map(name, descr, builder_map_, alias_map_);
109 std::map<std::string, builder_t*>* descr_map,
110 std::map<std::string, std::list<std::string> >* alias_map)
112 std::string space =
"|";
113 std::deque<std::string> tok;
116 std::deque<std::string>::iterator it, end = tok.end();
118 for (it = tok.begin(); it != end; it++) {
119 std::string temp = *it;
123 std::map<std::string, std::list<std::string> >::iterator it = alias_map->find(temp);
124 if (it != alias_map->end()){
125 std::list<std::string>& alias_list = it->second;
126 std::list<std::string>::iterator ait, end = alias_list.end();
127 for (ait=alias_list.begin(); ait != end; ++ait){
128 (*builder_map_)[*ait] = desc;
132 (*builder_map_)[temp] = desc;
140 sprockit::sim_parameters* params,
141 const Args&... args) {
144 "could not find name %s for factory %s. no classes are registered",
149 auto it = builder_map_->find(valname), end = builder_map_->end();
152 std::cerr <<
"Valid factories are:" << std::endl;
153 for (it = builder_map_->begin(); it != end; ++it) {
154 std::cerr << it->first << std::endl;
157 valname.c_str(), name_);
163 "initialized name %s with null descr for factory %s",
164 valname.c_str(), name_);
167 T* p = descr->build(args...);
168 p->init_factory_params(params);
176 sim_parameters* params,
177 const Args&... args) {
178 return _get_value(valname, params, args...);
183 sim_parameters* params,
184 const Args&... args) {
185 if (params->has_param(param_name)) {
195 sim_parameters* params,
196 const Args&... args) {
197 return _get_value(params->get_param(param_name),
203 const std::string& defval,
204 sim_parameters* params,
205 const Args&... args) {
206 return _get_value(params->get_optional_param(param_name, defval),
212 template<
class Child,
typename Parent,
typename... Args>
223 return new Child(args...);
232 virtual T* build(sim_parameters* params) = 0;
237 template <
class T,
class Factory>
242 : param_name_(param_name)
265 template <
class T,
class Factory>
270 : param_name_(param_name)
284 template <
class Factory>
294 #define FirstArgStr(X, ...) #X 295 #define FirstArgFactoryName(X, ...) X##_factory 297 #define DeclareFactory(...) \ 298 typedef ::sprockit::Factory<__VA_ARGS__> FirstArgFactoryName(__VA_ARGS__); 300 #define ImplementFactory(type_name) \ 301 template<> const char* type_name##_factory::name_ = #type_name; \ 302 template<> std::map<std::string, type_name##_factory::builder_t*>* type_name##_factory::builder_map_ = 0; \ 303 template<> std::map<std::string, std::list<std::string>>* type_name##_factory::alias_map_ = 0; \ 304 namespace { static sprockit::CleanupFactory<type_name##_factory> cleaner; } 308 #define SpktTemplateRegister(cls_str, parent_cls, child_cls, unique_name, ...) \ 309 static ::sprockit::SpktBuilderImpl<child_cls, parent_cls##_factory> unique_name##_cd(cls_str) 311 #define SpktRegister(cls_str, parent_cls, child_cls, ...) \ 312 static ::sprockit::SpktBuilderImpl<child_cls,parent_cls##_factory> child_cls##_cd(cls_str) 315 #define DeclareFactory1InitParam(type_name, param1_name) \ 316 DeclareFactory(type_name, param1_name); 318 #define DeclareFactory2InitParams(type_name, param1_name, param2_name) \ 319 DeclareFactory(type_name, param1_name, param2_name); 321 #define DeclareFactory3InitParams(type_name, param1_name, param2_name, param3_name) \ 322 DeclareFactory(type_name, param1_name, param2_name, param3_name); template_factory2(const std::string ¶m_name)
static void register_alias(const std::string &oldname, const std::string &newname)
static T * get_value(const std::string &valname, sim_parameters *params, const Args &...args)
const char * get_param(char *str)
static void add_to_map(const std::string &namestr, builder_t *desc, std::map< std::string, builder_t * > *descr_map, std::map< std::string, std::list< std::string > > *alias_map)
static T * _get_value(const std::string &valname, sprockit::sim_parameters *params, const Args &...args)
void tokenize(const typename std::basic_string< CharType, Traits, StrAlloc > &str, typename std::deque< typename std::basic_string< CharType, Traits, StrAlloc >, DqAlloc > &tok, const typename std::basic_string< CharType, Traits, StrAlloc > &space=" \t\n")
A utility function to tokenize a std::string.
std::string trim_str(const std::string &Src, const std::string &c=" \r\n")
std::map< std::string, std::list< std::string > > alias_map
SpktBuilder< T, Args... > builder_t
static T * get_extra_param(const std::string ¶m_name, sim_parameters *params, const Args &...args)
static void register_name(const std::string &name, builder_t *descr)
static T * get_param(const std::string ¶m_name, sim_parameters *params, const Args &...args)
virtual void finalize_init()
static alias_map * alias_map_
virtual void init_factory_params(sim_parameters *params)
template_factory(const std::string ¶m_name)
#define spkt_throw_printf(exc, template_str,...)
std::map< std::string, builder_t * > builder_map
Parent * build(const Args &...args)
static T * get_optional_param(const std::string ¶m_name, const std::string &defval, sim_parameters *params, const Args &...args)
static const char * name_
static builder_map * builder_map_
T * build(sim_parameters *params)
SpktBuilderImpl(const char *name)
Error indicating some internal value was unexpected.
T * build(sim_parameters *params, sprockit::factory_type *ft)