7 #ifndef NUKLEI_COMMON_H
8 #define NUKLEI_COMMON_H
17 #include <boost/filesystem.hpp>
18 #include <boost/config.hpp>
33 #define NUKLEI_UNIQUE_PTR_IMPL 2
35 #if NUKLEI_UNIQUE_PTR_IMPL == 0
41 # ifdef BOOST_NO_CXX11_SMART_PTR
42 # define NUKLEI_UNIQUE_PTR NUKLEI_UNIQUE_PTR
43 # define NUKLEI_MOVE(PTR) (PTR)
44 # define NUKLEI_RELEASE(PTR) (PTR)
46 # define NUKLEI_UNIQUE_PTR std::unique_ptr
47 # define NUKLEI_MOVE(PTR) std::move(PTR)
48 # define NUKLEI_RELEASE(PTR) (PTR).release()
50 #elif NUKLEI_UNIQUE_PTR_IMPL == 1
51 # define NUKLEI_UNIQUE_PTR NUKLEI_UNIQUE_PTR
52 # define NUKLEI_MOVE(PTR) (PTR)
53 # define NUKLEI_RELEASE(PTR) (PTR)
54 #elif NUKLEI_UNIQUE_PTR_IMPL == 2
55 # ifdef BOOST_NO_CXX11_SMART_PTR
56 # error When NUKLEI_UNIQUE_PTR_IMPL==2, Boost support for C++11 is required
58 # define NUKLEI_UNIQUE_PTR std::unique_ptr
59 # define NUKLEI_MOVE(PTR) std::move(PTR)
60 # define NUKLEI_RELEASE(PTR) (PTR).release()
62 # error NUKLEI_UNIQUE_PTR_IMPL must be 0, 1 or 2
76 # define NUKLEI_NVP(x) #x << "=" << x
79 # define NUKLEI_HERE() \
80 ((boost::filesystem::path(__FILE__).parent_path().filename().string() + "/" + \
81 boost::filesystem::path(__FILE__).filename().string() + ":" + \
82 nuklei::stringify(__LINE__)))
94 # define NUKLEI_THROW(x) \
96 std::ostringstream oss; \
98 throw nuklei::Error(oss.str()); \
113 # define NUKLEI_ASSERT(expression) \
116 throw nuklei::Error \
118 ": failed to assert `" + #expression + "'"); \
122 # define NUKLEI_DEBUG_ASSERT(expression) ((void)0)
124 # define NUKLEI_DEBUG_ASSERT NUKLEI_ASSERT
128 # define NUKLEI_FAST_ASSERT(expression) \
132 # define NUKLEI_FAST_DEBUG_ASSERT(expression) ((void)0)
134 # define NUKLEI_FAST_DEBUG_ASSERT NUKLEI_FAST_ASSERT
137 # define NUKLEI_ASSERT_AFE(e1, e2) \
140 if (!(nuklei::afe(e1, e2, tol))) \
141 NUKLEI_THROW(nuklei::stringify(e1, 20, 0) << " != " << nuklei::stringify(e2, 20, 0) << " (TOL=" << tol << ")"); \
144 # define NUKLEI_ASSERT_AFE_TOL(e1, e2, tol) \
146 if (!(nuklei::afe(e1, e2, tol))) \
147 NUKLEI_THROW(nuklei::stringify(e1, 20, 0) << " != " << nuklei::stringify(e2, 20, 0) << " (TOL=" << tol << ")"); \
151 # define NUKLEI_TRACE_BEGIN() {}
152 # define NUKLEI_TRACE_END() {}
154 # define NUKLEI_TRACE_BEGIN() try {
155 # define NUKLEI_TRACE_END() \
156 } catch (nuklei::Error &e) { \
157 e.addTrace(__PRETTY_FUNCTION__, NUKLEI_HERE()); \
160 catch (std::exception &e) { \
161 throw nuklei::Error(e, __PRETTY_FUNCTION__, NUKLEI_HERE()); \
176 # define NUKLEI_LBLACK "\033[0;30m"
177 # define NUKLEI_LRED "\033[0;31m"
178 # define NUKLEI_LGREEN "\033[0;32m"
179 # define NUKLEI_LBROWN "\033[0;33m"
180 # define NUKLEI_LBLUE "\033[0;34m"
181 # define NUKLEI_LPURPLE "\033[0;35m"
182 # define NUKLEI_LCYAN "\033[0;36m"
183 # define NUKLEI_LWHITE "\033[0;37m"
184 # define NUKLEI_DBLACK "\033[1;30m"
185 # define NUKLEI_DRED "\033[1;31m"
186 # define NUKLEI_DGREEN "\033[1;32m"
187 # define NUKLEI_DBROWN "\033[1;33m"
188 # define NUKLEI_DBLUE "\033[1;34m"
189 # define NUKLEI_DPURPLE "\033[1;35m"
190 # define NUKLEI_DCYAN "\033[1;36m"
191 # define NUKLEI_DWHITE "\033[1;37m"
193 # define NUKLEI_NOCOLOR "\033[0m"
202 inline std::string
stringify(
const T& x,
int precision = -1,
int width = 0);
213 explicit Error(
const std::string& msg) : msg_(msg), count_(0)
218 explicit Error(
const std::exception& error,
219 const std::string&
function,
220 const std::string& file,
221 const std::string& msg =
"") : count_(0)
224 type_ =
typeid(error).name();
226 addTrace(
function, file, msg);
233 if (INTERACTIVE_SHELL)
235 color_start_ = NUKLEI_LRED;
236 color_stop_ = NUKLEI_NOCOLOR;
241 void addTrace(
const std::string&
function,
242 const std::string& file,
243 const std::string& msg =
"")
245 trace_ += Log::breakLines(
" " + color_start_ +
stringify(count_++) +
246 ":\n " + color_stop_ +
function +
247 color_start_ +
" @[" + color_stop_ + file +
248 color_start_ +
"] [" +
249 color_stop_ + msg + color_start_ +
"]" +
254 void compileWhatString()
256 if (type_.empty()) type_ =
typeid(*this).name();
258 color_start_ +
"[Exception Trace]" + color_stop_ +
259 Log::breakLines(color_start_ +
"Type: " + color_stop_ + type_ +
260 color_start_ +
"\nMessage:\n " + color_stop_ + msg_ +
261 color_start_ +
"\nTrace:" + color_stop_) + trace_;
266 const char* what()
const throw()
268 return what_.c_str();
275 std::string color_start_;
276 std::string color_stop_;
292 inline std::istream& operator>>(std::istream &in, Vector3 &l);
293 inline std::istream& operator>>(std::istream &in, Quaternion &q);
294 inline std::ostream& operator<<(std::ostream &out,
const Quaternion &q);
295 inline std::ostream& operator<<(std::ostream &out,
const Matrix3 &m);
296 inline std::istream& operator>>(std::istream &in, Matrix3 &m);
300 template<
typename ForwardIterator>
302 int precision = -1,
int width = 0)
304 std::ostringstream o;
308 if (precision != -1) o.precision(precision);
309 char fill = o.fill();
311 if (width != 0) o.width(width);
313 ForwardIterator i = first;
317 (std::string(
"stringify(") +
typeid(*i).name() +
")");
319 for ( ; i != last; i++)
320 if (!(o <<
" " << *i))
322 (std::string(
"stringify(") +
typeid(*i).name() +
")");
333 inline std::string
stringify(
const T& x,
int precision,
int width)
335 std::ostringstream o;
336 if (precision != -1) o.precision(precision);
337 char fill = o.fill();
339 if (width != 0) o.width(width);
342 +
typeid(x).name() +
")");
350 bool failIfLeftoverChars =
true)
353 std::istringstream i(s);
355 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
360 inline std::string& cleanLine(std::string& line)
362 if(!line.empty() && *line.rbegin() ==
'\r')
363 line.erase( line.length()-1, 1);
367 inline void copy_file(
const std::string& input,
const std::string& output)
369 std::ifstream src(input.c_str(), std::ios::binary);
370 std::ofstream dst(output.c_str(), std::ios::binary);