SheafSystem  0.0.0.0
namespace_poset_dof_map.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 namespace_poset_dof_map
19 
20 #include "SheafSystem/namespace_poset_dof_map.h"
21 
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/error_message.h"
24 #include "SheafSystem/dof_map_factory.h"
25 #include "SheafSystem/index_space_iterator.h"
26 #include "SheafSystem/namespace_poset.h"
27 #include "SheafSystem/namespace_poset_schema.h"
28 #include "SheafSystem/poset_type.h"
29 #include "SheafSystem/primitive_type.h"
30 #include "SheafSystem/std_string.h"
31 
32 using namespace std;
33 
34 // PUBLIC MEMBER FUNCTIONS
35 
36 
37 
38 const std::string&
40 class_name() const
41 {
42  // Preconditions:
43 
44  // Body:
45 
46  const string& result = static_class_name();
47 
48  // Postconditions:
49 
50  ensure(!result.empty());
51 
52  // Exit:
53 
54  return result;
55 }
56 
57 const std::string&
60 {
61  // Preconditions:
62 
63  // Body:
64 
65  static const string result("namespace_poset_dof_map");
66 
67  // Postconditions:
68 
69  ensure(!result.empty());
70  ensure(result == "namespace_poset_dof_map");
71 
72  // Exit:
73 
74  return result;
75 }
76 
77 // CANONICAL MEMBERS
78 
81  : poset_dof_map()
82 {
83 
84  // Preconditions:
85 
86 
87  // Body:
88 
90  _dof_tuple_ub = sizeof(_dofs);
91  _dofs.pointer = 0;
92  _dofs.type_id = NOT_A_POSET_TYPE;
93  _dofs.class_name = 0;
94 
95  for(int i=0; i<PREREQ_IDS_UB; ++i)
96  {
97  _dofs.prereq_ids[i] = sheaf::invalid_pod_index();
98  }
99 
100  _storage_map.reserve(NAMESPACE_SCHEMA_DOF_CT);
101  for(pod_index_type i = 0; i < NAMESPACE_SCHEMA_DOF_CT; ++i)
102  {
103  _storage_map[i] = i;
104  }
105 
106  // Postconditions:
107 
108  ensure(!is_initialized());
109  ensure(host() == 0);
110 
111  ensure(!index().is_valid());
112 
113  ensure(is_table_dof_map() == false);
114  ensure(dof_ct() == NAMESPACE_SCHEMA_DOF_CT);
115  ensure(ref_ct() == 0);
116  //ensure(client_id_space() != 0);
117  ensure(poset_pointer() == 0);
118  ensure(poset_type_id() == NOT_A_POSET_TYPE);
119  ensure(poset_class() == 0);
120 
121  ensure_for_all(i, 1, PREREQ_IDS_UB, !(is_valid(poset_prereq_id(i))));
122 }
123 
124 
127 clone() const
128 {
129  namespace_poset_dof_map* result;
130 
131  // Preconditions:
132 
133 
134  // Body:
135 
136  result = new namespace_poset_dof_map();
137 
138  // Postconditions:
139 
140  ensure(result != 0);
141  ensure(result->is_same_type(this));
142  ensure(postcondition_of(namespace_poset_dof_map()));
143 
144  // Exit:
145 
146  return result;
147 }
148 
149 
150 
153  :poset_dof_map(xother)
154 {
155  // Preconditions:
156 
157  // Body:
158 
159  _dofs = xother._dofs;
160  _storage_map = xother._storage_map;
161 
162  // Postconditions:
163 
165 
166 }
167 
170 copy() const
171 {
172  namespace_poset_dof_map* result;
173 
174  // Preconditions:
175 
176 
177  // Body:
178 
179  result = new namespace_poset_dof_map(*this);
180 
181  // Postconditions:
182 
183  ensure(result != 0);
184  ensure(result->is_same_type(this));
185  ensure(postcondition_of(namespace_poset_dof_map(*this)));
186 
187  // Exit:
188 
189  return result;
190 }
191 
192 
196 {
197  // Preconditions:
198 
199  require(xother.is_initialized());
200 
201  // Body:
202 
203  poset_dof_map::operator=(xother);
204 
205  _dofs = xother._dofs;
206 
207  // Postconditions:
208 
209  // Exit
210 
211  return *this;
212 }
213 
214 
217 {
218  // Preconditions:
219 
220  // Body:
221 
222  // Postconditions:
223 
224 }
225 
226 
227 bool
229 invariant() const
230 {
231  bool result = true;
232 
233  // Preconditions:
234 
235  // Body:
236 
237  result = result && poset_dof_map::invariant();
238 
239  if(invariant_check())
240  {
242 
243  // The following is not true when reading the restricted tuples from a file.
244  // result = result && (dof_ct() == namespace_poset_schema::DOF_CT);
245 
247  }
248 
249  // Postconditions:
250 
251  // Exit
252 
253  return result;
254 }
255 
256 
257 // OTHER CONSTRUCTORS
258 
261  : poset_dof_map(xhost, false)
262 {
263 
264  // Preconditions:
265 
266  // Body:
267 
268  // Namespace_poset_dof_map features:
269 
270  _dofs.pointer = 0;
271  _dofs.type_id = NOT_A_POSET_TYPE;
272  _dofs.class_name = 0;
273 
274  for(int i=0; i<PREREQ_IDS_UB; ++i)
275  {
276  _dofs.prereq_ids[i] = sheaf::invalid_pod_index();
277  }
278 
279  _storage_map.reserve(NAMESPACE_SCHEMA_DOF_CT);
280  for(pod_index_type i = 0; i < NAMESPACE_SCHEMA_DOF_CT; ++i)
281  {
282  _storage_map[i] = i;
283  }
284 
285  // Postconditions:
286 
287  ensure(host() == xhost);
288 
289  ensure(!index().is_valid());
290 
291  ensure(dof_ct() == NAMESPACE_SCHEMA_DOF_CT);
292  ensure(ref_ct() == 0);
293  ensure(client_id_space().is_attached());
294  ensure(poset_pointer() == 0);
295  ensure(poset_type_id() == NOT_A_POSET_TYPE);
296  ensure(poset_class() == 0);
297 
298  ensure_for_all(i, 1, PREREQ_IDS_UB, !(is_valid(poset_prereq_id(i))));
299 
300  // Exit:
301 
302  return;
303 }
304 
305 // MAP INTERFACE
306 
307 // ===========================================================
308 // NEW DOF ACCESS FACET
309 // ===========================================================
310 
311 void
313 get_dof(pod_index_type xdof_id, void* xdof, size_type xdof_size) const
314 {
315  // Preconditions:
316 
317  require(schema().state_is_read_accessible());
318  require(schema().dof_id_space(is_table_dof_map()).contains(xdof_id));
319  require(unexecutable("xdof points to buffer of size xdof_size"));
320  require(unexecutable("xdof must be aligned for the given data type"));
321  require(xdof_size >= schema().size(xdof_id, is_table_dof_map()));
322 
323  // Body:
324 
325  // Map from the schema id to the storage id.
326 
327  pod_index_type lstorage_id = _storage_map[xdof_id];
328 
329  switch (lstorage_id)
330  {
331  case 0: // pointer dof is primitive type void*
332  *reinterpret_cast<poset_state_handle**>(xdof) = _dofs.pointer;
333  break;
334  case 1: // type_id dof is primitive type int
335  *reinterpret_cast<int*>(xdof) = _dofs.type_id;
336  break;
337  case 2: // class dof is primitive type char*
338  *reinterpret_cast<const char**>(xdof) = _dofs.class_name;
339  break;
340  case 3: // prereq_ids[0] is primitive type int
341  *reinterpret_cast<int*>(xdof) = _dofs.prereq_ids[0];
342  break;
343  case 4: // prereq_ids[1] is primitive type int
344  *reinterpret_cast<int*>(xdof) = _dofs.prereq_ids[1];
345  break;
346  case 5: // prereq_ids[2] is primitive type int
347  *reinterpret_cast<int*>(xdof) = _dofs.prereq_ids[2];
348  break;
349  case 6: // prereq_ids[3] is primitive type int
350  *reinterpret_cast<int*>(xdof) = _dofs.prereq_ids[3];
351  break;
352  case 7: // prereq_ids[4] is primitive type int
353  *reinterpret_cast<int*>(xdof) = _dofs.prereq_ids[4];
354  break;
355  default:
356  post_fatal_error_message("invalid dof id in call to namespace_poset_dof_map::get_dof");
357  }
358 
359  // Postconditions:
360 
361  // Exit
362 
363  return;
364 }
365 
366 void
368 put_dof(pod_index_type xdof_id, const void* xdof, size_type xdof_size)
369 {
370  // Preconditions:
371 
372  require(schema().state_is_read_accessible());
373  require(schema().dof_id_space(is_table_dof_map()).contains(xdof_id));
374  require(unexecutable("xdof points to buffer of size xdof_size"));
375  require(unexecutable("xdof must be aligned for the given data type"));
376  require(xdof_size >= schema().size(xdof_id, is_table_dof_map()));
377 
378  // Body:
379 
380  // Map from the schema id to the storage id.
381 
382  pod_index_type lstorage_id = _storage_map[xdof_id];
383 
386 
387  switch (lstorage_id)
388  {
389  case 0: // pointer dof is primitive type void*
390  _dofs.pointer = *reinterpret_cast<poset_state_handle**>(const_cast<void*>(xdof));
391  break;
392  case 1: // type_id dof is primitive type int
393  _dofs.type_id = *reinterpret_cast<int*>(const_cast<void*>(xdof));
394  break;
395  case 2: // class dof is primitive type char*
396  _dofs.class_name = *reinterpret_cast<char**>(const_cast<void*>(xdof));
397  break;
398  case 3: // prereq_ids[0] is primitive type int
399  _dofs.prereq_ids[0] = *reinterpret_cast<int*>(const_cast<void*>(xdof));
400  break;
401  case 4: // prereq_ids[1] is primitive type int
402  _dofs.prereq_ids[1] = *reinterpret_cast<int*>(const_cast<void*>(xdof));
403  break;
404  case 5: // prereq_ids[2] is primitive type int
405  _dofs.prereq_ids[2] = *reinterpret_cast<int*>(const_cast<void*>(xdof));
406  break;
407  case 6: // prereq_ids[3] is primitive type int
408  _dofs.prereq_ids[3] = *reinterpret_cast<int*>(const_cast<void*>(xdof));
409  break;
410  case 7: // prereq_ids[4] is primitive type int
411  _dofs.prereq_ids[4] = *reinterpret_cast<int*>(const_cast<void*>(xdof));
412  break;
413  default:
414  post_fatal_error_message("invalid dof id in call to namespace_poset_dof_map::put_dof");
415  }
416 
417  // Postconditions:
418 
419  // Exit
420 
421  return;
422 }
423 
424 // ===========================================================
425 // END NEW DOF ACCESS FACET
426 // ===========================================================
427 
428 void*
431 {
432  void* result;
433 
434  // Preconditions:
435 
436  // Body:
437 
438  result = &_dofs;
439 
440  // Postconditions:
441 
442  ensure(invariant());
443 
444  // Exit
445 
446  return result;
447 }
448 
449 const void*
451 dof_tuple() const
452 {
453  const void* result;
454 
455  // Preconditions:
456 
457  // Body:
458 
459  result = &_dofs;
460 
461  // Postconditions:
462 
463  ensure(invariant());
464 
465  // Exit
466 
467  return result;
468 }
469 
470 void
472 get_dof_tuple(void* xbuf, size_t xbuflen) const
473 {
474  // Preconditions:
475 
476  require(xbuf != 0);
477  require(unexecutable(xbuf points to buffer of size xbuflen));
478  require(xbuflen >= sizeof(void*));
479 
480  // Body:
481 
482  *reinterpret_cast<dof_struct*>(xbuf) = _dofs;
483 
484  // Postconditions:
485 
486  ensure(unexecutable(dof tuple copied to xbuf));
487 
488  // Exit
489 
490  return;
491 }
492 
493 
494 void
496 put_dof_tuple(const void* xbuf, size_t xbuflen)
497 {
498  // Preconditions:
499 
500  require(xbuf != 0);
501  require(unexecutable(xbuf points to buffer of size xbuflen));
502  require(xbuflen >= sizeof(void*));
503 
504  // Body:
505 
506  _dofs = *reinterpret_cast<const dof_struct*>(xbuf);
507 
508  // Postconditions:
509 
510  ensure(unexecutable(xbuf copied to dof tuple));
511 
512  // Exit
513 
514  return;
515 }
516 
517 
521 {
522  poset_state_handle* result;
523 
524  // Preconditions:
525 
526  // Body:
527 
528  result = _dofs.pointer;
529 
530  // Postconditions:
531 
532  // Exit
533 
534  return result;
535 }
536 
537 
541 {
542  poset_type result;
543 
544  // Preconditions:
545 
546  // Body:
547 
548  result = sheaf::poset_type_id(_dofs.type_id);
549 
550  // Postconditions:
551 
552  // Exit
553 
554  return result;
555 }
556 
557 
558 const char*
560 poset_class() const
561 {
562  const char* result;
563 
564  // Preconditions:
565 
566  // Body:
567 
568  result = _dofs.class_name;
569 
570  // Postconditions:
571 
572  // Exit
573 
574  return result;
575 }
576 
577 int
579 poset_prereq_id(int xi) const
580 {
581  int result;
582 
583  // Preconditions:
584 
585  require((0 <= xi) && (xi < PREREQ_IDS_UB));
586 
587  // Body:
588 
589  if(_dofs.prereq_ids[xi] == sheaf::invalid_pod_index())
590  {
591  result = invalid_pod_index();
592  }
593  else
594  {
595  result = _dofs.prereq_ids[xi];
596  }
597 
598  // Postconditions:
599 
600  // Exit:
601 
602  return result;
603 }
604 
605 
606 
607 // PROTECTED MEMBER FUNCTIONS
608 
609 
610 void
613 {
614  // Preconditions:
615 
616 
617  // Body:
618 
619  _schema->attach_to_state(TOP_INDEX);
620 
623 
624  _storage_map.clear();
625  for(pod_index_type i = 0; i < NAMESPACE_SCHEMA_DOF_CT; ++i)
626  {
627  _storage_map[i] = i;
628  }
629 
630  // Postconditions:
631 
632 
633  // Exit:
634 
635  return;
636 }
637 
638 void
641 {
642  // Preconditions:
643 
644  // Body:
645 
646  _dofs.pointer = const_cast<poset_state_handle*>(xposet);
647 
648  // Postconditions:
649 
650  ensure(poset_pointer() == xposet);
651 
652  // Exit
653 
654  return;
655 }
656 
657 
658 void
661 {
662  // Preconditions:
663 
664  // Body:
665 
666  _dofs.type_id = xtype_id;
667 
668  // Postconditions:
669 
670  ensure(poset_type_id() == xtype_id);
671 
672  // Exit
673 
674  return;
675 }
676 
677 void
679 put_poset_class(const char* xclass)
680 {
681  // Preconditions:
682 
683  // Body:
684 
685  _dofs.class_name = xclass;
686 
687  // Postconditions:
688 
689  ensure(poset_class() == xclass);
690 
691  // Exit
692 
693  return;
694 }
695 
696 void
698 put_poset_prereq_id(int xi, int xid)
699 {
700  // Preconditions:
701 
702  require((0 <= xi) && (xi < PREREQ_IDS_UB));
703 
704 
705  // Body:
706 
707  if(is_valid(xid))
708  {
709  _dofs.prereq_ids[xi] = xid;
710  }
711  else
712  {
713  _dofs.prereq_ids[xi] = sheaf::invalid_pod_index();
714  }
715 
716  // Postconditions:
717 
718  ensure(poset_prereq_id(xi) == xid);
719 
720  // Exit:
721 
722  return;
723 }
724 
725 void
728 {
729  // Preconditions:
730 
731 
732  // Body:
733 
734  _dofs.pointer = 0;
735  _dofs.type_id = NOT_A_POSET_TYPE;
736  _dofs.class_name = 0;
737 
738  _storage_map.clear();
739 
740  const index_space_handle& lstorage_id_space =
742 
743  index_space_iterator& litr =
745 
746  while(!litr.is_done())
747  {
748  pod_index_type lstorage_id =
749  lstorage_id_space.pod(litr.hub_pod());
750 
751  _storage_map[litr.pod()] = lstorage_id;
752 
753  litr.next();
754  }
755 
757 
758  // Postconditions:
759 
760 
761  // Exit:
762 
763  return;
764 }
765 
766 
767 
768 // PRIVATE MEMBERS
769 
770 bool
771 sheaf::namespace_poset_dof_map::
772 make_prototype()
773 {
774  // Preconditions:
775 
776 
777  // Body:
778 
779  dof_tuple_type ltype = NAMESPACE_POSET_DOF_TUPLE_ID;
780 
782 
783  factory().insert_prototype(lproto);
784  factory().insert_prototype(ltype, lproto);
785 
786  // Postconditions:
787 
788 
789  // Exit:
790 
791  return true;
792 }
793 
SHEAF_DLL_SPEC poset_type poset_type_id(int xi)
The poset type associated with int xi; converts int to poset type.
Definition: poset_type.cc:32
virtual const index_space_handle & client_id_space() const
The map from library ids to clients ids for the schema this is defined on.
const char * poset_class() const
The class of the poset associated with this namespace member.
bool is_table_dof_map() const
True if this is a table dof map.
virtual void put_dof_tuple(const void *xbuf, size_t xbuflen)
Copies the entire dof tuple from xbuf into internal storage.
namespace_poset_dof_map & operator=(const namespace_poset_dof_map &xother)
Assignment operator.
virtual void * dof_tuple()
The dof tuple (mutable version).
abstract_poset_member & top()
The top member of the poset (mutable version)
An abstract iterator over the ids of an id space.
static const std::string & static_class_name()
The name of this class.
const index_space_handle & dof_id_space(bool xis_table_dofs) const
The table dof (xis_table_dof true) or row dof id space for the schema defined by this.
virtual namespace_poset_dof_map * copy() const
Virtual copy constructor.
pod_type pod() const
The current id in the iteration.
The default name space; a poset which contains other posets as members.
const int NAMESPACE_SCHEMA_DOF_CT
The number of dofs defined by the namespace schema.
poset_type
Identifiers for poset types.
Definition: poset_type.h:41
poset_type poset_type_id() const
The type id of the poset associated with this namespace member.
virtual schema_poset_member & schema()
The schema on which this is allocated (mutable version).
primitive_value dof(pod_index_type xdof_id) const
The dof referred to by xdof_id.
A client handle for a general, abstract partially order set.
virtual bool invariant() const
The class invariant.
bool _is_table_dof_map
True if this is a table dof map.
void put_poset_type_id(poset_type xtype_id)
Set the type id of the poset associated with this namespace member to xtype_id.
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.
void reserve(index_type xub)
Makes ub() at least xub; if new storage is allocated, it is uninitialized.
An abstract handle to a space of alternate integer identifiers (aliases) for a subset of a hub set of...
poset_state_handle * poset_pointer() const
The pointer to the poset associated with this namespace member.
poset_dof_map & operator=(const poset_dof_map &xother)
Assignment operator.
virtual void next()=0
Makes id() the next id in the iteration.
int poset_prereq_id(int xi) const
The id of the xi-th prerequisite poset for the poset associated with this namespace member...
The general, abstract map from dof ids to dof values.
Definition: poset_dof_map.h:59
void put_poset_class(const char *xclass)
Set the class of the poset associated with this namespace member to xclass.
virtual void get_dof_tuple(void *xbuf, size_t xbuflen) const
Copies the entire dof tuple from xbuf into internal storage.
size_t _dof_tuple_ub
The size of the dof tuple.
void extend_to_top()
Extends the schema from member io to member top. /.
static dof_map_factory & factory()
The dof map factory.
void put_poset_pointer(const poset_state_handle *xposet)
Set the pointer to the poset associated with this namespace member to xposet.
bool is_done() const
True if iteration is finished.
virtual bool invariant() const
The class invariant.
void put_poset_prereq_id(int xi, int xid)
Set the id of the xi-th prerequisite poset for the poset associated with this namespace member...
virtual void release_iterator(index_space_iterator &xitr) const =0
Returns the id space iterator xitr to the iterator pool.
virtual namespace_poset_dof_map * clone() const
Virtual default constructor.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
namespace_poset_dof_map()
Default constructor.
dof_tuple_type
Identifiers for dof tuple types.
static int row_dof_ct(const namespace_poset &xns, const poset_path &xpath, bool xauto_access=true)
The number of row dofs defined by the schema specified by xns and xpath. Synonym for dof_ct(xns...
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
virtual pod_type pod(pod_type xid) const =0
The pod index in this space equivalent to xid in the hub id space.
virtual index_space_iterator & get_iterator() const =0
Allocates an id space iterator from the iterator pool.
int _dof_ct
The number of dofs in this map.
virtual schema_poset_member & schema()
The schema for this poset member (mutable version).
int dof_ct() const
The number of dofs in this map.
virtual void allocate_dofs()
Allocates dof storage.
virtual poset_state_handle * host() const
The poset which owns this.
size_t row_dof_tuple_ub() const
The size in bytes of the row dof tuple defined by this schema. Synonym for dof_tuple_ub(false).
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
schema_poset_member * _schema
The schema on which this is instantiated.
virtual void get_dof(pod_index_type xdof_id, void *xdof, size_type xdof_size) const
Copies the dof referred to by xdof_id into xdof.
virtual const std::string & class_name() const
The name of the actual (possibly derived) class of this instance.
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
void tuple(pod_index_type x, size_type xj_ub, pod_index_type &xi, pod_index_type &xj)
Ordinal to 2-tuple conversion.
void attach_to_state(const namespace_poset *xns, const poset_path &xpath, bool xauto_access=true)
Attach to the state specified by path xpath in the namespace xns.
poset_state_handle * _host
The host of the poset member for which this stores the dofs.
A map from schema poset member ids to dof values for namespace_poset members.
void insert_prototype(const poset_dof_map *xprototype)
Sets xprototype as the prototype for its client class.
void clear()
Remove all items.
SHEAF_DLL_SPEC bool is_valid(pod_index_type xpod_index)
True if an only if xpod_index is valid.
Definition: pod_types.cc:37
SHEAF_DLL_SPEC pod_index_type invalid_pod_index()
The invalid pod index value.
Definition: pod_types.cc:31
bool is_same_type(const any *other) const
True if other is the same type as this.
Definition: any.cc:79
void enable_invariant_check() const
Enable invariant checking.
Definition: any.h:87
virtual ~namespace_poset_dof_map()
Destructor.
unsigned int ref_ct() const
The number of references to this map.
const scoped_index & index() const
The index of this in host() dof tuple table.
pod_type hub_pod() const
The current unglued hub id in the iteration. synonym for unglued_hub_pod().
virtual void put_dof(pod_index_type xdof_id, const void *xdof, size_type xdof_size)
Sets the dof referred to by xdof_id to the value at xdof.
const int PREREQ_IDS_UB
Number of prerequisites ids. Must be consistent with PREREQ_ID_*_INDEX below.