Global.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. * Global.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. /* ---------------------------------------------------------------------------- */
  12. /* Compiler detection */
  13. /* ---------------------------------------------------------------------------- */
  14. // Fixed width bool data type is important for serialization
  15. static_assert(sizeof(bool) == 1, "Bool needs to be 1 byte in size.");
  16. /* ---------------------------------------------------------------------------- */
  17. /* System detection. */
  18. /* ---------------------------------------------------------------------------- */
  19. // Based on: http://sourceforge.net/p/predef/wiki/OperatingSystems/
  20. // and on: http://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor
  21. // TODO?: Should be moved to vstd\os_detect.h (and then included by Global.h)
  22. #ifdef _WIN16 // Defined for 16-bit environments
  23. # error "16-bit Windows isn't supported"
  24. #elif defined(_WIN64) // Defined for 64-bit environments
  25. # define VCMI_WINDOWS
  26. # define VCMI_WINDOWS_64
  27. #elif defined(_WIN32) // Defined for both 32-bit and 64-bit environments
  28. # define VCMI_WINDOWS
  29. # define VCMI_WINDOWS_32
  30. #elif defined(_WIN32_WCE)
  31. # error "Windows CE isn't supported"
  32. #elif defined(__linux__) || defined(__gnu_linux__) || defined(linux) || defined(__linux)
  33. # define VCMI_UNIX
  34. # define VCMI_XDG
  35. # if defined(__ANDROID__) || defined(ANDROID)
  36. # define VCMI_ANDROID
  37. # endif
  38. #elif defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
  39. # define VCMI_UNIX
  40. # define VCMI_XDG
  41. # define VCMI_FREEBSD
  42. #elif defined(__HAIKU__)
  43. # define VCMI_UNIX
  44. # define VCMI_XDG
  45. # define VCMI_HAIKU
  46. #elif defined(__GNU__) || defined(__gnu_hurd__) || (defined(__MACH__) && !defined(__APPLE__))
  47. # define VCMI_UNIX
  48. # define VCMI_XDG
  49. # define VCMI_HURD
  50. #elif defined(__APPLE__) && defined(__MACH__)
  51. # define VCMI_UNIX
  52. # define VCMI_APPLE
  53. # include "TargetConditionals.h"
  54. # if TARGET_OS_SIMULATOR || TARGET_IPHONE_SIMULATOR
  55. # define VCMI_IOS
  56. # define VCMI_IOS_SIM
  57. # elif TARGET_OS_IPHONE
  58. # define VCMI_IOS
  59. # elif TARGET_OS_MAC
  60. # define VCMI_MAC
  61. # else
  62. //# warning "Unknown Apple target."?
  63. # endif
  64. #else
  65. # error "This platform isn't supported"
  66. #endif
  67. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  68. #define VCMI_MOBILE
  69. #endif
  70. /* ---------------------------------------------------------------------------- */
  71. /* Commonly used C++, Boost headers */
  72. /* ---------------------------------------------------------------------------- */
  73. #ifdef VCMI_WINDOWS
  74. # ifndef WIN32_LEAN_AND_MEAN
  75. # define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers - delete this line if something is missing.
  76. # endif
  77. # ifndef NOMINMAX
  78. # define NOMINMAX // Exclude min/max macros from <Windows.h>. Use std::[min/max] from <algorithm> instead.
  79. # endif
  80. # ifndef _NO_W32_PSEUDO_MODIFIERS
  81. # define _NO_W32_PSEUDO_MODIFIERS // Exclude more macros for compiling with MinGW on Linux.
  82. # endif
  83. #endif
  84. /* ---------------------------------------------------------------------------- */
  85. /* A macro to force inlining some of our functions */
  86. /* ---------------------------------------------------------------------------- */
  87. // Compiler (at least MSVC) is not so smart here-> without that displaying is MUCH slower
  88. #ifdef _MSC_VER
  89. # define STRONG_INLINE __forceinline
  90. #elif __GNUC__
  91. # define STRONG_INLINE inline __attribute__((always_inline))
  92. #else
  93. # define STRONG_INLINE inline
  94. #endif
  95. #define _USE_MATH_DEFINES
  96. #include <algorithm>
  97. #include <any>
  98. #include <array>
  99. #include <atomic>
  100. #include <bitset>
  101. #include <cassert>
  102. #include <climits>
  103. #include <cmath>
  104. #include <codecvt>
  105. #include <cstdlib>
  106. #include <cstdio>
  107. #include <fstream>
  108. #include <functional>
  109. #include <iomanip>
  110. #include <iostream>
  111. #include <map>
  112. #include <memory>
  113. #include <mutex>
  114. #include <numeric>
  115. #include <optional>
  116. #include <queue>
  117. #include <random>
  118. #include <set>
  119. #include <sstream>
  120. #include <string>
  121. #include <unordered_map>
  122. #include <unordered_set>
  123. #include <utility>
  124. #include <variant>
  125. #include <vector>
  126. //The only available version is 3, as of Boost 1.50
  127. #include <boost/version.hpp>
  128. #define BOOST_FILESYSTEM_VERSION 3
  129. #if BOOST_VERSION > 105000
  130. # define BOOST_THREAD_VERSION 3
  131. #endif
  132. #define BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE 1
  133. //need to link boost thread dynamically to avoid https://stackoverflow.com/questions/35978572/boost-thread-interupt-does-not-work-when-crossing-a-dll-boundary
  134. #define BOOST_THREAD_USE_DLL //for example VCAI::finish() may freeze on thread join after interrupt when linking this statically
  135. #define BOOST_BIND_NO_PLACEHOLDERS
  136. #if BOOST_VERSION >= 106600
  137. #define BOOST_ASIO_ENABLE_OLD_SERVICES
  138. #endif
  139. #include <boost/algorithm/string.hpp>
  140. #include <boost/crc.hpp>
  141. #include <boost/current_function.hpp>
  142. #include <boost/date_time/posix_time/posix_time_types.hpp>
  143. #include <boost/date_time/posix_time/time_formatters.hpp>
  144. #include <boost/filesystem.hpp>
  145. #include <boost/filesystem/fstream.hpp>
  146. #include <boost/filesystem/path.hpp>
  147. #include <boost/format.hpp>
  148. #include <boost/functional/hash.hpp>
  149. #include <boost/lexical_cast.hpp>
  150. #ifdef VCMI_WINDOWS
  151. #include <boost/locale/generator.hpp>
  152. #endif
  153. #include <boost/logic/tribool.hpp>
  154. #include <boost/multi_array.hpp>
  155. #include <boost/range/adaptor/filtered.hpp>
  156. #include <boost/range/adaptor/reversed.hpp>
  157. #include <boost/range/algorithm.hpp>
  158. #include <boost/thread/thread_only.hpp>
  159. #include <boost/thread/shared_mutex.hpp>
  160. #include <boost/thread/recursive_mutex.hpp>
  161. #include <boost/thread/once.hpp>
  162. #ifndef M_PI
  163. # define M_PI 3.14159265358979323846
  164. #endif
  165. /* ---------------------------------------------------------------------------- */
  166. /* Usings */
  167. /* ---------------------------------------------------------------------------- */
  168. using namespace std::placeholders;
  169. namespace range = boost::range;
  170. /* ---------------------------------------------------------------------------- */
  171. /* Typedefs */
  172. /* ---------------------------------------------------------------------------- */
  173. // Integral data types
  174. typedef uint64_t ui64; //unsigned int 64 bits (8 bytes)
  175. typedef uint32_t ui32; //unsigned int 32 bits (4 bytes)
  176. typedef uint16_t ui16; //unsigned int 16 bits (2 bytes)
  177. typedef uint8_t ui8; //unsigned int 8 bits (1 byte)
  178. typedef int64_t si64; //signed int 64 bits (8 bytes)
  179. typedef int32_t si32; //signed int 32 bits (4 bytes)
  180. typedef int16_t si16; //signed int 16 bits (2 bytes)
  181. typedef int8_t si8; //signed int 8 bits (1 byte)
  182. // Lock typedefs
  183. using TLockGuard = std::lock_guard<std::mutex>;
  184. using TLockGuardRec = std::lock_guard<std::recursive_mutex>;
  185. /* ---------------------------------------------------------------------------- */
  186. /* Macros */
  187. /* ---------------------------------------------------------------------------- */
  188. // Import + Export macro declarations
  189. #ifdef VCMI_WINDOWS
  190. #ifdef VCMI_DLL_STATIC
  191. # define DLL_IMPORT
  192. # define DLL_EXPORT
  193. #elif defined(__GNUC__)
  194. # define DLL_IMPORT __attribute__((dllimport))
  195. # define DLL_EXPORT __attribute__((dllexport))
  196. # else
  197. # define DLL_IMPORT __declspec(dllimport)
  198. # define DLL_EXPORT __declspec(dllexport)
  199. # endif
  200. # define ELF_VISIBILITY
  201. #else
  202. # ifdef __GNUC__
  203. # define DLL_IMPORT __attribute__ ((visibility("default")))
  204. # define DLL_EXPORT __attribute__ ((visibility("default")))
  205. # define ELF_VISIBILITY __attribute__ ((visibility("default")))
  206. # endif
  207. #endif
  208. #ifdef VCMI_DLL
  209. # define DLL_LINKAGE DLL_EXPORT
  210. #else
  211. # define DLL_LINKAGE DLL_IMPORT
  212. #endif
  213. #define THROW_FORMAT(message, formatting_elems) throw std::runtime_error(boost::str(boost::format(message) % formatting_elems))
  214. // old iOS SDKs compatibility
  215. #ifdef VCMI_IOS
  216. #include <AvailabilityVersions.h>
  217. #ifndef __IPHONE_13_0
  218. #define __IPHONE_13_0 130000
  219. #endif
  220. #endif // VCMI_IOS
  221. // single-process build makes 2 copies of the main lib by wrapping it in a namespace
  222. #ifdef VCMI_LIB_NAMESPACE
  223. #define VCMI_LIB_NAMESPACE_BEGIN namespace VCMI_LIB_NAMESPACE {
  224. #define VCMI_LIB_NAMESPACE_END }
  225. #define VCMI_LIB_USING_NAMESPACE using namespace VCMI_LIB_NAMESPACE;
  226. #define VCMI_LIB_WRAP_NAMESPACE(x) VCMI_LIB_NAMESPACE::x
  227. #else
  228. #define VCMI_LIB_NAMESPACE_BEGIN
  229. #define VCMI_LIB_NAMESPACE_END
  230. #define VCMI_LIB_USING_NAMESPACE
  231. #define VCMI_LIB_WRAP_NAMESPACE(x) ::x
  232. #endif
  233. /* ---------------------------------------------------------------------------- */
  234. /* VCMI standard library */
  235. /* ---------------------------------------------------------------------------- */
  236. #include <vstd/CLoggerBase.h>
  237. VCMI_LIB_NAMESPACE_BEGIN
  238. namespace vstd
  239. {
  240. // combine hashes. Present in boost but not in std
  241. template <class T>
  242. inline void hash_combine(std::size_t& seed, const T& v)
  243. {
  244. std::hash<T> hasher;
  245. seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
  246. }
  247. //returns true if container c contains item i
  248. template <typename Container, typename Item>
  249. bool contains(const Container & c, const Item &i)
  250. {
  251. return std::find(std::begin(c), std::end(c),i) != std::end(c);
  252. }
  253. //returns true if container c contains item i
  254. template <typename Container, typename Pred>
  255. bool contains_if(const Container & c, Pred p)
  256. {
  257. return std::find_if(std::begin(c), std::end(c), p) != std::end(c);
  258. }
  259. //returns true if map c contains item i
  260. template <typename V, typename Item, typename Item2>
  261. bool contains(const std::map<Item,V> & c, const Item2 &i)
  262. {
  263. return c.find(i)!=c.end();
  264. }
  265. //returns true if unordered set c contains item i
  266. template <typename Item>
  267. bool contains(const std::unordered_set<Item> & c, const Item &i)
  268. {
  269. return c.find(i)!=c.end();
  270. }
  271. template <typename V, typename Item, typename Item2>
  272. bool contains(const std::unordered_map<Item,V> & c, const Item2 &i)
  273. {
  274. return c.find(i)!=c.end();
  275. }
  276. //returns position of first element in vector c equal to s, if there is no such element, -1 is returned
  277. template <typename Container, typename T2>
  278. int find_pos(const Container & c, const T2 &s)
  279. {
  280. int i=0;
  281. for (auto iter = std::begin(c); iter != std::end(c); iter++, i++)
  282. if(*iter == s)
  283. return i;
  284. return -1;
  285. }
  286. //Func f tells if element matches
  287. template <typename Container, typename Func>
  288. int find_pos_if(const Container & c, const Func &f)
  289. {
  290. auto ret = boost::range::find_if(c, f);
  291. if(ret != std::end(c))
  292. return std::distance(std::begin(c), ret);
  293. return -1;
  294. }
  295. //returns iterator to the given element if present in container, end() if not
  296. template <typename Container, typename Item>
  297. typename Container::iterator find(Container & c, const Item &i)
  298. {
  299. return std::find(c.begin(),c.end(),i);
  300. }
  301. //returns const iterator to the given element if present in container, end() if not
  302. template <typename Container, typename Item>
  303. typename Container::const_iterator find(const Container & c, const Item &i)
  304. {
  305. return std::find(c.begin(),c.end(),i);
  306. }
  307. //returns first key that maps to given value if present, returns success via found if provided
  308. template <typename Key, typename T>
  309. Key findKey(const std::map<Key, T> & map, const T & value, bool * found = nullptr)
  310. {
  311. for(auto iter = map.cbegin(); iter != map.cend(); iter++)
  312. {
  313. if(iter->second == value)
  314. {
  315. if(found)
  316. *found = true;
  317. return iter->first;
  318. }
  319. }
  320. if(found)
  321. *found = false;
  322. return Key();
  323. }
  324. //removes element i from container c, returns false if c does not contain i
  325. template <typename Container, typename Item>
  326. typename Container::size_type operator-=(Container &c, const Item &i)
  327. {
  328. typename Container::iterator itr = find(c,i);
  329. if(itr == c.end())
  330. return false;
  331. c.erase(itr);
  332. return true;
  333. }
  334. //assigns greater of (a, b) to a and returns maximum of (a, b)
  335. template <typename t1, typename t2>
  336. t1 &amax(t1 &a, const t2 &b)
  337. {
  338. if(a >= (t1)b)
  339. return a;
  340. else
  341. {
  342. a = t1(b);
  343. return a;
  344. }
  345. }
  346. //assigns smaller of (a, b) to a and returns minimum of (a, b)
  347. template <typename t1, typename t2>
  348. t1 &amin(t1 &a, const t2 &b)
  349. {
  350. if(a <= (t1)b)
  351. return a;
  352. else
  353. {
  354. a = t1(b);
  355. return a;
  356. }
  357. }
  358. //makes a to fit the range <b, c>
  359. template <typename T>
  360. void abetween(T &value, const T &min, const T &max)
  361. {
  362. value = std::clamp(value, min, max);
  363. }
  364. //checks if a is between b and c
  365. template <typename t1, typename t2, typename t3>
  366. bool isbetween(const t1 &value, const t2 &min, const t3 &max)
  367. {
  368. return value > (t1)min && value < (t1)max;
  369. }
  370. //checks if a is within b and c
  371. template <typename t1, typename t2, typename t3>
  372. bool iswithin(const t1 &value, const t2 &min, const t3 &max)
  373. {
  374. return value >= (t1)min && value <= (t1)max;
  375. }
  376. template <typename t1, typename t2>
  377. struct assigner
  378. {
  379. public:
  380. t1 &op1;
  381. t2 op2;
  382. assigner(t1 &a1, const t2 & a2)
  383. :op1(a1), op2(a2)
  384. {}
  385. void operator()()
  386. {
  387. op1 = op2;
  388. }
  389. };
  390. //deleted pointer and sets it to nullptr
  391. template <typename T>
  392. void clear_pointer(T* &ptr)
  393. {
  394. delete ptr;
  395. ptr = nullptr;
  396. }
  397. template <typename Container>
  398. typename Container::const_reference circularAt(const Container &r, size_t index)
  399. {
  400. assert(r.size());
  401. index %= r.size();
  402. auto itr = std::begin(r);
  403. std::advance(itr, index);
  404. return *itr;
  405. }
  406. template <typename Container, typename Item>
  407. void erase(Container &c, const Item &item)
  408. {
  409. c.erase(boost::remove(c, item), c.end());
  410. }
  411. template<typename Range, typename Predicate>
  412. void erase_if(Range &vec, Predicate pred)
  413. {
  414. vec.erase(boost::remove_if(vec, pred),vec.end());
  415. }
  416. template<typename Elem, typename Predicate>
  417. void erase_if(std::set<Elem> &setContainer, Predicate pred)
  418. {
  419. auto itr = setContainer.begin();
  420. auto endItr = setContainer.end();
  421. while(itr != endItr)
  422. {
  423. auto tmpItr = itr++;
  424. if(pred(*tmpItr))
  425. setContainer.erase(tmpItr);
  426. }
  427. }
  428. //works for map and std::map, maybe something else
  429. template<typename Key, typename Val, typename Predicate>
  430. void erase_if(std::map<Key, Val> &container, Predicate pred)
  431. {
  432. auto itr = container.begin();
  433. auto endItr = container.end();
  434. while(itr != endItr)
  435. {
  436. auto tmpItr = itr++;
  437. if(pred(*tmpItr))
  438. container.erase(tmpItr);
  439. }
  440. }
  441. template<typename InputRange, typename OutputIterator, typename Predicate>
  442. OutputIterator copy_if(const InputRange &input, OutputIterator result, Predicate pred)
  443. {
  444. return std::copy_if(std::cbegin(input), std::end(input), result, pred);
  445. }
  446. template <typename Container>
  447. std::insert_iterator<Container> set_inserter(Container &c)
  448. {
  449. return std::inserter(c, c.end());
  450. }
  451. //Returns iterator to the element for which the value of ValueFunction is minimal
  452. template<class ForwardRange, class ValueFunction>
  453. auto minElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(std::begin(rng))
  454. {
  455. /* Clang crashes when instantiating this function template and having PCH compilation enabled.
  456. * There is a bug report here: http://llvm.org/bugs/show_bug.cgi?id=18744
  457. * Current bugfix is to don't use a typedef for decltype(*std::begin(rng)) and to use decltype
  458. * directly for both function parameters.
  459. */
  460. return boost::min_element(rng, [&] (decltype(*std::begin(rng)) lhs, decltype(*std::begin(rng)) rhs) -> bool
  461. {
  462. return vf(lhs) < vf(rhs);
  463. });
  464. }
  465. //Returns iterator to the element for which the value of ValueFunction is maximal
  466. template<class ForwardRange, class ValueFunction>
  467. auto maxElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(std::begin(rng))
  468. {
  469. /* Clang crashes when instantiating this function template and having PCH compilation enabled.
  470. * There is a bug report here: http://llvm.org/bugs/show_bug.cgi?id=18744
  471. * Current bugfix is to don't use a typedef for decltype(*std::begin(rng)) and to use decltype
  472. * directly for both function parameters.
  473. */
  474. return boost::max_element(rng, [&] (decltype(*std::begin(rng)) lhs, decltype(*std::begin(rng)) rhs) -> bool
  475. {
  476. return vf(lhs) < vf(rhs);
  477. });
  478. }
  479. /// Increments value by specific delta
  480. /// similar to std::next but works with other types, e.g. enum class
  481. template<typename T>
  482. T next(const T &obj, int change)
  483. {
  484. return static_cast<T>(static_cast<ptrdiff_t>(obj) + change);
  485. }
  486. template <typename Container>
  487. typename Container::value_type backOrNull(const Container &c) //returns last element of container or nullptr if it is empty (to be used with containers of pointers)
  488. {
  489. if(c.size())
  490. return c.back();
  491. else
  492. return typename Container::value_type();
  493. }
  494. template <typename Container>
  495. typename Container::value_type frontOrNull(const Container &c) //returns first element of container or nullptr if it is empty (to be used with containers of pointers)
  496. {
  497. if(c.size())
  498. return c.front();
  499. else
  500. return nullptr;
  501. }
  502. template <typename Container, typename Index>
  503. bool isValidIndex(const Container &c, Index i)
  504. {
  505. return i >= 0 && i < c.size();
  506. }
  507. template <typename Container>
  508. typename Container::const_reference atOrDefault(const Container &r, size_t index, const typename Container::const_reference &defaultValue)
  509. {
  510. if(index < r.size())
  511. return r[index];
  512. return defaultValue;
  513. }
  514. template <typename Container, typename Item>
  515. bool erase_if_present(Container &c, const Item &item)
  516. {
  517. auto i = std::find(c.begin(), c.end(), item);
  518. if (i != c.end())
  519. {
  520. c.erase(i);
  521. return true;
  522. }
  523. return false;
  524. }
  525. template <typename V, typename Item, typename Item2>
  526. bool erase_if_present(std::map<Item,V> & c, const Item2 &item)
  527. {
  528. auto i = c.find(item);
  529. if (i != c.end())
  530. {
  531. c.erase(i);
  532. return true;
  533. }
  534. return false;
  535. }
  536. template<typename T>
  537. void removeDuplicates(std::vector<T> &vec)
  538. {
  539. std::sort(vec.begin(), vec.end());
  540. vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  541. }
  542. template <typename T>
  543. void concatenate(std::vector<T> &dest, const std::vector<T> &src)
  544. {
  545. dest.reserve(dest.size() + src.size());
  546. dest.insert(dest.end(), src.begin(), src.end());
  547. }
  548. template <typename T>
  549. std::vector<T> intersection(std::vector<T> &v1, std::vector<T> &v2)
  550. {
  551. std::vector<T> v3;
  552. std::sort(v1.begin(), v1.end());
  553. std::sort(v2.begin(), v2.end());
  554. std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v3));
  555. return v3;
  556. }
  557. template <typename T>
  558. std::set<T> difference(const std::set<T> &s1, const std::set<T> s2)
  559. {
  560. std::set<T> s3;
  561. std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), std::inserter(s3, s3.end()));
  562. return s3;
  563. }
  564. template <typename Key, typename V>
  565. bool containsMapping(const std::multimap<Key,V> & map, const std::pair<const Key,V> & mapping)
  566. {
  567. auto range = map.equal_range(mapping.first);
  568. for(auto contained = range.first; contained != range.second; contained++)
  569. {
  570. if(mapping.second == contained->second)
  571. return true;
  572. }
  573. return false;
  574. }
  575. template<class M, class Key, class F>
  576. typename M::mapped_type & getOrCompute(M & m, const Key & k, F f)
  577. {
  578. typedef typename M::mapped_type V;
  579. std::pair<typename M::iterator, bool> r = m.insert(typename M::value_type(k, V()));
  580. V & v = r.first->second;
  581. if(r.second)
  582. f(v);
  583. return v;
  584. }
  585. //c++20 feature
  586. template<typename Arithmetic, typename Floating>
  587. Arithmetic lerp(const Arithmetic & a, const Arithmetic & b, const Floating & f)
  588. {
  589. return a + (b - a) * f;
  590. }
  591. ///compile-time version of std::abs for ints for int3, in clang++15 std::abs is constexpr
  592. static constexpr int abs(int i) {
  593. if(i < 0) return -i;
  594. return i;
  595. }
  596. ///C++23
  597. template< class Enum > constexpr std::underlying_type_t<Enum> to_underlying( Enum e ) noexcept
  598. {
  599. return static_cast<std::underlying_type_t<Enum>>(e);
  600. }
  601. }
  602. using vstd::operator-=;
  603. VCMI_LIB_NAMESPACE_END