15 #ifndef SCARAB_INDEXED_FACTORY_HH_ 16 #define SCARAB_INDEXED_FACTORY_HH_ 29 LOGGER( slog_ind_fact,
"indexed_factory" );
35 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
40 template<
class XBaseType,
typename ... XArgs >
47 virtual XBaseType*
create( XArgs ... args )
const = 0;
53 template<
class XIndexType,
class XBaseType,
class XDerivedType,
typename ... XArgs >
60 void register_class()
const;
62 XBaseType*
create( XArgs ... args )
const;
70 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
80 XBaseType*
create(
const XIndexType& a_index, XArgs ... args );
81 XBaseType*
create(
const FactoryCIt& iter, XArgs ... args );
84 bool has_class(
const XIndexType& a_index )
const;
85 void remove_class(
const XIndexType& a_index );
87 FactoryCIt begin()
const;
88 FactoryCIt end()
const;
105 template<
class XBaseType >
112 virtual XBaseType*
create()
const = 0;
116 template<
class XIndexType,
class XBaseType,
class XDerivedType >
123 void register_class()
const;
125 XBaseType*
create()
const;
132 template<
class XIndexType,
class XBaseType >
136 typedef std::map< XIndexType, const base_registrar< XBaseType >* >
FactoryMap;
142 XBaseType*
create(
const XIndexType& a_index );
143 XBaseType*
create(
const FactoryCIt& iter );
146 bool has_class(
const XIndexType& a_index )
const;
147 void remove_class(
const XIndexType& a_index );
149 FactoryCIt begin()
const;
150 FactoryCIt end()
const;
169 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
172 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
173 return iter->second->create( args... );
176 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
184 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
186 if( it == fMap->end() )
188 LERROR( slog_ind_fact,
"Did not find indexed_factory for <" << a_index <<
">." );
192 return it->second->create( args... );
195 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
199 LOGGER( slog_ind_factory_reg,
"indexed_factory-register");
201 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
203 if (it != fMap->end())
205 throw error() <<
"Already have indexed_factory registered for <" << a_index <<
">.";
208 LDEBUG( slog_ind_factory_reg,
"Registered a indexed_factory for class " << a_index <<
" at " << (*fMap)[ a_index ] <<
", indexed_factory #" << fMap->size()-1 <<
" for " << this );
211 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
214 return fMap->find( a_index ) != fMap->end();
217 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
220 LDEBUG( slog_ind_fact,
"Removing indexed_factory for class " << a_index <<
" from " <<
this );
222 if( iter != fMap->end() ) fMap->erase( iter );
226 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
232 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
238 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
241 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
242 return fMap->begin();
245 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
248 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
254 template<
class XIndexType,
class XBaseType,
class XDerivedType,
typename ... XArgs >
262 template<
class XIndexType,
class XBaseType,
class XDerivedType,
typename ... XArgs >
268 template<
class XIndexType,
class XBaseType,
class XDerivedType,
typename ... XArgs >
275 template<
class XIndexType,
class XBaseType,
class XDerivedType,
typename ... XArgs >
278 return dynamic_cast< XBaseType*
>(
new XDerivedType( args... ) );
288 template<
class XIndexType,
class XBaseType >
291 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
292 return iter->second->create();
295 template<
class XIndexType,
class XBaseType >
303 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
305 if( it == fMap->end() )
307 LERROR( slog_ind_fact,
"Did not find indexed_factory for <" << a_index <<
">." );
311 return it->second->create();
314 template<
class XIndexType,
class XBaseType >
318 LOGGER( slog_ind_factory_reg,
"indexed_factory-register");
320 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
322 if (it != fMap->end())
324 throw error() <<
"Already have indexed_factory registered for <" << a_index <<
">.";
328 LDEBUG( slog_ind_factory_reg,
"Registered a indexed_factory for class " << a_index <<
", indexed_factory #" << fMap->size()-1 <<
" for " << this );
331 template<
class XIndexType,
class XBaseType >
334 return fMap->find( a_index ) != fMap->end();
337 template<
class XIndexType,
class XBaseType >
340 LDEBUG( slog_ind_fact,
"Removing indexed_factory for class " << a_index <<
" from " <<
this );
342 if( iter != fMap->end() ) fMap->erase( iter );
346 template<
class XIndexType,
class XBaseType >
352 template<
class XIndexType,
class XBaseType >
358 template<
class XIndexType,
class XBaseType >
362 return fMap->begin();
365 template<
class XIndexType,
class XBaseType >
374 template<
class XIndexType,
class XBaseType,
class XDerivedType >
382 template<
class XIndexType,
class XBaseType,
class XDerivedType >
388 template<
class XIndexType,
class XBaseType,
class XDerivedType >
395 template<
class XIndexType,
class XBaseType,
class XDerivedType >
398 return dynamic_cast< XBaseType*
>(
new XDerivedType() );
std::map< XIndexType, const base_registrar< XBaseType, XArgs... > *> FactoryMap
XBaseType * create(XArgs ... args) const
virtual ~base_registrar()
std::mutex f_factory_mutex
void register_class(const XIndexType &a_index, const base_registrar< XBaseType, XArgs... > *base_registrar)
XBaseType * create(const XIndexType &a_index, XArgs ... args)
FactoryMap::const_iterator FactoryCIt
FactoryMap::iterator FactoryIt
LOGGER(mtlog, "authentication")
FactoryMap::value_type FactoryEntry
static indexed_factory< XIndexType, XBaseType, XArgs... > * get_instance()
Base class that turns a class into a singleton.
std::map< XIndexType, const base_registrar< XBaseType > *> FactoryMap
Contains the logger class and macros, based on Kasper's KLogger class.
virtual ~base_registrar()
void remove_class(const XIndexType &a_index)
std::mutex f_factory_mutex
FactoryMap::iterator FactoryIt
void register_class() const
FactoryMap::value_type FactoryEntry
virtual ~indexed_registrar()
indexed_registrar(const XIndexType &a_index)
virtual XBaseType * create(XArgs ... args) const =0
bool has_class(const XIndexType &a_index) const
FactoryMap::const_iterator FactoryCIt
#define allow_singleton_access(class_name)