forpy  2
global.h
Go to the documentation of this file.
1 /* Author: Christoph Lassner. */
2 #pragma once
3 #ifndef FORPY_GLOBAL_H_
4 #define FORPY_GLOBAL_H_
5 
6 #include <glog/logging.h>
7 #include <iomanip> // std::setprecision
8 #include <iostream>
9 #include <thread>
10 #include "./version.h"
11 #ifdef WITHGPERFTOOLS
12 #include <gperftools/profiler.h>
13 #endif
14 
15 #if !(defined NDEBUG) && defined(_MSC_VER)
16 // Solve a MSVC specific name clash between WinDef.h and <algorithm> :(
17 // See http://www.suodenjoki.dk/us/archive/2010/min-max.htm.
18 #define NOMINMAX
19 #include <Windows.h>
20 #endif
21 
22 #include <string>
23 inline bool ends_with(std::string const& value, std::string const& ending) {
24  if (ending.size() > value.size()) return false;
25  return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
26 }
27 
28 // A define that makes it easier to find forbidden calls to pure virtual
29 // functions in debugging mode.
30 #if defined(NDEBUG) || !defined(_MSC_VER)
31 #define VIRTUAL(type) = 0
32 #define VIRTUAL_VOID = 0
33 #define VIRTUAL_PTR = 0
34 #else
35 #define VIRTUAL(type) \
36  { \
37  DebugBreak(); \
38  return type(); \
39  }
40 #define VIRTUAL_VOID \
41  { DebugBreak(); }
42 #define VIRTUAL_PTR \
43  { \
44  DebugBreak(); \
45  return nullptr; \
46  }
47 #endif
48 
54 #define NOASSIGN_BUT_MOVE(TypeName) \
55  TypeName(const TypeName&) = delete; \
56  TypeName& operator=(const TypeName&) = delete; \
57  TypeName(TypeName&&) = default; \
58  TypeName& operator=(TypeName&&) = default;
59 
60 #define MOVE_ASSIGN(TypeName) \
61  TypeName(TypeName&&) = default; \
62  TypeName& operator=(TypeName&&) = default;
63 
64 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
65  TypeName(const TypeName&); \
66  TypeName& operator=(const TypeName&);
67 
68 namespace forpy {
69 
70 inline void init() {
71  google::InitGoogleLogging("");
72  FLAGS_logtostderr = 1;
73  LOG(INFO) << "forpy version " << std::setprecision(2) << std::fixed
74  << static_cast<float>(FORPY_LIB_VERSION()) / 100.f
75  << " initialized." << std::defaultfloat;
76  LOG(INFO) << "Detected support for " << std::thread::hardware_concurrency()
77  << " hardware threads.";
78 }
79 
80 #pragma clang diagnostic push
81 #pragma clang diagnostic ignored "-Wunused-variable"
82 const static bool SKLEARN_COMPAT =
83 #ifdef FORPY_SKLEARN_COMPAT
84  true;
85 #else
86  false;
87 #endif
88 #pragma clang diagnostic pop
89 
90 // This library's exception type.
91 class ForpyException : public std::exception {
92  public:
93  explicit ForpyException(const std::string& what) : whatstr(what) {}
94 
95  virtual const char* what() const throw() { return whatstr.c_str(); };
96 
97  virtual ~ForpyException() throw(){};
98 
99  private:
100  const std::string whatstr;
101 };
102 
104  public:
105  EmptyException() : ForpyException("Tried to access an empty variant."){};
106 };
107 } // namespace forpy
108 
109 // Debugging support.
110 #if defined _MSC_VER
111 #define FBREAKP DebugBreak()
112 #else
113 #include <csignal>
114 #define FBREAKP std::raise(SIGINT)
115 #endif
116 
117 #if defined RUNTIME_CHECKS
118 #define FASSERT(condition) \
119  if (!(condition)) { \
120  LOG(ERROR) << "Assertion failed!"; \
121  FBREAKP; \
122  }
123 #else
124 #define FASSERT(condition)
125 #endif
126 
127 // Library exports.
128 #if !defined(_MSC_VER)
129 #define DllExport
130 #elif __BUILD_FORPY_LIBRARY
131 #define DllExport __declspec(dllexport)
132 #else
133 #define DllExport __declspec(dllimport)
134 #endif
135 
136 #ifdef __BUILD_FORPY_LIBRARY
137 #define TemplateExport template class
138 #define TemplateFuncExport template
139 #define ExportVar
140 #else
141 #define TemplateExport extern template class
142 #define TemplateFuncExport extern template
143 #define ExportVar extern
144 #endif
145 
146 #pragma clang diagnostic push
147 #pragma clang diagnostic ignored "-Wunused-function"
148 #ifdef WITH_OPENCV
149 static bool FORPY_OPENCV_AVAILABLE() { return true; }
150 #else
151 static bool FORPY_OPENCV_AVAILABLE() { return false; }
152 #endif
153 #pragma clang diagnostic pop
154 #endif // FORPY_GLOBAL_H_
virtual ~ForpyException()
Definition: global.h:97
static bool FORPY_OPENCV_AVAILABLE()
Definition: global.h:151
static unsigned int FORPY_LIB_VERSION()
Definition: version.h:4
virtual const char * what() const
Definition: global.h:95
ForpyException(const std::string &what)
Definition: global.h:93
static const bool SKLEARN_COMPAT
Definition: global.h:82
bool ends_with(std::string const &value, std::string const &ending)
Definition: global.h:23
void init()
Definition: global.h:70
const std::string whatstr
Definition: global.h:97