69 template <
typename AbstractProductType>
77 typedef std::string Attribute;
78 typedef std::string Value;
87 typedef std::list<Key>
Keys;
88 typedef std::list<std::string>
Values;
95 virtual AbstractProductType*
Create() = 0;
115 return creator.Create();
171 Keys GetKeys(
const std::string& attribute,
const std::string& value)
173 return _registry.
GetKeys(attribute, value);
203 void AddAttribute(
const std::string& key,
const std::string& attribute,
const std::string& value)
217 struct FactoryEntry {
221 typedef std::map<Key, FactoryEntry> FactoryEntries;
226 CLAM_ASSERT(_factoryEntries.begin() != _factoryEntries.end(),
227 "the Factory Registry shouldn't be empty");
229 Creator* res = CommonGetCreator(creatorId);
232 std::string errmsg(
"GetCreator invoked with a non existent key: ");
233 errmsg += creatorId +
"\nRegistered keys are:\n";
243 if ( _factoryEntries.begin() == _factoryEntries.end() )
244 throw ErrFactory(
"GetCreatorSafe invoked on an empty registry");
246 Creator* res = CommonGetCreator(creatorId);
249 std::string msg(
"GetCreatorSafe invoked with a non existent key: ");
251 msg +=
"\nRegistered keys are:\n";
260 bool res = CommonAddCreator(creatorId, creator);
263 std::string errmsg(
"Adding creator method in the factory: CreatorId '");
264 errmsg += creatorId +
"' was already registered.\nRegistered keys are:\n";
271 bool res = CommonAddCreator(creatorId, creator);
274 std::string errmsg(
"WARNING. While adding a creator method in the factory, id '");
275 errmsg += creatorId +
"' was already registered.";
283 if( !CommonAddCreator( creatorId, creator ) )
284 throw ErrFactory(
"A repeated key was passed");
289 if (CommonDeleteCreator(creatorId)==
false)
290 std::cout<<
"WARNING: attempted to delete an inexistent creator"<<std::endl;
295 _factoryEntries.clear();
298 std::size_t
Count() {
return _factoryEntries.size(); }
302 typename FactoryEntries::const_iterator i;
304 for ( i = _factoryEntries.begin(); i != _factoryEntries.end(); i++ )
306 namesList.push_back( i->first );
312 typedef std::list<RegistryKey> Names;
315 for(Names::iterator it=names.begin(); it!=names.end(); it++)
317 result += (*it)+
", ";
325 typename FactoryEntries::const_iterator it = _factoryEntries.find(key);
326 if(it == _factoryEntries.end())
335 typename Pairs::const_iterator itPairs;
336 for (itPairs=pairsFromKey.begin();itPairs!=pairsFromKey.end();itPairs++)
338 if (itPairs->attribute==attribute)
return true;
344 Keys GetKeys(
const std::string& attribute,
const std::string& value)
347 typename FactoryEntries::const_iterator it;
348 for(it = _factoryEntries.begin(); it != _factoryEntries.end(); it++)
350 if( (attribute ==
"") )
352 result.push_back(it->first);
355 Pairs attributes = it->second.pairs;
356 typename Pairs::const_iterator itAtt;
357 for(itAtt = attributes.begin(); itAtt != attributes.end(); itAtt++)
359 if( ((*itAtt).attribute == attribute) && ((*itAtt).value == value) )
361 result.push_back(it->first);
376 typename FactoryEntries::const_iterator it = _factoryEntries.find(key);
377 if(it!=_factoryEntries.end())
379 attributes = it->second.pairs;
387 std::set<Value> AttributeSet;
388 std::set<Value>::const_iterator itSet;
390 typename FactoryEntries::const_iterator it;
391 for(it = _factoryEntries.begin(); it != _factoryEntries.end(); it++)
393 Pairs attributes = it->second.pairs;
394 typename Pairs::const_iterator itAtt;
395 for(itAtt = attributes.begin(); itAtt != attributes.end(); itAtt++)
397 if((*itAtt).attribute == attribute)
399 itSet = AttributeSet.find((*itAtt).value);
400 if(itSet == AttributeSet.end())
402 AttributeSet.insert((*itAtt).value);
408 for(itSet = AttributeSet.begin(); itSet != AttributeSet.end(); itSet++)
410 values.push_back(*itSet);
418 typename FactoryEntries::const_iterator it = _factoryEntries.find(key);
419 if(it != _factoryEntries.end())
421 typename Pairs::const_iterator itAtt;
422 for(itAtt = it->second.pairs.begin(); itAtt != it->second.pairs.end(); itAtt++)
424 if((*itAtt).attribute == attribute)
426 values.push_back((*itAtt).value);
438 void AddAttribute(
const std::string& key,
const std::string& attribute,
const std::string& value)
440 typename FactoryEntries::const_iterator it;
441 it = _factoryEntries.find(key);
449 pair.attribute = attribute;
452 _factoryEntries[key].pairs.push_back(pair);
468 FactoryEntries _factoryEntries;
473 typename FactoryEntries::const_iterator i =
474 _factoryEntries.find(creatorId);
475 if ( i==_factoryEntries.end() )
477 return i->second.creator;
482 FactoryEntry factoryEntry;
484 factoryEntry.creator = creator;
485 factoryEntry.pairs = pairs;
487 typedef typename FactoryEntries::value_type ValueType;
488 return _factoryEntries.insert( ValueType( creatorId, factoryEntry ) ).second;
493 typename FactoryEntries::iterator i =
494 _factoryEntries.find(creatorId);
495 if ( i == _factoryEntries.end() )
497 Creator * creator = i->second.creator;
499 _factoryEntries.erase(i);
520 template<
typename TheFactoryType,
typename ConcreteProductType>
523 typedef typename TheFactoryType::AbstractProduct AbstractProduct;
524 typedef typename TheFactoryType::RegistryKey RegistryKey;
528 CLAM_ASSERT(std::string(metadata[0])==std::string(
"key"),
"FactoryRegistrator: first char* metadata should be 'key'");
529 CLAM_ASSERT(metadata[1],
"FactoryRegistrator: value for first attriute ('key') must not be 0");
530 std::string key = metadata[1];
532 TheFactoryType & factory = TheFactoryType::GetInstance();
534 std::string attribute, value;
535 for(
unsigned i = 2; metadata[i]; i++)
537 attribute = metadata[i];
540 std::cout <<
"[METADATA] error with attribute \"" << attribute <<
"\"" << std::endl;
544 value = metadata[++i];
545 factory.AddAttribute(key, attribute, value);
548 if (libraryFileName!=
"")
549 factory.AddAttribute(key,
"library",libraryFileName);
560 ConcreteProductType dummy;
561 RegistryKey key=dummy.GetClassName();
569 TheFactoryType::GetInstance().AddCreatorWarningRepetitions( key,
new ConcreteCreator() );
591 return new ConcreteProductType();
599 #endif // _Factory_hxx_