Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
Utility.h
Go to the documentation of this file.
1 //===-- serialbox/core/frontend/stella/Utility.h ------------------------------------*- 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 //
15 //===------------------------------------------------------------------------------------------===//
16 
17 #ifndef SERIALBOX_CORE_FRONTEND_STELLA_UTILITY_H
18 #define SERIALBOX_CORE_FRONTEND_STELLA_UTILITY_H
19 
22 #include "serialbox/core/Type.h"
24 #include <boost/format.hpp>
25 #include <boost/make_shared.hpp>
26 #include <boost/shared_ptr.hpp>
27 #include <memory>
28 #include <string>
29 
30 namespace serialbox {
31 
32 namespace stella {
33 
34 namespace internal {
35 
37 template <typename... Args>
38 SERIALBOX_ATTRIBUTE_NORETURN void throwSerializationException(const char* fmt, Args&&... args) {
39  boost::format f(fmt);
40 
41  int unroll[]{0, (f % std::forward<Args>(args), 0)...};
42  static_cast<void>(unroll);
43 
44  SerializationException exception;
45  exception.Init(boost::str(f));
46  throw exception;
47 }
48 
49 template <class StringType>
50 inline TypeID TypeNameToTypeID(StringType&& name) {
51  if(name == "bool")
52  return TypeID::Boolean;
53  else if(name == "int")
54  return TypeID::Int32;
55  else if(name == "float")
56  return TypeID::Float32;
57  else if(name == "double")
58  return TypeID::Float64;
59  throw Exception("unsupported type: %s", name);
60 }
61 
62 inline std::string TypeIDToTypeName(TypeID typeID) {
63  switch(typeID) {
64  case TypeID::Boolean:
65  return "bool";
66  case TypeID::Int32:
67  return "int";
68  case TypeID::Float32:
69  return "float";
70  case TypeID::Float64:
71  return "double";
72  default:
73  throw Exception("unsupported type: %s", TypeUtil::toString(typeID));
74  }
75 }
76 
77 template <typename T>
78 boost::shared_ptr<T> make_shared_ptr(std::shared_ptr<T>& ptr) {
79  return boost::shared_ptr<T>(ptr.get(), [ptr](T*) mutable { ptr.reset(); });
80 }
81 
82 template <typename T>
83 std::shared_ptr<T> make_shared_ptr(boost::shared_ptr<T>& ptr) {
84  return std::shared_ptr<T>(ptr.get(), [ptr](T*) mutable { ptr.reset(); });
85 }
86 
87 } // namespace internal
88 
89 } // namespace stella
90 
91 } // namespace serialbox
92 
93 #endif
Namespace of the STELLA frontend.
Definition: ForwardDecl.h:27
Namespace of the serialbox library.
Definition: Archive.h:25
TypeID
Type-id of types recognized by serialbox.
Definition: Type.h:55
serialbox::Exception exception
Exception class which stores a human-readable error description.
Definition: Exception.h:33
static std::string toString(TypeID id)
Convert to string.
Definition: Type.cpp:35
#define SERIALBOX_ATTRIBUTE_NORETURN
Mark a method as "no return".
Definition: Compiler.h:102