SheafSystem  0.0.0.0
primitives_poset_schema.cc
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 #include "SheafSystem/primitives_poset_schema.h"
22 
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/array_index_space_state.h"
25 #include "SheafSystem/array_poset_dof_map.h"
26 #include "SheafSystem/namespace_poset.impl.h"
27 #include "SheafSystem/namespace_poset_member.h"
28 #include "SheafSystem/poset_handle_factory.h"
29 #include "SheafSystem/primitives_poset_dof_map.h"
30 #include "SheafSystem/poset_type.h"
31 #include "SheafSystem/primitives_index_space_handle.h"
32 #include "SheafSystem/primitives_index_space_state.h"
33 #include "SheafSystem/std_iomanip.h"
34 #include "SheafSystem/std_iostream.h"
35 #include "SheafSystem/total_poset_member.h"
36 
37 using namespace std;
38 
39 // ===========================================================
40 // PRIMITIVES_POSET_SCHEMA FACET
41 // ===========================================================
42 
43 // PUBLIC FUNCTIONS
44 
45 const std::string&
48 {
49  // Preconditions:
50 
51  // Body:
52 
53  static const string result("primitives_schema");
54 
55  // Postconditions:
56 
57  ensure(result == "primitives_schema");
58 
59  // Exit:
60 
61  return result;
62 }
63 
64 // PROTECTED FUNCTIONS
65 
66 // PRIVATE FUNCTIONS
67 
68 sheaf::primitives_poset_schema::
69 primitives_poset_schema()
70 {
71  // Preconditions:
72 
73  // Body:
74 
75  // Nothing to do; poset_state_handle constructor
76  // does what needs to be done
77 
78  // Postconditions:
79 
80  ensure(!is_attached());
81 
82  return;
83 }
84 
85 sheaf::primitives_poset_schema::
86 ~primitives_poset_schema()
87 {
88 
89  // Preconditions:
90 
91  // Body:
92 
93  // Deleted by namespace_poset destructor; nothing to do.
94 
95  // Exit
96 
97  return;
98 }
99 
100 bool
101 sheaf::primitives_poset_schema::
102 make_prototype()
103 {
104  // Preconditions:
105 
106  // Body:
107 
108  poset_type ltype = PRIMITIVES_POSET_SCHEMA_ID;
109 
111 
112  factory().insert_prototype(lproto);
113  factory().insert_prototype(ltype, lproto);
114 
115  // Postconditions:
116 
117  // Exit:
118 
119  return true;
120 }
121 
122 
123 // ===========================================================
124 // POSET_STATE_HANDLE FACET
125 // ===========================================================
126 
127 // PUBLIC FUNCTIONS
128 
131 type_id() const
132 {
133  return PRIMITIVES_POSET_SCHEMA_ID;
134 }
135 
136 const char*
138 class_name() const
139 {
140  // Preconditions:
141 
142  // Body:
143 
144  static const char* result = "primitives_poset_schema";
145 
146  // Postconditions:
147 
148  // Exit:
149 
150  return result;
151 }
152 
153 bool
155 row_dof_map_conforms(const poset_dof_map* xdof_map) const
156 {
157  bool result;
158 
159  // Preconditions:
160 
161  require(xdof_map != 0);
162 
163  // Body:
164 
165  // Row dofs, must be primitives_poset_dof_map
166 
167  result = (dynamic_cast<const primitives_poset_dof_map*>(xdof_map) != 0);
168 
169  // Postconditions:
170 
171  ensure(result ? (dynamic_cast<const primitives_poset_dof_map*>(xdof_map) != 0) : true);
172 
173  // Exit
174 
175  return result;
176 }
177 
178 void
181 {
182  // Preconditions:
183 
184  require(is_attached());
185 
186  // Body:
187 
188  int old_access_request_depth = access_request_depth();
189 
190  read_write_monitor_handle::get_read_access();
191 
192  // Postconditions:
193 
194  ensure(state_is_read_accessible());
195  ensure(access_request_depth() == old_access_request_depth + 1);
196 
197  // Exit
198 
199  return;
200 }
201 
202 void
204 get_read_write_access(bool xrelease_read_only_access)
205 {
206  // Preconditions:
207 
208  require(is_attached());
209  require(!xrelease_read_only_access ? state_is_not_read_only_accessible() : true);
210 
211 
212  // Body:
213 
214  int old_access_request_depth = access_request_depth();
215 
216  read_write_monitor_handle::get_read_write_access(xrelease_read_only_access);
217 
218  // Postconditions:
219 
220  ensure(state_is_read_write_accessible());
221  ensure(access_request_depth() == old_access_request_depth + 1);
222 
223  // Exit
224 
225  return;
226 }
227 
228 void
230 release_access(bool xall) const
231 {
232  // Preconditions:
233 
234  require(state_is_read_accessible());
235 
236  // Body:
237 
238  int old_access_request_depth = access_request_depth();
239 
240  // Inherited poset_state_handle::release_access also releases schema.
241  // But this is its own schema and we don't want cess released twice,
242  // so just call the version in read_write_monitor_handle directly.
243 
244  read_write_monitor_handle::release_access(xall);
245 
246  // Postconditions:
247 
248  ensure(!xall ? access_request_depth() == old_access_request_depth - 1 :
249  access_request_depth() == 0);
250  ensure(access_request_depth() == 0 ? state_is_not_read_accessible() : true);
251 
252  // Exit
253 
254  return;
255 }
256 
259 prereq_id(int xi) const
260 {
261  // The primitives poset schema is its own schema and
262  // has no other prerequisites.
263 
264  return invalid_pod_index();
265 }
266 
267 // PROTECTED FUNCTIONS
268 
269 void
272 {
273  // Preconditions:
274 
275  // Body:
276 
277  // First create the standard members of the base class
278 
279  poset_state_handle::initialize_standard_members();
280 
281  // All jims will be members of "row_dofs" subposet;
282  // there are no table dofs
283 
284  // Enter edit mode
285 
286  begin_jim_edit_mode();
287 
288  primitives_poset_dof_map* ldof_map;
289  pod_index_type lmbr_id;
290 
291  // Create jim for size of primitive type.
292 
293  ldof_map = new primitives_poset_dof_map(this, SIZE_TYPE);
294  lmbr_id = new_member(true, ldof_map, false);
295  put_member_name(lmbr_id, "size", true, false);
296  _row_dof_subposet->insert_member(lmbr_id);
297 
298  // Create jim for alignment of primitive type
299 
300  ldof_map = new primitives_poset_dof_map(this, SIZE_TYPE);
301  lmbr_id = new_member(true, ldof_map, false);
302  put_member_name(lmbr_id, "alignment", true, false);
303  _row_dof_subposet->insert_member(lmbr_id);
304 
305  // Create jim for index of primitive type
306 
307  ldof_map = new primitives_poset_dof_map(this, POD_INDEX_TYPE);
308  lmbr_id = new_member(true, ldof_map, false);
309  put_member_name(lmbr_id, "type_id", true, false);
310  _row_dof_subposet->insert_member(lmbr_id);
311 
312  // Exit edit mode
313 
314  end_jim_edit_mode();
315 
316  // Now there's 5 standard members,
317  // top, bottom, size, alignment, and index
318 
319  // All the members and row dof tuples are standard.
320 
321  put_standard_member_ct(member_ct());
322  put_standard_row_dof_tuple_ct(row_dof_tuple_ct());
323 
324  // Postconditions:
325 
326  ensure(has_standard_member_ct());
327  ensure(has_standard_row_dof_tuple_ct());
328 
329  // Exit:
330 
331  return;
332 }
333 
334 void
336 initialize_standard_subposets(const std::string& xname)
337 {
338 
339  // Preconditions:
340 
341  require(poset_path::is_valid_name(xname));
342  require(state_is_read_write_accessible());
343 
344  // Body:
345 
346  // Initialize the subposets common to all posets.
347 
348  poset_state_handle::initialize_standard_subposets(xname);
349 
350  // Create empty table and row dof subposets;
351  // Standard names will be given in member function schematize.
352 
353  _table_dof_subposet = new subposet(this);
354  _row_dof_subposet = new subposet(this);
355 
356  put_standard_subposet_ct(subposet_ct());
357 
358  // Standard subposet count will be reset in new_state
359  // after schematizing, since that creates new subposets.
360 
361  // Postconditions:
362 
363  ensure(has_standard_subposet_ct());
364 
365  // Exit:
366 
367  return;
368 }
369 
370 void
372 schematize(subposet* xtable_dof_subposet,
373  subposet* xrow_dof_subposet,
374  bool xall_members)
375 {
376  // Preconditions:
377 
378  require(state_is_read_write_accessible());
379  require(xtable_dof_subposet != 0);
380  require(includes_subposet(xtable_dof_subposet));
381  require(xrow_dof_subposet != 0);
382  require(includes_subposet(xrow_dof_subposet));
383 
384 
385  // Body:
386 
387  // Initialize the table dof subposet:
388 
389  // Initialize the name
390 
391  xtable_dof_subposet->put_name(schema_poset_member::table_dof_subposet_name("top"), true, false);
392 
393  // Initialize the dof id space, empty
394 
395  xtable_dof_subposet->new_id_space("array_index_space_state");
396 
397  // Initialize is_dof_subposet
398 
399  xtable_dof_subposet->put_is_dof_subposet(true);
400 
401  // Initialize dof count
402 
403  xtable_dof_subposet->put_dof_ct(0);
404 
405  // Initialize the table dof offsets array
406 
407  dof_descriptor_array& ltable_dof_descriptors = *(new dof_descriptor_array(0));
408  ltable_dof_descriptors[0].size = 0;
409  ltable_dof_descriptors[0].alignment = 0;
410  ltable_dof_descriptors[0].type = NOT_A_PRIMITIVE_TYPE;
411  ltable_dof_descriptors[0].offset = 0;
412  xtable_dof_subposet->put_dof_descriptors(&ltable_dof_descriptors);
413 
414 
415  // Initialize the row dof subposet:
416 
417  // Initialize the name
418 
419  xrow_dof_subposet->put_name(schema_poset_member::row_dof_subposet_name("top"), true, false);
420 
421  // Initialize the dof client id map
422 
423  string lname = xrow_dof_subposet->name();
424 
425  primitives_index_space_state::new_space(member_id_spaces(false), lname);
426 
427  // Initialize is_dof_subposet
428 
429  xrow_dof_subposet->put_is_dof_subposet(true);
430 
431  // Initialize dof count
432  // There are 3 dofs: size, alignment, index
433 
434  xrow_dof_subposet->put_dof_ct(PRIMITIVES_SCHEMA_DOF_CT);
435 
436  // Initialize the dof offsets array.
437  // Constructor argument is dof count; allocates dof count + 1 entries
438 
440 
441  // Size dof is type size_t
442 
443  const primitive_descriptor size_t_desc = primitive_attributes::descriptor(SIZE_TYPE);
444 
445  lrow_dof_descriptors[0].size = size_t_desc.size;
446  lrow_dof_descriptors[0].alignment = size_t_desc.alignment;
447  lrow_dof_descriptors[0].type = size_t_desc.index;
448  lrow_dof_descriptors[0].offset = 0;
449  size_t loffset = size_t_desc.size;
450 
451  // Alignment dof is type size_t
452 
453  lrow_dof_descriptors[1].size = size_t_desc.size;
454  lrow_dof_descriptors[1].alignment = size_t_desc.alignment;
455  lrow_dof_descriptors[1].type = size_t_desc.index;
456  loffset = align(loffset, size_t_desc.alignment);
457  lrow_dof_descriptors[1].offset = loffset;
458  loffset += size_t_desc.size;
459 
460  // Index dof is type int
461 
462  const primitive_descriptor int_desc = primitive_attributes::descriptor(INT_TYPE);
463 
464  lrow_dof_descriptors[2].size = int_desc.size;
465  lrow_dof_descriptors[2].alignment = int_desc.alignment;
466  lrow_dof_descriptors[2].type = int_desc.index;
467  loffset = align(loffset, int_desc.alignment);
468  lrow_dof_descriptors[2].offset = loffset;
469  loffset += int_desc.size;
470 
471  // Last dof descriptor just stores size of entire dof tuple in offset.
472 
473  lrow_dof_descriptors[3].size = 0;
474  lrow_dof_descriptors[3].alignment = 0;
475  lrow_dof_descriptors[3].type = NOT_A_PRIMITIVE_TYPE;
476  lrow_dof_descriptors[3].offset = loffset;
477 
478  xrow_dof_subposet->put_dof_descriptors(&lrow_dof_descriptors);
479 
480  // Postconditions:
481 
482  ensure(is_schematized(false));
483 
484  // Exit
485 
486  return;
487 }
488 
489 void
492 {
493  // Preconditions:
494 
495  require(state_is_read_write_accessible());
496 
497  // Body:
498 
499  // This is it's own schema;
500  // just detach and delete the state.
501 
502  poset_state* lstate = _state;
503  detach_from_state();
504  delete lstate;
505 
506  // Postconditions:
507 
508  ensure(!is_attached());
509  ensure(unexecutable(state has been deleted));
510 
511  // Exit
512 
513  return;
514 }
515 
516 // PRIVATE FUNCTIONS
517 
518 void
519 sheaf::primitives_poset_schema::
520 new_state()
521 {
522  // Preconditions:
523 
524  // Body:
525 
526  // Disable invariant checking in
527  // member functions until construction finished.
528 
529  disable_invariant_check();
530 
531  // Create the poset state object without a schema because we don't have one yet.
532  // Initial space for 4 members, 8 links, 4 subposets
533 
534  // _state = new poset_state(0, PRIMITIVES_POSET_SCHEMA_ID, 8, 16, 32);
535  _state = new poset_state(0, PRIMITIVES_POSET_SCHEMA_ID, standard_name());
536 
537  // Get write access.
538 
539  get_read_write_access();
540 
541  // Initialize additional row structure.
542 
543  // Create subposets for jims and whole, and schema subposets for top.
544 
545  initialize_standard_subposets(standard_name());
546 
547  // Create the bottom, top, and jims.
548 
549  initialize_standard_members();
550 
551  // Now that the standard members have all been inserted in the row dofs,
552  // we can initialize the other schema info derived from the dofs subposets.
553 
554  schematize(_table_dof_subposet, _row_dof_subposet);
555 
556  // Schematize has created new subposets, reset the standard subposet count.
557  // Every subposet is a standard subposet.
558 
559  put_standard_subposet_ct(subposet_ct());
560 
561  // Set the standard id spaces.
562 
563  update_standard_member_id_spaces();
564 
565  // We won't use the two subposet handles again;
566  // detach and delete them.
567 
568  _table_dof_subposet->detach_from_state();
569  delete _table_dof_subposet;
570  _table_dof_subposet = 0;
571 
572  _row_dof_subposet->detach_from_state();
573  delete _row_dof_subposet;
574  _row_dof_subposet = 0;
575 
576  // Initialize the schema;
577  //
578  // Since the jims of any schema poset are necessarily copies
579  // of jims of the primitives poset, the schema of any schema
580  // poset must be the same as the schema of the primitives
581  // poset. Thus, this schema poset must be its own schema.
582  // More precisely, the schema for this poset must be
583  // its own top member.
584 
585  schema().attach_to_state(&(top()));
586 
587  // Initialize the table dof map.
588  // This class has no table dofs, but we want a non-null dof map anyway.
589  // Requires schema.
590 
591  initialize_table_dofs(0,0);
592 
593  // Now the invariant should be satisfied.
594 
595  enable_invariant_check();
596 
597  // Postconditions:
598 
599  ensure(invariant());
600  ensure(!in_jim_edit_mode());
601  ensure(has_standard_member_ct());
602  ensure(has_standard_row_dof_tuple_ct());
603  ensure(has_standard_subposet_ct());
604 
605  // Release thread access to poset state.
606 
607  release_access();
608 
609  ensure(state_is_not_read_accessible());
610 }
611 
612 
613 // ===========================================================
614 // ANY FACET
615 // ===========================================================
616 
617 // PUBLIC FUNCTIONS
618 
619 bool
621 is_ancestor_of(const any* xother) const
622 {
623  bool result;
624 
625  // Preconditions:
626 
627  // Body:
628 
629  result = dynamic_cast<const primitives_poset_schema*>(xother) != 0;
630 
631  // Postconditions:
632 
633  // Exit
634 
635  return result;
636 }
637 
638 
639 bool
641 invariant() const
642 {
643  bool result = true;
644 
645  // Preconditions:
646 
647  // Body:
648 
649  result = result && poset_state_handle::invariant();
650 
651  if(invariant_check())
652  {
653  disable_invariant_check();
654 
655  if(is_attached())
656  {
658  }
659 
660  enable_invariant_check();
661  }
662 
663  // Postconditions:
664 
665  // Exit
666 
667  return result;
668 }
669 
670 // PROTECTED FUNCTIONS
671 
672 // PRIVATE FUNCTIONS
673 
675 sheaf::primitives_poset_schema::
676 clone() const
677 {
678  primitives_poset_schema* result;
679 
680  // Preconditions:
681 
682  // Body:
683 
684  result = new primitives_poset_schema();
685 
686  // Postconditions:
687 
688  ensure(result != 0);
689  ensure(!result->is_attached());
690 
691  // Exit
692 
693  return result;
694 }
695 
std::string name() const
Definition: subposet.h:796
A client handle for a subposet.
Definition: subposet.h:86
The private state of a partially ordered set.
Definition: poset_state.h:49
const int PRIMITIVES_SCHEMA_DOF_CT
Number of dofs defined by primitives schema.
virtual void get_read_access() const
Get read access to the state associated with this.
virtual bool is_ancestor_of(const any *other) const
True if other conforms to this.
poset_type
Identifiers for poset types.
Definition: poset_type.h:41
size_type size() const
The number of bytes in the dof tuple described by this.
bool invariant() const
Class invariant.
STL namespace.
virtual void get_read_write_access(bool xrelease_read_only_access=false)
Get read write access to the state associated with this. If release_read_only_access is requested...
The general, abstract map from dof ids to dof values.
Definition: poset_dof_map.h:59
virtual void initialize_standard_members()
Initializes the predefined members of the poset.
virtual void terminate_access()
Release all access to posets this depends on, then detach and delete the state.
Abstract base class with useful features for all objects.
Definition: any.h:39
virtual void put_name(const std::string &xname, bool xunique, bool xauto_access)
Make xname a name for this; if xunique, make xname the only name.
Definition: subposet.cc:2782
A factory for instanting descendants of an abstract type T, given the class name of the descendant...
Definition: eval_family.h:49
A map from schema poset member ids to dof values for primitives_poset members.
virtual scattered_insertion_index_space_handle & new_id_space(const std::string &xstate_class_name)
Creates an id space for the members of this.
Definition: subposet.cc:554
virtual const char * class_name() const
The name of this class.
The schema poset for the primitives.
virtual void release_access(bool xall=false) const
Release access. If xall is true, release all levels of access. Otherwise, release one level of access...
virtual void put_dof_ct(int xct)
Set the number of dofs defined this, considered as a dof_subposet, to xct; Note: dof_ct() == member_c...
Definition: subposet.cc:812
virtual poset_type type_id() const
Identifier for the type of this poset.
An array for storing structs which describe the size, alignment, and offset of dofs within a dof tupl...
virtual void initialize_standard_subposets(const std::string &xname)
Creates the subposets common to every poset (i.e._whole and _jims) plus those required of a schema po...
virtual void put_dof_descriptors(const dof_descriptor_array *xoffsets)
Set the offset from beginning of the dof tuple of each dof to xoffsets.
Definition: subposet.cc:855
virtual void schematize(subposet *xtable_dof_subposet, subposet *xrow_dof_subposet, bool xall_members=true)
Prepare this for use as a schema All members will be schematized; argument xall_members is ignored...
virtual bool row_dof_map_conforms(const poset_dof_map *xdof_map) const
True if xdof_map conforms to (i.e. is derived from) the type of row dof map required by this poset...
virtual void put_is_dof_subposet(bool xis_dof_subposet)
True if this is a dofs subposet.
Definition: subposet.cc:768
virtual bool is_attached() const
True if this is attached to a state.
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
size_t align(const size_t xoffset, const size_t xalignment)
Smallest offset greater than or equal to xoffset that has alignment xalignment.
static const std::string & standard_name()
The standard name for an instance of this class.
void insert_prototype(T *xprototype)
Sets xprototype as the prototype for its client class.
Definition: factory.impl.h:144
virtual pod_index_type prereq_id(int xi) const
The id of the xi-th prerequisite poset for this.
SHEAF_DLL_SPEC pod_index_type invalid_pod_index()
The invalid pod index value.
Definition: pod_types.cc:31