SheafSystem  0.0.0.0
subposet_names_record.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 subposet_names_record
19 
20 #include "SheafSystem/subposet_names_record.h"
21 
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/std_sstream.h"
24 
25 using namespace std;
26 
27 // =============================================================================
28 // ANY FACET
29 // =============================================================================
30 
34 clone() const
35 {
36  subposet_names_record* result;
37 
38  // Preconditions:
39 
40  // Body:
41 
42  result = new subposet_names_record(*this);
43 
44  // Postconditions:
45 
46  ensure(result != 0);
47  ensure(is_same_type(result));
48 
49  // Exit:
50 
51  return result;
52 }
53 
54 
56 bool
58 invariant() const
59 {
60  bool result = true;
61 
62  // Preconditions:
63 
64  // Body:
65 
66  // Must satisfy base class invariant
67 
68  result = result && attributes_record::invariant();
69 
70  if(invariant_check())
71  {
72  // Prevent recursive calls to invariant
73 
74  disable_invariant_check();
75 
76  // Finished, turn invariant checking back on.
77 
78  enable_invariant_check();
79  }
80 
81  // Postconditions:
82 
83  // Exit
84 
85  return result;
86 }
87 
89 bool
91 is_ancestor_of(const any* other) const
92 {
93 
94  // Preconditions:
95 
96  require(other != 0);
97 
98  // Body:
99 
100  // True if other conforms to this
101 
102  bool result = dynamic_cast<const subposet_names_record*>(other) != 0;
103 
104  // Postconditions:
105 
106  return result;
107 
108 }
109 
110 // =============================================================================
111 // SUBPOSET_NAMES_RECORD FACET
112 // =============================================================================
113 
117  : attributes_record(xscaffold)
118 {
119 
120  // Preconditions:
121 
122 
123  // Body:
124 
125  // Postconditions:
126 
127  ensure(invariant());
128 
129  // Exit:
130 
131  return;
132 }
133 
137  : attributes_record(xother)
138 {
139 
140  // Preconditions:
141 
142  // Body:
143 
144  not_implemented();
145 
146  // Postconditions:
147 
148  ensure(invariant());
149 }
150 
154 {
155 
156  // Preconditions:
157 
158  // Body:
159 
160  // Nothing specific to do.
161 
162  // Postconditions:
163 
164  // Exit:
165 
166  return;
167 }
168 
169 
170 // =============================================================================
171 // PROTECTED MEMBER FUNCTIONS
172 // =============================================================================
173 
175 void
178 {
179  // Preconditions:
180 
181  require(is_internal());
182  require(scaffold().structure().state_is_read_write_accessible());
183 
184  // Body:
185 
186 // if(scaffold().structure_is_namespace())
187 // {
188 // // Delete the existing name for the namespace;
189 // // let it be reset from the file.
190 
191 // scaffold().structure().powerset().whole().delete_all_names();
192 // }
193 
194  if(scaffold().is_versioned())
195  {
196  // Version 0 is just an alias for the coarsest common refinement
197  // until version 1 is created, at which time the version 0 subposets
198  // are created independently. This poset has multiple versions and
199  // hence independent version 0 subposets; delete the aliases created
200  // by default in poset_state_handle::initialize_standard_subposets
201  // so that put_name below will succeed for version 0 subposets.
202 
203  string ver_0_whole_name(scaffold().structure().version_to_name(0));
204 
205  scaffold().structure().delete_subposet_name(ver_0_whole_name, false);
206  scaffold().structure().delete_subposet_name(ver_0_whole_name+"_jims", false);
207  }
208 
209  // Initialize the stream.
210 
211  stringstream lstream(_str_buf);
212 
213  // Skip the header line:
214 
215  lstream.ignore(numeric_limits<streamsize>::max(), '\n');
216 
217  // The subposets included in the poset when the file was written
218  // can be categorized according to 3 attributes:
219  // persistent/volatile, name/unnamed, standard/non-standard
220  //
221  // None of the volatile subposets are written to the file,
222  // and we assume all standard subposets are named, leaving 3
223  // categories:
224  //
225  // persistent, named, standard
226  // persistent, named, non-standard
227  // persistent, unnamed, non-standard
228  //
229  // All the standard subposets are created and given
230  // at least one name when the poset is created,
231  // so we have to make sure that any additional names
232  // for the standard members are set and
233  // the non-standard subposets are created and named.
234 
235  // Initialize the subposet id map.
236 
238 
239  poset_state_handle& lposet = scaffold().structure();
240  vector<subposet*>& lspv = scaffold().subposets();
241  int lsp_id;
242  bool lis_std_sp;
243  string lsp_name;
244  block<string> lall_names(4);
245 
246  while(lstream >> lsp_id)
247  {
248  // Skip the space before the name
249 
250  lstream.ignore();
251 
252  // The rest of the line is a string of tab-separated names,
253  // including possible leading or embedded white space.
254 
255  lall_names.set_ct(0);
256  getline(lstream, lsp_name, '\t');
257  while(!lsp_name.empty())
258  {
259  lall_names.push_back(lsp_name);
260  getline(lstream, lsp_name, '\t');
261  }
262 
263 #ifdef DIAGNOSTIC_OUTPUT
264  cout << "lsp_id: " << lsp_id
265  << " names: " << lall_names
266  << endl;
267 #endif
268 
269 
270  if(lall_names.ct() == 0)
271  {
272  // This subposet has no name;
273  // just create it.
274 
275  lspv[lsp_id] = new subposet(&lposet);
276  }
277  else
278  {
279  if(lposet.includes_subposet(lall_names[0], false))
280  {
281  // A subposet with this primary name already exists,
282  // get a handle and put it in the buffer.
283 
284  lspv[lsp_id] = new subposet(&lposet, lall_names[0]);
285  }
286  else
287  {
288  // A subposet with this primary name does not exist.
289 
290  if(lsp_id == 1)
291  {
292  // This is the whole subposet for the namespace poset;
293  // the subposet exists, but we've just deleted all its names.
294  // Don't create the subposet.
295 
296  lspv[lsp_id] = new subposet(&lposet, WHOLE_INDEX);
297  }
298  else
299  {
300  // Create the subposet.
301 
302  lspv[lsp_id] = new subposet(&lposet);
303  }
304 
305  // Give the subposet its primary name.
306 
307  lspv[lsp_id]->put_name(lall_names[0], true, false);
308  }
309 
310  // Put the rest of the names.
311 
312  for(int i=1; i<lall_names.ct(); ++i)
313  {
314  if(!lspv[lsp_id]->has_name(lall_names[i]))
315  {
316  lspv[lsp_id]->put_name(lall_names[i], false, false);
317  }
318  }
319  }
320 
321  // Enter the subposet in the subposet index map.
322 
323  scaffold().subposet_id_space().insert(lsp_id, lspv[lsp_id]->index());
324  }
325 
326  // Now that the subposets have been created, we can attach
327  // the resident subposet handle in the scaffold.
328 
329  scaffold().resident().attach_to_state(&lposet, "resident");
330 
331 
332  // Postconditions:
333 
334  ensure(invariant());
335  ensure(is_internal());
336 
337  // Exit
338 
339  return;
340 }
341 
343 void
346 {
347  // Preconditions:
348 
349  require(scaffold().structure().state_is_read_accessible());
350 
351  // Body:
352 
353  stringstream lstream;
354 
355  lstream << "Subposet names:" << endl;
356 
357  // Put all the names for all the subposets in the file;
358  // don't skip the standard subposets.
359  // This ensures we get any alternate names for standard subposets;
360  // will skip standard names when we read the file.
361 
362  block<string> lall_names;
363  int lsp_ub = scaffold().subposets().size();
364  for(int i=0; i<lsp_ub; i++)
365  {
366  subposet* lsp = scaffold().subposets()[i];
367 
368  lsp->all_names(lall_names);
369 
370  lstream << i << " ";
371  for(int j=0; j<lall_names.ct(); ++j)
372  {
373  lstream << lall_names[j] << '\t';
374  }
375  lstream << '\t' << endl;
376 
377  }
378 
379  // Have to copy stringstream to string
380  // because can't get C string from stringstream.
381 
382  _str_buf = lstream.str();
383 
384  put_is_internal(true);
385  put_is_external(false);
386 
387  // Postconditions:
388 
389  ensure(is_internal());
390  ensure(!is_external());
391 
392  // Exit
393 
394  return;
395 }
396 
397 
398 
void delete_subposet_name(const std::string &xname, bool xauto_access=false)
Make xname not a name for any subposet.
A client handle for a subposet.
Definition: subposet.h:86
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
poset_scaffold & scaffold()
The scaffold for the poset associated with this record (mutable version).
Definition: record.h:112
size_type ct() const
The number of items currently in use.
virtual void transfer_poset_to_internal_buffer()
Initializes the internal buffer from the member with index xmbr_id.
A client handle for a general, abstract partially order set.
virtual bool includes_subposet(pod_index_type xsubposet_hub_id, bool xauto_access=true) const
True if this poset includes subposet with hub id xsubposet_hub_id.
A wrapper/adapter for the subposet names record. Intended for transferring subposet name data between...
std::vector< subposet * > & subposets()
Buffer for subposet handles (mutable version).
STL namespace.
virtual bool invariant() const
Class invariant.
void attach_to_state(const poset_state_handle *xhost, pod_index_type xhub_id)
Attach this handle to the state with hub id xhub_id in the current version of host xhost...
virtual void all_names(block< std::string > &xresult, bool xauto_access=false) const
All the names for this.
Definition: subposet.cc:2719
Abstract base class with useful features for all objects.
Definition: any.h:39
subposet_names_record(poset_scaffold &xscaffold)
Creates an instance with type map xtype_map.
void push_back(const_reference_type item)
Insert item at the end of the items in the auto_block.
std::string _str_buf
The internal/external buffer.
bool is_internal() const
True if the internal buffer has been initialized.
void insert(pod_type xid, const scoped_index &xhub_id)
Make id xid in this id space equivalent to xhub_id in the hub id space. synonym for insert(xid...
virtual ~subposet_names_record()
Destructor.
void put_is_internal(bool xis_internal)
Sets is_internal to xis_internal.
SHEAF_DLL_SPEC void max(const vd &x0, vd_value_type &xresult, bool xauto_access)
Maximum component of x0, pre-allocated version.
Definition: vd.cc:2097
poset_state_handle & structure()
The handle for the poset being transferred. (Name chosen to void name conflict with class poset...
subposet_index_space_type & subposet_id_space()
External to internal subposet index map for powerset() (mutable version).
A poset specific collection of data converters, various buffers and other data used while transferrin...
virtual subposet_names_record * clone() const
Virtual constructor; makes a new instance of the same type as this.
bool is_external() const
True if the external buffer has been initialized.
subposet & resident()
The resident subposet for structure() (mutable version).
An abstract wrapper/adapter for attributes records. Intended for transferring data between the kernel...
void initialize_subposet_id_space()
Initializes subposet_id_space().
An auto_block with a no-initialization initialization policy.
virtual void transfer_internal_buffer_to_poset()
Initializes member with index xmbr_id from the internal buffer.
void put_is_external(bool xis_external)
Sets is_external to xis_external.