fix doc example typo
[boost.git] / boost / intrusive / pointer_plus_bits.hpp
blobb2b51e80ae167cfa6be9dedef0f254e6b68e5a4e
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2007-2008
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // See http://www.boost.org/libs/intrusive for documentation.
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef BOOST_INTRUSIVE_POINTER_PLUS_BITS_HPP
14 #define BOOST_INTRUSIVE_POINTER_PLUS_BITS_HPP
16 #include <boost/intrusive/detail/mpl.hpp> //ls_zeros
17 #include <boost/intrusive/detail/assert.hpp> //BOOST_INTRUSIVE_INVARIANT_ASSERT
19 namespace boost {
20 namespace intrusive {
22 //!This trait class is used to know if a pointer
23 //!can embed extra bits of information if
24 //!it's going to be used to point to objects
25 //!with an alignment of "Alignment" bytes.
26 template<class VoidPointer, std::size_t Alignment>
27 struct max_pointer_plus_bits
29 static const std::size_t value = 0;
32 //!This is a specialization for raw pointers.
33 //!Raw pointers can embed extra bits in the lower bits
34 //!if the alignment is multiple of 2pow(NumBits).
35 template<std::size_t Alignment>
36 struct max_pointer_plus_bits<void*, Alignment>
38 static const std::size_t value = detail::ls_zeros<Alignment>::value;
41 //!This is class that is supposed to have static methods
42 //!to embed extra bits of information in a pointer.
43 //!This is a declaration and there is no default implementation,
44 //!because operations to embed the bits change with every pointer type.
45 //!
46 //!An implementation that detects that a pointer type whose
47 //!has_pointer_plus_bits<>::value is non-zero can make use of these
48 //!operations to embed the bits in the pointer.
49 template<class Pointer, std::size_t NumBits>
50 struct pointer_plus_bits;
52 //!This is the specialization to embed extra bits of information
53 //!in a raw pointer. The extra bits are stored in the lower bit of the pointer.
54 template<class T, std::size_t NumBits>
55 struct pointer_plus_bits<T*, NumBits>
57 static const std::size_t Mask = ((std::size_t(1u) << NumBits) - 1);
58 typedef T* pointer;
60 static pointer get_pointer(pointer n)
61 { return pointer(std::size_t(n) & ~Mask); }
63 static void set_pointer(pointer &n, pointer p)
65 BOOST_INTRUSIVE_INVARIANT_ASSERT(0 == (std::size_t(p) & Mask));
66 n = pointer(std::size_t(p) | (std::size_t(n) & Mask));
69 static std::size_t get_bits(pointer n)
70 { return (std::size_t(n) & Mask); }
72 static void set_bits(pointer &n, std::size_t c)
74 BOOST_INTRUSIVE_INVARIANT_ASSERT(c <= Mask);
75 n = pointer(std::size_t(get_pointer(n)) | c);
79 } //namespace intrusive
80 } //namespace boost
82 #endif //BOOST_INTRUSIVE_POINTER_PLUS_BITS_HPP