PageRenderTime 25ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 14#ifndef BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP
 15#define BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP
 16
 17#if (defined _MSC_VER) && (_MSC_VER >= 1200)
 18#  pragma once
 19#endif
 20
 21#include <boost/interprocess/detail/config_begin.hpp>
 22#include <boost/interprocess/detail/workaround.hpp>
 23
 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>
 35
 36namespace boost {
 37namespace interprocess { 
 38namespace detail {
 39
 40template<class SmartPtr>
 41struct smart_ptr_type
 42{
 43   typedef typename SmartPtr::value_type value_type;
 44   typedef value_type *pointer;
 45   static pointer get (const SmartPtr &smartptr)
 46   {  return smartptr.get();}
 47};
 48
 49template<class T>
 50struct smart_ptr_type<T*>
 51{
 52   typedef T value_type;
 53   typedef value_type *pointer;
 54   static pointer get (pointer ptr)
 55   {  return ptr;}
 56};
 57
 58//!Overload for smart pointers to avoid ADL problems with get_pointer
 59template<class Ptr>
 60inline typename smart_ptr_type<Ptr>::pointer
 61get_pointer(const Ptr &ptr)
 62{  return smart_ptr_type<Ptr>::get(ptr);   }
 63
 64//!To avoid ADL problems with swap
 65template <class T>
 66inline void do_swap(T& x, T& y)
 67{
 68   using std::swap;
 69   swap(x, y);
 70}
 71
 72//Rounds "orig_size" by excess to round_to bytes
 73inline std::size_t get_rounded_size(std::size_t orig_size, std::size_t round_to)
 74{
 75   return ((orig_size-1)/round_to+1)*round_to;
 76}
 77
 78//Truncates "orig_size" to a multiple of "multiple" bytes.
 79inline std::size_t get_truncated_size(std::size_t orig_size, std::size_t multiple)
 80{
 81   return orig_size/multiple*multiple;
 82}
 83
 84//Rounds "orig_size" by excess to round_to bytes. round_to must be power of two
 85inline std::size_t get_rounded_size_po2(std::size_t orig_size, std::size_t round_to)
 86{
 87   return ((orig_size-1)&(~(round_to-1))) + round_to;
 88}
 89
 90//Truncates "orig_size" to a multiple of "multiple" bytes. multiple must be power of two
 91inline std::size_t get_truncated_size_po2(std::size_t orig_size, std::size_t multiple)
 92{
 93   return (orig_size & (~(multiple-1)));
 94}
 95
 96template <std::size_t OrigSize, std::size_t RoundTo>
 97struct ct_rounded_size
 98{
 99   enum { value = ((OrigSize-1)/RoundTo+1)*RoundTo };
100};
101
102// Gennaro Prota wrote this. Thanks!
103template <int p, int n = 4>
104struct ct_max_pow2_less
105{
106   enum { c = 2*n < p };
107
108   static const std::size_t value =
109         c ? (ct_max_pow2_less< c*p, 2*c*n>::value) : n;
110};
111
112template <>
113struct ct_max_pow2_less<0, 0>
114{
115   static const std::size_t value = 0;
116};
117
118}  //namespace detail {
119
120//!Trait class to detect if an index is a node
121//!index. This allows more efficient operations
122//!when deallocating named objects.
123template <class Index>
124struct is_node_index
125{
126   enum {   value = false };
127};
128
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.
133template <class Index>
134struct is_intrusive_index
135{
136   enum {   value = false };
137};
138
139template <typename T> T*
140addressof(T& v)
141{
142  return reinterpret_cast<T*>(
143       &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
144}
145
146//Anti-exception node eraser
147template<class Cont>
148class value_eraser
149{
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);  }
155
156   void release() {  m_erase = false;  }
157
158   private:
159   Cont                   &m_cont;
160   typename Cont::iterator m_index_it;
161   bool                    m_erase;
162};
163
164}  //namespace interprocess { 
165}  //namespace boost {
166
167#include <boost/interprocess/detail/config_end.hpp>
168
169#endif   //#ifndef BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP
170