common.h
Go to the documentation of this file.
00001 // Useful utility functions that don't belong anywhere else
00002 #ifndef MEGATREE_COMMON_H
00003 #define MEGATREE_COMMON_H
00004 
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <boost/date_time/posix_time/posix_time_types.hpp>
00008 
00009 namespace megatree {
00010 
00011 int parseNumberSuffixed(const char* s);
00012 
00013   class Tictoc
00014   {
00015   public:
00016     Tictoc() : started(boost::posix_time::microsec_clock::universal_time()) {}
00017 
00018     float tic()
00019     {
00020       boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
00021       long m = (now - started).total_milliseconds();
00022       started = now;
00023       return float(m) / 1000.0f;
00024     }
00025 
00026     float toc()
00027     {
00028       boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
00029       long m = (now - started).total_milliseconds();
00030       return float(m) / 1000.0f;
00031     }
00032     boost::posix_time::ptime started;
00033   };
00034 
00035   class PausingTimer
00036   {
00037   public:
00038     PausingTimer() : started(boost::posix_time::microsec_clock::universal_time()), elapsed(0,0,0,0) {}
00039 
00040     void pause()
00041     {
00042       boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
00043       elapsed += (now - started);
00044     }
00045 
00046     void start()  // Continues timing
00047     {
00048       started = boost::posix_time::microsec_clock::universal_time();
00049     }
00050 
00051     float getElapsed() const
00052     {
00053       return elapsed.total_milliseconds() / 1000.0f;
00054     }
00055     void reset()
00056     {
00057       elapsed = boost::posix_time::time_duration(0,0,0,0);
00058     }
00059 
00060     boost::posix_time::ptime started;
00061     boost::posix_time::time_duration elapsed;
00062   };
00063 
00064 }
00065 
00066 class AssertionException : std::exception
00067 {
00068 public:
00069   AssertionException(const char* _file, long _line, const char* _pretty_function, const char* _message)
00070     : file(_file), line(_line), pretty_function(_pretty_function), message(_message)
00071   {
00072     fprintf(stderr, "%s:%ld  %s: %s\n", file, line, pretty_function, message);
00073   }
00074   AssertionException(const AssertionException &o)
00075     : file(o.file), line(o.line), message(o.message)  {}
00076   virtual ~AssertionException() throw() {}
00077 
00078   virtual const char* what() const throw()
00079   {
00080     try {
00081       std::stringstream ss;
00082       ss << file << ":" << line << "  " << pretty_function << ": " << message;
00083       __internal = ss.str();
00084       return __internal.c_str();
00085     }
00086     catch (...)
00087     {
00088       return message;
00089     }
00090   }
00091 
00092   const char* file;
00093   long line;
00094   const char* pretty_function;
00095   const char* message;
00096   mutable std::string __internal;
00097 };
00098 
00099 #define Assert(cond)                                                   \
00100   ((cond)                                                              \
00101    ? (void)(0)                                                         \
00102    : throw AssertionException(__FILE__, __LINE__, __PRETTY_FUNCTION__, \
00103                               "Assertion failed: " #cond))
00104 
00105 #endif


megatree_core
Author(s): Stuart Glaser
autogenerated on Thu Nov 28 2013 11:30:23