BsStdHeaders.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #include <unordered_map>
  27. #include <unordered_set>
  28. // STL algorithms & functions
  29. #include <algorithm>
  30. #include <functional>
  31. #include <limits>
  32. // C++ Stream stuff
  33. #include <fstream>
  34. #include <iostream>
  35. #include <iomanip>
  36. #include <sstream>
  37. extern "C" {
  38. # include <sys/types.h>
  39. # include <sys/stat.h>
  40. }
  41. #if BS_PLATFORM == BS_PLATFORM_WIN32
  42. # undef min
  43. # undef max
  44. # if !defined(NOMINMAX) && defined(_MSC_VER)
  45. # define NOMINMAX // required to stop windows.h messing up std::min
  46. # endif
  47. # if defined( __MINGW32__ )
  48. # include <unistd.h>
  49. # endif
  50. #endif
  51. #if BS_PLATFORM == BS_PLATFORM_LINUX
  52. extern "C" {
  53. # include <unistd.h>
  54. # include <dlfcn.h>
  55. }
  56. #endif
  57. #if BS_PLATFORM == BS_PLATFORM_OSX
  58. extern "C" {
  59. # include <unistd.h>
  60. # include <sys/param.h>
  61. # include <CoreFoundation/CoreFoundation.h>
  62. }
  63. #endif
  64. namespace BansheeEngine
  65. {
  66. /** @addtogroup Containers
  67. * @{
  68. */
  69. /** Double ended queue. Allows for fast insertion and removal at both its beggining and end. */
  70. template <typename T, typename A = StdAlloc<T>>
  71. using Deque = std::deque<T, A>;
  72. /** Dynamically sized array that stores element contigously. */
  73. template <typename T, typename A = StdAlloc<T>>
  74. using Vector = std::vector<T, A>;
  75. /** Container that supports constant time insertion and removal, but without fast random access to elements. */
  76. template <typename T, typename A = StdAlloc<T>>
  77. using List = std::list<T, A>;
  78. /** First-in, last-out data structure. */
  79. template <typename T, typename A = StdAlloc<T>>
  80. using Stack = std::stack<T, std::deque<T, A>>;
  81. /** First-in, first-out data structure. */
  82. template <typename T, typename A = StdAlloc<T>>
  83. using Queue = std::queue<T, std::deque<T, A>>;
  84. /** An associative container containing an ordered set of elements. */
  85. template <typename T, typename P = std::less<T>, typename A = StdAlloc<T>>
  86. using Set = std::set<T, P, A>;
  87. /** An associative container containing an ordered set of key-value pairs. */
  88. template <typename K, typename V, typename P = std::less<K>, typename A = StdAlloc<std::pair<const K, V>>>
  89. using Map = std::map<K, V, P, A>;
  90. /** An associative container containing an ordered set of key-value pairs where multiple elements can have the same key. */
  91. template <typename K, typename V, typename P = std::less<K>, typename A = StdAlloc<std::pair<const K, V>>>
  92. using MultiMap = std::multimap<K, V, P, A>;
  93. /** An associative container containing an unordered set of elements. Usually faster than Set for larger data sets. */
  94. template <typename T, typename H = std::hash<T>, typename C = std::equal_to<T>, typename A = StdAlloc<T>>
  95. using UnorderedSet = std::unordered_set<T, H, C, A>;
  96. /** An associative container containing an ordered set of key-value pairs. Usually faster than Map for larger data sets. */
  97. template <typename K, typename V, typename H = std::hash<K>, typename C = std::equal_to<K>, typename A = StdAlloc<std::pair<const K, V>>>
  98. using UnorderedMap = std::unordered_map<K, V, H, C, A>;
  99. /**
  100. * An associative container containing an ordered set of key-value pairs where multiple elements can have the same key.
  101. * Usually faster than MultiMap for larger data sets.
  102. */
  103. template <typename K, typename V, typename H = std::hash<K>, typename C = std::equal_to<K>, typename A = StdAlloc<std::pair<const K, V>>>
  104. using UnorderedMultimap = std::unordered_multimap<K, V, H, C, A>;
  105. /** @} */
  106. /** @addtogroup Memory
  107. * @{
  108. */
  109. /**
  110. * Smart pointer that retains shared ownership of an project through a pointer. The object is destroyed automatically
  111. * when the last shared pointer to the object is destroyed.
  112. */
  113. template <typename T>
  114. using SPtr = std::shared_ptr<T>;
  115. /**
  116. * Smart pointer that retains shared ownership of an project through a pointer. Reference to the object must be unique.
  117. * The object is destroyed automatically when the pointer to the object is destroyed.
  118. */
  119. template <typename T, typename Alloc = GenAlloc>
  120. using UPtr = std::unique_ptr<T, decltype(&bs_delete<T, Alloc>)>;
  121. /** Create a new shared pointer using a custom allocator category. */
  122. template<class Type, class AllocCategory, class... Args>
  123. SPtr<Type> bs_shared_ptr_new(Args &&... args)
  124. {
  125. return std::allocate_shared<Type>(StdAlloc<Type, AllocCategory>(), std::forward<Args>(args)...);
  126. }
  127. /** Create a new shared pointer using the default allocator category. */
  128. template<class Type, class... Args>
  129. SPtr<Type> bs_shared_ptr_new(Args &&... args)
  130. {
  131. return std::allocate_shared<Type>(StdAlloc<Type, GenAlloc>(), std::forward<Args>(args)...);
  132. }
  133. /**
  134. * Create a new shared pointer from a previously constructed object.
  135. * Pointer specific data will be allocated using the provided allocator category.
  136. */
  137. template<class Type, class MainAlloc = GenAlloc, class PtrDataAlloc = GenAlloc>
  138. SPtr<Type> bs_shared_ptr(Type* data)
  139. {
  140. return SPtr<Type>(data, &bs_delete<Type, MainAlloc>, StdAlloc<Type, PtrDataAlloc>());
  141. }
  142. /**
  143. * Create a new unique pointer from a previously constructed object.
  144. * Pointer specific data will be allocated using the provided allocator category.
  145. */
  146. template<class Type, class Alloc = GenAlloc>
  147. UPtr<Type, Alloc> bs_unique_ptr(Type* data)
  148. {
  149. return std::unique_ptr<Type, decltype(&bs_delete<Type, Alloc>)>(data, bs_delete<Type, Alloc>);
  150. }
  151. /** Create a new unique pointer using a custom allocator category. */
  152. template<class Type, class Alloc, class... Args>
  153. UPtr<Type> bs_unique_ptr_new(Args &&... args)
  154. {
  155. Type* rawPtr = bs_new<Type, Alloc>(std::forward<Args>(args)...);
  156. return bs_unique_ptr<Type, Alloc>(rawPtr);
  157. }
  158. /** Create a new unique pointer using the default allocator category. */
  159. template<class Type, class... Args>
  160. UPtr<Type> bs_unique_ptr_new(Args &&... args)
  161. {
  162. Type* rawPtr = bs_new<Type, GenAlloc>(std::forward<Args>(args)...);
  163. return bs_unique_ptr<Type, GenAlloc>(rawPtr);
  164. }
  165. /** @} */
  166. }