SheafSystem  0.0.0.0
primitives_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 primitives_poset_dof_map
19 
20 #include "SheafSystem/primitives_poset_dof_map.h"
21 
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/dof_map_factory.h"
24 #include "SheafSystem/primitive_traits.h"
25 #include "SheafSystem/primitives_poset.h"
26 #include "SheafSystem/primitives_poset_schema.h"
27 #include "SheafSystem/std_string.h"
28 
29 using namespace std;
30 
31 // PUBLIC MEMBER FUNCTIONS
32 
33 const std::string&
35 class_name() const
36 {
37  // Preconditions:
38 
39  // Body:
40 
41  const string& result = static_class_name();
42 
43  // Postconditions:
44 
45  ensure(!result.empty());
46 
47  // Exit:
48 
49  return result;
50 }
51 
52 const std::string&
55 {
56  // Preconditions:
57 
58  // Body:
59 
60  static const string result("primitives_poset_dof_map");
61 
62  // Postconditions:
63 
64  ensure(!result.empty());
65  ensure(result == "primitives_poset_dof_map");
66 
67  // Exit:
68 
69  return result;
70 }
71 
72 // CANONICAL MEMBERS
73 
76 {
77 
78  // Preconditions:
79 
80 
81  // Body:
82 
83  _dof_ct = PRIMITIVES_SCHEMA_DOF_CT;
84  _dof_tuple_ub = sizeof(primitive_descriptor);
85  _dofs.size = 0;
86  _dofs.alignment = 0;
87  _dofs.index = NOT_A_PRIMITIVE_TYPE;
88 
89  // Postconditions:
90 
91  ensure(invariant());
92  ensure(!is_initialized());
93  ensure(host() == 0);
94 
95  ensure(!index().is_valid());
96 
97  ensure(!is_table_dof_map());
98  ensure(dof_ct() == PRIMITIVES_SCHEMA_DOF_CT);
99  ensure(dof_tuple_ub() == sizeof(primitive_descriptor));
100  ensure(ref_ct() == 0);
101 
102 }
103 
106 clone() const
107 {
108  primitives_poset_dof_map* result;
109 
110  // Preconditions:
111 
112 
113  // Body:
114 
115  result = new primitives_poset_dof_map();
116 
117  // Postconditions:
118 
119  ensure(result != 0);
120  ensure(result->is_same_type(this));
121  ensure(postcondition_of(primitives_poset_dof_map()));
122 
123  // Exit:
124 
125  return result;
126 }
127 
128 
129 
132  :poset_dof_map()
133 {
134  // Preconditions:
135 
136  require(xother.is_initialized());
137 
138  // Body:
139 
140  (*this) = xother;
141 
142  // Postconditions:
143 
144  ensure(invariant());
145  ensure(is_initialized());
146  ensure(host() == xother.host());
147  ensure(index() == xother.index());
148  ensure(schema().is_same_state(&xother.schema()));
149  ensure(is_table_dof_map() == xother.is_table_dof_map());
150  ensure(dof_ct() == xother.dof_ct());
151  ensure(dof_tuple_ub() == xother.dof_tuple_ub());
152  ensure(ref_ct() == 0);
153 
154 }
155 
158 copy() const
159 {
160  primitives_poset_dof_map* result;
161 
162  // Preconditions:
163 
164 
165  // Body:
166 
167  result = new primitives_poset_dof_map(*this);
168 
169  // Postconditions:
170 
171  ensure(result != 0);
172  ensure(result->is_same_type(this));
173  ensure(postcondition_of(primitives_poset_dof_map(*this)));
174 
175  // Exit:
176 
177  return result;
178 }
179 
180 
184 {
185  // Preconditions:
186 
187  // Body:
188 
189  require(xother.is_initialized());
190 
191  // Body:
192 
193  // Can't call poset_dof_map::operator= because it
194  // deletes the schema then clones xother schema.
195  // This class just keeps a pointer to the schema
196  // handle in the poset.
197 
198  _host = xother._host;
199  _index = xother._index;
200  _schema = xother._schema;
202  _dof_ct = xother._dof_ct;
203  _dof_tuple_ub = xother._dof_tuple_ub;
204  _ref_ct = 0;
205 
206  _dofs = xother._dofs;
207 
208  // Postconditions:
209 
210  ensure(invariant());
211  ensure(is_initialized());
212  ensure(host() == xother.host());
213  ensure(index() == xother.index());
214  ensure(schema().is_same_state(&xother.schema()));
215  ensure(is_table_dof_map() == xother.is_table_dof_map());
216  ensure(dof_ct() == xother.dof_ct());
217  ensure(dof_tuple_ub() == xother.dof_tuple_ub());
218  ensure(ref_ct() == 0);
219 
220 
221  // Exit
222 
223  return *this;
224 }
225 
226 
229 {
230  // Preconditions:
231 
232  // Body:
233 
234  // The poset_dof_map dtor will delete the schema
235  // if it is not null. _schema in this class is a
236  // pointer to the schema in the host state, so we can't
237  // let it be deleted.
238 
239  _schema = 0;
240 
241  // Postconditions:
242 
243 }
244 
245 bool
247 invariant() const
248 {
249  bool result = true;
250 
251  // Preconditions:
252 
253  // Body:
254 
255  result = result && poset_dof_map::invariant();
256 
257  if(invariant_check())
258  {
259  // Prevent recursive calls to invariant
260 
262 
263  // Invariants for this class:
264 
265  result = result && (is_initialized() ? &(schema()) == &(host()->state_obj()->table()->schema()) : true);
266 
267  // Finished, turn invariant checking back on.
268 
270  }
271 
272  // Postconditions:
273 
274  // Exit
275 
276  return result;
277 }
278 
279 
280 // OTHER CONSTRUCTORS
281 
283 primitives_poset_dof_map(const poset_state_handle* xhost, int xprimitive_index)
284  : poset_dof_map()
285 {
286 
287  // Preconditions:
288 
289  require(xhost != 0);
290  require(xhost->state_is_read_accessible());
291  require(is_primitive_index(xprimitive_index));
292 
293  // Body:
294 
295  // The bootstrap problem: the schema for this is the primitives_poset_schema
296  // and creating it requires creating these dof maps first.
297  //
298  // When this constructor is called, the schema handle in primitives_poset_schema
299  // has been created but not attached. So we can save a pointer to it, which will
300  // be a valid schema handle once the host constructor is finished.
301 
302  // Note: must call default poset_dof_map constructor above,
303  // because poset_dof_map::poset_dof_map(schema_poset_member* xschema, ...)
304  // uses xschema to get _dof_ct and _dof_tuple_ub.
305 
306  // Initialize poset_dof_map members:
307 
308  _host = const_cast<poset_state_handle*>(xhost);
309 
310  _index.invalidate();
312 
313  // Save a pointer to the host schema. Make sure to get
314  // the one in the state, in case the one in the handle xhost
315  // changes. Have to be careful about deleting this, see the dtor.
316 
317  _schema = &(xhost->state_obj()->table()->schema());
318 
319  _is_table_dof_map = false;
320 
321  // Initialize the schema properties without reference to the schema.
322 
325 
326  _ref_ct = 0;
327 
328  // Initialize dofs to the specified primitive dofs
329 
330  _dofs = primitive_attributes::descriptor(xprimitive_index);
331 
332  // Postconditions:
333 
334  ensure(invariant());
335  ensure(is_initialized());
336  ensure(host() == xhost);
337  ensure(!index().is_valid());
338  ensure(index().same_scope(host()->dof_tuple_hub_id_space(false)));
339  ensure(!is_table_dof_map());
340  ensure(dof_ct() == PRIMITIVES_SCHEMA_DOF_CT);
341  ensure(dof_tuple_ub() == sizeof(primitive_descriptor));
342 
343  // Exit:
344 
345  return;
346 }
347 
348 
349 void
352  pod_index_type xschema_mbr_id,
353  int xschema_version)
354 {
355 
356  // Preconditions:
357 
358  require(xhost != 0);
359  require(xhost->state_is_read_accessible());
360  require(xhost->schema().index() == xschema_mbr_id);
361  require(xhost->schema().host()->version() == xschema_version);
362 
363  // Body:
364 
365  // Initialize poset_dof_map members:
366 
367  _host = const_cast<poset_state_handle*>(xhost);
368 
369  _index.invalidate();
370  _index.put_scope(_host->dof_tuple_hub_id_space(false));
371 
373 
374  // Save a pointer to the host schema. Make sure to get
375  // the one in the state, in case the one in the handle xhost
376  // changes. Have to be careful about deleting this, see the dtor.
377 
378  _schema = &(xhost->state_obj()->table()->schema());
379 
380  _is_table_dof_map = false;
381 
382  // Initialize the schema properties without reference to the schema.
383 
386 
387  _ref_ct = 0;
388 
389  // Don't need to allocate _dofs; it's already allocated.
390 
391  // Postconditions:
392 
393  ensure(invariant());
394  ensure(is_initialized());
395  ensure(host() == xhost);
396  ensure(!index().is_valid());
397  ensure(index().same_scope(host()->dof_tuple_hub_id_space(false)));
398  ensure(!is_table_dof_map());
399  ensure(dof_ct() == PRIMITIVES_SCHEMA_DOF_CT);
400  ensure(dof_tuple_ub() == sizeof(primitive_descriptor));
401 
402  // Exit:
403 
404  return;
405 }
406 
407 
408 
409 // MAP INTERFACE
410 
411 // ===========================================================
412 // NEW DOF ACCESS FACET
413 // ===========================================================
414 
415 void
417 get_dof(pod_index_type xdof_id, void* xdof, size_type xdof_size) const
418 {
419  // Preconditions:
420 
421  require(schema().state_is_read_accessible());
422  require(schema().dof_id_space(is_table_dof_map()).contains(xdof_id));
423  require(unexecutable("xdof points to buffer of size xdof_size"));
424  require(xdof_id == 0 ? xdof_size >= primitive_traits<size_type>::size() : true);
425  require(xdof_id == 1 ? xdof_size >= primitive_traits<size_type>::size() : true);
426  require(xdof_id == 2 ? xdof_size >= primitive_traits<pod_index_type>::size() : true);
427 
428  // Body:
429 
430  switch (xdof_id)
431  {
432  case 0: // size dof is primitive type size_type
433  memcpy(xdof, &(_dofs.size), primitive_traits<size_type>::size());
434  break;
435  case 1: // alignment dof is primitive type size_type
436  memcpy(xdof, &(_dofs.alignment), primitive_traits<size_type>::size());
437  break;
438  case 2: // index dof is primtive type pod_index_type
439  memcpy(xdof, &(_dofs.index), primitive_traits<pod_index_type>::size());
440  break;
441  default:
442  post_fatal_error_message("invalid xdof_id in call to primitives_poset_dof_map::get_dof");
443  }
444 
445  // Postconditions:
446 
447  ensure(unexecutable(xdof holds dofs referred to by xdof_id and xis_poset_id));
448 
449  // Exit
450 
451  return;
452 }
453 
454 void
456 put_dof(pod_index_type xdof_id, const void* xdof, size_type xdof_size)
457 {
458  // Preconditions:
459 
460  require(schema().state_is_read_accessible());
461  require(schema().dof_id_space(is_table_dof_map()).contains(xdof_id));
462  require(unexecutable("xdof points to buffer of size xdof_size"));
463  require(xdof_id == 0 ? xdof_size >= primitive_traits<size_type>::size() : true);
464  require(xdof_id == 1 ? xdof_size >= primitive_traits<size_type>::size() : true);
465  require(xdof_id == 2 ? xdof_size >= primitive_traits<pod_index_type>::size() : true);
466 
467 
468  // Body:
469 
470  switch (xdof_id)
471  {
472  case 0: // size dof is primitive type size_type
473  memcpy(&(_dofs.size), xdof, primitive_traits<size_type>::size());
474  break;
475  case 1: // alignment dof is primitive type size_type
476  memcpy(&(_dofs.alignment), xdof, primitive_traits<size_type>::size());
477  break;
478  case 2: // index dof is primtive type pod_index_type
479  memcpy(&(_dofs.index), xdof, primitive_traits<pod_index_type>::size());
480  break;
481  default:
482  post_fatal_error_message("invalid xdof_id in call to primitives_poset_dof_map::put_dof");
483  }
484 
485  // Postconditions:
486 
487  ensure(unexecutable(_dofs holds dofs referred to by xdof_id and xis_poset_id));
488 
489  // Exit
490 
491  return;
492 }
493 
494 // ===========================================================
495 // END NEW DOF ACCESS FACET
496 // ===========================================================
497 
498 void*
501 {
502  void* result;
503 
504  // Preconditions:
505 
506  // Body:
507 
508  result = &_dofs;
509 
510  // Postconditions:
511 
512  ensure(invariant());
513 
514  // Exit
515 
516  return result;
517 }
518 
519 const void*
521 dof_tuple() const
522 {
523  const void* result;
524 
525  // Preconditions:
526 
527  // Body:
528 
529  result = &_dofs;
530 
531  // Postconditions:
532 
533  ensure(invariant());
534 
535  // Exit
536 
537  return result;
538 }
539 
540 void
542 get_dof_tuple(void* xbuf, size_t xbuflen) const
543 {
544  // Preconditions:
545 
546  require(xbuf != 0);
547  require(unexecutable(xbuf points to buffer of size xbuflen));
548  require(xbuflen >= sizeof(primitive_descriptor));
549  require(unexecutable("xbuf id properly aligned for primitive_descriptor"));
550 
551  // Body:
552 
553  *static_cast<primitive_descriptor*>(xbuf) = _dofs;
554 
555  // Postconditions:
556 
557  ensure(unexecutable(dof tuple copied to xbuf));
558 
559  // Exit
560 
561  return;
562 }
563 
564 
565 void
567 put_dof_tuple(const void* xbuf, size_t xbuflen)
568 {
569  // Preconditions:
570 
571  require(xbuf != 0);
572  require(unexecutable(xbuf points to buffer of size xbuflen));
573  require(xbuflen >= sizeof(primitive_descriptor));
574  require(unexecutable("xbuf id properly aligned for primitive_descriptor"));
575 
576  // Body:
577 
578  _dofs = *static_cast<const primitive_descriptor*>(xbuf);
579 
580  // Postconditions:
581 
582  ensure(unexecutable(xbuf copied to dof tuple));
583 
584  // Exit
585 
586  return;
587 }
588 
589 size_t
591 size() const
592 {
593  return _dofs.size;
594 }
595 
596 void
598 put_size(size_t xsize)
599 {
600  // Preconditions:
601 
602  // Body:
603 
604  _dofs.size = xsize;
605 
606  // Postconditions:
607 
608  ensure(size() == xsize);
609 
610  // Exit:
611 
612  return;
613 }
614 
615 size_t
617 alignment() const
618 {
619  return _dofs.alignment;
620 }
621 
622 void
624 put_alignment(size_t xalignment)
625 {
626  // Preconditions:
627 
628  // Body:
629 
630  _dofs.alignment = xalignment;
631 
632  // Postconditions:
633 
634  ensure(alignment() == xalignment);
635 
636  // Exit:
637 
638  return;
639 }
640 
643 type() const
644 {
645  return primitive_attributes::id(_dofs.index);
646 }
647 
648 void
650 put_type(int xtype)
651 {
652  // Preconditions:
653 
654  // Body:
655 
656  _dofs.index = xtype;
657 
658  // Postconditions:
659 
660  ensure(type() == xtype);
661 
662  // Exit:
663 
664  return;
665 }
666 
667 // PRIVATE STATIC MEMBERS
668 
669 void
672 {
673  // Preconditions:
674 
675  // Redefined in this class to relax preconditions
676  // which can not be satisfied in the bootstrap.
677 
678  require(xhost->state_is_read_accessible());
679 
680  // Body:
681 
682  _host = const_cast<poset_state_handle*>(xhost);
683 
684  // Schema of this must be same as schema of
685  // host to prevent dangling pointer if other
686  // schema handle goes away.
687 
688  _schema = &(xhost->state_obj()->table()->schema());
689 
690  // Postconditions:
691 
692  ensure(invariant());
693  ensure(host() == xhost);
694 
695  // Exit
696 
697  return;
698 }
699 
700 bool
701 sheaf::primitives_poset_dof_map::
702 make_prototype()
703 {
704  // Preconditions:
705 
706 
707  // Body:
708 
709  dof_tuple_type ltype = PRIMITIVES_POSET_DOF_TUPLE_ID;
710 
712 
713  factory().insert_prototype(lproto);
714  factory().insert_prototype(ltype, lproto);
715 
716  // Postconditions:
717 
718 
719  // Exit:
720 
721  return true;
722 }
723 
724 
725 // ===========================================================
726 // NON-MEMBER FUNCTIONS
727 // ===========================================================
728 
poset_state_handle * host() const
The poset which this is a handle to a component of.
bool is_table_dof_map() const
True if this is a table dof map.
static primitive_descriptor descriptor(pod_index_type xid)
Primitive descriptor for the primitive type with id xid.
const int PRIMITIVES_SCHEMA_DOF_CT
Number of dofs defined by primitives schema.
void put_alignment(size_t xalignment)
Put alignment dof.
virtual poset_state * state_obj() const
State object for this poset.
bool state_is_read_accessible() const
True if this is attached and if the state is accessible for read or access control is disabled...
void put_type(int xindex)
Put type dof. Note: have to use "type" in name in order to avoid name conflict with index() inherited...
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.
unsigned int _ref_ct
The number of references to this map /.
A client handle for a general, abstract partially order set.
virtual void * dof_tuple()
The dof tuple (mutable version).
schema_poset_member & schema()
Schema (mutable version)
bool _is_table_dof_map
True if this is a table dof map.
bool is_initialized() const
True if this has been initialized, that is, if the schema has been set and the dof map storage alloca...
virtual bool invariant() const
The class invariant.
STL namespace.
virtual void put_host(const poset_state_handle *xhost)
Sets host() to xhost.
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 int version() const
The current version.
void invalidate()
Make this id invalid.
Definition: scoped_index.h:852
const scoped_index & index() const
The index of the component state this handle is attached to.
The general, abstract map from dof ids to dof values.
Definition: poset_dof_map.h:59
primitives_poset_dof_map()
Default constructor;.
poset_table_state * table() const
Table (dof tuples).
Definition: poset_state.cc:260
virtual primitives_poset_dof_map * clone() const
Virtual default constructor.
const hub_index_space_handle & dof_tuple_hub_id_space(bool xauto_access) const
The dof tuple hub id space.
size_t _dof_tuple_ub
The size of the dof tuple.
static const std::string & static_class_name()
The name of this class.
static dof_map_factory & factory()
The dof map factory.
void put_scope(const index_space_handle &xid_space)
Sets the scope to xid_space.
Definition: scoped_index.h:441
virtual schema_poset_member & schema()
The schema for this poset (mutable version).
primitives_poset_dof_map & operator=(const primitives_poset_dof_map &xother)
Assignment operator.
primitive_type
Type ids for sheaf primitives.
primitive_type type() const
Get type dof. Note: have to use "type" in name in order to avoid name conflict with index() inherited...
virtual bool invariant() const
The class invariant.
A map from schema poset member ids to dof values for primitives_poset members.
size_t dof_tuple_ub() const
The size of the dof tuple in bytes.
virtual void get_dof_tuple(void *xbuf, size_t xbuflen) const
Copies the entire dof tuple from internal storage into xbuf.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
SHEAF_DLL_SPEC bool is_primitive_index(pod_index_type xindex)
True if xindex is a valid primitive index.
scoped_index _index
The poset member for which this stores the dofs.
virtual void init_row_dof_map(const poset_state_handle *xhost, pod_index_type xschema_mbr_id, int xschema_version)
Initializes this as a map for row dofs in host xhost, with schema member specified by xschema_mbr_id ...
dof_tuple_type
Identifiers for dof tuple types.
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
size_t alignment() const
Get alignment dof.
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.
Traits for primitive type T.
int _dof_ct
The number of dofs in this map.
int dof_ct() const
The number of dofs in this map.
virtual poset_state_handle * host() const
The poset which owns this.
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.
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
primitive_type id() const
The id for the primitive type associated with this.
void put_size(size_t xsize)
Put size dof.
virtual void put_dof_tuple(const void *xbuf, size_t xbuflen)
Copies the entire dof tuple from xbuf into internal storage.
void tuple(pod_index_type x, size_type xj_ub, pod_index_type &xi, pod_index_type &xj)
Ordinal to 2-tuple conversion.
poset_state_handle * _host
The host of the poset member for which this stores the dofs.
void insert_prototype(const poset_dof_map *xprototype)
Sets xprototype as the prototype for its client class.
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
virtual const std::string & class_name() const
The name of the actual (possibly derived) class of this instance.
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 primitives_poset_dof_map * copy() const
Virtual copy constructor.
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.