SheafSystem  0.0.0.0
factory_2.impl.h
Go to the documentation of this file.
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 
20 
21 #ifndef FACTORY_2_IMPL_H
22 #define FACTORY_2_IMPL_H
23 
24 #ifndef SHEAF_DLL_SPEC_H
25 #include "SheafSystem/sheaf_dll_spec.h"
26 #endif
27 
28 #ifndef FACTORY_2_H
29 #include "SheafSystem/factory_2.h"
30 #endif
31 
32 #ifndef ARG_LIST_H
33 #include "SheafSystem/arg_list.h"
34 #endif
35 
36 #ifndef ASSERT_CONTRACT_H
37 #include "SheafSystem/assert_contract.h"
38 #endif
39 
40 #ifndef NAMESPACE_POSET_H
41 #include "SheafSystem/namespace_poset.h"
42 #endif
43 
44 #ifndef RC_PTR_H
45 #include "SheafSystem/rc_ptr.h"
46 #endif
47 
48 namespace sheaf
49 {
50 
51 // ===========================================================
52 // FACTORY_2 FACET
53 // ===========================================================
54 
55 // PUBLIC MEMBER FUNCTIONS
56 
57 template <typename T, typename R>
60 {
61  // Preconditions:
62 
63  // Body:
64 
65  // Exit:
66 
67  return;
68 };
69 
70 template <typename T, typename R>
73 {
74  // Preconditions:
75 
76  // Body:
77 
78  // Delete all the prototypes.
79 
80  typename prototypes_map_type::iterator itr = _prototypes_map.begin();
81  while(itr != _prototypes_map.end())
82  {
83  delete itr->second;
84  ++itr;
85  }
86  _prototypes_map.clear();
87 
88  // Postconditions:
89 
90  ensure(unexecutable("All prototypes have been deleted."));
91 
92  // Exit:
93 
94  return;
95 };
96 
97 // template <typename T, typename R>
98 // R
99 // factory_2<T, R>::
100 // new_instance(const string& xclient_class_name)
101 // {
102 // // Preconditions:
103 
104 // require(contains_prototype(xclient_class_name));
105 
106 // // Body:
107 
108 // R result = _prototypes_map.find(xclient_class_name)->second->clone();
109 
110 // // Postconditions:
111 
112 // ensure(result != 0);
113 // ensure(unexecutable("result is default constructed"));
114 
115 // // Exit:
116 
117 // return result;
118 // };
119 
120 template <typename T, typename R>
121 R
123 new_instance(const string& xclient_class_name, const arg_list& xargs)
124 {
125  // Preconditions:
126 
127  require(contains_prototype(xclient_class_name));
128 
129  // Body:
130 
131  R result = _prototypes_map.find(xclient_class_name)->second->clone(xargs);
132 
133  // Postconditions:
134 
135  ensure(result != 0);
136  ensure(unexecutable("result is constructed with args, xargs"));
137 
138  // Exit:
139 
140  return result;
141 };
142 
143 template <typename T, typename R>
144 void
146 insert_prototype(T* xprototype)
147 {
148  // Preconditions:
149 
150  require(xprototype != 0);
151 
152  // Body:
153 
154  // If the map already contains a prototype for the
155  // given class name, this insert will do nothing.
156  // For this reason, the postcondition does not ensure
157  // the prototype == xprototype.
158 
159  // string lclass_name = typeid(*xprototype).name();
160  string lclass_name = xprototype->class_name();
161 
162 #ifdef DIAGNOSTIC_OUTPUT
163 
164  cout << "in factory_2<T, R>::insert_prototype lclass_name = "
165  << lclass_name
166  << endl;
167 #endif
168 
169  typename prototypes_map_type::value_type lval(lclass_name, xprototype);
170  _prototypes_map.insert(lval);
171 
172  // Postconditions:
173 
174  ensure(contains_prototype(xprototype->class_name()));
175 
176  // Exit:
177 
178  return;
179 };
180 
181 template <typename T, typename R>
182 void
184 delete_prototype(const string& xclass_name)
185 {
186  // Preconditions:
187 
188  // Body:
189 
190  if(!xclass_name.empty())
191  {
192  typename prototypes_map_type::iterator itr = _prototypes_map.find(xclass_name);
193  if(itr != _prototypes_map.end())
194  {
195  T* lproto = itr->second;
196  _prototypes_map.erase(itr);
197 
198  delete lproto;
199  }
200  }
201 
202  // Postconditions:
203 
204  ensure(!contains_prototype(xclass_name));
205 
206  // Exit:
207 
208  return;
209 };
210 
211 template <typename T, typename R>
212 bool
214 contains_prototype(const string& xclass_name) const
215 {
216  bool result;
217 
218  // Preconditions:
219 
220  // Body:
221 
222  result =
223  !xclass_name.empty() &&
224  (_prototypes_map.find(xclass_name) != _prototypes_map.end());
225 
226  // Postconditions:
227 
228 
229  // Exit:
230 
231  return result;
232 };
233 
234 // PROTECTED MEMBER FUNCTIONS
235 
236 // PRIVATE MEMBER FUNCTIONS
237 
238 template <typename T, typename R>
240 factory_2(const factory_2& xother) {};
241 
242 // ===========================================================
243 // NON-MEMBER FUNCTIONS
244 // ===========================================================
245 
246 template <typename T, typename R>
247 ostream& operator << (ostream& xos, const factory_2<T, R>& xf)
248 {
249  // Preconditions:
250 
251  // Body:
252 
254 
255  for(litr = xf._prototypes_map.begin(); litr != xf._prototypes_map.end(); ++litr)
256  {
257  xos << " class: " << litr->first << endl;
258  }
259 
260  // Postconditions:
261 
262  // Exit:
263 
264  return xos;
265 };
266 
267 } // namespace sheaf
268 
269 #endif // ifndef FACTORY_2_IMPL_H
R new_instance(const string &xclient_class_name, const arg_list &xargs)
Creates an instance of type xclient_class_name with arguments xargs.
A whitespace separated list of arguments. Insertion operaters are used to insert arguments into the l...
Definition: arg_list.h:63
factory_2()
Default constructor.
void insert_prototype(T *xprototype)
Sets xprototype as the prototype for its client class.
A factory for instanting descendants of an abstract type T, given the class name of the descendant...
Definition: factory_2.h:52
bool contains_prototype(const string &xclass_name) const
True if the set of prototypes contains a prototype for handles of type xclass_name.
void delete_prototype(const string &xclass_name)
Removes the prototype for handles of type xclass_name.
Namespace for the sheaves component of the sheaf system.
virtual ~factory_2()
Destructor.