Thea
Common.hpp
1 //============================================================================
2 //
3 // This file is part of the Thea toolkit.
4 //
5 // This software is distributed under the BSD license, as detailed in the
6 // accompanying LICENSE.txt file. Portions are derived from other works:
7 // their respective licenses and copyright information are reproduced in
8 // LICENSE.txt and/or in the relevant source files.
9 //
10 // Author: Siddhartha Chaudhuri
11 // First version: 2009
12 //
13 //============================================================================
14 
15 #ifndef __Thea_Common_hpp__
16 #define __Thea_Common_hpp__
17 
18 #include "Platform.hpp"
19 #include "AtomicInt32.hpp"
20 #include "CommonEnums.hpp"
21 #include "EnumClass.hpp"
22 #include "Error.hpp"
23 #include "Hash.hpp"
24 #include "Memory.hpp"
25 #include "Noncopyable.hpp"
26 #include "NumericType.hpp"
27 #include <cassert>
28 #include <cstdlib>
29 #include <iostream>
30 #include <sstream>
31 #include <string>
32 #include <typeinfo>
33 #include <vector>
34 
35 #if defined(__GNUC__) && __GNUC__ >= 3
36 # include <cxxabi.h>
37 # define THEA_HAVE_CXA_DEMANGLE
38 #endif
39 
40 // Visual Studio requires templates instantiations to be explicitly imported from DLL's to avoid conflicts like
41 // http://www.codesynthesis.com/~boris/blog/2010/01/18/dll-export-cxx-templates/
42 #ifdef _MSC_VER
43 # define THEA_EXPORT_INSTANTIATION
44 #endif
45 
47 namespace Thea {
48 
50 template <typename CondT, typename MessageT>
51 inline void
52 debugAssertM(CondT const & test, MessageT const & msg)
53 {
54 #ifdef THEA_DEBUG_BUILD
55  if (!test)
56  {
57  std::cerr << "!!! Debug-mode assertion failed !!! " << msg << std::endl;
58  std::exit(-1);
59  }
60 #endif
61 }
62 
64 template <typename CondT, typename MessageT>
65 inline void
66 alwaysAssertM(CondT const & test, MessageT const & msg)
67 {
68  if (!test)
69  {
70  std::cerr << "!!! Assertion failed !!! " << msg << std::endl;
71  std::exit(-1);
72  }
73 }
74 
75 // GCC doesn't demangle type_info::name(). New versions include a demangling function in the ABI.
76 #ifdef THEA_HAVE_CXA_DEMANGLE
77 
78 namespace CommonInternal {
79 
80 inline std::string
81 demangle(char const * class_name)
82 {
83  size_t length = 1024;
84  int status;
85 
86  char * ret = abi::__cxa_demangle(class_name, nullptr, &length, &status);
87  if (ret)
88  {
89  std::string result(ret);
90  std::free(ret);
91  return result;
92  }
93  else
94  {
95  std::free(ret);
96  return class_name;
97  }
98 }
99 
100 } // namespace CommonInternal
101 
102 #endif // THEA_HAVE_CXA_DEMANGLE
103 
105 template <typename T>
106 std::string
108 {
109 #ifdef THEA_HAVE_CXA_DEMANGLE
110  return CommonInternal::demangle(typeid(T).name());
111 #else
112  return typeid(T).name();
113 #endif
114 }
115 
117 template <typename T>
118 std::string
119 getTypeName(T const & obj)
120 {
121 #ifdef THEA_HAVE_CXA_DEMANGLE
122  return CommonInternal::demangle(typeid(obj).name());
123 #else
124  return typeid(obj).name();
125 #endif
126 }
127 
128 } // namespace Thea
129 
130 // Commonly-used but requires the stuff in Common.hpp to be declared first, so is included here.
131 #include "StringAlg.hpp"
132 
133 #endif
std::string getTypeName()
Get the name of a type, ignoring reference and cv-qualifiers.
Definition: Common.hpp:107
Root namespace for the Thea library.
void alwaysAssertM(CondT const &test, MessageT const &msg)
Check if a test condition is true, and immediately abort the program with an error code if not...
Definition: Common.hpp:66
This file must be included in every file in the project, before any other include.
void debugAssertM(CondT const &test, MessageT const &msg)
Check if a test condition is true, and immediately abort the program with an error code if not...
Definition: Common.hpp:52