Global.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. // Import + Export macro declarations
  74. #ifdef _WIN32
  75. #define DLL_EXPORT __declspec(dllexport)
  76. #else
  77. #if defined(__GNUC__) && __GNUC__ >= 4
  78. #define DLL_EXPORT __attribute__ ((visibility("default")))
  79. #else
  80. #define DLL_EXPORT
  81. #endif
  82. #endif
  83. #ifdef _WIN32
  84. #define DLL_IMPORT __declspec(dllimport)
  85. #else
  86. #if defined(__GNUC__) && __GNUC__ >= 4
  87. #define DLL_IMPORT __attribute__ ((visibility("default")))
  88. #else
  89. #define DLL_IMPORT
  90. #endif
  91. #endif
  92. #ifdef VCMI_DLL
  93. #define DLL_LINKAGE DLL_EXPORT
  94. #else
  95. #define DLL_LINKAGE DLL_IMPORT
  96. #endif
  97. //a normal std::map with a const operator[] for sanity
  98. template<typename KeyT, typename ValT>
  99. class bmap : public std::map<KeyT, ValT>
  100. {
  101. public:
  102. const ValT & operator[](KeyT key) const
  103. {
  104. return find(key)->second;
  105. }
  106. ValT & operator[](KeyT key)
  107. {
  108. return static_cast<std::map<KeyT, ValT> &>(*this)[key];
  109. }
  110. template <typename Handler> void serialize(Handler &h, const int version)
  111. {
  112. h & static_cast<std::map<KeyT, ValT> &>(*this);
  113. }
  114. };
  115. namespace vstd
  116. {
  117. //returns true if container c contains item i
  118. template <typename Container, typename Item>
  119. bool contains(const Container & c, const Item &i)
  120. {
  121. return std::find(c.begin(),c.end(),i) != c.end();
  122. }
  123. //returns true if map c contains item i
  124. template <typename V, typename Item, typename Item2>
  125. bool contains(const std::map<Item,V> & c, const Item2 &i)
  126. {
  127. return c.find(i)!=c.end();
  128. }
  129. //returns true if bmap c contains item i
  130. template <typename V, typename Item, typename Item2>
  131. bool contains(const bmap<Item,V> & c, const Item2 &i)
  132. {
  133. return c.find(i)!=c.end();
  134. }
  135. //returns true if unordered set c contains item i
  136. template <typename Item>
  137. bool contains(const boost::unordered_set<Item> & c, const Item &i)
  138. {
  139. return c.find(i)!=c.end();
  140. }
  141. //returns position of first element in vector c equal to s, if there is no such element, -1 is returned
  142. template <typename T1, typename T2>
  143. int find_pos(const std::vector<T1> & c, const T2 &s)
  144. {
  145. for(size_t i=0; i < c.size(); ++i)
  146. if(c[i] == s)
  147. return i;
  148. return -1;
  149. }
  150. //Func(T1,T2) must say if these elements matches
  151. template <typename T1, typename T2, typename Func>
  152. int find_pos(const std::vector<T1> & c, const T2 &s, const Func &f)
  153. {
  154. for(size_t i=0; i < c.size(); ++i)
  155. if(f(c[i],s))
  156. return i;
  157. return -1;
  158. }
  159. //returns iterator to the given element if present in container, end() if not
  160. template <typename Container, typename Item>
  161. typename Container::iterator find(Container & c, const Item &i)
  162. {
  163. return std::find(c.begin(),c.end(),i);
  164. }
  165. //returns const iterator to the given element if present in container, end() if not
  166. template <typename Container, typename Item>
  167. typename Container::const_iterator find(const Container & c, const Item &i)
  168. {
  169. return std::find(c.begin(),c.end(),i);
  170. }
  171. //removes element i from container c, returns false if c does not contain i
  172. template <typename Container, typename Item>
  173. typename Container::size_type operator-=(Container &c, const Item &i)
  174. {
  175. typename Container::iterator itr = find(c,i);
  176. if(itr == c.end())
  177. return false;
  178. c.erase(itr);
  179. return true;
  180. }
  181. //assigns greater of (a, b) to a and returns maximum of (a, b)
  182. template <typename t1, typename t2>
  183. t1 &amax(t1 &a, const t2 &b)
  184. {
  185. if(a >= b)
  186. return a;
  187. else
  188. {
  189. a = b;
  190. return a;
  191. }
  192. }
  193. //assigns smaller of (a, b) to a and returns minimum of (a, b)
  194. template <typename t1, typename t2>
  195. t1 &amin(t1 &a, const t2 &b)
  196. {
  197. if(a <= b)
  198. return a;
  199. else
  200. {
  201. a = b;
  202. return a;
  203. }
  204. }
  205. //makes a to fit the range <b, c>
  206. template <typename t1, typename t2, typename t3>
  207. t1 &abetween(t1 &a, const t2 &b, const t3 &c)
  208. {
  209. amax(a,b);
  210. amin(a,c);
  211. return a;
  212. }
  213. //checks if a is between b and c
  214. template <typename t1, typename t2, typename t3>
  215. bool isbetween(const t1 &a, const t2 &b, const t3 &c)
  216. {
  217. return a > b && a < c;
  218. }
  219. //checks if a is within b and c
  220. template <typename t1, typename t2, typename t3>
  221. bool iswithin(const t1 &a, const t2 &b, const t3 &c)
  222. {
  223. return a >= b && a <= c;
  224. }
  225. template <typename t1, typename t2>
  226. struct assigner
  227. {
  228. public:
  229. t1 &op1;
  230. t2 op2;
  231. assigner(t1 &a1, const t2 & a2)
  232. :op1(a1), op2(a2)
  233. {}
  234. void operator()()
  235. {
  236. op1 = op2;
  237. }
  238. };
  239. // Assigns value a2 to a1. The point of time of the real operation can be controlled
  240. // with the () operator.
  241. template <typename t1, typename t2>
  242. assigner<t1,t2> assigno(t1 &a1, const t2 &a2)
  243. {
  244. return assigner<t1,t2>(a1,a2);
  245. }
  246. //deleted pointer and sets it to NULL
  247. template <typename T>
  248. void clear_pointer(T* &ptr)
  249. {
  250. delete ptr;
  251. ptr = NULL;
  252. }
  253. }
  254. using vstd::operator-=;
  255. // can be used for counting arrays
  256. template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
  257. #define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr)))
  258. //for explicit overrides
  259. #ifdef _MSC_VER
  260. #define OVERRIDE override
  261. #else
  262. #define OVERRIDE //is there any working counterpart?
  263. #endif
  264. //XXX pls dont - 'debug macros' are usually more trouble than it's worth
  265. #define HANDLE_EXCEPTION \
  266. catch (const std::exception& e) { \
  267. tlog1 << e.what() << std::endl; \
  268. throw; \
  269. } \
  270. catch (const std::exception * e) \
  271. { \
  272. tlog1 << e->what()<< std::endl; \
  273. throw; \
  274. } \
  275. catch (const std::string& e) { \
  276. tlog1 << e << std::endl; \
  277. throw; \
  278. }
  279. #define HANDLE_EXCEPTIONC(COMMAND) \
  280. catch (const std::exception& e) { \
  281. COMMAND; \
  282. tlog1 << e.what() << std::endl; \
  283. throw; \
  284. } \
  285. catch (const std::string &e) \
  286. { \
  287. COMMAND; \
  288. tlog1 << e << std::endl; \
  289. throw; \
  290. }
  291. #include "lib/CLogger.h"