SheafSystem  0.0.0.0
any.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 any
19 
20 #include "SheafSystem/any.h"
21 #include "SheafSystem/assert_contract.h"
22 
23 
24 // PUBLIC MEMBER FUNCTIONS
25 
26 // CANONICAL MEMBERS
27 
30 any()
31 {
32 
33  // Preconditions:
34 
35 
36  // Body:
37 
38  // Invariant checking enabled unless specifcally disabled
39 
40  _disable_invariance_check_request_depth = 0;
41 
42  // Postconditions:
43 
44  ensure(invariant_check());
45 
46  // Exit:
47 
48  return;
49 }
50 
51 // GENERIC OBJECT INTERFACE
52 
54 bool
56 is_ancestor_of(const any* other) const
57 {
58 
59  // Preconditions:
60 
61  require(other != 0);
62 
63  // Body:
64 
65  // True if other conforms to this
66 
67  bool result = dynamic_cast<const any*>(other) != 0;
68 
69  // Postconditions:
70 
71  return result;
72 
73 }
74 
75 
77 bool
79 is_same_type(const any* other) const
80 {
81 
82  // Preconditions:
83 
84  require(other != 0);
85 
86  // Body:
87 
88  // True if other is same type as this
89 
90  bool result = this->is_ancestor_of(other) && other->is_ancestor_of(this);
91 
92  // Note: could use typeid, but this implementation
93  // doesn't require <typeinfo>
94 
95  // Postconditions:
96 
97  return result;
98 
99 }
100 
102 sheaf::any*
104 clone() const
105 {
106  any* result = 0;
107 
108  // Preconditions:
109 
110  // Body:
111 
112  // Does nothing here, since any is abstract,
113  // just establishes the postcondition.
114 
115  // Should never be called, but just in case,
116  // make it fail an assertion
117 
118  is_abstract();
119 
120  // Postconditions:
121 
122  ensure(result != 0);
123  ensure(is_same_type(result));
124 
125  // Exit
126 
127  return result;
128 }
129 
131 
134 {
135  // Preconditions:
136 
137  // Body:
138 
139  // Nothing to do in this class.
140  // Destructor defined just to make it virtual in all descendants.
141 
142  // Postconditions:
143 
144 
145  // Exit:
146 
147  return;
148 }
149 
150 
151 bool
153 invariant() const
154 {
155  // Body:
156 
157  invariance(disable_invariance_check_request_depth() >= 0);
158 
159  // Exit
160 
161  return true;
162 }
virtual bool invariant() const
Class invariant, intended to be redefined in each descendant. See below for template for invariant in...
Definition: any.cc:153
virtual bool is_ancestor_of(const any *other) const
True if other conforms to this.
Definition: any.cc:56
any()
default constructor
Definition: any.cc:30
Abstract base class with useful features for all objects.
Definition: any.h:39
virtual ~any()
Destructor.
Definition: any.cc:133
int disable_invariance_check_request_depth() const
Number of times disable_invariant_check has been called without matching call to enable_invariant_che...
Definition: any.h:106
virtual any * clone() const
Virtual constructor, makes a new instance of the same type as this.
Definition: any.cc:104
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
bool is_same_type(const any *other) const
True if other is the same type as this.
Definition: any.cc:79