SheafSystem  0.0.0.0
id_block.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 id_block
19 
20 #include "SheafSystem/id_block.h"
21 
22 #include "SheafSystem/any.h"
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/error_message.h"
25 #include "SheafSystem/hub_index_space_handle.h"
26 #include "SheafSystem/poset_state_handle.h"
27 
28 // =============================================================================
29 // ANY FACET
30 // =============================================================================
31 
33 bool
35 is_ancestor_of(const any* other) const
36 {
37  // Preconditions:
38 
39  require(other != 0);
40 
41  // Body:
42 
43  // True if other conforms to this
44 
45  bool result = dynamic_cast<const id_block*>(other) != 0;
46 
47  // Postconditions:
48 
49  return result;
50 }
51 
55 clone() const
56 {
57  id_block* result;
58 
59  // Preconditions:
60 
61  // Body:
62 
63  result = new id_block(this->host(), id_space(), 0);
64 
65  // Postconditions:
66 
67  ensure(result != 0);
68  ensure(unexecutable("is_same_type(result)"));
69  ensure(&result->host() == &host());
70  ensure(result->id_space() == id_space());
71  ensure(result->ub() == 0);
72 
73  // Exit:
74 
75  return result;
76 }
77 
78 
82 operator=(const id_block& xother)
83 {
84 
85  // Preconditions:
86 
87 
88  // Body:
89 
90  //block<int>::operator=(xother);
92 
93  _host = xother._host;
94  _id_space = &xother._id_space->get_id_space();
95 
96  // Postconditions:
97 
98  ensure(invariant());
99 
100  // Exit
101 
102  return *this;
103 }
104 
105 
106 
108 bool
110 invariant() const
111 {
112  bool result = true;
113 
114  // Must satisfy base class invariant
115 
116  invariance(block<scoped_index>::invariant());
117 
118  // Invariances for this class:
119 
120  invariance(host().is_attached());
121 
122  // Exit
123 
124  return result;
125 }
126 
127 
128 
129 // =============================================================================
130 // ID_BLOCK FACET
131 // =============================================================================
132 
134 sheaf::id_block::
135 id_block(const id_block& xother)
136  : block<scoped_index>::block(xother)
137 {
138 
139  // Preconditions:
140 
141 
142  // Body:
143 
144  _host = xother._host;
145  _id_space = &xother._id_space->get_id_space();
146 
147  // Postconditions:
148 
149  ensure(invariant());
150 
151  // Exit:
152 
153  return;
154 }
155 
156 
160 {
161  // Preconditions:
162 
163 
164  // Body:
165 
166  if(_id_space != 0)
167  {
168  _id_space->release_id_space();
169  }
170 
171  // Postconditions:
172 
173  // Exit:
174 
175  return;
176 }
177 
179 sheaf::id_block::
180 id_block(const poset_state_handle& xhost,
181  const index_space_handle& xid_space,
182  int xub,
183  const scoped_index* xids,
184  int xct)
185  : block<scoped_index>::block(xub, xct, xids)
186 {
187  // Preconditions:
188 
189  require(xhost.is_attached());
190  require(xhost.member_id_spaces(true).contains(xid_space.index()));
191  require(xub >= 0);
192  require((0 <= xct) && (xct <= xub));
193  require((xids != 0) == (xct > 0));
194 
195  // Body:
196 
197  _host = const_cast<poset_state_handle*>(&xhost);
198  _id_space = &xid_space.get_id_space();
199 
200  // Postconditions:
201 
202  ensure(&host() == &xhost);
203  ensure(id_space() == xid_space);
204  ensure(ub() >= xub);
205  ensure(ct() == xct);
206  ensure_for_all(i, 0, xct, item(i) == xids[i]);
207 
208  // Exit:
209 
210  return;
211 }
212 
217 {
218  return *_host;
219 }
220 
224 host() const
225 {
226  return *_host;
227 }
228 
229 
233 id_space() const
234 {
235  return *_id_space;
236 }
237 
239 void
241 put_id_space(const index_space_handle& xnew_id_space, bool xauto_access)
242 {
243  // Preconditions:
244 
245  require(xauto_access || host().state_is_read_accessible());
246  require(host().member_id_spaces(xauto_access).contains(xnew_id_space.index()));
247 
248  // Body:
249 
250  if(xauto_access)
251  {
253  }
254 
255  if(xnew_id_space == (*_id_space))
256  {
257  // Nothing to do.
258  }
259  else
260  {
261  // The id space has changed. Release the old id space.
262 
263  _id_space->release_id_space();
264 
265  // Have to convert ids.
266 
267  if(xnew_id_space == _host->member_hub_id_space(false))
268  {
269  // Just translate to top ids.
270 
271  for(int i=0; i<_ct; ++i)
272  {
273  item(i).put(xnew_id_space, item(i).hub_pod());
274  }
275  }
276  else
277  {
278  // Translate to top id and then to new id space.
279 
280  for(int i=0; i<_ct; ++i)
281  {
282  item(i).put(xnew_id_space, xnew_id_space.pod(item(i).hub_pod()));
283  }
284  }
285 
286  _id_space = &xnew_id_space.get_id_space();
287  }
288 
289  if(xauto_access)
290  {
291  host().release_access();
292  }
293 
294  // Postconditions:
295 
296  ensure(id_space() == xnew_id_space);
297 
298  // Exit:
299 
300  return;
301 }
302 
304 void
306 put_member_id_space(bool xauto_access)
307 {
308  // Preconditions:
309 
310  require(xauto_access || host().state_is_read_accessible());
311 
312  // Body:
313 
314  put_id_space(host().member_hub_id_space(xauto_access), xauto_access);
315 
316  // Postconditions:
317 
318  ensure(id_space() == host().member_hub_id_space(xauto_access));
319 
320  // Exit:
321 
322  return;
323 }
324 
326 bool
329 {
330  bool result;
331 
332  // Preconditions:
333 
334  // Body:
335 
336  result = (_id_space == &_host->member_hub_id_space(true));
337 
338  // Postconditions:
339 
340  ensure(result == (&id_space() == &host().member_hub_id_space(true)));
341 
342  // Exit:
343 
344  return result;
345 }
346 
347 // =============================================================================
348 // PRIVATE MEMBER FUNCTIONS
349 // =============================================================================
350 
352 sheaf::id_block::
353 id_block()
355 {
356 
357  // Preconditions:
358 
359 
360  // Body:
361 
362  // Not implementable because we don't have a host.
363 
364  not_implemented();
365 
366  // Postconditions:
367 
368  ensure(invariant());
369  ensure(ct() == 0);
370 
371  // Exit:
372 
373  return;
374 }
375 
376 
377 
378 // =============================================================================
379 // NON-MEMBER FUNCTIONS
380 // =============================================================================
381 
382 std::ostream&
383 sheaf::
384 operator << (std::ostream& xos, const id_block& xblk)
385 {
386 
387  // Preconditions:
388 
389  require(xblk.host().state_is_read_accessible());
390 
391 
392  // Body:
393 
394  xos << "host name: " << xblk.host().name()
395  << " id space: " << xblk.id_space().name()
396  << " ids: " << static_cast<const block<scoped_index>&>(xblk);
397 
398  // Postconditions:
399 
400  // Exit:
401 
402  return xos;
403 
404 }
405 
406 
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...
virtual id_block * clone() const
Virtual constructor, makes a new instance of the same type as this.
Definition: id_block.cc:55
index_type ub() const
The upper bound on the storage array. The number of items current allocated in the storage array...
size_type ct() const
The number of items currently in use.
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_id_space(const index_space_handle &xnew_id_space, bool xauto_access)
Sets space() to xid_space and translates the ids.
Definition: id_block.cc:241
block & operator=(const block &xother)
Assignment operator.
Definition: block.impl.h:99
A client handle for a general, abstract partially order set.
id_block & operator=(const id_block &xother)
Assignment operator.
Definition: id_block.cc:82
void put_member_id_space(bool xauto_access)
Sets space() to host().member_id_space() and translates the ids.
Definition: id_block.cc:306
const index_space_family & member_id_spaces(bool xauto_access) const
Collection of member id spaces for this (const version).
An abstract handle to a space of alternate integer identifiers (aliases) for a subset of a hub set of...
virtual void release_id_space() const =0
Returns this id space handle to the handle pool.
virtual index_space_handle & get_id_space() const =0
Allocates an id space handle from the handle pool attached to the same id space.
virtual pod_type index() const =0
Index of this space.
block(index_type xub=0)
Creates an instance with ub() == xub; storage is uninitialized.
Definition: block.impl.h:42
reference_type item(index_type xindex) const
The item at index xindex.
Abstract base class with useful features for all objects.
Definition: any.h:39
std::string name() const
Name of this space.
An index within the external ("client") scope of a given id space.
Definition: scoped_index.h:116
virtual bool invariant() const
Class invariant.
Definition: id_block.cc:110
virtual std::string name() const
The name of this poset.
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 ~id_block()
Destructor.
Definition: id_block.cc:159
virtual pod_type pod(pod_type xid) const =0
The pod index in this space equivalent to xid in the hub id space.
size_type _ct
The number of items currently stored.
Definition: auto_block.h:316
const hub_index_space_handle & member_hub_id_space(bool xauto_access) const
The member hub id space.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
Definition: id_block.cc:35
virtual bool is_attached() const
True if this is attached to a state.
bool is_member_hub_id_space() const
True if space() == host().member_hub_id_space().
Definition: id_block.cc:328
SHEAF_DLL_SPEC std::ostream & operator<<(std::ostream &os, const dof_descriptor_array &p)
Insert dof_descriptor_array& p into ostream& os.
bool contains(pod_type xid) const
True, if this contains an id space with id xid.
poset_state_handle & host()
The poset that id_space resides in (mutable version).
Definition: id_block.cc:216
An auto_block with a no-initialization initialization policy.
A block of ids all in the same id space.
Definition: id_block.h:48
const index_space_handle & id_space() const
The id space the ids in this block belong to.
Definition: id_block.cc:233