+++ /dev/null
-// Macro Guard\r
-#ifndef CSE473_FACTORY_H\r
-#define CSE473_FACTORY_H\r
-\r
-// Files included\r
-#include "Assertion.h"\r
-#include "CommonErrors.h"\r
-#include "Exceptions.h"\r
-#include "StandardFiles.h"\r
-\r
-\r
-//=============================================================\r
-// Factory design idea "borrowed" from 'Modern C++ Design'\r
-// by Andrei Alexandrescu and modified slightly from his \r
-// implementation in the Loki Library.\r
-//=============================================================\r
-\r
-/*============================================================================//\r
- Factory.h: Generic abstract factory \r
-\r
- Implementation: Shane J. Neph, June 2004, University of Washington\r
-//============================================================================*/\r
-\r
-\r
-template\r
- <\r
- typename AbstractProduct,\r
- typename IdentifierType,\r
- typename ProductCreator = AbstractProduct* (*)()\r
- >\r
-struct Factory {\r
- typedef IdentifierType IDType;\r
-\r
- bool IsRegistered(const IdentifierType& id) {\r
- return(associations_.find(id) != associations_.end());\r
- }\r
-\r
- bool Register(const IdentifierType& id, ProductCreator creator) {\r
- return(associations_.insert(typename AssocMap::value_type(id, creator)).second);\r
- }\r
-\r
- bool Unregister(const IdentifierType& id) {\r
- return(associations_.erase(id) == 1);\r
- }\r
-\r
- std::set<IdentifierType> GetIDs() const {\r
- std::set<IdentifierType> toRtn;\r
- typename AssocMap::const_iterator i = associations_.begin();\r
- while ( i != associations_.end() ) {\r
- toRtn.insert(i->first);\r
- ++i;\r
- }\r
- return(toRtn);\r
- }\r
-\r
- AbstractProduct* CreateObject(const IdentifierType& id) {\r
- typename AssocMap::const_iterator i = associations_.find(id);\r
- Assert<CE::BadArg>(i != associations_.end(), "Factory Class");\r
- return((i->second)()); // Create an instance and return\r
- }\r
-private:\r
- typedef std::map<IdentifierType, ProductCreator> AssocMap;\r
- AssocMap associations_;\r
-};\r
-\r
-#endif // CSE473_FACTORY_H\r