SheafSystem  0.0.0.0
poset_handle_factory.cc
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 // Implementation for class poset_handle_factory
19 
20 #include "SheafSystem/poset_handle_factory.h"
21 
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/error_message.h"
24 #include "SheafSystem/namespace_poset.h"
25 #include "SheafSystem/poset.h"
26 #include "SheafSystem/poset_type.h"
27 #include "SheafSystem/primitives_poset.h"
28 #include "SheafSystem/primitives_poset_schema.h"
29 #include "SheafSystem/refinable_poset.h"
30 
31 //#define DIAGNOSTIC_OUTPUT
32 //#undef DIAGNOSTIC_OUTPUT
33 
34 // ===========================================================
35 // POSET_HANDLE_FACTORY FACET
36 // ===========================================================
37 
41 {
42 
43  // Preconditions:
44 
45 
46  // Body:
47 
48  // Initialize the sheaf prototypes map.
49 
50  _sheaf_prototypes_map.reserve(NOT_A_POSET_TYPE);
51  _sheaf_prototypes_map.set_ct(NOT_A_POSET_TYPE);
52  _sheaf_prototypes_map.assign(0);
53 
54  // Prototypes are created in the private make_prototype routines of each
55  // descendant of class poset_state_handle.
56 
57  // Postconditions:
58 
59 
60  // Exit:
61 
62  return;
63 }
64 
65 
69 {
70 
71  // Preconditions:
72 
73 
74  // Body:
75 
76  // Delete all the prototypes.
77 
78  prototypes_map_type::iterator itr = _prototypes_map.begin();
79  while(itr != _prototypes_map.end())
80  {
81  delete itr->second;
82  ++itr;
83  }
84  _prototypes_map.clear();
85 
86  // Don't need to delete the entries in _sheaf_prototypes_map
87  // because they are all duplicates that have just been deleted.
88 
89  // Postconditions:
90 
91  ensure(unexecutable("All prototypes have been deleted."));
92 
93  // Exit:
94 
95  return;
96 }
97 
101 new_poset_handle(const std::string& xclient_class_name,
102  poset_type xsheaf_base_class_id)
103 {
104  poset_state_handle* result = 0;
105 
106  // Preconditions:
107 
108  require(contains_prototype(xclient_class_name) ||
109  contains_prototype(xsheaf_base_class_id));
110 
111  // Body:
112 
113 #ifdef DIAGNOSTIC_OUTPUT
114 
115  cout << SOURCE_CODE_LOCATION
116  << " class name= " << xclient_class_name
117  << " class id= " << xsheaf_base_class_id
118  << endl;
119 #endif
120 
121  prototypes_map_type::iterator itr = _prototypes_map.find(xclient_class_name);
122  if( itr != _prototypes_map.end())
123  {
124  // Found a prototype; clone it.
125 
126  result = itr->second->clone();
127  }
128  else
129  {
130  // No client prototype; clone sheaf base class
131 
132  result = _sheaf_prototypes_map[xsheaf_base_class_id]->clone();
133  }
134 
135 #ifdef DIAGNOSTIC_OUTPUT
136  cout << "result class: " << result->class_name() << endl;
137 #endif
138 
139  // Postconditions:
140 
141  ensure(result != 0);
142  ensure(!result->is_attached());
143 
144  // Can't ensure the following because type_id() requires read access.
145 
146  // ensure(result->class_name() == xclass_name ||
147  // result->type_id() == xsheaf_base_class_id);
148 
149  // Exit:
150 
151  return result;
152 }
153 
155 void
158 {
159  // Preconditions:
160 
161  require(xprototype != 0);
162  require(!xprototype->is_attached());
163 
164  // Body:
165 
166  // If the map already contains a prototype for the
167  // given class name, this insert will do nothing.
168  // For this reason, the postcondition does not ensure
169  // the prototype == xprototype.
170 
171  prototypes_map_type::value_type lval(xprototype->class_name(), const_cast<poset_state_handle*>(xprototype));
172  _prototypes_map.insert(lval);
173 
174  // Postconditions:
175 
176  ensure(contains_prototype(xprototype->class_name()));
177 
178  // Exit:
179 
180  return;
181 }
182 
184 void
186 delete_prototype(const std::string& xclass_name)
187 {
188  // Preconditions:
189 
190  // Body:
191 
192  if(!xclass_name.empty())
193  {
194  prototypes_map_type::iterator itr = _prototypes_map.find(xclass_name);
195  if(itr != _prototypes_map.end())
196  {
197  poset_state_handle* lproto = itr->second;
198  _prototypes_map.erase(itr);
199 
200  assertion(!lproto->is_attached());
201 
202  delete lproto;
203  }
204  }
205 
206  // Postconditions:
207 
208  ensure(!contains_prototype(xclass_name));
209 
210  // Exit:
211 
212  return;
213 }
214 
215 
216 
218 bool
220 contains_prototype(const std::string& xclass_name) const
221 {
222  bool result;
223 
224  // Preconditions:
225 
226  // Body:
227 
228  result =
229  !xclass_name.empty() &&
230  (_prototypes_map.find(xclass_name) != _prototypes_map.end());
231 
232  // Postconditions:
233 
234 
235  // Exit:
236 
237  return result;
238 }
239 
240 
241 
243 bool
246 {
247  bool result;
248 
249  // Preconditions:
250 
251 
252  // Body:
253 
254  result = (_sheaf_prototypes_map[xtype_id] != 0);
255 
256  // Postconditions:
257 
258 
259  // Exit:
260 
261  return result;
262 }
263 
264 // ===========================================================
265 // PRIVATE MEMBER FUNCTIONS
266 // ===========================================================
267 
268 
270 void
272 insert_prototype(poset_type xtype_id, const poset_state_handle* xprototype)
273 {
274  // Preconditions:
275 
276  require(xprototype != 0);
277  require(!xprototype->is_attached());
278 
279  // Body:
280 
281  _sheaf_prototypes_map[xtype_id] = const_cast<poset_state_handle*>(xprototype);
282 
283  // Postconditions:
284 
285  ensure(contains_prototype(xtype_id));
286 
287  // Exit:
288 
289  return;
290 }
291 
292 
293 
294 // ===========================================================
295 // NON-MEMBER FUNCTIONS
296 // ===========================================================
297 
void insert_prototype(const poset_state_handle *xprototype)
Sets xprototype as the prototype for its client class.
virtual ~poset_handle_factory()
Destructor.
poset_type
Identifiers for poset types.
Definition: poset_type.h:41
A client handle for a general, abstract partially order set.
poset_state_handle * new_poset_handle(const std::string &xclient_class_name, poset_type xsheaf_base_class_id)
Creates an unattached handle of type xclient_class_name or type xsheaf_base_class_id if no prototype ...
void delete_prototype(const std::string &xclass_name)
Removes the prototype for handles of type xclass_name.
virtual const char * class_name() const
The name of this class.
virtual bool is_attached() const
True if this is attached to a state.
bool contains_prototype(const std::string &xclass_name) const
True if the set of prototypes contains a prototype for handles of type xclass_name.
virtual poset_state_handle * clone() const
Virtual constructor; creates a new handle of the same actual type as this, attached to the same state...
poset_handle_factory()
Default constructor.