Global.h 9.9 KB

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