#pragma once // Standard include file // Contents: // Includes C/C++ libraries, STL libraries, IOStream and String libraries // Includes the most important boost headers // Defines the import + export, override and exception handling macros // Defines the vstd library // Includes the logger // This file shouldn't be changed, except if there is a important header file missing which is shared among several projects. /* * Global.h, part of VCMI engine * * Authors: listed in file AUTHORS in main folder * * License: GNU General Public License v2.0 or later * Full text of license available in license.txt file, in main folder * */ #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers #include #include #ifdef _WIN32 #include #else #include "tchar_amigaos4.h" #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include #include #include //The only available version is 3, as of Boost 1.50 #include #define BOOST_FILESYSTEM_VERSION 3 #if ( BOOST_VERSION>105000 ) #define BOOST_THREAD_VERSION 3 #endif #define BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE 1 //#define BOOST_SYSTEM_NO_DEPRECATED 1 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef ANDROID #include #endif // Integral data types typedef boost::uint64_t ui64; //unsigned int 64 bits (8 bytes) typedef boost::uint32_t ui32; //unsigned int 32 bits (4 bytes) typedef boost::uint16_t ui16; //unsigned int 16 bits (2 bytes) typedef boost::uint8_t ui8; //unsigned int 8 bits (1 byte) typedef boost::int64_t si64; //signed int 64 bits (8 bytes) typedef boost::int32_t si32; //signed int 32 bits (4 bytes) typedef boost::int16_t si16; //signed int 16 bits (2 bytes) typedef boost::int8_t si8; //signed int 8 bits (1 byte) #if defined _M_X64 && defined _WIN32 //Win64 -> cannot load 32-bit DLLs for video handling #define DISABLE_VIDEO #endif #ifdef __GNUC__ #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__ * 10 + __GNUC_PATCHLEVEL__) #endif #if defined(__GNUC__) && (GCC_VERSION == 470 || GCC_VERSION == 471) #error This GCC version has buggy std::array::at version and should not be used. Please update to 4.7.2 or use 4.6.x. #endif // Import + Export macro declarations #ifdef _WIN32 #ifdef __GNUC__ #define DLL_EXPORT __attribute__((dllexport)) #else #define DLL_EXPORT __declspec(dllexport) #endif #else #if defined(__GNUC__) && GCC_VERSION >= 400 #define DLL_EXPORT __attribute__ ((visibility("default"))) #else #define DLL_EXPORT #endif #endif #ifdef _WIN32 #ifdef __GNUC__ #define DLL_IMPORT __attribute__((dllimport)) #else #define DLL_IMPORT __declspec(dllimport) #endif #else #if defined(__GNUC__) && GCC_VERSION >= 400 #define DLL_IMPORT __attribute__ ((visibility("default"))) #else #define DLL_IMPORT #endif #endif #ifdef VCMI_DLL #define DLL_LINKAGE DLL_EXPORT #else #define DLL_LINKAGE DLL_IMPORT #endif //defining available c++11 features //initialization lists - only gcc-4.4 or later #if defined(__clang__) || (defined(__GNUC__) && (GCC_VERSION >= 440)) #define CPP11_USE_INITIALIZERS_LIST #endif //nullptr - only msvc and gcc-4.6 or later, othervice define it as NULL #if !defined(_MSC_VER) && !(defined(__GNUC__) && (GCC_VERSION >= 460)) #define nullptr NULL #endif //override keyword - only msvc and gcc-4.7 or later. #if !defined(_MSC_VER) && !(defined(__GNUC__) && (GCC_VERSION >= 470)) #define override #endif //workaround to support existing code #define OVERRIDE override //a normal std::map with a const operator[] for sanity template class bmap : public std::map { public: const ValT & operator[](KeyT key) const { return this->find(key)->second; } ValT & operator[](KeyT key) { return static_cast &>(*this)[key]; } template void serialize(Handler &h, const int version) { h & static_cast &>(*this); } bmap() {} #if 0 // What is _Myt? gcc\clang does not have that bmap(const typename std::map::_Myt& _Right) : std::map(_Right) {} #endif explicit bmap(const typename std::map::key_compare& _Pred) : std::map(_Pred) {} bmap(const typename std::map::key_compare& _Pred, const typename std::map::allocator_type& _Al) : std::map(_Pred, _Al) {} template bmap(_Iter _First, _Iter _Last) : std::map(_First, _Last) {} template bmap(_Iter _First, _Iter _Last, const typename std::map::key_compare& _Pred) : std::map(_First, _Last, _Pred) {} template bmap(_Iter _First, _Iter _Last, const typename std::map::key_compare& _Pred, const typename std::map::allocator_type& _Al) : std::map(_First, _Last, _Pred, _Al) {} }; namespace vstd { //returns true if container c contains item i template bool contains(const Container & c, const Item &i) { return std::find(boost::begin(c), boost::end(c),i) != boost::end(c); } //returns true if container c contains item i template bool contains_if(const Container & c, Pred p) { return std::find_if(boost::begin(c), boost::end(c), p) != boost::end(c); } //returns true if map c contains item i template bool contains(const std::map & c, const Item2 &i) { return c.find(i)!=c.end(); } //returns true if bmap c contains item i template bool contains(const bmap & c, const Item2 &i) { return c.find(i)!=c.end(); } //returns true if unordered set c contains item i template bool contains(const boost::unordered_set & c, const Item &i) { return c.find(i)!=c.end(); } //returns position of first element in vector c equal to s, if there is no such element, -1 is returned template int find_pos(const Container & c, const T2 &s) { size_t i=0; for (auto iter = boost::begin(c); iter != boost::end(c); iter++, i++) if(*iter == s) return i; return -1; } //Func(T1,T2) must say if these elements matches template int find_pos(const std::vector & c, const T2 &s, const Func &f) { for(size_t i=0; i < c.size(); ++i) if(f(c[i],s)) return i; return -1; } //returns iterator to the given element if present in container, end() if not template typename Container::iterator find(Container & c, const Item &i) { return std::find(c.begin(),c.end(),i); } //returns const iterator to the given element if present in container, end() if not template typename Container::const_iterator find(const Container & c, const Item &i) { return std::find(c.begin(),c.end(),i); } //removes element i from container c, returns false if c does not contain i template typename Container::size_type operator-=(Container &c, const Item &i) { typename Container::iterator itr = find(c,i); if(itr == c.end()) return false; c.erase(itr); return true; } //assigns greater of (a, b) to a and returns maximum of (a, b) template t1 &amax(t1 &a, const t2 &b) { if(a >= b) return a; else { a = b; return a; } } //assigns smaller of (a, b) to a and returns minimum of (a, b) template t1 &amin(t1 &a, const t2 &b) { if(a <= b) return a; else { a = b; return a; } } //makes a to fit the range template t1 &abetween(t1 &a, const t2 &b, const t3 &c) { amax(a,b); amin(a,c); return a; } //checks if a is between b and c template bool isbetween(const t1 &value, const t2 &min, const t3 &max) { return value > min && value < max; } //checks if a is within b and c template bool iswithin(const t1 &value, const t2 &min, const t3 &max) { return value >= min && value <= max; } template struct assigner { public: t1 &op1; t2 op2; assigner(t1 &a1, const t2 & a2) :op1(a1), op2(a2) {} void operator()() { op1 = op2; } }; // Assigns value a2 to a1. The point of time of the real operation can be controlled // with the () operator. template assigner assigno(t1 &a1, const t2 &a2) { return assigner(a1,a2); } //deleted pointer and sets it to NULL template void clear_pointer(T* &ptr) { delete ptr; ptr = NULL; } template std::unique_ptr make_unique() { return std::unique_ptr(new T()); } template std::unique_ptr make_unique(Arg1 &&arg1) { return std::unique_ptr(new T(std::forward(arg1))); } template std::unique_ptr make_unique(Arg1 &&arg1, Arg2 &&arg2) { return std::unique_ptr(new T(std::forward(arg1), std::forward(arg2))); } template typename Container::const_reference circularAt(const Container &r, size_t index) { assert(r.size()); index %= r.size(); auto itr = boost::begin(r); std::advance(itr, index); return *itr; } template void erase_if(Range &vec, Predicate pred) { vec.erase(boost::remove_if(vec, pred),vec.end()); } template void erase_if(std::set &setContainer, Predicate pred) { auto itr = setContainer.begin(); auto endItr = setContainer.end(); while(itr != endItr) { auto tmpItr = itr++; if(pred(*tmpItr)) setContainer.erase(tmpItr); } } //works for map and bmap, maybe something else template void erase_if(std::map &container, Predicate pred) { auto itr = container.begin(); auto endItr = container.end(); while(itr != endItr) { auto tmpItr = itr++; if(pred(*tmpItr)) container.erase(tmpItr); } } template OutputIterator copy_if(const InputRange &input, OutputIterator result, Predicate pred) { return std::copy_if(boost::const_begin(input), boost::end(input), result, pred); } template std::insert_iterator set_inserter(Container &c) { return std::inserter(c, c.end()); } //Returns iterator to the element for which the value of ValueFunction is minimal template auto minElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(boost::begin(rng)) { typedef decltype(*boost::begin(rng)) ElemType; return boost::min_element(rng, [&] (ElemType lhs, ElemType rhs) -> bool { return vf(lhs) < vf(rhs); }); } //Returns iterator to the element for which the value of ValueFunction is maximal template auto maxElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(boost::begin(rng)) { typedef decltype(*boost::begin(rng)) ElemType; return boost::max_element(rng, [&] (ElemType lhs, ElemType rhs) -> bool { return vf(lhs) < vf(rhs); }); } } using std::shared_ptr; using std::unique_ptr; using std::make_shared; using vstd::make_unique; using vstd::operator-=; namespace range = boost::range; // can be used for counting arrays template char (&_ArrayCountObj(const T (&)[N]))[N]; #define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr))) #define THROW_FORMAT(message, formatting_elems) throw std::runtime_error(boost::str(boost::format(message) % formatting_elems)) //XXX pls dont - 'debug macros' are usually more trouble than it's worth #define HANDLE_EXCEPTION \ catch (const std::exception& e) { \ tlog1 << e.what() << std::endl; \ throw; \ } \ catch (const std::exception * e) \ { \ tlog1 << e->what()<< std::endl; \ throw; \ } \ catch (const std::string& e) { \ tlog1 << e << std::endl; \ throw; \ } #define HANDLE_EXCEPTIONC(COMMAND) \ catch (const std::exception& e) { \ COMMAND; \ tlog1 << e.what() << std::endl; \ throw; \ } \ catch (const std::string &e) \ { \ COMMAND; \ tlog1 << e << std::endl; \ throw; \ } #include "lib/CLogger.h"