SheafSystem  0.0.0.0
forwarding_index_space_handle.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/abstract_product_structure.h"
22 #include "SheafSystem/forwarding_index_space_handle.h"
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/hub_index_space_handle.h"
25 #include "SheafSystem/index_space_family.h"
26 #include "SheafSystem/index_space_iterator.h"
27 
28 // ===========================================================
29 // FORWARDING_INDEX_SPACE_HANDLE FACET
30 // ===========================================================
31 
32 // PUBLIC MEMBER FUNCTIONS
33 
36  : _host(0),
37  _local_id(invalid_pod_index())
38 {
39  // Preconditions:
40 
41  // Body:
42 
43  // Postconditions:
44 
45  ensure(invariant());
46 
47  // Exit:
48 
49  return;
50 }
51 
54  : _host(0),
56 {
57  // Preconditions:
58 
59  // Body:
60 
61  (*this) = xother;
62 
63  // Postconditions:
64 
65  ensure(invariant());
66  ensure((*this) == xother);
67 
68  // Exit:
69 
70  return;
71 }
72 
75  pod_type xindex)
76 {
77  // Preconditions:
78 
79  require(conforms_to_state(xid_spaces, xindex));
80 
81  // Body:
82 
83  attach_to(xid_spaces, xindex);
84 
85  // Postconditions:
86 
87  ensure(invariant());
88  ensure(is_attached());
89  ensure(&id_spaces() == &xid_spaces);
90  ensure(index() == xindex);
91 
92  // Exit:
93 
94  return;
95 }
96 
99  const std::string& xname)
100 {
101  // Preconditions:
102 
103  require(conforms_to_state(xid_spaces, xname));
104 
105  // Body:
106 
107  attach_to(xid_spaces, xname);
108 
109  // Postconditions:
110 
111  ensure(invariant());
112  ensure(is_attached());
113  ensure(&id_spaces() == &xid_spaces);
114  ensure(name() == xname);
115 
116  // Exit:
117 
118  return;
119 }
120 
124 {
125  // Preconditions:
126 
127  require(xother.is_attached() ? conforms_to_state(xother) : true);
128 
129  // Body:
130 
131  attach_to(xother);
132 
133  // Postconditions:
134 
135  ensure(invariant());
136  ensure((*this) == xother);
137 
138  // Exit:
139 
140  return *this;
141 }
142 
145 {
146  // Preconditions:
147 
148  // Body:
149 
150  // nothing to do.
151 
152  // Postconditions:
153 
154  // Exit:
155 
156  return;
157 }
158 
159 // PROTECTED MEMBER FUNCTIONS
160 
161 // PRIVATE MEMBER FUNCTIONS
162 
163 
164 // ===========================================================
165 // INDEX_SPACE_HANDLE FACET
166 // ===========================================================
167 
168 // PUBLIC MEMBER FUNCTIONS
169 
173 {
174  // Preconditions:
175 
176  require(xother.is_attached() ? conforms_to_state(xother) : true);
177 
178  // Body:
179 
180  attach_to(xother);
181 
182  // Postconditions:
183 
184  ensure(invariant());
185  ensure((*this) == xother);
186 
187  // Exit:
188 
189  return *this;
190 }
191 
194 clone() const
195 {
197 
198  // Preconditions:
199 
200  // Body:
201 
202  result = new forwarding_index_space_handle(*this);
203 
204  // Postconditions:
205 
206  ensure(result != 0);
207  ensure(is_same_type(result));
208  ensure(*result == *this);
209 
210  // Exit:
211 
212  return result;
213 }
214 
215 // PROTECTED MEMBER FUNCTIONS
216 
217 // PRIVATE MEMBER FUNCTIONS
218 
219 
220 // ===========================================================
221 // INDEX SPACE FACET
222 // ===========================================================
223 
224 // PUBLIC MEMBER FUNCTIONS
225 
228 id_spaces() const
229 {
230  // Preconditions:
231 
232  require(is_attached());
233 
234  // Body:
235 
236  const index_space_family& result = _host->id_spaces();
237 
238  // Postconditions:
239 
240  ensure(is_basic_query);
241 
242  // Exit:
243 
244  return result;
245 }
246 
250 {
251  // Preconditions:
252 
253  require(is_attached());
254 
255  // Body:
256 
260 
261  index_space_family& result =
262  const_cast<index_space_collection*>(_host)->id_spaces();
263 
264  // Postconditions:
265 
266  ensure(is_basic_query);
267 
268  // Exit:
269 
270  return result;
271 }
272 
275 ct() const
276 {
277  // Preconditions:
278 
279  require(is_attached());
280 
281  // Body:
282 
283  size_type result = _host->ct(_local_id);
284 
285  // Postconditions:
286 
287  ensure(is_basic_query);
288 
289  // Exit:
290 
291  return result;
292 }
293 
296 begin() const
297 {
298  // Preconditions:
299 
300  require(is_attached());
301 
302  // Body:
303 
304  pod_type result = _host->begin(_local_id);
305 
306  // Postconditions:
307 
308  ensure(is_basic_query);
309 
310  // Exit:
311 
312  return result;
313 }
314 
317 end() const
318 {
319  // Preconditions:
320 
321  require(is_attached());
322 
323  // Body:
324 
325  pod_type result = _host->end(_local_id);
326 
327  // Postconditions:
328 
329  ensure(is_basic_query);
330 
331  // Exit:
332 
333  return result;
334 }
335 
336 bool
338 contains(pod_type xid) const
339 {
340  // Preconditions:
341 
342  require(is_attached());
343 
344  // Body:
345 
346  bool result = _host->contains(_local_id, xid);
347 
348  // Postconditions:
349 
350  ensure(is_basic_query);
351 
352  // Exit:
353 
354  return result;
355 }
356 
357 bool
360 {
361  // Preconditions:
362 
363  require(is_attached());
364 
365  // Body:
366 
367  bool result = _host->contains_unglued_hub(_local_id, xid);
368 
369  // Postconditions:
370 
371  ensure(is_basic_query);
372 
373  // Exit:
374 
375  return result;
376 }
377 
378 bool
381 {
382  // Preconditions:
383 
384  require(is_attached());
385 
386  // Body:
387 
388  bool result = _host->contains_glued_hub(_local_id, xid);
389 
390  // Postconditions:
391 
392  ensure(is_basic_query);
393 
394  // Exit:
395 
396  return result;
397 }
398 
399 bool
401 contains(pod_type xid, pod_type xhub_id) const
402 {
403  // Preconditions:
404 
405  require(is_attached());
406 
407  // Body:
408 
409  bool result = _host->contains(_local_id, xid, xhub_id);
410 
411  // Postconditions:
412 
413  ensure(is_basic_query);
414 
415  // Exit:
416 
417  return result;
418 }
419 
422 pod(pod_type xid) const
423 {
424  // Preconditions:
425 
426  require(is_attached());
427 
428  // Body:
429 
430  pod_type result = _host->pod(_local_id, xid);
431 
432  // Postconditions:
433 
434  ensure(!is_valid(result) || contains(result));
435 
436  // Exit:
437 
438  return result;
439 }
440 
444 {
445  // Preconditions:
446 
447  require(is_attached());
448 
449  // Body:
450 
451  pod_type result = _host->unglued_hub_pod(_local_id, xid);
452 
453  // Postconditions:
454 
455  ensure(is_valid(result) || hub_id_space().contains(result));
456 
457  // Exit:
458 
459  return result;
460 }
461 
465 {
466  // Preconditions:
467 
468  require(is_attached());
469 
470  // Body:
471 
472  pod_type result = _host->glued_hub_pod(_local_id, xid);
473 
474  // Postconditions:
475 
476  ensure(is_valid(result) || hub_id_space().contains(result));
477 
478  // Exit:
479 
480  return result;
481 }
482 
483 bool
486 {
487  // Preconditions:
488 
489  require(is_attached());
490 
491  // Body:
492 
493  bool result = _host->is_persistent(_local_id);
494 
495  // Postconditions:
496 
497  ensure(is_basic_query);
498 
499  // Exit:
500 
501  return result;
502 }
503 
504 // PROTECTED MEMBER FUNCTIONS
505 
506 // PRIVATE MEMBER FUNCTIONS
507 
508 
509 // ===========================================================
510 // PRODUCT STRUCTURE FACET
511 // ===========================================================
512 
513 // PUBLIC MEMBER FUNCTIONS
514 
515 void
518 {
519  // Preconditions:
520 
521  require(is_attached());
522 
523  // Body:
524 
531 
532  const_cast<index_space_collection*>(_host)->
533  new_product_structure(_local_id, xproduct);
534 
535  // Postconditions:
536 
537  ensure(has_product_structure());
538 
539  // Exit:
540 
541  return;
542 }
543 
544 void
547 {
548  // Preconditions:
549 
550  require(is_attached());
551 
552  // Body:
553 
554  const_cast<index_space_collection*>(_host)->
556 
557  // Postconditions:
558 
559  ensure(!has_product_structure());
560 
561  // Exit:
562 
563  return;
564 }
565 
569 {
570  // Preconditions:
571 
572  require(is_attached());
573  require(has_product_structure());
574 
575  // Body:
576 
578 
579  // Postconditions:
580 
581  ensure(is_basic_query);
582 
583  // Exit:
584 
585  return result;
586 }
587 
591 {
592  // Preconditions:
593 
594  require(is_attached());
595  require(has_product_structure());
596 
597  // Body:
598 
601 
602  // Postconditions:
603 
604  ensure(is_basic_query);
605 
606  // Exit:
607 
608  return result;
609 }
610 
611 bool
614 {
615  // Preconditions:
616 
617  require(is_attached());
618 
619  // Body:
620 
621  bool result = _host->has_product_structure(_local_id);
622 
623  // Postconditions:
624 
625  ensure(is_basic_query);
626 
627  // Exit:
628 
629  return result;
630 }
631 
632 // PROTECTED MEMBER FUNCTIONS
633 
634 // PRIVATE MEMBER FUNCTIONS
635 
636 
637 // ===========================================================
638 // HANDLE FACET
639 // ===========================================================
640 
641 // PUBLIC MEMBER FUNCTIONS
642 
645 host() const
646 {
647  // Preconditions:
648 
649  require(is_attached());
650 
651  // Body:
652 
653  const index_space_collection& result = *_host;
654 
655  // Postconditions:
656 
657  ensure(is_basic_query);
658 
659  // Exit:
660 
661  return result;
662 }
663 
666 index() const
667 {
668  // Preconditions:
669 
670  require(is_attached());
671 
672  // Body:
673 
675 
676  // Postconditions:
677 
678  ensure(id_spaces().contains(result));
679 
680  // Exit:
681 
682  return result;
683 }
684 
685 bool
687 is_attached() const
688 {
689  // Preconditions:
690 
691  // Body:
692 
693  bool result = is_valid(_local_id);
694 
695  // Postconditions:
696 
697  ensure(is_basic_query);
698 
699  // Exit:
700 
701  return result;
702 }
703 
704 void
707 {
708  // Preconditions:
709 
710  require(is_attached());
711  require(id_spaces().contains(xindex));
712  require(conforms_to_state(xindex));
713 
714  // Body:
715 
716  define_old_variable(const index_space_family& old_id_spaces = id_spaces());
717 
718  _host = id_spaces().collection(xindex);
719  _local_id = _host->local_scope(xindex);
720 
721  // Postconditions:
722 
723  ensure(is_attached());
724  ensure(&id_spaces() == &old_id_spaces);
725  ensure(index() == xindex);
726 
727  // Exit:
728 
729  return;
730 }
731 
732 void
734 attach_to(const index_space_collection& xhost, pod_type xlocal_id)
735 {
736  // Preconditions:
737 
738  require(xhost.contains(xlocal_id));
739  require(conforms_to_state(xhost, xlocal_id));
740 
741  // Body:
742 
743  _host = &xhost;
744  _local_id = xlocal_id;
745 
746  // Postconditions:
747 
748  ensure(is_attached());
749  ensure(&host() == &xhost);
750  ensure(index() == xhost.family_scope(xlocal_id));
751 
752  // Exit:
753 
754  return;
755 }
756 
757 void
760 {
761  // Preconditions:
762 
763  // Body:
764 
766 
767  // Postconditions:
768 
769  ensure(!is_attached());
770 
771  // Exit:
772 
773  return;
774 }
775 
776 bool
779  pod_type xlocal_id) const
780 {
781  // Preconditions:
782 
783  require(xhost.contains(xlocal_id));
784 
785  // Body:
786 
787  bool result = true; // The precondition guarantees this to be true.
788 
789  // Postconditions:
790 
791  ensure(is_basic_query);
792 
793  // Exit:
794 
795  return result;
796 }
797 
798 // PROTECTED MEMBER FUNCTIONS
799 
800 // PRIVATE MEMBER FUNCTIONS
801 
802 
803 // ===========================================================
804 // HANDLE POOL FACET
805 // ===========================================================
806 
807 // PUBLIC MEMBER FUNCTIONS
808 
812 {
813  // Preconditions:
814 
815  require(is_attached());
816 
817  // Body:
818 
820 
821  // Postconditions:
822 
823  ensure(result.is_attached());
824 
825  // Exit:
826 
827  return result;
828 }
829 
830 void
833 {
834  // Preconditions:
835 
836  require(is_attached());
837  require(allocated_id_space());
838 
839  // Body:
840 
841  _host->release_id_space(const_cast<forwarding_index_space_handle&>(*this));
842 
843  // Postconditions:
844 
845  ensure(is_basic_query);
846 
847  // Exit:
848 
849  return;
850 }
851 
852 bool
855 {
856  // Preconditions:
857 
858  require(is_attached());
859 
860  // Body:
861 
862  bool result = _host->allocated_id_space(*this);
863 
864  // Postconditions:
865 
866  ensure(is_basic_query);
867 
868  // Exit:
869 
870  return result;
871 }
872 
873 // PROTECTED MEMBER FUNCTIONS
874 
875 // PRIVATE MEMBER FUNCTIONS
876 
877 
878 // ===========================================================
879 // ITERATOR POOL FACET
880 // ===========================================================
881 
882 // PUBLIC MEMBER FUNCTIONS
883 
887 {
888  // Preconditions:
889 
890  require(is_attached());
891 
892  // Body:
893 
895 
896  // Postconditions:
897 
898  ensure(result.is_attached());
899 
900  // Exit:
901 
902  return result;
903 }
904 
905 void
908 {
909  // Preconditions:
910 
911  require(is_attached());
912  require(allocated_iterator(xitr));
913 
914  // Body:
915 
917 
918  // Postconditions:
919 
920  ensure(is_basic_query);
921 
922  // Exit:
923 
924  return;
925 }
926 
927 bool
930 {
931  // Preconditions:
932 
933  require(is_attached());
934 
935  // Body:
936 
937  bool result = _host->allocated_id_space_iterator(xitr);
938 
939  // Postconditions:
940 
941  ensure(is_basic_query);
942 
943  // Exit:
944 
945  return result;
946 }
947 
948 // PROTECTED MEMBER FUNCTIONS
949 
950 // PRIVATE MEMBER FUNCTIONS
951 
952 
953 // ===========================================================
954 // ANY FACET
955 // ===========================================================
956 
957 // PUBLIC MEMBER FUNCTIONS
958 
959 bool
961 is_ancestor_of(const any *other) const
962 {
963  // Preconditions:
964 
965  require(other != 0);
966 
967  // Body:
968 
969  // True if other conforms to this
970 
971  bool result = dynamic_cast<const forwarding_index_space_handle*>(other) != 0;
972 
973  // Postconditions:
974 
975  // Exit:
976 
977  return result;
978 }
979 
980 bool
982 invariant() const
983 {
984  bool result = true;
985 
986  if(invariant_check())
987  {
988  // Prevent recursive calls to invariant
989 
991 
992  // Must satisfy base class invariant
993 
994  invariance(index_space_handle::invariant());
995 
996  // Invariances for this class:
997 
998  // Finished, turn invariant checking back on.
999 
1001  }
1002 
1003  // Exit
1004 
1005  return result;
1006 }
1007 
1008 // PROTECTED MEMBER FUNCTIONS
1009 
1010 // PRIVATE MEMBER FUNCTIONS
1011 
1012 
1013 // ===========================================================
1014 // NON-MEMBER FUNCTIONS
1015 // ===========================================================
1016 
1017 
virtual void attach_to(pod_type xindex)
Attach to the state with index xindex in the id space family id_spaces().
const index_space_collection * collection(pod_type xid) const
Id space collection for id xid (const version).
virtual bool allocated_id_space_iterator(const index_space_iterator &xitr) const =0
True if and only if id space iterator xitr was allocated by the iterator pool.
An abstract class that defines the product structure for an id space.
pod_type _local_id
The id of this space in the local scope of the host collection.
An abstract iterator over the ids of an id space.
virtual void detach()
Detach this handle form its state, if any.
virtual bool allocated_iterator(const index_space_iterator &xitr) const
True if and only if id space iterator xitr was allocated by the iterator pool.
virtual pod_type family_scope(pod_type xlocal_id) const =0
Id relative to the id space family equivalent to the local id xlocal_id.
virtual void release_id_space() const
Returns this id space handle to the handle pool.
bool contains_glued_hub(pod_type xlocal_id, pod_type xid) const
True if the space with id xlocal_id contains an id equivalent to xid in the glued hub id space...
virtual size_type ct() const
The number of members.
forwarding_index_space_handle & operator=(const forwarding_index_space_handle &xother)
Assignment operator; attach this handle to the state of xother. synonym for attach_to(xother).
virtual pod_type end(pod_type xlocal_id) const =0
Ending id of the space with id xlocal_id.
virtual pod_type glued_hub_pod(pod_type xid) const
The pod index in the hub id space equivalent to xid in this id space. synonym for unglued_hub_pod(xid...
An abstract handle to a space of alternate integer identifiers (aliases) for a subset of a hub set of...
virtual bool allocated_id_space(const index_space_handle &xid_space) const =0
True if and only if id space handle xid_space was allocated by the handle pool.
virtual bool is_persistent() const
True if this id space should be written to disk.
virtual bool is_attached() const =0
True if this handle is attached to a state.
const hub_index_space_handle & hub_id_space() const
The hub id space.
virtual bool contains_unglued_hub(pod_type xlocal_id, pod_type xid) const =0
True if the space with id xlocal_id contains an id equivalent to xid in the unglued hub id space...
virtual const index_space_family & id_spaces() const =0
The id space family for this (const version).
virtual bool has_product_structure(pod_type xlocal_id) const =0
True if the id space with id xlocal_id has a product structure.
virtual pod_type unglued_hub_pod(pod_type xid) const
The pod index in the unglued hub id space equivalent to xid in this id space.
virtual pod_type local_scope(pod_type xspace_id) const =0
Id relative to this collection equivalent to the id space family id xspace_id.
virtual bool allocated_id_space() const
True if and only if this id space handle was allocated by the handle pool.
virtual forwarding_index_space_handle * clone() const
Virtual constructor, makes a new instance of the same type as this. If the handle is attached...
Abstract base class with useful features for all objects.
Definition: any.h:39
virtual void new_product_structure(const abstract_product_structure &xproduct)
Creates a new product structure for this id space by cloning the product structure, xproduct.
virtual pod_type begin(pod_type xlocal_id) const =0
Beginning id of the space with id xlocal_id.
virtual bool invariant() const
Class invariant.
std::string name() const
Name of this space.
pod_index_type pod_type
The "plain old data" index type for this.
An implementation of class index_space_handle for an forwarding_index_space_state.
virtual const index_space_family & id_spaces() const
The id space family for this (const version).
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
virtual void delete_product_structure()
Deletes the product structure for this id space.
virtual size_type ct(pod_type xlocal_id) const =0
The number of members for the id space with id xlocal_id.
virtual pod_type pod(pod_type xlocal_id, pod_type xid) const =0
The pod index in the space with id xlocal_id equivalent to xid in the hub id space.
pod_type glued_hub_pod(pod_type xlocal_id, pod_type xid) const
The pod index in the unglued hub id space equivalent to xid in the id space with id xlocal_id...
virtual bool is_persistent(pod_type xlocal_id) const =0
True if the id space with id xlocal_id should be written to disk.
virtual pod_type unglued_hub_pod(pod_type xlocal_id, pod_type xid) const =0
The pod index in the glued hub id space equivalent to xid in the id space with id xlocal_id...
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
virtual bool has_product_structure() const
True if this id space has a product structure.
A collection of id space states. This is a virtual class with provides an interface for accessing the...
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
virtual const abstract_product_structure & product_structure(pod_type xlocal_id) const =0
The product structure for the id space with id xlocal_id (const version).
virtual bool contains(pod_type xlocal_id) const =0
True if this collection contains the id space with local id xlocal_id.
virtual bool is_attached() const
True if this handle is attached to a state.
virtual index_space_iterator & get_iterator() const
Allocates an id space iterator from the iterator pool.
virtual void release_iterator(index_space_iterator &xitr) const
Returns the id space iterator xitr to the iterator pool.
virtual index_space_handle & get_id_space() const
Allocates an id space handle from the handle pool.
const index_space_collection * _host
The host collection.
virtual pod_type index() const
Index of this space.
virtual bool is_attached() const =0
True if this iterator is attached to a state.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
virtual index_space_handle & get_id_space(pod_type xlocal_id) const =0
Allocates an id space handle from the handle pool attached to state with id xlocal_id.
virtual index_space_iterator & get_id_space_iterator(pod_type xlocal_id) const =0
Allocates an id space iterator from the iterator pool attached to state with id xlocal_id.
virtual const abstract_product_structure & product_structure() const
The product structure for this id space (const version).
virtual void release_id_space_iterator(index_space_iterator &xitr) const =0
Returns the id space iterator xitr to the iterator pool.
virtual bool contains(pod_type xid) const
True if this space contains id xid.
virtual pod_type end() const
Ending id of this space.
virtual const index_space_collection & host() const
The host collection.
virtual pod_type begin() const
Beginning id of this space.
virtual bool invariant() const
Class invariant.
Factory and container for a family of id spaces.
virtual pod_type pod(pod_type xid) const
The pod index in this space equivalent to xid in the hub id space.
virtual bool contains_unglued_hub(pod_type xid) const
True if this space contains an id equivalent to xid in the unglued hub id space.
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
virtual bool conforms_to_state(const index_space_collection &xhost, pod_type xlocal_id) const
True if this conforms to the handle type required by the state with local scope id xlocal_id in the h...
void enable_invariant_check() const
Enable invariant checking.
Definition: any.h:87
virtual void release_id_space(index_space_handle &xid_space) const =0
Returns the id space handle xid_space to the handle pool.
virtual bool contains_glued_hub(pod_type xid) const
True if this space contains an id equivalent to xid in the glued hub id space.