fix doc example typo
[boost.git] / boost / interprocess / detail / utilities.hpp
blob166cf2e66070f8200a3975601947a46185faf99b
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2008.
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.
12 //////////////////////////////////////////////////////////////////////////////
14 #ifndef BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP
15 #define BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP
17 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
18 # pragma once
19 #endif
21 #include <boost/interprocess/detail/config_begin.hpp>
22 #include <boost/interprocess/detail/workaround.hpp>
24 #include <boost/interprocess/interprocess_fwd.hpp>
25 #include <boost/interprocess/detail/move.hpp>
26 #include <boost/type_traits/has_trivial_destructor.hpp>
27 #include <boost/interprocess/detail/min_max.hpp>
28 #include <boost/interprocess/detail/type_traits.hpp>
29 #include <boost/interprocess/detail/transform_iterator.hpp>
30 #include <boost/interprocess/detail/mpl.hpp>
31 #include <boost/interprocess/containers/version_type.hpp>
32 #include <boost/interprocess/detail/move.hpp>
33 #include <utility>
34 #include <algorithm>
36 namespace boost {
37 namespace interprocess {
38 namespace detail {
40 template<class SmartPtr>
41 struct smart_ptr_type
43 typedef typename SmartPtr::value_type value_type;
44 typedef value_type *pointer;
45 static pointer get (const SmartPtr &smartptr)
46 { return smartptr.get();}
49 template<class T>
50 struct smart_ptr_type<T*>
52 typedef T value_type;
53 typedef value_type *pointer;
54 static pointer get (pointer ptr)
55 { return ptr;}
58 //!Overload for smart pointers to avoid ADL problems with get_pointer
59 template<class Ptr>
60 inline typename smart_ptr_type<Ptr>::pointer
61 get_pointer(const Ptr &ptr)
62 { return smart_ptr_type<Ptr>::get(ptr); }
64 //!To avoid ADL problems with swap
65 template <class T>
66 inline void do_swap(T& x, T& y)
68 using std::swap;
69 swap(x, y);
72 //Rounds "orig_size" by excess to round_to bytes
73 inline std::size_t get_rounded_size(std::size_t orig_size, std::size_t round_to)
75 return ((orig_size-1)/round_to+1)*round_to;
78 //Truncates "orig_size" to a multiple of "multiple" bytes.
79 inline std::size_t get_truncated_size(std::size_t orig_size, std::size_t multiple)
81 return orig_size/multiple*multiple;
84 //Rounds "orig_size" by excess to round_to bytes. round_to must be power of two
85 inline std::size_t get_rounded_size_po2(std::size_t orig_size, std::size_t round_to)
87 return ((orig_size-1)&(~(round_to-1))) + round_to;
90 //Truncates "orig_size" to a multiple of "multiple" bytes. multiple must be power of two
91 inline std::size_t get_truncated_size_po2(std::size_t orig_size, std::size_t multiple)
93 return (orig_size & (~(multiple-1)));
96 template <std::size_t OrigSize, std::size_t RoundTo>
97 struct ct_rounded_size
99 enum { value = ((OrigSize-1)/RoundTo+1)*RoundTo };
102 // Gennaro Prota wrote this. Thanks!
103 template <int p, int n = 4>
104 struct ct_max_pow2_less
106 enum { c = 2*n < p };
108 static const std::size_t value =
109 c ? (ct_max_pow2_less< c*p, 2*c*n>::value) : n;
112 template <>
113 struct ct_max_pow2_less<0, 0>
115 static const std::size_t value = 0;
118 } //namespace detail {
120 //!Trait class to detect if an index is a node
121 //!index. This allows more efficient operations
122 //!when deallocating named objects.
123 template <class Index>
124 struct is_node_index
126 enum { value = false };
129 //!Trait class to detect if an index is an intrusive
130 //!index. This will embed the derivation hook in each
131 //!allocation header, to provide memory for the intrusive
132 //!container.
133 template <class Index>
134 struct is_intrusive_index
136 enum { value = false };
139 template <typename T> T*
140 addressof(T& v)
142 return reinterpret_cast<T*>(
143 &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
146 //Anti-exception node eraser
147 template<class Cont>
148 class value_eraser
150 public:
151 value_eraser(Cont & cont, typename Cont::iterator it)
152 : m_cont(cont), m_index_it(it), m_erase(true){}
153 ~value_eraser()
154 { if(m_erase) m_cont.erase(m_index_it); }
156 void release() { m_erase = false; }
158 private:
159 Cont &m_cont;
160 typename Cont::iterator m_index_it;
161 bool m_erase;
164 } //namespace interprocess {
165 } //namespace boost {
167 #include <boost/interprocess/detail/config_end.hpp>
169 #endif //#ifndef BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP