Global.h 14 KB

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