/Src/Dependencies/Boost/boost/interprocess/containers/container/detail/utilities.hpp

http://hadesmem.googlecode.com/ · C++ Header · 148 lines · 102 code · 28 blank · 18 comment · 6 complexity · 6b7bb117a6e99afc7d971446eb4399ea MD5 · raw file

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2009. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINERS_DETAIL_UTILITIES_HPP
  11. #define BOOST_CONTAINERS_DETAIL_UTILITIES_HPP
  12. #include "config_begin.hpp"
  13. #include <cstdio>
  14. #include <boost/type_traits/is_fundamental.hpp>
  15. #include <boost/type_traits/is_pointer.hpp>
  16. #include <boost/type_traits/is_enum.hpp>
  17. #include <boost/type_traits/is_member_pointer.hpp>
  18. #include INCLUDE_BOOST_CONTAINER_MOVE_HPP
  19. #include INCLUDE_BOOST_CONTAINER_DETAIL_MPL_HPP
  20. #include INCLUDE_BOOST_CONTAINER_DETAIL_TYPE_TRAITS_HPP
  21. #include <algorithm>
  22. namespace boost {
  23. namespace container {
  24. namespace containers_detail {
  25. template<class T>
  26. const T &max_value(const T &a, const T &b)
  27. { return a > b ? a : b; }
  28. template<class T>
  29. const T &min_value(const T &a, const T &b)
  30. { return a < b ? a : b; }
  31. template <class SizeType>
  32. SizeType
  33. get_next_capacity(const SizeType max_size
  34. ,const SizeType capacity
  35. ,const SizeType n)
  36. {
  37. // if (n > max_size - capacity)
  38. // throw std::length_error("get_next_capacity");
  39. const SizeType m3 = max_size/3;
  40. if (capacity < m3)
  41. return capacity + max_value(3*(capacity+1)/5, n);
  42. if (capacity < m3*2)
  43. return capacity + max_value((capacity+1)/2, n);
  44. return max_size;
  45. }
  46. template<class SmartPtr>
  47. struct smart_ptr_type
  48. {
  49. typedef typename SmartPtr::value_type value_type;
  50. typedef value_type *pointer;
  51. static pointer get (const SmartPtr &smartptr)
  52. { return smartptr.get();}
  53. };
  54. template<class T>
  55. struct smart_ptr_type<T*>
  56. {
  57. typedef T value_type;
  58. typedef value_type *pointer;
  59. static pointer get (pointer ptr)
  60. { return ptr;}
  61. };
  62. //!Overload for smart pointers to avoid ADL problems with get_pointer
  63. template<class Ptr>
  64. inline typename smart_ptr_type<Ptr>::pointer
  65. get_pointer(const Ptr &ptr)
  66. { return smart_ptr_type<Ptr>::get(ptr); }
  67. //!To avoid ADL problems with swap
  68. template <class T>
  69. inline void do_swap(T& x, T& y)
  70. {
  71. using std::swap;
  72. swap(x, y);
  73. }
  74. //Rounds "orig_size" by excess to round_to bytes
  75. inline std::size_t get_rounded_size(std::size_t orig_size, std::size_t round_to)
  76. {
  77. return ((orig_size-1)/round_to+1)*round_to;
  78. }
  79. template <std::size_t OrigSize, std::size_t RoundTo>
  80. struct ct_rounded_size
  81. {
  82. enum { value = ((OrigSize-1)/RoundTo+1)*RoundTo };
  83. };
  84. template <class _TypeT>
  85. struct __rw_is_enum
  86. {
  87. struct _C_no { };
  88. struct _C_yes { int _C_dummy [2]; };
  89. struct _C_indirect {
  90. // prevent classes with user-defined conversions from matching
  91. // use double to prevent float->int gcc conversion warnings
  92. _C_indirect (double);
  93. };
  94. // nested struct gets rid of bogus gcc errors
  95. struct _C_nest {
  96. // supply first argument to prevent HP aCC warnings
  97. static _C_no _C_is (int, ...);
  98. static _C_yes _C_is (int, _C_indirect);
  99. static _TypeT _C_make_T ();
  100. };
  101. enum {
  102. _C_val = sizeof (_C_yes)
  103. == sizeof (_C_nest::_C_is (0, _C_nest::_C_make_T ()))
  104. && !::boost::is_fundamental<_TypeT>::value
  105. };
  106. };
  107. template<class T>
  108. struct move_const_ref_type
  109. : if_c
  110. < ::boost::is_fundamental<T>::value || ::boost::is_pointer<T>::value ||
  111. ::boost::is_member_pointer<T>::value || ::boost::is_enum<T>::value
  112. ,const T &
  113. ,BOOST_MOVE_MACRO_CATCH_CONST_RLVALUE(T)
  114. >
  115. {};
  116. } //namespace containers_detail {
  117. } //namespace container {
  118. } //namespace boost {
  119. #include INCLUDE_BOOST_CONTAINER_DETAIL_CONFIG_END_HPP
  120. #endif //#ifndef BOOST_CONTAINERS_DETAIL_UTILITIES_HPP