SheafSystem  0.0.0.0
factory.impl.h
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 // Interface for class factory
19 
20 #ifndef FACTORY_IMPL_H
21 #define FACTORY_IMPL_H
22 
23 #ifndef SHEAF_DLL_SPEC_H
24 #include "SheafSystem/sheaf_dll_spec.h"
25 #endif
26 
27 #ifndef FACTORY_H
28 #include "SheafSystem/factory.h"
29 #endif
30 
31 #ifndef ASSERT_CONTRACT_H
32 #include "SheafSystem/assert_contract.h"
33 #endif
34 
35 #ifndef NAMESPACE_POSET_H
36 #include "SheafSystem/namespace_poset.h"
37 #endif
38 
39 namespace sheaf
40 {
41 
42 // ===========================================================
43 // FACTORY FACET
44 // ===========================================================
45 
46 template <typename T>
49 {
50  // Preconditions:
51 
52  // Body:
53 
54  // Exit:
55 
56  return;
57 };
58 
59 template <typename T>
62 {
63 
64  // Preconditions:
65 
66 
67  // Body:
68 
69  // Delete all the prototypes.
70 
71  typename prototypes_map_type::iterator itr = _prototypes_map.begin();
72  while(itr != _prototypes_map.end())
73  {
74  delete itr->second;
75  ++itr;
76  }
77  _prototypes_map.clear();
78 
79  // Postconditions:
80 
81  ensure(unexecutable("All prototypes have been deleted."));
82 
83  // Exit:
84 
85  return;
86 };
87 
88 template <typename T>
89 T*
91 new_instance(const std::string& xclient_class_name)
92 {
93  T* result = 0;
94 
95  // Preconditions:
96 
97  require(contains_prototype(xclient_class_name));
98 
99  // Body:
100 
101  result = _prototypes_map.find(xclient_class_name)->second->clone();
102 
103  // Postconditions:
104 
105  ensure(result != 0);
106  ensure(unexecutable("result is default constructed"));
107 
108  // Exit:
109 
110  return result;
111 };
112 
113 template <typename T>
114 template <typename S>
115 T*
117 new_instance(const std::string& xclient_class_name, S& xarg)
118 {
119  T* result = 0;
120 
121  // Preconditions:
122 
123  require(contains_prototype(xclient_class_name));
124 
125  // Body:
126 
127  result = new_instance(xclient_class_name);
128  result->initialize(xarg);
129 
130  // Postconditions:
131 
132  ensure(result != 0);
133  ensure(result->is_initialized());
134 
135  // Exit:
136 
137  return result;
138 };
139 
140 
141 template <typename T>
142 void
144 insert_prototype(T* xprototype)
145 {
146  // Preconditions:
147 
148  require(xprototype != 0);
149 
150  // Body:
151 
152  // If the map already contains a prototype for the
153  // given class name, this insert will do nothing.
154  // For this reason, the postcondition does not ensure
155  // the prototype == xprototype.
156 
157  // string lclass_name = typeid(*xprototype).name();
158  std::string lclass_name = xprototype->class_name();
159 
160 #ifdef DIAGNOSTIC_OUTPUT
161 
162  cout << "in factory<T>::insert_prototype lclass_name = "
163  << lclass_name
164  << endl;
165 #endif
166 
167  typename prototypes_map_type::value_type lval(lclass_name, xprototype);
168  _prototypes_map.insert(lval);
169 
170  // Postconditions:
171 
172  ensure(contains_prototype(xprototype->class_name()));
173 
174  // Exit:
175 
176  return;
177 };
178 
179 template <typename T>
180 void
182 delete_prototype(const std::string& xclass_name)
183 {
184  // Preconditions:
185 
186  // Body:
187 
188  if(!xclass_name.empty())
189  {
190  typename prototypes_map_type::iterator itr = _prototypes_map.find(xclass_name);
191  if(itr != _prototypes_map.end())
192  {
193  T* lproto = itr->second;
194  _prototypes_map.erase(itr);
195 
196  delete lproto;
197  }
198  }
199 
200  // Postconditions:
201 
202  ensure(!contains_prototype(xclass_name));
203 
204  // Exit:
205 
206  return;
207 };
208 
209 template <typename T>
210 bool
212 contains_prototype(const std::string& xclass_name) const
213 {
214  bool result;
215 
216  // Preconditions:
217 
218  // Body:
219 
220  result =
221  !xclass_name.empty() &&
222  (_prototypes_map.find(xclass_name) != _prototypes_map.end());
223 
224  // Postconditions:
225 
226 
227  // Exit:
228 
229  return result;
230 };
231 
232 // =============================================================================
233 // PRIVATE MEMBER FUNCTIONS
234 // =============================================================================
235 
236 template <typename T>
238 factory(const factory& xother)
239 {}
240 ;
241 
242 // ===========================================================
243 // NON-MEMBER FUNCTIONS
244 // ===========================================================
245 
246 template <typename T>
247 std::ostream& operator << (std::ostream& xos, const factory<T>& xf)
248 {
249  // Preconditions:
250 
251 
252  // Body:
253 
255 
256  for(litr = xf._prototypes_map.begin(); litr != xf._prototypes_map.end(); ++litr)
257  {
258  xos << " class: " << litr->first << std::endl;
259  }
260 
261  // Postconditions:
262 
263 
264  // Exit:
265 
266  return xos;
267 };
268 
269 } // namespace sheaf
270 
271 #endif // ifndef FACTORY_IMPL_H
bool contains_prototype(const std::string &xclass_name) const
True if the set of prototypes contains a prototype for handles of type xclass_name.
Definition: factory.impl.h:212
A factory for instanting descendants of an abstract type T, given the class name of the descendant...
Definition: eval_family.h:49
T * new_instance(const std::string &xclient_class_name)
Creates an instance of type xclient_class_name.
Definition: factory.impl.h:91
Namespace for the sheaves component of the sheaf system.
virtual ~factory()
Destructor.
Definition: factory.impl.h:61
factory()
Default constructor.
Definition: factory.impl.h:48
void insert_prototype(T *xprototype)
Sets xprototype as the prototype for its client class.
Definition: factory.impl.h:144
void delete_prototype(const std::string &xclass_name)
Removes the prototype for handles of type xclass_name.
Definition: factory.impl.h:182