BsStdHeaders.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #ifdef __BORLANDC__
  5. #define __STD_ALGORITHM
  6. #endif
  7. #include <cassert>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <ctime>
  11. #include <cstring>
  12. #include <cstdarg>
  13. #include <cmath>
  14. #include <memory>
  15. // STL containers
  16. #include <vector>
  17. #include <stack>
  18. #include <map>
  19. #include <string>
  20. #include <set>
  21. #include <list>
  22. #include <deque>
  23. #include <queue>
  24. #include <bitset>
  25. #include <array>
  26. // Note - not in the original STL, but exists in SGI STL and STLport
  27. // For gcc 4.3 see http://gcc.gnu.org/gcc-4.3/changes.html
  28. #if (BS_COMPILER == BS_COMPILER_GNUC)
  29. # if BS_COMP_VER >= 430
  30. # include <tr1/unordered_map>
  31. # include <tr1/unordered_set>
  32. # else
  33. # include <ext/hash_map>
  34. # include <ext/hash_set>
  35. # endif
  36. #else
  37. # if (BS_COMPILER == BS_COMPILER_MSVC) && BS_COMP_VER >= 1600 // VC++ 10.0
  38. # include <unordered_map>
  39. # include <unordered_set>
  40. # else
  41. # include <hash_set>
  42. # include <hash_map>
  43. # endif
  44. #endif
  45. // STL algorithms & functions
  46. #include <algorithm>
  47. #include <functional>
  48. #include <limits>
  49. // C++ Stream stuff
  50. #include <fstream>
  51. #include <iostream>
  52. #include <iomanip>
  53. #include <sstream>
  54. #ifdef __BORLANDC__
  55. namespace BansheeEngine
  56. {
  57. using namespace std;
  58. }
  59. #endif
  60. extern "C" {
  61. # include <sys/types.h>
  62. # include <sys/stat.h>
  63. }
  64. #if BS_PLATFORM == BS_PLATFORM_WIN32
  65. # undef min
  66. # undef max
  67. # if !defined(NOMINMAX) && defined(_MSC_VER)
  68. # define NOMINMAX // required to stop windows.h messing up std::min
  69. # endif
  70. # if defined( __MINGW32__ )
  71. # include <unistd.h>
  72. # endif
  73. #endif
  74. #if BS_PLATFORM == BS_PLATFORM_LINUX
  75. extern "C" {
  76. # include <unistd.h>
  77. # include <dlfcn.h>
  78. }
  79. #endif
  80. #if BS_PLATFORM == BS_PLATFORM_OSX
  81. extern "C" {
  82. # include <unistd.h>
  83. # include <sys/param.h>
  84. # include <CoreFoundation/CoreFoundation.h>
  85. }
  86. #endif
  87. namespace BansheeEngine
  88. {
  89. /** @addtogroup Containers
  90. * @{
  91. */
  92. /** Double ended queue. Allows for fast insertion and removal at both its beggining and end. */
  93. template <typename T, typename A = StdAlloc<T>>
  94. using Deque = std::deque<T, A>;
  95. /** Dynamically sized array that stores element contigously. */
  96. template <typename T, typename A = StdAlloc<T>>
  97. using Vector = std::vector<T, A>;
  98. /** Container that supports constant time insertion and removal, but without fast random access to elements. */
  99. template <typename T, typename A = StdAlloc<T>>
  100. using List = std::list<T, A>;
  101. /** First-in, last-out data structure. */
  102. template <typename T, typename A = StdAlloc<T>>
  103. using Stack = std::stack<T, std::deque<T, A>>;
  104. /** First-in, first-out data structure. */
  105. template <typename T, typename A = StdAlloc<T>>
  106. using Queue = std::queue<T, std::deque<T, A>>;
  107. /** An associative container containing an ordered set of elements. */
  108. template <typename T, typename P = std::less<T>, typename A = StdAlloc<T>>
  109. using Set = std::set<T, P, A>;
  110. /** An associative container containing an ordered set of key-value pairs. */
  111. template <typename K, typename V, typename P = std::less<K>, typename A = StdAlloc<std::pair<const K, V>>>
  112. using Map = std::map<K, V, P, A>;
  113. /** An associative container containing an ordered set of key-value pairs where multiple elements can have the same key. */
  114. template <typename K, typename V, typename P = std::less<K>, typename A = StdAlloc<std::pair<const K, V>>>
  115. using MultiMap = std::multimap<K, V, P, A>;
  116. /** An associative container containing an unordered set of elements. Usually faster than Set for larger data sets. */
  117. template <typename T, typename H = std::hash<T>, typename C = std::equal_to<T>, typename A = StdAlloc<T>>
  118. using UnorderedSet = std::unordered_set<T, H, C, A>;
  119. /** An associative container containing an ordered set of key-value pairs. Usually faster than Map for larger data sets. */
  120. template <typename K, typename V, typename H = std::hash<K>, typename C = std::equal_to<K>, typename A = StdAlloc<std::pair<const K, V>>>
  121. using UnorderedMap = std::unordered_map<K, V, H, C, A>;
  122. /**
  123. * An associative container containing an ordered set of key-value pairs where multiple elements can have the same key.
  124. * Usually faster than MultiMap for larger data sets.
  125. */
  126. template <typename K, typename V, typename H = std::hash<K>, typename C = std::equal_to<K>, typename A = StdAlloc<std::pair<const K, V>>>
  127. using UnorderedMultimap = std::unordered_multimap<K, V, H, C, A>;
  128. /** @} */
  129. /** @addtogroup Memory
  130. * @{
  131. */
  132. /**
  133. * Smart pointer that retains shared ownership of an project through a pointer. The object is destroyed automatically
  134. * when the last shared pointer to the object is destroyed.
  135. */
  136. template <typename T>
  137. using SPtr = std::shared_ptr<T>;
  138. /**
  139. * Smart pointer that retains shared ownership of an project through a pointer. Reference to the object must be unique.
  140. * The object is destroyed automatically when the pointer to the object is destroyed.
  141. */
  142. template <typename T, typename Alloc = GenAlloc>
  143. using UPtr = std::unique_ptr<T, decltype(&bs_delete<T, Alloc>)>;
  144. /** Create a new shared pointer using a custom allocator category. */
  145. template<class Type, class AllocCategory, class... Args>
  146. SPtr<Type> bs_shared_ptr_new(Args &&... args)
  147. {
  148. return std::allocate_shared<Type>(StdAlloc<Type, AllocCategory>(), std::forward<Args>(args)...);
  149. }
  150. /** Create a new shared pointer using the default allocator category. */
  151. template<class Type, class... Args>
  152. SPtr<Type> bs_shared_ptr_new(Args &&... args)
  153. {
  154. return std::allocate_shared<Type>(StdAlloc<Type, GenAlloc>(), std::forward<Args>(args)...);
  155. }
  156. /**
  157. * Create a new shared pointer from a previously constructed object.
  158. * Pointer specific data will be allocated using the provided allocator category.
  159. */
  160. template<class Type, class MainAlloc = GenAlloc, class PtrDataAlloc = GenAlloc>
  161. SPtr<Type> bs_shared_ptr(Type* data)
  162. {
  163. return std::shared_ptr<Type>(data, &bs_delete<Type, MainAlloc>, StdAlloc<Type, PtrDataAlloc>());
  164. }
  165. /** Create a new unique pointer using a custom allocator category. */
  166. template<class Type, class Alloc, class... Args>
  167. UPtr<Type> bs_unique_ptr_new(Args &&... args)
  168. {
  169. Type* rawPtr = bs_new<Type, Alloc>(std::forward<Args>(args)...);
  170. return bs_unique_ptr<Type, Alloc>(rawPtr);
  171. }
  172. /** Create a new unique pointer using the default allocator category. */
  173. template<class Type, class... Args>
  174. UPtr<Type> bs_unique_ptr_new(Args &&... args)
  175. {
  176. Type* rawPtr = bs_new<Type, GenAlloc>(std::forward<Args>(args)...);
  177. return bs_unique_ptr<Type, GenAlloc>(rawPtr);
  178. }
  179. /**
  180. * Create a new unique pointer from a previously constructed object.
  181. * Pointer specific data will be allocated using the provided allocator category.
  182. */
  183. template<class Type, class Alloc = GenAlloc>
  184. UPtr<Type, Alloc> bs_unique_ptr(Type* data)
  185. {
  186. return std::unique_ptr<Type, decltype(&bs_delete<Type, Alloc>)>(data, bs_delete<Type, Alloc>);
  187. }
  188. /** @} */
  189. }