CmStdHeaders.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #pragma once
  2. #ifdef __BORLANDC__
  3. #define __STD_ALGORITHM
  4. #endif
  5. #include <cassert>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <ctime>
  9. #include <cstring>
  10. #include <cstdarg>
  11. #include <cmath>
  12. #include <memory>
  13. #include <boost/shared_array.hpp>
  14. #include <boost/preprocessor.hpp>
  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. // Note - not in the original STL, but exists in SGI STL and STLport
  26. // For gcc 4.3 see http://gcc.gnu.org/gcc-4.3/changes.html
  27. #if (CM_COMPILER == CM_COMPILER_GNUC)
  28. # if CM_COMP_VER >= 430
  29. # include <tr1/unordered_map>
  30. # include <tr1/unordered_set>
  31. # else
  32. # include <ext/hash_map>
  33. # include <ext/hash_set>
  34. # endif
  35. #else
  36. # if (CM_COMPILER == CM_COMPILER_MSVC) && CM_COMP_VER >= 1600 // VC++ 10.0
  37. # include <unordered_map>
  38. # include <unordered_set>
  39. # else
  40. # include <hash_set>
  41. # include <hash_map>
  42. # endif
  43. #endif
  44. // STL algorithms & functions
  45. #include <algorithm>
  46. #include <functional>
  47. #include <limits>
  48. // C++ Stream stuff
  49. #include <fstream>
  50. #include <iostream>
  51. #include <iomanip>
  52. #include <sstream>
  53. #ifdef __BORLANDC__
  54. namespace CamelotFramework
  55. {
  56. using namespace std;
  57. }
  58. #endif
  59. extern "C" {
  60. # include <sys/types.h>
  61. # include <sys/stat.h>
  62. }
  63. #if CM_PLATFORM == CM_PLATFORM_WIN32
  64. # undef min
  65. # undef max
  66. # if !defined(NOMINMAX) && defined(_MSC_VER)
  67. # define NOMINMAX // required to stop windows.h messing up std::min
  68. # endif
  69. # if defined( __MINGW32__ )
  70. # include <unistd.h>
  71. # endif
  72. #endif
  73. #if CM_PLATFORM == CM_PLATFORM_LINUX
  74. extern "C" {
  75. # include <unistd.h>
  76. # include <dlfcn.h>
  77. }
  78. #endif
  79. #if CM_PLATFORM == CM_PLATFORM_APPLE
  80. extern "C" {
  81. # include <unistd.h>
  82. # include <sys/param.h>
  83. # include <CoreFoundation/CoreFoundation.h>
  84. }
  85. #endif
  86. namespace CamelotFramework
  87. {
  88. // Standard containers, for easier access in my own namespace
  89. template <typename T, typename A = StdAlloc<T>>
  90. struct Deque
  91. {
  92. typedef typename std::deque<T, A> type;
  93. };
  94. template <typename T, typename A = StdAlloc<T>>
  95. struct Vector
  96. {
  97. typedef typename std::vector<T, A> type;
  98. };
  99. template <typename T, typename A = StdAlloc<T>>
  100. struct List
  101. {
  102. typedef typename std::list<T, A> type;
  103. };
  104. template <typename T, typename A = StdAlloc<T>>
  105. struct Stack
  106. {
  107. typedef typename std::stack<T, std::deque<T, A>> type;
  108. };
  109. template <typename T, typename A = StdAlloc<T>>
  110. struct Queue
  111. {
  112. typedef typename std::queue<T, std::deque<T, A>> type;
  113. };
  114. template <typename T, typename P = std::less<T>, typename A = StdAlloc<T>>
  115. struct Set
  116. {
  117. typedef typename std::set<T, P, A> type;
  118. };
  119. template <typename K, typename V, typename P = std::less<K>, typename A = StdAlloc<std::pair<const K, V>>>
  120. struct Map
  121. {
  122. typedef typename std::map<K, V, P, A> type;
  123. };
  124. template <typename K, typename V, typename P = std::less<K>, typename A = StdAlloc<std::pair<const K, V>>>
  125. struct Multimap
  126. {
  127. typedef typename std::multimap<K, V, P, A> type;
  128. };
  129. template <typename T, typename H = std::hash<T>, typename C = std::equal_to<T>, typename A = StdAlloc<T>>
  130. struct UnorderedSet
  131. {
  132. typedef typename std::unordered_set<T, H, C, A> type;
  133. };
  134. template <typename K, typename V, typename H = std::hash<K>, typename C = std::equal_to<K>, typename A = StdAlloc<std::pair<const K, V>>>
  135. struct UnorderedMap
  136. {
  137. typedef typename std::unordered_map<K, V, H, C, A> type;
  138. };
  139. template <typename K, typename V, typename H = std::hash<K>, typename C = std::equal_to<K>, typename A = StdAlloc<std::pair<const K, V>>>
  140. struct UnorderedMultimap
  141. {
  142. typedef typename std::unordered_multimap<K, V, H, C, A> type;
  143. };
  144. //// Create a new shared pointer with a custom allocator category
  145. // Implementation for 0-5 parameters uses allocate_shared
  146. #define MAKE_CM_NEW_SHARED(z, n, unused) \
  147. template<class Type, class AllocCategory BOOST_PP_ENUM_TRAILING_PARAMS(n, class T)> \
  148. std::shared_ptr<Type> cm_shared_ptr(BOOST_PP_ENUM_BINARY_PARAMS(n, T, t) ) { \
  149. return std::allocate_shared<Type>(StdAlloc<AllocCategory>() BOOST_PP_ENUM_TRAILING_PARAMS (n, t)); \
  150. }
  151. BOOST_PP_REPEAT_FROM_TO(0, 6, MAKE_CM_NEW_SHARED, ~)
  152. #undef MAKE_CM_NEW_SHARED
  153. // Implementation for more than 5 params uses shared_ptr constructor as allocate_shared only accepts up to 5 params
  154. #define MAKE_CM_NEW_SHARED(z, n, unused) \
  155. template<class Type, class AllocCategory BOOST_PP_ENUM_TRAILING_PARAMS(n, class T)> \
  156. std::shared_ptr<Type> cm_shared_ptr(BOOST_PP_ENUM_BINARY_PARAMS(n, T, t) ) { \
  157. return std::shared_ptr<Type>(cm_new<Type, AllocCategory>(BOOST_PP_ENUM_PARAMS (n, t)), &cm_delete<AllocCategory, Type>, StdAlloc<AllocCategory>()); \
  158. }
  159. BOOST_PP_REPEAT_FROM_TO(6, 15, MAKE_CM_NEW_SHARED, ~)
  160. #undef MAKE_CM_NEW_SHARED
  161. //// Create a new shared pointer with a general allocator category
  162. // Implementation for 0-5 parameters uses allocate_shared
  163. #define MAKE_CM_NEW_SHARED(z, n, unused) \
  164. template<class Type BOOST_PP_ENUM_TRAILING_PARAMS(n, class T)> \
  165. std::shared_ptr<Type> cm_shared_ptr(BOOST_PP_ENUM_BINARY_PARAMS(n, T, t) ) { \
  166. return std::allocate_shared<Type>(StdAlloc<GenAlloc>() BOOST_PP_ENUM_TRAILING_PARAMS (n, t)); \
  167. }
  168. BOOST_PP_REPEAT_FROM_TO(0, 6, MAKE_CM_NEW_SHARED, ~)
  169. #undef MAKE_CM_NEW_SHARED
  170. // Implementation for more than 5 params uses shared_ptr constructor as allocate_shared only accepts up to 5 params
  171. #define MAKE_CM_NEW_SHARED(z, n, unused) \
  172. template<class Type BOOST_PP_ENUM_TRAILING_PARAMS(n, class T)> \
  173. std::shared_ptr<Type> cm_shared_ptr(BOOST_PP_ENUM_BINARY_PARAMS(n, T, t) ) { \
  174. return std::shared_ptr<Type>(cm_new<Type, GenAlloc>(BOOST_PP_ENUM_PARAMS (n, t)), &cm_delete<GenAlloc, Type>, StdAlloc<GenAlloc>()); \
  175. }
  176. BOOST_PP_REPEAT_FROM_TO(6, 15, MAKE_CM_NEW_SHARED, ~)
  177. #undef MAKE_CM_NEW_SHARED
  178. template<class Type, class MainAlloc>
  179. std::shared_ptr<Type> cm_shared_ptr(Type* data)
  180. {
  181. return std::shared_ptr<Type>(data, &cm_delete<MainAlloc, Type>, StdAlloc<GenAlloc>());
  182. }
  183. template<class Type, class MainAlloc, class PtrDataAlloc>
  184. std::shared_ptr<Type> cm_shared_ptr(Type* data)
  185. {
  186. return std::shared_ptr<Type>(data, &cm_delete<MainAlloc, Type>, StdAlloc<PtrDataAlloc>());
  187. }
  188. // TODO - Once VC2012 grows up and adds proper C++11 support, uncomment this
  189. //template <typename T, typename A = char>
  190. //using deque = std::deque<T, A>;
  191. //template <typename T, typename A = char >
  192. //using vector = std::vector<T, A>;
  193. //template <typename T, typename A = char >
  194. //using list = std::list<T, A>;
  195. //template <typename T, typename P = std::less<T>, typename A = char >
  196. //using set = std::set<T, P, A>;
  197. //template <typename K, typename V, typename P = std::less<K>, typename A = char >
  198. //using map = std::map<K, V, P, A>;
  199. //template <typename K, typename V, typename P = std::less<K>, typename A = char >
  200. //using multimap = std::multimap<K, V, P, A>;
  201. }