Btk
build.hpp
1 #if !defined(_BTK_BUILD_HPP_)
2 #define _BTK_BUILD_HPP_
3 //For Trigger BreakPoint
4 #include <SDL2/SDL_assert.h>
5 #include <SDL2/SDL_log.h>
6 #include <typeinfo>
7 #include <utility>
8 #include <cstdarg>
9 #include <cstring>
10 #include <string>
11 //Is sourse
12 #define _BTK_SOURCE
13 #include <Btk/defs.hpp>
14 
15 #if defined(_MSC_VER)
16  //MSVC
17  #define BTK_FUNCTION __FUNCSIG__
18 #elif defined(__GNUC__)
19  //GCC
20  #include <strings.h>
21  #include <cxxabi.h>
22  #define BTK_FUNCTION __PRETTY_FUNCTION__
23 #else
24  #define BTK_FUNCTION SDL_FUNCTION
25 #endif
26 //Debug Info
27 #ifndef NDEBUG
28  #define BTK_LOGINFO(...) SDL_Log(__VA_ARGS__)
29  #define BTK_LOGWARN(...) SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,__VA_ARGS__)
30 #else
31  #define BTK_LOGINFO(...)
32  #define BTK_LOGWARN(...)
33 #endif
34 //Assert
35 #ifndef NDEBUG
36  #define BTK_ASSERT(EXP) if(not(EXP)){\
37  _Btk_ReportFailure(__FILE__,__LINE__,BTK_FUNCTION,#EXP);\
38  SDL_TriggerBreakpoint();\
39  }
40 #else
41  #define BTK_ASSERT(EXP)
42 #endif
43 
44 
45 
46 #ifndef NDEBUG
47 
51 extern "C" void BTKAPI _Btk_Backtrace();
56 extern "C" void BTKAPI _Btk_ReportFailure(
57  const char *file,
58  int line,
59  const char *fn,
60  const char *exp
61 );
62 #else
63 extern "C" inline void _Btk_Backtrace(){};
64 extern "C" inline void _Btk_ReportFailure(
65  const char *file,
66  int line,
67  const char *fn,
68  const char *exp
69 ){};
70 #endif
71 
72 namespace Btk{
73  //Cast event for debugging
74  template<class T,class U>
75  T event_cast(U &&u){
76  #ifndef NDEBUG
77  return dynamic_cast<T>(std::forward<U>(u));
78  #else
79  return static_cast<T>(std::forward<U>(u));
80  #endif
81  }
82  inline int vscprintf(const char *fmt,va_list varg){
83  #ifdef _WIN32
84  return _vscprintf(fmt,varg);
85  #else
86  return vsnprintf(nullptr,0,fmt,varg);
87  #endif
88  };
96  inline std::string get_typename(const std::type_info &info){
97  #ifdef __GNUC__
98  char *ret = abi::__cxa_demangle(info.name(),nullptr,nullptr,nullptr);
99  if(ret == nullptr){
100  //failed to demangle the name
101  return info.name();
102  }
103  else{
104  std::string name(ret);
105  free(ret);
106  return name;
107  }
108  #else
109  return info.name();
110  #endif
111  }
112  template<class T>
113  inline std::string get_typename(const T *ptr){
114  return get_typename(typeid(*ptr));
115  }
123  inline std::string cformat(const char *fmt,...){
124  int strsize;
125 
126  //Get the size of the string
127  va_list varg;
128  va_start(varg,fmt);
129  #ifdef _WIN32
130  strsize = _vscprintf(fmt,varg);
131  #else
132  strsize = vsnprintf(nullptr,0,fmt,varg);
133  #endif
134  va_end(varg);
135 
136  std::string str;
137  str.resize(strsize);
138 
139  //start formatting
140  va_start(varg,fmt);
141  vsprintf(&str[0],fmt,varg);
142  va_end(varg);
143 
144  return str;
145  }
153  inline void cformat(std::string &str,const char *fmt,...){
154  int strsize;
155  //Get the size of the string
156  va_list varg;
157  va_start(varg,fmt);
158  #ifdef _WIN32
159  strsize = _vscprintf(fmt,varg);
160  #else
161  strsize = vsnprintf(nullptr,0,fmt,varg);
162  #endif
163  va_end(varg);
164  //Get the '\0'
165  size_t length = str.length();
166 
167  str.resize(strsize + str.size());
168  char *end = &str[length];
169  //start formatting
170  va_start(varg,fmt);
171  vsprintf(end,fmt,varg);
172  va_end(varg);
173  }
174 };
175 #endif // _BTK_BUILD_HPP_
This header include many useful containers.
Definition: async.hpp:7
std::string cformat(const char *fmt,...)
Using c-syle formatting.
Definition: build.hpp:123
std::string get_typename(const std::type_info &info)
Get the typename of a type.
Definition: build.hpp:96