SheafSystem  0.0.0.0
primitive_attributes.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 //
19 // Implementation for class primitive_attributes
20 
21 #include "SheafSystem/primitive_attributes.h"
22 
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/block.impl.h"
25 #include "SheafSystem/namespace_relative_member_index.h"
26 #include "SheafSystem/namespace_relative_subposet_index.h"
27 #include "SheafSystem/primitive_traits.h"
28 #include "SheafSystem/std_map.h"
29 #include "SheafSystem/std_sstream.h"
30 
31 using namespace sheaf;
32 using namespace std;
33 
34 namespace
35 {
39 typedef map<string, sheaf::primitive_type> map_type;
40 
44 map_type& make_name_to_id_map()
45 {
46  // Preconditions:
47 
48 
49  // Body:
50 
51  static map_type result;
52 
53  if(result.empty())
54  {
55  for(sheaf::primitive_type i=sheaf::PRIMITIVE_TYPE_BEGIN; i<sheaf::PRIMITIVE_TYPE_END; ++i)
56  {
58  result[lprim.name()] = i;
59  stringstream lstrm(lprim.aliases());
60  string lalias;
61  while(lstrm >> lalias)
62  {
63  result[lalias] = i;
64  lalias.clear();
65  }
66  }
67  }
68 
69 #ifdef DIAGNOSTIC_OUTPUT
70  cout << "name to id map: " << endl;
71  for(map_type::const_iterator litr = result.begin(); litr != result.end(); ++litr)
72  {
73  cout << litr->first << " " << litr->second << endl;
74  }
75 #endif
76 
77  // Postconditions:
78 
79  ensure(!result.empty());
80 
81  // Exit:
82 
83  return result;
84 };
85 }
86 
87 // ===========================================================
88 // STATIC FUNCTIONS
89 // ===========================================================
90 
95 {
96  // Preconditions:
97 
98  // require(is_primitive_index(xid));
99 
100  // Body:
101 
102  xid = is_primitive_index(xid) ? xid : PRIMITIVE_TYPE_END;
103 
104  static const block<primitive_attributes>& lprototypes = make_prototypes();
105  primitive_attributes& result = lprototypes[xid];
106 
107  // Postconditions:
108 
109  ensure(static_cast<pod_index_type>(result.id()) ==
110  (is_primitive_index(xid) ? xid : static_cast<pod_index_type>(NOT_A_PRIMITIVE_TYPE)));
111 
112  // Exit:
113 
114  return result;
115 }
116 
120 id(const std::string& xtype_name)
121 {
122  // Preconditions:
123 
124  require(!xtype_name.empty());
125 
126  // Body:
127 
128  static const map_type& lmap = make_name_to_id_map();
129 
130  primitive_type result = NOT_A_PRIMITIVE_TYPE;
131 
132  map_type::const_iterator litr = lmap.find(xtype_name);
133  if(litr != lmap.end())
134  {
135  result = litr->second;
136  }
137 
138  // Postconditions:
139 
140  // Exit:
141 
142  return result;
143 }
144 
145 
146 
147 // ===========================================================
148 // CONSTRUCTORS
149 // ===========================================================
150 
154 {
157  _alignment = primitive_traits<void>::alignment();
158  _name = &primitive_traits<void>::name();
159  _aliases = &primitive_traits<void>::aliases();
160  _hdf_type_name = &primitive_traits<void>::hdf_type_name();
161  _hdf_type = primitive_traits<void>::hdf_type();
162 }
163 
167 {
168  (*this) = x;
169 }
170 
174 {
175  // Preconditions:
176 
177  require(is_primitive_index(xid));
178 
179  // Body:
180 
181  (*this) = prototype(xid);
182 
183  // Postconditions:
184 
185  ensure(id() == xid);
186 
187  // Exit:
188 
189  return;
190 }
191 
194 primitive_attributes(const void* x)
195 {
196  // Preconditions:
197 
198  // Body;
199 
202  _alignment = primitive_traits<void*>::alignment();
204  _aliases = &primitive_traits<void*>::aliases();
205  _hdf_type_name = &primitive_traits<void*>::hdf_type_name();
206  _hdf_type = primitive_traits<void*>::hdf_type();
207 
208  // Postconditions:
209 
210  ensure(id() == primitive_traits<void*>::id());
211 
212  // Exit:
213 
214  return;
215 }
216 
219 primitive_attributes(const char* x)
220 {
221  // Preconditions:
222 
223  // Body:
224 
227  _alignment = primitive_traits<char*>::alignment();
229  _aliases = &primitive_traits<char*>::aliases();
230  _hdf_type_name = &primitive_traits<char*>::hdf_type_name();
231  _hdf_type = primitive_traits<char*>::hdf_type();
232 
233  // Postconditions:
234 
235  ensure(id() == primitive_traits<char*>::id());
236 
237  // Exit:
238 
239  return;
240 }
241 
243 template <typename T>
246 {
247  // Preconditions:
248 
249  // Body:
250 
251  _id = primitive_traits<T>::id();
252  _size = primitive_traits<T>::size();
253  _alignment = primitive_traits<T>::alignment();
254  _name = &primitive_traits<T>::name();
255  _aliases = &primitive_traits<T>::aliases();
256  _hdf_type_name = &primitive_traits<T>::hdf_type_name();
257  _hdf_type = primitive_traits<T>::hdf_type();
258 
259  // Postconditions:
260 
261  ensure(id() == primitive_traits<T>::id());
262 
263  // Exit:
264 
265  return;
266 }
267 
268 // Template specializations
269 namespace sheaf
270 {
271 
273 template <>
275 primitive_attributes(const std::string& x)
276 {
277  // Preconditions:
278 
279  // Body:
280 
281  (*this) = prototype(C_STRING);
282 
283  // Postconditions:
284 
285  ensure(id() == primitive_traits<char *>::id());
286 
287  // Exit:
288 
289  return;
290 }
291 
293 template <>
296 {
297  // Preconditions:
298 
299  // Body:
300 
301  (*this) = prototype(POD_INDEX_TYPE);
302 
303  // Postconditions:
304 
305  ensure(id() == primitive_traits<pod_index_type>::id());
306 
307  // Exit:
308 
309  return;
310 }
311 
313 template <>
316 {
317  // Preconditions:
318 
319  // Body:
320 
321  (*this) = prototype(NAMESPACE_RELATIVE_MEMBER_INDEX);
322 
323  // Postconditions:
324 
326 
327  // Exit:
328 
329  return;
330 }
331 
333 template <>
336 {
337  // Preconditions:
338 
339  // Body:
340 
341  (*this) = prototype(NAMESPACE_RELATIVE_SUBPOSET_INDEX);
342 
343  // Postconditions:
344 
346 
347  // Exit:
348 
349  return;
350 }
351 } // namespace sheaf
352 
353 // Explicit instantiations.
354 
355 #ifndef DOXYGEN_SKIP_PRIMITIVE_INSTANTIATIONS
358 
359 template sheaf::primitive_attributes::primitive_attributes(const signed char& x);
360 template sheaf::primitive_attributes::primitive_attributes(const short int& x);
362 template sheaf::primitive_attributes::primitive_attributes(const long int& x);
363 template sheaf::primitive_attributes::primitive_attributes(const long long int& x);
364 
365 template sheaf::primitive_attributes::primitive_attributes(const unsigned char& x);
366 template sheaf::primitive_attributes::primitive_attributes(const unsigned short int& x);
367 template sheaf::primitive_attributes::primitive_attributes(const unsigned int& x);
368 template sheaf::primitive_attributes::primitive_attributes(const unsigned long int& x);
369 template sheaf::primitive_attributes::primitive_attributes(const unsigned long long int& x);
370 
372 template sheaf::primitive_attributes::primitive_attributes(const double& x);
373 template sheaf::primitive_attributes::primitive_attributes(const long double& x);
374 
377 #endif // ifndef DOXYGEN_SKIP_PRIMITIVE_INSTANTIATIONS
378 
379 #ifndef DOXYGEN_SHOULD_SKIP_THIS
380 // $$SCRIBBLE doxygen - Warning: no matching class member found for
381 template
383 
384 template
386 #endif // DOXYGEN_SHOULD_SKIP_THIS
387 
392 {
393  // Preconditions:
394 
395 
396  // Body:
397 
398  if(&x != this)
399  {
400  _id = x._id;
401  _size = x._size;
402  _alignment = x._alignment;
403  _name = x._name;
404  _aliases = x._aliases;
405  _hdf_type_name = x._hdf_type_name;
406  _hdf_type = x._hdf_type;
407  }
408 
409  // Postconditions:
410 
411 
412  // Exit:
413 
414  return *this;
415 }
416 
417 
420 {
421  // Nothing to do.
422  return;
423 }
424 
425 sheaf::primitive_attributes::
426 operator primitive_descriptor() const
427 {
428  primitive_descriptor result;
429 
430  result.size = _size;
431  result.alignment = _alignment;
432  result.index = _id;
433 
434  return result;
435 }
436 
437 // ===========================================================
438 // PRIVATE MEMBER FUNCTIONS
439 // ===========================================================
440 
443 sheaf::primitive_attributes::
444 make_prototypes()
445 {
446  static block<primitive_attributes> result(PRIMITIVE_TYPE_END+1);
447 
450 
451  if(result.ct() == 0)
452  {
453  result[0] = primitive_attributes();
454  result[1] = primitive_attributes();
455  result[BOOL] = primitive_attributes(static_cast<bool>(0));
456  result[CHAR] = primitive_attributes(static_cast<char>(0));
457 
458  result[SIGNED_CHAR] = primitive_attributes(static_cast<signed char>(0));
459  result[SHORT_INT] = primitive_attributes(static_cast<short int>(0));
460  result[INT] = primitive_attributes(static_cast<int>(0));
461  result[LONG_INT] = primitive_attributes(static_cast<long int>(0));
462  result[LONG_LONG_INT] = primitive_attributes(static_cast<long long int>(0));
463 
464  result[UNSIGNED_CHAR] = primitive_attributes(static_cast<unsigned char>(0));
465  result[UNSIGNED_SHORT_INT] = primitive_attributes(static_cast<unsigned short int>(0));
466  result[UNSIGNED_INT] = primitive_attributes(static_cast<unsigned int>(0));
467  result[UNSIGNED_LONG_INT] = primitive_attributes(static_cast<unsigned long int>(0));
468  result[UNSIGNED_LONG_LONG_INT] = primitive_attributes(static_cast<unsigned long long int>(0));
469 
470  result[FLOAT] = primitive_attributes(static_cast<float>(0));
471  result[DOUBLE] = primitive_attributes(static_cast<double>(0));
472  result[LONG_DOUBLE] = primitive_attributes(static_cast<long double>(0));
473 
474  result[VOID_STAR] = primitive_attributes(static_cast<void_star>(0));
475  result[C_STRING] = primitive_attributes(static_cast<c_string>(0));
476 
477  result[NAMESPACE_RELATIVE_MEMBER_INDEX] = primitive_attributes(x);
478 
479  result[NAMESPACE_RELATIVE_SUBPOSET_INDEX] = primitive_attributes(y);
480  result[PRIMITIVE_TYPE_END] = primitive_attributes();
481 
482  result.set_ct(PRIMITIVE_TYPE_END+1);
483  }
484 
485  return result;
486 }
487 
488 // ===========================================================
489 // NON-MEMBER FUNCTIONS
490 // ===========================================================
491 
Index for identifying a poset member relative to a given name space.
primitive_attributes()
Default constructor.
static size_type size()
The size of T in bytes.
size_type ct() const
The number of items currently in use.
STL namespace.
primitive_attributes & operator=(const primitive_attributes &)
Copy assignment operator.
static std::string name()
The name of T.
static std::string hdf_type_name()
The name of T in the hdf file.
static int hdf_type()
The hdf type used to represent T in the hdf file.
primitive_type
Type ids for sheaf primitives.
static const std::string & aliases(pod_index_type xid)
Other names for the primitive type with id xid.
An index within the external ("client") scope of a given id space.
Definition: scoped_index.h:116
static primitive_type id(const std::string &xname)
Type id of the primitive type with name or alias xname.
POD type associated with namespace_relative_subposet_index.
Definition: pod_types.h:111
POD type for namespace_relative_member_index.
Definition: pod_types.h:79
Abstract object wrapper for an instance of a primitive type.
SHEAF_DLL_SPEC bool is_primitive_index(pod_index_type xindex)
True if xindex is a valid primitive index.
~primitive_attributes()
Destructor; not virtual, this can not be a base class.
Traits for primitive type T.
Index for identifying a subposet relative to a given name space.
static primitive_type id()
The primitive type id for T.
static std::string aliases()
Other names for T.
static const primitive_attributes & prototype(pod_index_type xid)
The prototype for the primitive value of the type associated with xid.
static const std::string & name(pod_index_type xid)
The name of primitive type with id xid.
static size_type alignment()
The alignment for T in bytes.
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
primitive_type id() const
The id for the primitive type associated with this.
Namespace for the sheaves component of the sheaf system.
An auto_block with a no-initialization initialization policy.