Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
factory.h
Go to the documentation of this file.
1 
14 #ifndef FACTORY_INCLUDED
15 #define FACTORY_INCLUDED
16 
18 
30 #include <string>
31 #include <map>
32 
33 #ifdef DEBUG
34 #include <algorithm>
35 #include "IO/bridgeIO.h"
36 using Bridge::vout;
37 #endif
38 
39 typedef std::string IdentifierType;
40 
41 template<class AbstractProduct, typename ProductCreator>
43 {
44  private:
45  typedef std::map<IdentifierType, ProductCreator> Map;
46 
47 
48  public:
49  static ProductCreator Find(const IdentifierType& subtype)
50  {
51  return Instance().find(subtype);
52  }
53 
54  static bool Register(const IdentifierType& subtype, ProductCreator creator)
55  {
56  return Instance().append(subtype, creator);
57  }
58 
59  ProductCreator find(const IdentifierType& subtype)
60  {
61  typename Map::const_iterator i = m_map.find(subtype);
62  if (i != m_map.end()) {
63  return i->second;
64  } else {
65  fprintf(stderr, "Factory::find: unknown type \"%s\"\n", subtype.c_str());
66 
67  exit(EXIT_FAILURE);
68 
69  return 0;
70  }
71  }
72 
73  bool append(const IdentifierType& subtype, ProductCreator creator)
74  {
75  if ((m_map.find(subtype) == m_map.end()) &&
76  m_map.insert(typename Map::value_type(subtype, creator)).second) {
77  return true;
78  } else {
79  fprintf(stderr, "Factory::append: duplicate key \"%s\"\n", subtype.c_str());
80  return false;
81  }
82  }
83 
85  {
86  if (!s_instance) {
87  // lock
88  if (!s_instance) {
90  }
91  // unlock
92  }
93  return *s_instance;
94  }
95 
96  private:
98 
99  // singleton
103 
104  virtual ~FactoryTemplate() {}
105 
106  static inline void create_instance()
107  {
108  static FactoryTemplate instance;
109 
110  s_instance = &instance;
111  }
112 
114 
115 // for debug
116 #ifdef DEBUG
117  private:
118  void show_entries(const char *tag)
119  {
120  if (tag) {
121  vout.general("%s:\n", tag);
122  }
123  for (typename Map::const_iterator p = m_map.begin(); p != m_map.end(); ++p) {
124  vout.general("\t%s\n", p->first.c_str());
125  }
126  }
127 
128  public:
129  static void print(const char *tag = NULL)
130  {
131  return Instance().show_entries(tag);
132  }
133 #endif
134 };
135 
136 template<class AbstractProduct, typename ProductCreator>
138 #endif
static FactoryTemplate & Instance()
Definition: factory.h:84
BridgeIO vout
Definition: bridgeIO.cpp:503
static bool Register(const IdentifierType &subtype, ProductCreator creator)
Definition: factory.h:54
static void create_instance()
Definition: factory.h:106
void general(const char *format,...)
Definition: bridgeIO.cpp:197
static ProductCreator Find(const IdentifierType &subtype)
Definition: factory.h:49
static FactoryTemplate * s_instance
Definition: factory.h:113
bool append(const IdentifierType &subtype, ProductCreator creator)
Definition: factory.h:73
virtual ~FactoryTemplate()
Definition: factory.h:104
std::map< IdentifierType, ProductCreator > Map
Definition: factory.h:45
ProductCreator find(const IdentifierType &subtype)
Definition: factory.h:59
FactoryTemplate & operator=(const FactoryTemplate &)
FactoryTemplate(const FactoryTemplate &)
Definition: factory.h:101
std::string IdentifierType
Factory template class.
Definition: factory.h:39