Global.h 15 KB

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