Global.h 9.7 KB

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