Global.h 8.9 KB

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