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

http://hadesmem.googlecode.com/ · C++ Header · 170 lines · 115 code · 28 blank · 27 comment · 2 complexity · 731266ac60fccffa229c049948ba1696 MD5 · raw file

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2009.
  4. // (C) Copyright Gennaro Prota 2003 - 2004.
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // See http://www.boost.org/libs/interprocess for documentation.
  11. //
  12. //////////////////////////////////////////////////////////////////////////////
  13. #ifndef BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP
  14. #define BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP
  15. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  16. # pragma once
  17. #endif
  18. #include <boost/interprocess/detail/config_begin.hpp>
  19. #include <boost/interprocess/detail/workaround.hpp>
  20. #include <boost/interprocess/interprocess_fwd.hpp>
  21. #include <boost/interprocess/detail/move.hpp>
  22. #include <boost/type_traits/has_trivial_destructor.hpp>
  23. #include <boost/interprocess/detail/min_max.hpp>
  24. #include <boost/interprocess/detail/type_traits.hpp>
  25. #include <boost/interprocess/detail/transform_iterator.hpp>
  26. #include <boost/interprocess/detail/mpl.hpp>
  27. #include <boost/interprocess/containers/version_type.hpp>
  28. #include <boost/interprocess/detail/move.hpp>
  29. #include <utility>
  30. #include <algorithm>
  31. namespace boost {
  32. namespace interprocess {
  33. namespace detail {
  34. template<class SmartPtr>
  35. struct smart_ptr_type
  36. {
  37. typedef typename SmartPtr::value_type value_type;
  38. typedef value_type *pointer;
  39. static pointer get (const SmartPtr &smartptr)
  40. { return smartptr.get();}
  41. };
  42. template<class T>
  43. struct smart_ptr_type<T*>
  44. {
  45. typedef T value_type;
  46. typedef value_type *pointer;
  47. static pointer get (pointer ptr)
  48. { return ptr;}
  49. };
  50. //!Overload for smart pointers to avoid ADL problems with get_pointer
  51. template<class Ptr>
  52. inline typename smart_ptr_type<Ptr>::pointer
  53. get_pointer(const Ptr &ptr)
  54. { return smart_ptr_type<Ptr>::get(ptr); }
  55. //!To avoid ADL problems with swap
  56. template <class T>
  57. inline void do_swap(T& x, T& y)
  58. {
  59. using std::swap;
  60. swap(x, y);
  61. }
  62. //Rounds "orig_size" by excess to round_to bytes
  63. inline std::size_t get_rounded_size(std::size_t orig_size, std::size_t round_to)
  64. {
  65. return ((orig_size-1)/round_to+1)*round_to;
  66. }
  67. //Truncates "orig_size" to a multiple of "multiple" bytes.
  68. inline std::size_t get_truncated_size(std::size_t orig_size, std::size_t multiple)
  69. {
  70. return orig_size/multiple*multiple;
  71. }
  72. //Rounds "orig_size" by excess to round_to bytes. round_to must be power of two
  73. inline std::size_t get_rounded_size_po2(std::size_t orig_size, std::size_t round_to)
  74. {
  75. return ((orig_size-1)&(~(round_to-1))) + round_to;
  76. }
  77. //Truncates "orig_size" to a multiple of "multiple" bytes. multiple must be power of two
  78. inline std::size_t get_truncated_size_po2(std::size_t orig_size, std::size_t multiple)
  79. {
  80. return (orig_size & (~(multiple-1)));
  81. }
  82. template <std::size_t OrigSize, std::size_t RoundTo>
  83. struct ct_rounded_size
  84. {
  85. enum { value = ((OrigSize-1)/RoundTo+1)*RoundTo };
  86. };
  87. // Gennaro Prota wrote this. Thanks!
  88. template <int p, int n = 4>
  89. struct ct_max_pow2_less
  90. {
  91. enum { c = 2*n < p };
  92. static const std::size_t value =
  93. c ? (ct_max_pow2_less< c*p, 2*c*n>::value) : n;
  94. };
  95. template <>
  96. struct ct_max_pow2_less<0, 0>
  97. {
  98. static const std::size_t value = 0;
  99. };
  100. } //namespace detail {
  101. //!Trait class to detect if an index is a node
  102. //!index. This allows more efficient operations
  103. //!when deallocating named objects.
  104. template <class Index>
  105. struct is_node_index
  106. {
  107. enum { value = false };
  108. };
  109. //!Trait class to detect if an index is an intrusive
  110. //!index. This will embed the derivation hook in each
  111. //!allocation header, to provide memory for the intrusive
  112. //!container.
  113. template <class Index>
  114. struct is_intrusive_index
  115. {
  116. enum { value = false };
  117. };
  118. template <typename T> T*
  119. addressof(T& v)
  120. {
  121. return reinterpret_cast<T*>(
  122. &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
  123. }
  124. //Anti-exception node eraser
  125. template<class Cont>
  126. class value_eraser
  127. {
  128. public:
  129. value_eraser(Cont & cont, typename Cont::iterator it)
  130. : m_cont(cont), m_index_it(it), m_erase(true){}
  131. ~value_eraser()
  132. { if(m_erase) m_cont.erase(m_index_it); }
  133. void release() { m_erase = false; }
  134. private:
  135. Cont &m_cont;
  136. typename Cont::iterator m_index_it;
  137. bool m_erase;
  138. };
  139. } //namespace interprocess {
  140. } //namespace boost {
  141. #include <boost/interprocess/detail/config_end.hpp>
  142. #endif //#ifndef BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP