Factory.h
Go to the documentation of this file.
1 /* -*- mode: c++ -*- */
2 
14 #ifndef _Factory_H_
15 #define _Factory_H_
16 
17 #include "FactoryException.h"
18 
19 #ifdef _MSC_VER
20 #include "msdevstudio/MSconfig.h"
21 #endif
22 
23 #include <map>
24 #include <vector>
25 
26 namespace hippodraw {
27 
43 template < class Type > class Factory
44 {
45 private:
46 
48  Factory ( const Factory< Type > & );
49 
50 protected:
51 
53  std::map < std::string, Type * > m_types;
54 
58  mutable std::vector< std::string > m_names;
59 
62  Factory ( );
63 
66  virtual ~Factory();
67 
68  public:
69 
71  void add ( Type * );
72 
75  void remove ( const std::string & name );
76 
80  bool exists ( const std::string & name ) const;
81 
85  Type * prototype ( const std::string & name ) const;
86 
91  Type * create ( const std::string & name );
92 
94  const std::vector< std::string > & names () const;
95 
96 };
97 
98 template < class Type >
100 {
101 }
102 
103 template< class Type >
105 {
106  typename std::map < std::string, Type * > ::iterator first
107  = m_types.begin();
108  for ( ; first != m_types.end(); ++first ) {
109  delete first->second;
110  }
111 
112  m_types.clear ();
113 }
114 
115 template< class Type >
116 void Factory<Type>::add ( Type * obj )
117 {
118  const std::string & name = obj->name ();
119  m_types[name] = obj;
120 }
121 
122 template < class Type >
123 void
125 remove ( const std::string & name )
126 {
127  typename std::map < std::string, Type * > ::iterator it
128  = m_types.find ( name );
129  if ( it != m_types.end () ) {
130  m_types.erase ( it );
131  }
132 }
133 
134 template < class Type >
135 bool Factory < Type>::
136 exists ( const std::string & name ) const
137 {
138  // Don't use map::operator[]() to find the name, as it will create
139  // one if it doesn't exist.
140  typename std::map< std::string, Type * >::const_iterator it
141  = m_types.find ( name );
142 
143  return it != m_types.end ();
144 }
145 
146 
147 template< class Type >
148 Type * Factory<Type>::prototype ( const std::string & name ) const
149 {
150  // Don't use map::operator[]() to find the name, as it will create
151  // one if it doesn't exist.
152  typename std::map< std::string, Type * >::const_iterator it
153  = m_types.find ( name );
154  if ( it == m_types.end () ) throw FactoryException ( name );
155 
156  return it->second;
157 }
158 
159 template< class Type >
160 Type * Factory<Type>::create ( const std::string & name )
161 {
162  Type * obj = prototype ( name );
163  if ( obj == 0 ) {
164  return 0;
165  }
166  return obj->clone ();
167 }
168 
169 template< class Type >
170 const std::vector< std::string > & Factory<Type>::names () const
171 {
172  m_names.clear ();
173  typename std::map <std::string, Type *>::const_iterator it
174  = m_types.begin ();
175  for ( ; it != m_types.end (); ++it ) {
176  m_names.push_back ( it->first );
177  }
178 
179  return m_names;
180 }
181 
182 } // namespace hippodraw
183 
184 #endif // _Factory_H_

Generated for HippoDraw Class Library by doxygen