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 const base_registrar< XBaseType, XArgs... >* get_registrar(
const XIndexType& a_index )
const;
89 FactoryCIt begin()
const;
90 FactoryCIt end()
const;
107 template<
class XBaseType >
114 virtual XBaseType*
create()
const = 0;
118 template<
class XIndexType,
class XBaseType,
class XDerivedType >
125 void register_class()
const;
127 XBaseType*
create()
const;
134 template<
class XIndexType,
class XBaseType >
138 typedef std::map< XIndexType, const base_registrar< XBaseType >* >
FactoryMap;
144 XBaseType*
create(
const XIndexType& a_index );
145 XBaseType*
create(
const FactoryCIt& iter );
148 bool has_class(
const XIndexType& a_index )
const;
149 void remove_class(
const XIndexType& a_index );
153 FactoryCIt begin()
const;
154 FactoryCIt end()
const;
173 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
176 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
177 return iter->second->create( args... );
180 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
188 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
190 if( it == fMap->end() )
192 LERROR( slog_ind_fact,
"Did not find indexed_factory for <" << a_index <<
">." );
196 return it->second->create( args... );
199 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
203 LOGGER( slog_ind_factory_reg,
"indexed_factory-register");
205 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
207 if (it != fMap->end())
209 throw error() <<
"Already have indexed_factory registered for <" << a_index <<
">.";
212 LDEBUG( slog_ind_factory_reg,
"Registered a indexed_factory for class " << a_index <<
" at " << (*fMap)[ a_index ] <<
", indexed_factory #" << fMap->size()-1 <<
" for " << this );
215 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
218 return fMap->find( a_index ) != fMap->end();
221 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
224 LDEBUG( slog_ind_fact,
"Removing indexed_factory for class " << a_index <<
" from " <<
this );
226 if( iter != fMap->end() ) fMap->erase( iter );
230 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
233 return fMap->at( a_index );
236 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
242 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
248 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
251 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
252 return fMap->begin();
255 template<
class XIndexType,
class XBaseType,
typename ... XArgs >
258 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
264 template<
class XIndexType,
class XBaseType,
class XDerivedType,
typename ... XArgs >
272 template<
class XIndexType,
class XBaseType,
class XDerivedType,
typename ... XArgs >
278 template<
class XIndexType,
class XBaseType,
class XDerivedType,
typename ... XArgs >
285 template<
class XIndexType,
class XBaseType,
class XDerivedType,
typename ... XArgs >
288 return dynamic_cast< XBaseType*
>(
new XDerivedType( args... ) );
298 template<
class XIndexType,
class XBaseType >
301 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
302 return iter->second->create();
305 template<
class XIndexType,
class XBaseType >
313 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
315 if( it == fMap->end() )
317 LERROR( slog_ind_fact,
"Did not find indexed_factory for <" << a_index <<
">." );
321 return it->second->create();
324 template<
class XIndexType,
class XBaseType >
328 LOGGER( slog_ind_factory_reg,
"indexed_factory-register");
330 std::unique_lock< std::mutex > t_lock( this->f_factory_mutex );
332 if (it != fMap->end())
334 throw error() <<
"Already have indexed_factory registered for <" << a_index <<
">.";
338 LDEBUG( slog_ind_factory_reg,
"Registered a indexed_factory for class " << a_index <<
", indexed_factory #" << fMap->size()-1 <<
" for " << this );
341 template<
class XIndexType,
class XBaseType >
344 return fMap->find( a_index ) != fMap->end();
347 template<
class XIndexType,
class XBaseType >
350 LDEBUG( slog_ind_fact,
"Removing indexed_factory for class " << a_index <<
" from " <<
this );
352 if( iter != fMap->end() ) fMap->erase( iter );
356 template<
class XIndexType,
class XBaseType >
359 return fMap->at( a_index );
362 template<
class XIndexType,
class XBaseType >
368 template<
class XIndexType,
class XBaseType >
374 template<
class XIndexType,
class XBaseType >
378 return fMap->begin();
381 template<
class XIndexType,
class XBaseType >
390 template<
class XIndexType,
class XBaseType,
class XDerivedType >
398 template<
class XIndexType,
class XBaseType,
class XDerivedType >
404 template<
class XIndexType,
class XBaseType,
class XDerivedType >
411 template<
class XIndexType,
class XBaseType,
class XDerivedType >
414 return dynamic_cast< XBaseType*
>(
new XDerivedType() );
const base_registrar< XBaseType, XArgs... > * get_registrar(const XIndexType &a_index) const
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)