Global.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * Global.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. /* ---------------------------------------------------------------------------- */
  12. /* Compiler detection */
  13. /* ---------------------------------------------------------------------------- */
  14. // Fixed width bool data type is important for serialization
  15. static_assert(sizeof(bool) == 1, "Bool needs to be 1 byte in size.");
  16. #if defined _M_X64 && defined _WIN32 //Win64 -> cannot load 32-bit DLLs for video handling
  17. #define DISABLE_VIDEO
  18. #endif
  19. #ifdef __GNUC__
  20. #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__ * 10 + __GNUC_PATCHLEVEL__)
  21. #endif
  22. #if defined(__GNUC__) && (GCC_VERSION == 470 || GCC_VERSION == 471)
  23. #error This GCC version has buggy std::array::at version and should not be used. Please update to 4.7.2 or use 4.6.x.
  24. #endif
  25. /* ---------------------------------------------------------------------------- */
  26. /* Guarantee compiler features */
  27. /* ---------------------------------------------------------------------------- */
  28. //defining available c++11 features
  29. //initialization lists - only gcc-4.4 or later
  30. #if defined(__clang__) || (defined(__GNUC__) && (GCC_VERSION >= 440))
  31. #define CPP11_USE_INITIALIZERS_LIST
  32. #endif
  33. //nullptr - only msvc and gcc-4.6 or later, othervice define it as NULL
  34. #if !defined(_MSC_VER) && !(defined(__GNUC__) && (GCC_VERSION >= 460)) && !(defined(__clang__))
  35. #define nullptr NULL
  36. #endif
  37. //override keyword - only msvc and gcc-4.7 or later.
  38. #if !defined(_MSC_VER) && !(defined(__GNUC__) && (GCC_VERSION >= 470)) && !(defined(__clang__))
  39. #define override
  40. #endif
  41. //workaround to support existing code
  42. #define OVERRIDE override
  43. /* ---------------------------------------------------------------------------- */
  44. /* Suppress some compiler warnings */
  45. /* ---------------------------------------------------------------------------- */
  46. #ifdef _MSC_VER
  47. #pragma warning (disable : 4800 ) /* disable conversion to bool warning -- I think it's intended in all places */
  48. #endif
  49. /* ---------------------------------------------------------------------------- */
  50. /* Commonly used C++, Boost headers */
  51. /* ---------------------------------------------------------------------------- */
  52. #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
  53. #include <cstdio>
  54. #include <stdio.h>
  55. #ifdef _WIN32
  56. #include <tchar.h>
  57. #else
  58. #include "tchar_amigaos4.h"
  59. #endif
  60. #include <algorithm>
  61. #include <array>
  62. #include <cassert>
  63. #include <climits>
  64. #include <cmath>
  65. #include <cstdlib>
  66. #include <fstream>
  67. #include <iomanip>
  68. #include <iostream>
  69. #include <map>
  70. #include <memory>
  71. #include <numeric>
  72. #include <queue>
  73. #include <set>
  74. #include <sstream>
  75. #include <string>
  76. #include <utility>
  77. #include <vector>
  78. //The only available version is 3, as of Boost 1.50
  79. #include <boost/version.hpp>
  80. #define BOOST_FILESYSTEM_VERSION 3
  81. #if ( BOOST_VERSION>105000 )
  82. #define BOOST_THREAD_VERSION 3
  83. #endif
  84. #define BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE 1
  85. //#define BOOST_SYSTEM_NO_DEPRECATED 1
  86. #include <boost/algorithm/string.hpp>
  87. #include <boost/assert.hpp>
  88. #include <boost/assign.hpp>
  89. #include <boost/bind.hpp>
  90. #include <boost/cstdint.hpp>
  91. #include <boost/current_function.hpp>
  92. #include <boost/date_time/posix_time/posix_time.hpp>
  93. #include <boost/date_time/posix_time/posix_time_io.hpp>
  94. #include <boost/filesystem.hpp>
  95. #include <boost/foreach.hpp>
  96. #include <boost/format.hpp>
  97. #include <boost/function.hpp>
  98. #include <boost/lexical_cast.hpp>
  99. #include <boost/logic/tribool.hpp>
  100. #include <boost/program_options.hpp>
  101. #include <boost/optional.hpp>
  102. #include <boost/range/algorithm.hpp>
  103. #include <boost/range/adaptor/filtered.hpp>
  104. #include <boost/thread.hpp>
  105. #include <boost/unordered_map.hpp>
  106. #include <boost/unordered_set.hpp>
  107. #include <boost/unordered_map.hpp>
  108. #include <boost/variant.hpp>
  109. #ifdef ANDROID
  110. #include <android/log.h>
  111. #endif
  112. /* ---------------------------------------------------------------------------- */
  113. /* Usings */
  114. /* ---------------------------------------------------------------------------- */
  115. using std::shared_ptr;
  116. using std::unique_ptr;
  117. using std::make_shared;
  118. namespace range = boost::range;
  119. /* ---------------------------------------------------------------------------- */
  120. /* Typedefs */
  121. /* ---------------------------------------------------------------------------- */
  122. // Integral data types
  123. typedef boost::uint64_t ui64; //unsigned int 64 bits (8 bytes)
  124. typedef boost::uint32_t ui32; //unsigned int 32 bits (4 bytes)
  125. typedef boost::uint16_t ui16; //unsigned int 16 bits (2 bytes)
  126. typedef boost::uint8_t ui8; //unsigned int 8 bits (1 byte)
  127. typedef boost::int64_t si64; //signed int 64 bits (8 bytes)
  128. typedef boost::int32_t si32; //signed int 32 bits (4 bytes)
  129. typedef boost::int16_t si16; //signed int 16 bits (2 bytes)
  130. typedef boost::int8_t si8; //signed int 8 bits (1 byte)
  131. // Lock typedefs
  132. typedef boost::lock_guard<boost::mutex> TLockGuard;
  133. typedef boost::lock_guard<boost::recursive_mutex> TLockGuardRec;
  134. /* ---------------------------------------------------------------------------- */
  135. /* Macros */
  136. /* ---------------------------------------------------------------------------- */
  137. // Import + Export macro declarations
  138. #ifdef _WIN32
  139. #ifdef __GNUC__
  140. #define DLL_EXPORT __attribute__((dllexport))
  141. #else
  142. #define DLL_EXPORT __declspec(dllexport)
  143. #endif
  144. #else
  145. #if defined(__GNUC__) && GCC_VERSION >= 400
  146. #define DLL_EXPORT __attribute__ ((visibility("default")))
  147. #else
  148. #define DLL_EXPORT
  149. #endif
  150. #endif
  151. #ifdef _WIN32
  152. #ifdef __GNUC__
  153. #define DLL_IMPORT __attribute__((dllimport))
  154. #else
  155. #define DLL_IMPORT __declspec(dllimport)
  156. #endif
  157. #else
  158. #if defined(__GNUC__) && GCC_VERSION >= 400
  159. #define DLL_IMPORT __attribute__ ((visibility("default")))
  160. #else
  161. #define DLL_IMPORT
  162. #endif
  163. #endif
  164. #ifdef VCMI_DLL
  165. #define DLL_LINKAGE DLL_EXPORT
  166. #else
  167. #define DLL_LINKAGE DLL_IMPORT
  168. #endif
  169. #define THROW_FORMAT(message, formatting_elems) throw std::runtime_error(boost::str(boost::format(message) % formatting_elems))
  170. #define ASSERT_IF_CALLED_WITH_PLAYER if(!player) {logGlobal->errorStream() << BOOST_CURRENT_FUNCTION; assert(0);}
  171. //XXX pls dont - 'debug macros' are usually more trouble than it's worth
  172. #define HANDLE_EXCEPTION \
  173. catch (const std::exception& e) { \
  174. logGlobal->errorStream() << e.what(); \
  175. throw; \
  176. } \
  177. catch (const std::exception * e) \
  178. { \
  179. logGlobal->errorStream() << e->what(); \
  180. throw; \
  181. } \
  182. catch (const std::string& e) { \
  183. logGlobal->errorStream() << e; \
  184. throw; \
  185. }
  186. #define HANDLE_EXCEPTIONC(COMMAND) \
  187. catch (const std::exception& e) { \
  188. COMMAND; \
  189. logGlobal->errorStream() << e.what(); \
  190. throw; \
  191. } \
  192. catch (const std::string &e) \
  193. { \
  194. COMMAND; \
  195. logGlobal->errorStream() << e; \
  196. throw; \
  197. }
  198. // can be used for counting arrays
  199. template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
  200. #define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr)))
  201. /* ---------------------------------------------------------------------------- */
  202. /* VCMI standard library */
  203. /* ---------------------------------------------------------------------------- */
  204. //a normal std::map with a const operator[] for sanity
  205. template<typename KeyT, typename ValT>
  206. class bmap : public std::map<KeyT, ValT>
  207. {
  208. public:
  209. const ValT & operator[](KeyT key) const
  210. {
  211. return this->find(key)->second;
  212. }
  213. ValT & operator[](KeyT key)
  214. {
  215. return static_cast<std::map<KeyT, ValT> &>(*this)[key];
  216. }
  217. template <typename Handler> void serialize(Handler &h, const int version)
  218. {
  219. h & static_cast<std::map<KeyT, ValT> &>(*this);
  220. }
  221. bmap()
  222. {}
  223. #if 0 // What is _Myt? gcc\clang does not have that
  224. bmap(const typename std::map<KeyT, ValT>::_Myt& _Right)
  225. : std::map<KeyT, ValT>(_Right)
  226. {}
  227. #endif
  228. explicit bmap(const typename std::map<KeyT, ValT>::key_compare& _Pred)
  229. : std::map<KeyT, ValT>(_Pred)
  230. {}
  231. bmap(const typename std::map<KeyT, ValT>::key_compare& _Pred, const typename std::map<KeyT, ValT>::allocator_type& _Al)
  232. : std::map<KeyT, ValT>(_Pred, _Al)
  233. {}
  234. template<class _Iter>
  235. bmap(_Iter _First, _Iter _Last)
  236. : std::map<KeyT, ValT>(_First, _Last)
  237. {}
  238. template<class _Iter>
  239. bmap(_Iter _First, _Iter _Last,
  240. const typename std::map<KeyT, ValT>::key_compare& _Pred)
  241. : std::map<KeyT, ValT>(_First, _Last, _Pred)
  242. {}
  243. template<class _Iter>
  244. bmap(_Iter _First, _Iter _Last,
  245. const typename std::map<KeyT, ValT>::key_compare& _Pred, const typename std::map<KeyT, ValT>::allocator_type& _Al)
  246. : std::map<KeyT, ValT>(_First, _Last, _Pred, _Al)
  247. {}
  248. };
  249. namespace vstd
  250. {
  251. //returns true if container c contains item i
  252. template <typename Container, typename Item>
  253. bool contains(const Container & c, const Item &i)
  254. {
  255. return std::find(boost::begin(c), boost::end(c),i) != boost::end(c);
  256. }
  257. //returns true if container c contains item i
  258. template <typename Container, typename Pred>
  259. bool contains_if(const Container & c, Pred p)
  260. {
  261. return std::find_if(boost::begin(c), boost::end(c), p) != boost::end(c);
  262. }
  263. //returns true if map c contains item i
  264. template <typename V, typename Item, typename Item2>
  265. bool contains(const std::map<Item,V> & c, const Item2 &i)
  266. {
  267. return c.find(i)!=c.end();
  268. }
  269. //returns true if bmap c contains item i
  270. template <typename V, typename Item, typename Item2>
  271. bool contains(const bmap<Item,V> & c, const Item2 &i)
  272. {
  273. return c.find(i)!=c.end();
  274. }
  275. //returns true if unordered set c contains item i
  276. template <typename Item>
  277. bool contains(const boost::unordered_set<Item> & c, const Item &i)
  278. {
  279. return c.find(i)!=c.end();
  280. }
  281. template <typename V, typename Item, typename Item2>
  282. bool contains(const boost::unordered_map<Item,V> & c, const Item2 &i)
  283. {
  284. return c.find(i)!=c.end();
  285. }
  286. //returns position of first element in vector c equal to s, if there is no such element, -1 is returned
  287. template <typename Container, typename T2>
  288. int find_pos(const Container & c, const T2 &s)
  289. {
  290. size_t i=0;
  291. for (auto iter = boost::begin(c); iter != boost::end(c); iter++, i++)
  292. if(*iter == s)
  293. return i;
  294. return -1;
  295. }
  296. //Func(T1,T2) must say if these elements matches
  297. template <typename T1, typename T2, typename Func>
  298. int find_pos(const std::vector<T1> & c, const T2 &s, const Func &f)
  299. {
  300. for(size_t i=0; i < c.size(); ++i)
  301. if(f(c[i],s))
  302. return i;
  303. return -1;
  304. }
  305. //returns iterator to the given element if present in container, end() if not
  306. template <typename Container, typename Item>
  307. typename Container::iterator find(Container & c, const Item &i)
  308. {
  309. return std::find(c.begin(),c.end(),i);
  310. }
  311. //returns const iterator to the given element if present in container, end() if not
  312. template <typename Container, typename Item>
  313. typename Container::const_iterator find(const Container & c, const Item &i)
  314. {
  315. return std::find(c.begin(),c.end(),i);
  316. }
  317. //removes element i from container c, returns false if c does not contain i
  318. template <typename Container, typename Item>
  319. typename Container::size_type operator-=(Container &c, const Item &i)
  320. {
  321. typename Container::iterator itr = find(c,i);
  322. if(itr == c.end())
  323. return false;
  324. c.erase(itr);
  325. return true;
  326. }
  327. //assigns greater of (a, b) to a and returns maximum of (a, b)
  328. template <typename t1, typename t2>
  329. t1 &amax(t1 &a, const t2 &b)
  330. {
  331. if(a >= b)
  332. return a;
  333. else
  334. {
  335. a = b;
  336. return a;
  337. }
  338. }
  339. //assigns smaller of (a, b) to a and returns minimum of (a, b)
  340. template <typename t1, typename t2>
  341. t1 &amin(t1 &a, const t2 &b)
  342. {
  343. if(a <= b)
  344. return a;
  345. else
  346. {
  347. a = b;
  348. return a;
  349. }
  350. }
  351. //makes a to fit the range <b, c>
  352. template <typename t1, typename t2, typename t3>
  353. t1 &abetween(t1 &a, const t2 &b, const t3 &c)
  354. {
  355. amax(a,b);
  356. amin(a,c);
  357. return a;
  358. }
  359. //checks if a is between b and c
  360. template <typename t1, typename t2, typename t3>
  361. bool isbetween(const t1 &value, const t2 &min, const t3 &max)
  362. {
  363. return value > min && value < max;
  364. }
  365. //checks if a is within b and c
  366. template <typename t1, typename t2, typename t3>
  367. bool iswithin(const t1 &value, const t2 &min, const t3 &max)
  368. {
  369. return value >= min && value <= max;
  370. }
  371. template <typename t1, typename t2>
  372. struct assigner
  373. {
  374. public:
  375. t1 &op1;
  376. t2 op2;
  377. assigner(t1 &a1, const t2 & a2)
  378. :op1(a1), op2(a2)
  379. {}
  380. void operator()()
  381. {
  382. op1 = op2;
  383. }
  384. };
  385. // Assigns value a2 to a1. The point of time of the real operation can be controlled
  386. // with the () operator.
  387. template <typename t1, typename t2>
  388. assigner<t1,t2> assigno(t1 &a1, const t2 &a2)
  389. {
  390. return assigner<t1,t2>(a1,a2);
  391. }
  392. //deleted pointer and sets it to NULL
  393. template <typename T>
  394. void clear_pointer(T* &ptr)
  395. {
  396. delete ptr;
  397. ptr = NULL;
  398. }
  399. template<typename T>
  400. std::unique_ptr<T> make_unique()
  401. {
  402. return std::unique_ptr<T>(new T());
  403. }
  404. template<typename T, typename Arg1>
  405. std::unique_ptr<T> make_unique(Arg1 &&arg1)
  406. {
  407. return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1)));
  408. }
  409. template<typename T, typename Arg1, typename Arg2>
  410. std::unique_ptr<T> make_unique(Arg1 &&arg1, Arg2 &&arg2)
  411. {
  412. return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2)));
  413. }
  414. template<typename T, typename Arg1, typename Arg2, typename Arg3>
  415. std::unique_ptr<T> make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3)
  416. {
  417. return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2), std::forward<Arg3>(arg3)));
  418. }
  419. template <typename Container>
  420. typename Container::const_reference circularAt(const Container &r, size_t index)
  421. {
  422. assert(r.size());
  423. index %= r.size();
  424. auto itr = boost::begin(r);
  425. std::advance(itr, index);
  426. return *itr;
  427. }
  428. template<typename Range, typename Predicate>
  429. void erase_if(Range &vec, Predicate pred)
  430. {
  431. vec.erase(boost::remove_if(vec, pred),vec.end());
  432. }
  433. template<typename Elem, typename Predicate>
  434. void erase_if(std::set<Elem> &setContainer, Predicate pred)
  435. {
  436. auto itr = setContainer.begin();
  437. auto endItr = setContainer.end();
  438. while(itr != endItr)
  439. {
  440. auto tmpItr = itr++;
  441. if(pred(*tmpItr))
  442. setContainer.erase(tmpItr);
  443. }
  444. }
  445. //works for map and bmap, maybe something else
  446. template<typename Key, typename Val, typename Predicate>
  447. void erase_if(std::map<Key, Val> &container, Predicate pred)
  448. {
  449. auto itr = container.begin();
  450. auto endItr = container.end();
  451. while(itr != endItr)
  452. {
  453. auto tmpItr = itr++;
  454. if(pred(*tmpItr))
  455. container.erase(tmpItr);
  456. }
  457. }
  458. template<typename InputRange, typename OutputIterator, typename Predicate>
  459. OutputIterator copy_if(const InputRange &input, OutputIterator result, Predicate pred)
  460. {
  461. return std::copy_if(boost::const_begin(input), boost::end(input), result, pred);
  462. }
  463. template <typename Container>
  464. std::insert_iterator<Container> set_inserter(Container &c)
  465. {
  466. return std::inserter(c, c.end());
  467. }
  468. //Returns iterator to the element for which the value of ValueFunction is minimal
  469. template<class ForwardRange, class ValueFunction>
  470. auto minElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(boost::begin(rng))
  471. {
  472. typedef decltype(*boost::begin(rng)) ElemType;
  473. return boost::min_element(rng, [&] (ElemType lhs, ElemType rhs) -> bool
  474. {
  475. return vf(lhs) < vf(rhs);
  476. });
  477. }
  478. //Returns iterator to the element for which the value of ValueFunction is maximal
  479. template<class ForwardRange, class ValueFunction>
  480. auto maxElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(boost::begin(rng))
  481. {
  482. typedef decltype(*boost::begin(rng)) ElemType;
  483. return boost::max_element(rng, [&] (ElemType lhs, ElemType rhs) -> bool
  484. {
  485. return vf(lhs) < vf(rhs);
  486. });
  487. }
  488. static inline int retreiveRandNum(const boost::function<int()> &randGen)
  489. {
  490. if (randGen)
  491. return randGen();
  492. else
  493. return rand();
  494. }
  495. template <typename T> const T & pickRandomElementOf(const std::vector<T> &v, const boost::function<int()> &randGen)
  496. {
  497. return v.at(retreiveRandNum(randGen) % v.size());
  498. }
  499. template<typename T>
  500. void advance(T &obj, int change)
  501. {
  502. obj = (T)(((int)obj) + change);
  503. }
  504. }
  505. using vstd::operator-=;
  506. using vstd::make_unique;
  507. /* ---------------------------------------------------------------------------- */
  508. /* VCMI headers */
  509. /* ---------------------------------------------------------------------------- */
  510. #include "lib/logging/CLogger.h"