Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
Type.cpp
Go to the documentation of this file.
1 //===-- serialbox/core/Type.cpp -----------------------------------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
13 //===------------------------------------------------------------------------------------------===//
14 
15 #include "serialbox/core/Type.h"
18 #include <ostream>
19 
20 namespace serialbox {
21 
22 std::ostream& operator<<(std::ostream& stream, const OpenModeKind& mode) {
23  switch(mode) {
24  case OpenModeKind::Write:
25  return (stream << std::string("Write"));
26  case OpenModeKind::Read:
27  return (stream << std::string("Read"));
28  case OpenModeKind::Append:
29  return (stream << std::string("Append"));
30  default:
31  serialbox_unreachable("invalid OpenModeKind");
32  }
33 }
34 
35 std::string TypeUtil::toString(TypeID id) {
36  std::string str;
37 
38  if(TypeUtil::isArray(id))
39  str += "std::vector<";
40 
41  switch(TypeUtil::getPrimitive(id)) {
42  case TypeID::Boolean:
43  str += "bool";
44  break;
45  case TypeID::Int32:
46  str += "int";
47  break;
48  case TypeID::Int64:
49  str += "std::int64_t";
50  break;
51  case TypeID::Float32:
52  str += "float";
53  break;
54  case TypeID::Float64:
55  str += "double";
56  break;
57  case TypeID::String:
58  str += "std::string";
59  break;
60  case TypeID::Invalid:
61  str += "invalid-type";
62  break;
63  default:
64  serialbox_unreachable("invalid TypeID for TypeUtil::toString");
65  }
66 
67  if(TypeUtil::isArray(id))
68  str += ">";
69 
70  return str;
71 }
72 
74  switch(id) {
75  case TypeID::Boolean:
76  return 1;
77  case TypeID::Float32:
78  case TypeID::Int32:
79  return 4;
80  case TypeID::Int64:
81  case TypeID::Float64:
82  return 8;
83  default:
84  throw Exception("invalid type '%s' for TypeUtil::sizeOf", TypeUtil::toString(id));
85  }
86  serialbox_unreachable("unreachable");
87 }
88 
89 bool TypeUtil::isPrimitive(TypeID id) noexcept { return (!TypeUtil::isArray(id)); }
90 
91 bool TypeUtil::isArray(TypeID id) noexcept { return ((int)id & (int)TypeID::Array); }
92 
94  if(TypeUtil::isArray(id))
95  return TypeID((int)id & ~((int)TypeID::Array));
96  return id;
97 }
98 
100  if(TypeUtil::isArray(id))
101  return id;
102  return TypeID((int)id | ((int)TypeID::Array));
103 }
104 
105 std::ostream& operator<<(std::ostream& stream, const TypeID& t) {
106  return (stream << TypeUtil::toString(t));
107 }
108 
109 } // namespace serialbox
static bool isArray(TypeID id) noexcept
Check if type is an array of types.
Definition: Type.cpp:91
static TypeID getArray(TypeID id) noexcept
Return the array type.
Definition: Type.cpp:99
static int sizeOf(TypeID id)
Get size of the type.
Definition: Type.cpp:73
Namespace of the serialbox library.
Definition: Archive.h:25
TypeID
Type-id of types recognized by serialbox.
Definition: Type.h:55
static std::string toString(TypeID id)
Convert to string.
Definition: Type.cpp:35
static bool isPrimitive(TypeID id) noexcept
Check if type is primitive.
Definition: Type.cpp:89
static TypeID getPrimitive(TypeID id) noexcept
Return the underlying primitve type.
Definition: Type.cpp:93
OpenModeKind
Policy for opening files in the Serializer and Archive.
Definition: Type.h:40
#define serialbox_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: Unreachable.h:41
std::ostream & operator<<(std::ostream &stream, const FieldID &f)
Convert FieldID to stream.
Definition: FieldID.cpp:26
Exception class which stores a human-readable error description.
Definition: Exception.h:30