Global.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 <cmath>
  28. #include <cassert>
  29. #include <assert.h>
  30. #include <vector>
  31. #include <string>
  32. #include <map>
  33. #include <queue>
  34. #include <set>
  35. #include <utility>
  36. #include <numeric>
  37. #include <iostream>
  38. #include <fstream>
  39. #include <sstream>
  40. #include <iomanip>
  41. #include <algorithm>
  42. #include <memory>
  43. #include <cstdlib>
  44. //filesystem version 3 causes problems (and it's default as of boost 1.46)
  45. #define BOOST_FILESYSTEM_VERSION 2
  46. #include <boost/algorithm/string.hpp>
  47. #include <boost/assert.hpp>
  48. #include <boost/assign.hpp>
  49. #include <boost/bind.hpp>
  50. #include <boost/cstdint.hpp>
  51. #include <boost/date_time/posix_time/posix_time.hpp>
  52. #include <boost/filesystem.hpp>
  53. #include <boost/foreach.hpp>
  54. #include <boost/format.hpp>
  55. #include <boost/function.hpp>
  56. #include <boost/lexical_cast.hpp>
  57. #include <boost/logic/tribool.hpp>
  58. #include <boost/program_options.hpp>
  59. #include <boost/range/algorithm.hpp>
  60. #include <boost/thread.hpp>
  61. #include <boost/unordered_set.hpp>
  62. #include <boost/variant.hpp>
  63. #ifdef ANDROID
  64. #include <android/log.h>
  65. #endif
  66. // Integral data types
  67. typedef boost::uint64_t ui64; //unsigned int 64 bits (8 bytes)
  68. typedef boost::uint32_t ui32; //unsigned int 32 bits (4 bytes)
  69. typedef boost::uint16_t ui16; //unsigned int 16 bits (2 bytes)
  70. typedef boost::uint8_t ui8; //unsigned int 8 bits (1 byte)
  71. typedef boost::int64_t si64; //signed int 64 bits (8 bytes)
  72. typedef boost::int32_t si32; //signed int 32 bits (4 bytes)
  73. typedef boost::int16_t si16; //signed int 16 bits (2 bytes)
  74. typedef boost::int8_t si8; //signed int 8 bits (1 byte)
  75. #ifdef __GNUC__
  76. #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__ )
  77. #endif
  78. // Import + Export macro declarations
  79. #ifdef _WIN32
  80. #define DLL_EXPORT __declspec(dllexport)
  81. #else
  82. #if defined(__GNUC__) && GCC_VERSION >= 400
  83. #define DLL_EXPORT __attribute__ ((visibility("default")))
  84. #else
  85. #define DLL_EXPORT
  86. #endif
  87. #endif
  88. #ifdef _WIN32
  89. #define DLL_IMPORT __declspec(dllimport)
  90. #else
  91. #if defined(__GNUC__) && GCC_VERSION >= 400
  92. #define DLL_IMPORT __attribute__ ((visibility("default")))
  93. #else
  94. #define DLL_IMPORT
  95. #endif
  96. #endif
  97. #ifdef VCMI_DLL
  98. #define DLL_LINKAGE DLL_EXPORT
  99. #else
  100. #define DLL_LINKAGE DLL_IMPORT
  101. #endif
  102. //defining available c++11 features
  103. //initialization lists - only gcc-4.4 or later
  104. #if defined(__GNUC__) && (GCC_VERSION >= 404)
  105. #define CPP11_USE_INITIALIZERS_LIST
  106. #endif
  107. //nullptr - only msvc and gcc-4.6 or later, othervice define it as NULL
  108. #if !defined(_MSC_VER) && !(defined(__GNUC__) && (GCC_VERSION >= 406))
  109. #define nullptr NULL
  110. #endif
  111. //override keyword - only msvc and gcc-4.7 or later.
  112. #if !defined(_MSC_VER) && !(defined(__GNUC__) && (GCC_VERSION >= 407))
  113. #define override
  114. #endif
  115. //workaround to support existing code
  116. #define OVERRIDE override
  117. //a normal std::map with a const operator[] for sanity
  118. template<typename KeyT, typename ValT>
  119. class bmap : public std::map<KeyT, ValT>
  120. {
  121. public:
  122. const ValT & operator[](KeyT key) const
  123. {
  124. return this->find(key)->second;
  125. }
  126. ValT & operator[](KeyT key)
  127. {
  128. return static_cast<std::map<KeyT, ValT> &>(*this)[key];
  129. }
  130. template <typename Handler> void serialize(Handler &h, const int version)
  131. {
  132. h & static_cast<std::map<KeyT, ValT> &>(*this);
  133. }
  134. };
  135. namespace vstd
  136. {
  137. //returns true if container c contains item i
  138. template <typename Container, typename Item>
  139. bool contains(const Container & c, const Item &i)
  140. {
  141. return std::find(c.begin(),c.end(),i) != c.end();
  142. }
  143. //returns true if map c contains item i
  144. template <typename V, typename Item, typename Item2>
  145. bool contains(const std::map<Item,V> & c, const Item2 &i)
  146. {
  147. return c.find(i)!=c.end();
  148. }
  149. //returns true if bmap c contains item i
  150. template <typename V, typename Item, typename Item2>
  151. bool contains(const bmap<Item,V> & c, const Item2 &i)
  152. {
  153. return c.find(i)!=c.end();
  154. }
  155. //returns true if unordered set c contains item i
  156. template <typename Item>
  157. bool contains(const boost::unordered_set<Item> & c, const Item &i)
  158. {
  159. return c.find(i)!=c.end();
  160. }
  161. //returns position of first element in vector c equal to s, if there is no such element, -1 is returned
  162. template <typename T1, typename T2>
  163. int find_pos(const std::vector<T1> & c, const T2 &s)
  164. {
  165. for(size_t i=0; i < c.size(); ++i)
  166. if(c[i] == s)
  167. return i;
  168. return -1;
  169. }
  170. //Func(T1,T2) must say if these elements matches
  171. template <typename T1, typename T2, typename Func>
  172. int find_pos(const std::vector<T1> & c, const T2 &s, const Func &f)
  173. {
  174. for(size_t i=0; i < c.size(); ++i)
  175. if(f(c[i],s))
  176. return i;
  177. return -1;
  178. }
  179. //returns iterator to the given element if present in container, end() if not
  180. template <typename Container, typename Item>
  181. typename Container::iterator find(Container & c, const Item &i)
  182. {
  183. return std::find(c.begin(),c.end(),i);
  184. }
  185. //returns const iterator to the given element if present in container, end() if not
  186. template <typename Container, typename Item>
  187. typename Container::const_iterator find(const Container & c, const Item &i)
  188. {
  189. return std::find(c.begin(),c.end(),i);
  190. }
  191. //removes element i from container c, returns false if c does not contain i
  192. template <typename Container, typename Item>
  193. typename Container::size_type operator-=(Container &c, const Item &i)
  194. {
  195. typename Container::iterator itr = find(c,i);
  196. if(itr == c.end())
  197. return false;
  198. c.erase(itr);
  199. return true;
  200. }
  201. //assigns greater of (a, b) to a and returns maximum of (a, b)
  202. template <typename t1, typename t2>
  203. t1 &amax(t1 &a, const t2 &b)
  204. {
  205. if(a >= b)
  206. return a;
  207. else
  208. {
  209. a = b;
  210. return a;
  211. }
  212. }
  213. //assigns smaller of (a, b) to a and returns minimum of (a, b)
  214. template <typename t1, typename t2>
  215. t1 &amin(t1 &a, const t2 &b)
  216. {
  217. if(a <= b)
  218. return a;
  219. else
  220. {
  221. a = b;
  222. return a;
  223. }
  224. }
  225. //makes a to fit the range <b, c>
  226. template <typename t1, typename t2, typename t3>
  227. t1 &abetween(t1 &a, const t2 &b, const t3 &c)
  228. {
  229. amax(a,b);
  230. amin(a,c);
  231. return a;
  232. }
  233. //checks if a is between b and c
  234. template <typename t1, typename t2, typename t3>
  235. bool isbetween(const t1 &a, const t2 &b, const t3 &c)
  236. {
  237. return a > b && a < c;
  238. }
  239. //checks if a is within b and c
  240. template <typename t1, typename t2, typename t3>
  241. bool iswithin(const t1 &a, const t2 &b, const t3 &c)
  242. {
  243. return a >= b && a <= c;
  244. }
  245. template <typename t1, typename t2>
  246. struct assigner
  247. {
  248. public:
  249. t1 &op1;
  250. t2 op2;
  251. assigner(t1 &a1, const t2 & a2)
  252. :op1(a1), op2(a2)
  253. {}
  254. void operator()()
  255. {
  256. op1 = op2;
  257. }
  258. };
  259. // Assigns value a2 to a1. The point of time of the real operation can be controlled
  260. // with the () operator.
  261. template <typename t1, typename t2>
  262. assigner<t1,t2> assigno(t1 &a1, const t2 &a2)
  263. {
  264. return assigner<t1,t2>(a1,a2);
  265. }
  266. //deleted pointer and sets it to NULL
  267. template <typename T>
  268. void clear_pointer(T* &ptr)
  269. {
  270. delete ptr;
  271. ptr = NULL;
  272. }
  273. template<typename T>
  274. std::unique_ptr<T> make_unique()
  275. {
  276. return std::unique_ptr<T>(new T());
  277. }
  278. template<typename T, typename Arg1>
  279. std::unique_ptr<T> make_unique(Arg1 &&arg1)
  280. {
  281. return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1)));
  282. }
  283. template<typename T, typename Arg1, typename Arg2>
  284. std::unique_ptr<T> make_unique(Arg1 &&arg1, Arg2 &&arg2)
  285. {
  286. return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2)));
  287. }
  288. template <typename Container>
  289. typename Container::const_reference circularAt(const Container &r, size_t index)
  290. {
  291. assert(r.size());
  292. index %= r.size();
  293. auto itr = std::begin(r);
  294. std::advance(itr, index);
  295. return *itr;
  296. }
  297. }
  298. using std::shared_ptr;
  299. using std::unique_ptr;
  300. using std::make_shared;
  301. using vstd::make_unique;
  302. using vstd::operator-=;
  303. namespace range = boost::range;
  304. // can be used for counting arrays
  305. template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
  306. #define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr)))
  307. //XXX pls dont - 'debug macros' are usually more trouble than it's worth
  308. #define HANDLE_EXCEPTION \
  309. catch (const std::exception& e) { \
  310. tlog1 << e.what() << std::endl; \
  311. throw; \
  312. } \
  313. catch (const std::exception * e) \
  314. { \
  315. tlog1 << e->what()<< std::endl; \
  316. throw; \
  317. } \
  318. catch (const std::string& e) { \
  319. tlog1 << e << std::endl; \
  320. throw; \
  321. }
  322. #define HANDLE_EXCEPTIONC(COMMAND) \
  323. catch (const std::exception& e) { \
  324. COMMAND; \
  325. tlog1 << e.what() << std::endl; \
  326. throw; \
  327. } \
  328. catch (const std::string &e) \
  329. { \
  330. COMMAND; \
  331. tlog1 << e << std::endl; \
  332. throw; \
  333. }
  334. #include "lib/CLogger.h"