SheafSystem  0.0.0.0
dof_map_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 dof_map_factory
19 
20 #include "SheafSystem/dof_map_factory.h"
21 
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/poset_dof_map.h"
24 
25 using namespace std;
26 
27 //#define DIAGNOSTIC_OUTPUT
28 //#undef DIAGNOSTIC_OUTPUT
29 
30 // ===========================================================
31 // DOF_MAP_FACTORY FACET
32 // ===========================================================
33 
37 {
38 
39  // Preconditions:
40 
41 
42  // Body:
43 
44  // Delete all the prototypes.
45 
46  prototypes_map_type::iterator itr = _prototypes_map.begin();
47  while(itr != _prototypes_map.end())
48  {
49  delete itr->second;
50  ++itr;
51  }
52  _prototypes_map.clear();
53 
54  // Don't need to delete the entries in _sheaf_prototypes_map
55  // because they are all duplicates that have just been deleted.
56 
57  // Postconditions:
58 
59  ensure(unexecutable("All prototypes have been deleted."));
60 
61  // Exit:
62 
63  return;
64 }
65 
69 new_dof_map(const std::string& xclient_class_name, dof_tuple_type xsheaf_base_class_id)
70 {
71  poset_dof_map* result = 0;
72 
73  // Preconditions:
74 
75  require(contains_prototype(xclient_class_name) ||
76  contains_prototype(xsheaf_base_class_id));
77 
78  // Body:
79 
80  if(!xclient_class_name.empty())
81  {
82  // Class name given.
83 
84  prototypes_map_type::iterator itr = _prototypes_map.find(xclient_class_name);
85  if( itr != _prototypes_map.end())
86  {
87  // Found a prototype; clone it.
88 
89  result = itr->second->clone();
90  }
91  else
92  {
93  // No client prototype; clone sheaf base class
94 
95  result = _sheaf_prototypes_map[xsheaf_base_class_id]->clone();
96  }
97  }
98  else
99  {
100  // Class name not given; clone sheaf base class
101 
102  result = _sheaf_prototypes_map[xsheaf_base_class_id]->clone();
103  }
104 
105  // Postconditions:
106 
107  ensure(result != 0);
108  ensure(!result->is_initialized());
109 
110  // Exit:
111 
112  return result;
113 }
114 
116 void
118 insert_prototype(const poset_dof_map* xprototype)
119 {
120  // Preconditions:
121 
122  require(xprototype != 0);
123  require(!xprototype->is_initialized());
124 
125  // Body:
126 
127  // If the map already contains a prototype for the
128  // given class name, this insert will do nothing.
129  // For this reason, the postcondition does not ensure
130  // the prototype == xprototype.
131 
132  string lclass_name = xprototype->class_name();
133 
134 #ifdef DIAGNOSTIC_OUTPUT
135 
136  cout << "in dof_map_factory::insert_prototype lclass_name = "
137  << lclass_name
138  << endl;
139 #endif
140 
141  prototypes_map_type::value_type lval(lclass_name, const_cast<poset_dof_map*>(xprototype));
142  _prototypes_map.insert(lval);
143 
144  // Postconditions:
145 
146  ensure(contains_prototype(xprototype->class_name()));
147 
148  // Exit:
149 
150  return;
151 }
152 
154 void
156 insert_prototype(dof_tuple_type xtype_id, const poset_dof_map* xprototype)
157 {
158  // Preconditions:
159 
160  require(xprototype != 0);
161  require(!xprototype->is_initialized());
162 
163  // Body:
164 
165  _sheaf_prototypes_map[xtype_id] = const_cast<poset_dof_map*>(xprototype);
166 
167  // Postconditions:
168 
169  ensure(contains_prototype(xtype_id));
170 
171  // Exit:
172 
173  return;
174 }
175 
177 void
179 delete_prototype(const std::string& xclass_name)
180 {
181  // Preconditions:
182 
183  // Body:
184 
185  if(!xclass_name.empty())
186  {
187  prototypes_map_type::iterator itr = _prototypes_map.find(xclass_name);
188  if(itr != _prototypes_map.end())
189  {
190  poset_dof_map* lproto = itr->second;
191  _prototypes_map.erase(itr);
192 
193  assertion(!lproto->is_initialized());
194 
195  delete lproto;
196  }
197  }
198 
199  // Postconditions:
200 
201  ensure(!contains_prototype(xclass_name));
202 
203  // Exit:
204 
205  return;
206 }
207 
209 bool
211 contains_prototype(const std::string& xclass_name) const
212 {
213  bool result;
214 
215  // Preconditions:
216 
217  // Body:
218 
219  result =
220  !xclass_name.empty() &&
221  (_prototypes_map.find(xclass_name) != _prototypes_map.end());
222 
223  // Postconditions:
224 
225 
226  // Exit:
227 
228  return result;
229 }
230 
232 bool
235 {
236  bool result;
237 
238  // Preconditions:
239 
240 
241  // Body:
242 
243  result = (_sheaf_prototypes_map[xtype_id] != 0);
244 
245  // Postconditions:
246 
247 
248  // Exit:
249 
250  return result;
251 }
252 
253 // ===========================================================
254 // PRIVATE MEMBER FUNCTIONS
255 // ===========================================================
256 
257 
259 sheaf::dof_map_factory::
260 dof_map_factory()
261 {
262 
263  // Preconditions:
264 
265 
266  // Body:
267 
268  // Initialize the sheaf prototypes map.
269 
270  _sheaf_prototypes_map.reserve(NOT_A_DOF_TUPLE_TYPE);
271  _sheaf_prototypes_map.set_ct(NOT_A_DOF_TUPLE_TYPE);
272  _sheaf_prototypes_map.assign(0);
273 
274  // Prototypes are created in the private make_prototype routines of each
275  // descendant of class poset_dof_map..
276 
277  // Postconditions:
278 
279 
280  // Exit:
281 
282  return;
283 }
284 
285 
286 // ===========================================================
287 // NON-MEMBER FUNCTIONS
288 // ===========================================================
289 
bool contains_prototype(const std::string &xclass_name) const
True if the set of prototypes contains a prototype for handles of type xclass_name.
bool is_initialized() const
True if this has been initialized, that is, if the schema has been set and the dof map storage alloca...
STL namespace.
The general, abstract map from dof ids to dof values.
Definition: poset_dof_map.h:59
virtual ~dof_map_factory()
Destructor.
poset_dof_map * new_dof_map(const std::string &xclient_class_name, dof_tuple_type xsheaf_base_class_id)
Creates an uninitialized dof map of type xclient_class_name or type xsheaf_base_class_id if no protot...
dof_tuple_type
Identifiers for dof tuple types.
void delete_prototype(const std::string &xclass_name)
Removes the prototype for handles of type xclass_name.
void insert_prototype(const poset_dof_map *xprototype)
Sets xprototype as the prototype for its client class.
virtual const std::string & class_name() const
The name of the actual (possibly derived) class of this instance.
virtual poset_dof_map * clone() const =0
Virtual default constructor.