StdInc.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #pragma once
  2. // Standard include file
  3. // Should be treated as a precompiled header file in the compiler settings
  4. // We generate a .PCH file for every project due to simplicity and some annoying bugs in VisualStudio
  5. // This file shouldn't be changed, except if there is a important header file which is missing.
  6. /*
  7. * StdInc.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
  16. #include <cstdio>
  17. #include <stdio.h>
  18. #ifdef _WIN32
  19. #include <tchar.h>
  20. #else
  21. #include "../tchar_amigaos4.h"
  22. #endif
  23. #include <cmath>
  24. #include <cassert>
  25. #include <assert.h>
  26. #include <vector>
  27. #include <string>
  28. #include <map>
  29. #include <queue>
  30. #include <set>
  31. #include <utility>
  32. #include <numeric>
  33. #include <iostream>
  34. #include <fstream>
  35. #include <sstream>
  36. #include <iomanip>
  37. #include <algorithm>
  38. #include <memory>
  39. #include <cstdlib>
  40. //filesystem version 3 causes problems (and it's default as of boost 1.46)
  41. #define BOOST_FILESYSTEM_VERSION 2
  42. #include <boost/algorithm/string.hpp>
  43. #include <boost/assert.hpp>
  44. #include <boost/assign.hpp>
  45. #include <boost/bind.hpp>
  46. #include <boost/cstdint.hpp>
  47. #include <boost/date_time/posix_time/posix_time.hpp>
  48. #include <boost/filesystem.hpp>
  49. #include <boost/foreach.hpp>
  50. #include <boost/format.hpp>
  51. #include <boost/function.hpp>
  52. #include <boost/lexical_cast.hpp>
  53. #include <boost/logic/tribool.hpp>
  54. #include <boost/program_options.hpp>
  55. #include <boost/thread.hpp>
  56. #include <boost/unordered_set.hpp>
  57. #ifdef ANDROID
  58. #include <android/log.h>
  59. #endif
  60. // Integral data types
  61. typedef boost::uint64_t ui64; //unsigned int 64 bits (8 bytes)
  62. typedef boost::uint32_t ui32; //unsigned int 32 bits (4 bytes)
  63. typedef boost::uint16_t ui16; //unsigned int 16 bits (2 bytes)
  64. typedef boost::uint8_t ui8; //unsigned int 8 bits (1 byte)
  65. typedef boost::int64_t si64; //signed int 64 bits (8 bytes)
  66. typedef boost::int32_t si32; //signed int 32 bits (4 bytes)
  67. typedef boost::int16_t si16; //signed int 16 bits (2 bytes)
  68. typedef boost::int8_t si8; //signed int 8 bits (1 byte)
  69. // Import + Export macro declarations
  70. #ifdef _WIN32
  71. #define DLL_EXPORT __declspec(dllexport)
  72. #else
  73. #if defined(__GNUC__) && __GNUC__ >= 4
  74. #define DLL_EXPORT __attribute__ ((visibility("default")))
  75. #else
  76. #define DLL_EXPORT
  77. #endif
  78. #endif
  79. #ifdef _WIN32
  80. #define DLL_IMPORT __declspec(dllimport)
  81. #else
  82. #if defined(__GNUC__) && __GNUC__ >= 4
  83. #define DLL_IMPORT __attribute__ ((visibility("default")))
  84. #else
  85. #define DLL_IMPORT
  86. #endif
  87. #endif
  88. #ifdef VCMI_DLL
  89. #define DLL_LINKAGE DLL_EXPORT
  90. #else
  91. #define DLL_LINKAGE DLL_IMPORT
  92. #endif
  93. //a normal std::map with a const operator[] for sanity
  94. template<typename KeyT, typename ValT>
  95. class bmap : public std::map<KeyT, ValT>
  96. {
  97. public:
  98. const ValT & operator[](KeyT key) const
  99. {
  100. return find(key)->second;
  101. }
  102. ValT & operator[](KeyT key)
  103. {
  104. return static_cast<std::map<KeyT, ValT> &>(*this)[key];
  105. }
  106. template <typename Handler> void serialize(Handler &h, const int version)
  107. {
  108. h & static_cast<std::map<KeyT, ValT> &>(*this);
  109. }
  110. };
  111. namespace vstd
  112. {
  113. //returns true if container c contains item i
  114. template <typename Container, typename Item>
  115. bool contains(const Container & c, const Item &i)
  116. {
  117. return std::find(c.begin(),c.end(),i) != c.end();
  118. }
  119. //returns true if map c contains item i
  120. template <typename V, typename Item, typename Item2>
  121. bool contains(const std::map<Item,V> & c, const Item2 &i)
  122. {
  123. return c.find(i)!=c.end();
  124. }
  125. //returns true if bmap c contains item i
  126. template <typename V, typename Item, typename Item2>
  127. bool contains(const bmap<Item,V> & c, const Item2 &i)
  128. {
  129. return c.find(i)!=c.end();
  130. }
  131. //returns true if unordered set c contains item i
  132. template <typename Item>
  133. bool contains(const boost::unordered_set<Item> & c, const Item &i)
  134. {
  135. return c.find(i)!=c.end();
  136. }
  137. //returns position of first element in vector c equal to s, if there is no such element, -1 is returned
  138. template <typename T1, typename T2>
  139. int find_pos(const std::vector<T1> & c, const T2 &s)
  140. {
  141. for(size_t i=0; i < c.size(); ++i)
  142. if(c[i] == s)
  143. return i;
  144. return -1;
  145. }
  146. //Func(T1,T2) must say if these elements matches
  147. template <typename T1, typename T2, typename Func>
  148. int find_pos(const std::vector<T1> & c, const T2 &s, const Func &f)
  149. {
  150. for(size_t i=0; i < c.size(); ++i)
  151. if(f(c[i],s))
  152. return i;
  153. return -1;
  154. }
  155. //returns iterator to the given element if present in container, end() if not
  156. template <typename Container, typename Item>
  157. typename Container::iterator find(Container & c, const Item &i)
  158. {
  159. return std::find(c.begin(),c.end(),i);
  160. }
  161. //returns const iterator to the given element if present in container, end() if not
  162. template <typename Container, typename Item>
  163. typename Container::const_iterator find(const Container & c, const Item &i)
  164. {
  165. return std::find(c.begin(),c.end(),i);
  166. }
  167. //removes element i from container c, returns false if c does not contain i
  168. template <typename Container, typename Item>
  169. typename Container::size_type operator-=(Container &c, const Item &i)
  170. {
  171. typename Container::iterator itr = find(c,i);
  172. if(itr == c.end())
  173. return false;
  174. c.erase(itr);
  175. return true;
  176. }
  177. //assigns greater of (a, b) to a and returns maximum of (a, b)
  178. template <typename t1, typename t2>
  179. t1 &amax(t1 &a, const t2 &b)
  180. {
  181. if(a >= b)
  182. return a;
  183. else
  184. {
  185. a = b;
  186. return a;
  187. }
  188. }
  189. //assigns smaller of (a, b) to a and returns minimum of (a, b)
  190. template <typename t1, typename t2>
  191. t1 &amin(t1 &a, const t2 &b)
  192. {
  193. if(a <= b)
  194. return a;
  195. else
  196. {
  197. a = b;
  198. return a;
  199. }
  200. }
  201. //makes a to fit the range <b, c>
  202. template <typename t1, typename t2, typename t3>
  203. t1 &abetween(t1 &a, const t2 &b, const t3 &c)
  204. {
  205. amax(a,b);
  206. amin(a,c);
  207. return a;
  208. }
  209. //checks if a is between b and c
  210. template <typename t1, typename t2, typename t3>
  211. bool isbetween(const t1 &a, const t2 &b, const t3 &c)
  212. {
  213. return a > b && a < c;
  214. }
  215. //checks if a is within b and c
  216. template <typename t1, typename t2, typename t3>
  217. bool iswithin(const t1 &a, const t2 &b, const t3 &c)
  218. {
  219. return a >= b && a <= c;
  220. }
  221. template <typename t1, typename t2>
  222. struct assigner
  223. {
  224. public:
  225. t1 &op1;
  226. t2 op2;
  227. assigner(t1 &a1, const t2 & a2)
  228. :op1(a1), op2(a2)
  229. {}
  230. void operator()()
  231. {
  232. op1 = op2;
  233. }
  234. };
  235. // Assigns value a2 to a1. The point of time of the real operation can be controlled
  236. // with the () operator.
  237. template <typename t1, typename t2>
  238. assigner<t1,t2> assigno(t1 &a1, const t2 &a2)
  239. {
  240. return assigner<t1,t2>(a1,a2);
  241. }
  242. //deleted pointer and sets it to NULL
  243. template <typename T>
  244. void clear_pointer(T* &ptr)
  245. {
  246. delete ptr;
  247. ptr = NULL;
  248. }
  249. }
  250. using vstd::operator-=;
  251. // can be used for counting arrays
  252. template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
  253. #define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr)))
  254. //for explicit overrides
  255. #ifdef _MSC_VER
  256. #define OVERRIDE override
  257. #else
  258. #define OVERRIDE //is there any working counterpart?
  259. #endif
  260. //XXX pls dont - 'debug macros' are usually more trouble than it's worth
  261. #define HANDLE_EXCEPTION \
  262. catch (const std::exception& e) { \
  263. tlog1 << e.what() << std::endl; \
  264. throw; \
  265. } \
  266. catch (const std::exception * e) \
  267. { \
  268. tlog1 << e->what()<< std::endl; \
  269. throw; \
  270. } \
  271. catch (const std::string& e) { \
  272. tlog1 << e << std::endl; \
  273. throw; \
  274. }
  275. #define HANDLE_EXCEPTIONC(COMMAND) \
  276. catch (const std::exception& e) { \
  277. COMMAND; \
  278. tlog1 << e.what() << std::endl; \
  279. throw; \
  280. } \
  281. catch (const std::string &e) \
  282. { \
  283. COMMAND; \
  284. tlog1 << e << std::endl; \
  285. throw; \
  286. }
  287. #include "../lib/CLogger.h"