24 #ifndef __AIBO_BUILD__ 28 #include <boost/algorithm/string/case_conv.hpp> 29 #include <boost/algorithm/string/split.hpp> 30 #include <boost/random/mersenne_twister.hpp> 31 #include <boost/random/uniform_int.hpp> 32 #include <boost/random/variate_generator.hpp> 33 #include <boost/shared_ptr.hpp> 36 #if defined(protected) 40 #define protected public 41 #define private public 48 #if defined(__unix__) || defined(__APPLE__) || defined(__MINGW32__) || defined(__MINGW64__) 62 #define foreach_i(_i, _iter, _container) \ 64 unsigned int _i = 0; \ 66 for (auto _iter = _container.begin(); _iter != _container.end(); ++_i, ++_iter) 96 typedef typename iterator_extractor<T>::type inner_iterator;
98 typedef typename std::iterator_traits<inner_iterator>::reference inner_reference;
102 reference (
const size_t& _i,
const inner_reference& _ref) : i(_i), ref(_ref) { }
104 inner_reference operator*()
const {
return ref; }
110 explicit iterator(inner_iterator it): _pos(0), _it(it) {}
112 reference operator*()
const {
return reference(_pos, *_it); }
114 iterator& operator++() { ++_pos; ++_it;
return *
this; }
115 iterator operator++(
int) { iterator tmp(*
this); ++*
this;
return tmp; }
117 bool operator==(iterator
const& it)
const {
return _it == it._it; }
118 bool operator!=(iterator
const& it)
const {
return !(*
this == it); }
125 explicit Indexer(T& t): _container(t) {}
127 iterator begin()
const {
return iterator(_container.begin()); }
128 iterator end()
const {
return iterator(_container.end()); }
145 template <
typename T>
149 #if defined(__AIBO_BUILD__) 152 extern jmp_buf JumpPoint;
153 extern int JumpPointValid;
154 extern void RaiseCppException();
157 #define MC_TRY_BEGIN MC_TRY_BEGIN_1(1) 158 #define MC_CATCH_BEGIN MC_CATCH_BEGIN_1(1) 159 #define MC_CATCH_END MC_CATCH_END_1(1) 161 #define MC_TRY_BEGIN_1(_num) \ 163 JumpPointValid = 1; \ 164 if (setjmp(JumpPoint) != 0) \ 166 if (JumpPointValid) \ 167 goto exception_happened##_num; \ 169 if (!JumpPointValid) \ 172 #define MC_CATCH_BEGIN_1(_num) \ 175 exception_happened##_num: 177 #define MC_CATCH_END_1(_num) \ 182 #define MC_TRY_BEGIN try { 183 #define MC_CATCH_BEGIN } catch(...) { 184 #define MC_CATCH_END } 185 #define MC_TRY_BEGIN_1(...) try { 186 #define MC_CATCH_BEGIN_1(...) } catch(...) { 187 #define MC_CATCH_END_1(...) } 192 typedef boost::variate_generator<boost::mt19937&, boost::uniform_int<> > RandomGeneratorType;
212 static void SetNewSeed(
int seed = (
int)time(
nullptr));
229 template <
typename T>
234 return (T)Generator();
247 template <
typename T>
250 return rand() % (int)(max-min+1)+min;
269 extern void MCSleep(
unsigned int msecs);
271 #if !defined(__AIBO_BUILD__) && defined(__unix__) 279 extern int MCGetSystemMemorySize();
333 extern float MCRound(
float number);
347 extern float MCScaleValue(
float value,
float min,
float max,
float new_max);
359 template <
typename T>
360 std::string
MCToStr(
const T value,
bool hex_manipulator =
false)
362 std::stringstream Tmp;
379 template <
typename T>
382 std::stringstream Tmp(str);
398 template <
typename T>
401 return value >= 0 ? value : -value;
418 extern int MCCompare(
const std::string& item,
const std::string& a,
const std::string& b);
432 template <
typename T>
435 T Diff1 =
MCAbs(a-item);
436 T Diff2 =
MCAbs(b-item);
442 return Diff1 < Diff2 ? -1 : 1;
456 extern int MCCompare(
const std::string& a,
const std::string& b);
469 template <
typename T>
475 return a > b ? -1 : 1;
488 template <
typename T>
489 const T&
MCMin(
const T& a,
const T& b)
506 template <
typename T>
507 const T&
MCMax(
const T& a,
const T& b)
526 template <
typename T>
527 const T&
MCBound(
const T& min,
const T& value,
const T& max)
543 template <
typename T>
546 if (value <
MCMin(min, max) || value >
MCMax(min, max))
552 #if defined(__GNUC__) || defined(__clang__) 553 #define likely(x) __builtin_expect(!!(x), 1) 555 #define unlikely(x) __builtin_expect(!!(x), 0) 559 #define unlikely(x) x 563 #ifndef __has_feature 564 #define __has_feature(x) 0 // Compatibility with non-clang compilers. 566 #ifndef CLANG_ANALYZER_NORETURN 567 #if __has_feature(attribute_analyzer_noreturn) 568 #define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn)) 570 #define CLANG_ANALYZER_NORETURN 575 #if defined(__MINGW32__) || defined(__MINGW64__) 576 #define MC_DIR_SEPARATOR "\\" 578 #define MC_DIR_SEPARATOR "/" 581 #if defined(__unix__) 582 #define MC_ANSI_RED "\033[1;31m" 584 #define MC_ANSI_BLACK "\033[1;30m" 586 #define MC_ANSI_PURPLE "\033[1;35m" 588 #define MC_ANSI_GREEN "\033[1;32m" 590 #define MC_ANSI_COLOR_END "\033[0;39m" 593 #define MC_ANSI_RED "" 594 #define MC_ANSI_BLACK "" 595 #define MC_ANSI_PURPLE "" 596 #define MC_ANSI_GREEN "" 597 #define MC_ANSI_COLOR_END "" 601 #define MC_UNUSED(a) (void)a; 604 #define MC_DISABLE_COPY(class_name) \ 606 class_name(const class_name&); \ 607 class_name& operator=(const class_name&); \ 611 #define MC_DISABLE_DEFAULT_CTOR(class_name) \ 626 template <
typename T>
627 std::string
MCGetClassName(T* instance =
nullptr,
const std::string& name_suffix =
"")
632 char* ClassName =
nullptr;
633 std::string ClassNameStr =
"";
634 int DemangleStatus = 0;
636 ClassName = abi::__cxa_demangle(
typeid(T).name(), 0, 0, &DemangleStatus);
639 ClassNameStr = ClassName;
640 ClassNameStr += name_suffix;
669 #ifndef __AIBO_BUILD__ 670 #if defined(__unix__) && !defined(__ANDROID__) 676 extern void MCGenerateBacktrace();
710 extern QString
MCFixPath(
const QString& path);
bool MCCreatePath(const QString &path_str)
Create a new path if it does not exist.
Random seed generation based on boost::mt19937.
int MCCompare(const std::string &item, const std::string &a, const std::string &b)
Decide whether a string is relative closer to other strings.
bool MCRangeCheck(const T &value, const T &min, const T &max)
Range check of a value.
int MCGetFileSize(const std::string &file_name)
Get file size.
double MCDoubleInfinity()
Get double infinity.
bool MCIsDoubleInfinity(const double value)
Check a value for double infinity.
Iterator magic for for loops to get the index while iterating through.
std::string MCToStr(const T value, bool hex_manipulator=false)
Convert an other type to string with std::stringstream.
const T & MCBound(const T &min, const T &value, const T &max)
Check a value bound according to a range.
Indexer< T > index(T &t)
Iterator magic for for loops to get the index while iterating through.
T MCAbs(const T &value)
Calculate absolute value.
T MCRand(const T &min, const T &max)
Get a random number generated with standard calls.
std::string MCGetClassName(T *instance=nullptr, const std::string &name_suffix="")
Get a class name.
void MCSleep(unsigned int msecs)
Wait for some milliseconds.
QString MCGetDataFile(const QString &file_name)
Search a data file.
T MCRandComplex(const T &min, const T &max)
Get a random number generated with an advanced algorithm.
bool MCIsFloatInfinity(const float value)
Check a value for float infinity.
const T & MCMax(const T &a, const T &b)
Get the maximum of two values.
#define MC_UNUSED(a)
Helper macro to avoid compiler warning about unused function parameters.
const T & MCMin(const T &a, const T &b)
Get the minimum of two values.
bool MCFileExists(const std::string &file_name)
Check whether a file exists.
QString MCFixPath(const QString &path)
Fix the (back)slashes in a path according to the platform.
static boost::mt19937 Algorithm
Random number generation algorithm.
float MCRound(float number)
Round a float number.
float MCFloatInfinity()
Get float infinity.
bool MCRemoveDirectory(const QString &dir_str)
Remove a directory with its content.
float MCScaleValue(float value, float min, float max, float new_max)
Scale a value to a new range.
int MCRandSign()
Generate a random sign.
T MCStrConvert(const std::string &str)
Convert a string to an other type with std::stringstream.
QString MCGetDataSubdirectory(const QString &subdirectory)
Get a data subdirectory.
void MCAddSearchPath(const QString &path)
Add a location to the search path for data files.