annotate DEPENDENCIES/generic/include/boost/interprocess/offset_ptr.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 //////////////////////////////////////////////////////////////////////////////
Chris@16 2 //
Chris@16 3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
Chris@16 4 // Software License, Version 1.0. (See accompanying file
Chris@16 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 6 //
Chris@16 7 // See http://www.boost.org/libs/interprocess for documentation.
Chris@16 8 //
Chris@16 9 //////////////////////////////////////////////////////////////////////////////
Chris@16 10
Chris@16 11 #ifndef BOOST_INTERPROCESS_OFFSET_PTR_HPP
Chris@16 12 #define BOOST_INTERPROCESS_OFFSET_PTR_HPP
Chris@16 13
Chris@16 14 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
Chris@16 15 # pragma once
Chris@16 16 #endif
Chris@16 17
Chris@16 18 #include <boost/interprocess/detail/config_begin.hpp>
Chris@16 19 #include <boost/interprocess/detail/workaround.hpp>
Chris@16 20
Chris@16 21 #include <boost/interprocess/interprocess_fwd.hpp>
Chris@16 22 #include <boost/interprocess/detail/utilities.hpp>
Chris@16 23 #include <boost/interprocess/detail/cast_tags.hpp>
Chris@16 24 #include <boost/interprocess/detail/mpl.hpp>
Chris@16 25 #include <boost/assert.hpp>
Chris@16 26 #include <ostream>
Chris@16 27 #include <istream>
Chris@16 28 #include <iterator>
Chris@16 29 #include <boost/aligned_storage.hpp>
Chris@16 30 #include <boost/type_traits/alignment_of.hpp>
Chris@16 31
Chris@16 32 //!\file
Chris@16 33 //!Describes a smart pointer that stores the offset between this pointer and
Chris@16 34 //!target pointee, called offset_ptr.
Chris@16 35
Chris@16 36 namespace boost {
Chris@16 37
Chris@16 38 //Predeclarations
Chris@16 39 template <class T>
Chris@16 40 struct has_trivial_constructor;
Chris@16 41
Chris@16 42 template <class T>
Chris@16 43 struct has_trivial_destructor;
Chris@16 44
Chris@16 45 namespace interprocess {
Chris@16 46
Chris@16 47 /// @cond
Chris@16 48 namespace ipcdetail {
Chris@16 49
Chris@16 50 template<class OffsetType, std::size_t OffsetAlignment>
Chris@16 51 union offset_ptr_internal
Chris@16 52 {
Chris@16 53 explicit offset_ptr_internal(OffsetType off)
Chris@16 54 : m_offset(off)
Chris@16 55 {}
Chris@16 56 OffsetType m_offset; //Distance between this object and pointee address
Chris@16 57 typename ::boost::aligned_storage
Chris@16 58 < sizeof(OffsetType)
Chris@16 59 , (OffsetAlignment == offset_type_alignment) ?
Chris@16 60 ::boost::alignment_of<OffsetType>::value : OffsetAlignment
Chris@16 61 >::type alignment_helper;
Chris@16 62 };
Chris@16 63
Chris@16 64 //Note: using the address of a local variable to point to another address
Chris@16 65 //is not standard conforming and this can be optimized-away by the compiler.
Chris@16 66 //Non-inlining is a method to remain illegal but correct
Chris@16 67
Chris@16 68 //Undef BOOST_INTERPROCESS_OFFSET_PTR_INLINE_XXX if your compiler can inline
Chris@16 69 //this code without breaking the library
Chris@16 70
Chris@16 71 ////////////////////////////////////////////////////////////////////////
Chris@16 72 //
Chris@16 73 // offset_ptr_to_raw_pointer
Chris@16 74 //
Chris@16 75 ////////////////////////////////////////////////////////////////////////
Chris@16 76 #define BOOST_INTERPROCESS_OFFSET_PTR_INLINE_TO_PTR
Chris@16 77 #define BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_PTR
Chris@16 78
Chris@16 79 template<int Dummy>
Chris@16 80 #ifndef BOOST_INTERPROCESS_OFFSET_PTR_INLINE_TO_PTR
Chris@16 81 BOOST_INTERPROCESS_NEVER_INLINE
Chris@16 82 #elif defined(NDEBUG)
Chris@16 83 inline
Chris@16 84 #endif
Chris@16 85 void * offset_ptr_to_raw_pointer(const volatile void *this_ptr, std::size_t offset)
Chris@16 86 {
Chris@16 87 typedef pointer_size_t_caster<void*> caster_t;
Chris@16 88 #ifndef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_PTR
Chris@16 89 if(offset == 1){
Chris@16 90 return 0;
Chris@16 91 }
Chris@16 92 else{
Chris@16 93 const caster_t caster((void*)this_ptr);
Chris@16 94 return caster_t(caster.size() + offset).pointer();
Chris@16 95 }
Chris@16 96 #else
Chris@16 97 const caster_t caster((void*)this_ptr);
Chris@16 98 return caster_t((caster.size() + offset) & -std::size_t(offset != 1)).pointer();
Chris@16 99 #endif
Chris@16 100 }
Chris@16 101
Chris@16 102 #ifdef BOOST_INTERPROCESS_OFFSET_PTR_INLINE_TO_PTR
Chris@16 103 #undef BOOST_INTERPROCESS_OFFSET_PTR_INLINE_TO_PTR
Chris@16 104 #endif
Chris@16 105 #ifdef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_PTR
Chris@16 106 #undef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_PTR
Chris@16 107 #endif
Chris@16 108
Chris@16 109 ////////////////////////////////////////////////////////////////////////
Chris@16 110 //
Chris@16 111 // offset_ptr_to_offset
Chris@16 112 //
Chris@16 113 ////////////////////////////////////////////////////////////////////////
Chris@16 114 #define BOOST_INTERPROCESS_OFFSET_PTR_INLINE_TO_OFF
Chris@16 115 //Branchless seems slower in x86
Chris@16 116 #define BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF
Chris@16 117
Chris@16 118 template<int Dummy>
Chris@16 119 #ifndef BOOST_INTERPROCESS_OFFSET_PTR_INLINE_TO_OFF
Chris@16 120 BOOST_INTERPROCESS_NEVER_INLINE
Chris@16 121 #elif defined(NDEBUG)
Chris@16 122 inline
Chris@16 123 #endif
Chris@16 124 std::size_t offset_ptr_to_offset(const volatile void *ptr, const volatile void *this_ptr)
Chris@16 125 {
Chris@16 126 typedef pointer_size_t_caster<void*> caster_t;
Chris@16 127 #ifndef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF
Chris@16 128 //offset == 1 && ptr != 0 is not legal for this pointer
Chris@16 129 if(!ptr){
Chris@16 130 return 1;
Chris@16 131 }
Chris@16 132 else{
Chris@16 133 const caster_t this_caster((void*)this_ptr);
Chris@16 134 const caster_t ptr_caster((void*)ptr);
Chris@16 135 std::size_t offset = ptr_caster.size() - this_caster.size();
Chris@16 136 BOOST_ASSERT(offset != 1);
Chris@16 137 return offset;
Chris@16 138 }
Chris@16 139 #else
Chris@16 140 const caster_t this_caster((void*)this_ptr);
Chris@16 141 const caster_t ptr_caster((void*)ptr);
Chris@16 142 //std::size_t other = -std::size_t(ptr != 0);
Chris@16 143 //std::size_t offset = (ptr_caster.size() - this_caster.size()) & other;
Chris@16 144 //return offset + !other;
Chris@16 145 //
Chris@16 146 std::size_t offset = (ptr_caster.size() - this_caster.size() - 1) & -std::size_t(ptr != 0);
Chris@16 147 return ++offset;
Chris@16 148 #endif
Chris@16 149 }
Chris@16 150
Chris@16 151 #ifdef BOOST_INTERPROCESS_OFFSET_PTR_INLINE_TO_OFF
Chris@16 152 #undef BOOST_INTERPROCESS_OFFSET_PTR_INLINE_TO_OFF
Chris@16 153 #endif
Chris@16 154 #ifdef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF
Chris@16 155 #undef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF
Chris@16 156 #endif
Chris@16 157
Chris@16 158 ////////////////////////////////////////////////////////////////////////
Chris@16 159 //
Chris@16 160 // offset_ptr_to_offset_from_other
Chris@16 161 //
Chris@16 162 ////////////////////////////////////////////////////////////////////////
Chris@16 163 #define BOOST_INTERPROCESS_OFFSET_PTR_INLINE_TO_OFF_FROM_OTHER
Chris@16 164 //Branchless seems slower in x86
Chris@16 165 #define BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF_FROM_OTHER
Chris@16 166
Chris@16 167 template<int Dummy>
Chris@16 168 #ifndef BOOST_INTERPROCESS_OFFSET_PTR_INLINE_TO_OFF_FROM_OTHER
Chris@16 169 BOOST_INTERPROCESS_NEVER_INLINE
Chris@16 170 #elif defined(NDEBUG)
Chris@16 171 inline
Chris@16 172 #endif
Chris@16 173 std::size_t offset_ptr_to_offset_from_other
Chris@16 174 (const volatile void *this_ptr, const volatile void *other_ptr, std::size_t other_offset)
Chris@16 175 {
Chris@16 176 typedef pointer_size_t_caster<void*> caster_t;
Chris@16 177 #ifndef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF_FROM_OTHER
Chris@16 178 if(other_offset == 1){
Chris@16 179 return 1;
Chris@16 180 }
Chris@16 181 else{
Chris@16 182 const caster_t this_caster((void*)this_ptr);
Chris@16 183 const caster_t other_caster((void*)other_ptr);
Chris@16 184 std::size_t offset = other_caster.size() - this_caster.size() + other_offset;
Chris@16 185 BOOST_ASSERT(offset != 1);
Chris@16 186 return offset;
Chris@16 187 }
Chris@16 188 #else
Chris@16 189 const caster_t this_caster((void*)this_ptr);
Chris@16 190 const caster_t other_caster((void*)other_ptr);
Chris@16 191 return ((other_caster.size() - this_caster.size()) & -std::size_t(other_offset != 1)) + other_offset;
Chris@16 192 #endif
Chris@16 193 }
Chris@16 194
Chris@16 195 #ifdef BOOST_INTERPROCESS_OFFSET_PTR_INLINE_TO_OFF_FROM_OTHER
Chris@16 196 #undef BOOST_INTERPROCESS_OFFSET_PTR_INLINE_TO_OFF_FROM_OTHER
Chris@16 197 #endif
Chris@16 198 #ifdef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF_FROM_OTHER
Chris@16 199 #undef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF_FROM_OTHER
Chris@16 200 #endif
Chris@16 201
Chris@16 202 ////////////////////////////////////////////////////////////////////////
Chris@16 203 //
Chris@16 204 // Let's assume cast to void and cv cast don't change any target address
Chris@16 205 //
Chris@16 206 ////////////////////////////////////////////////////////////////////////
Chris@16 207 template<class From, class To>
Chris@16 208 struct offset_ptr_maintains_address
Chris@16 209 {
Chris@16 210 static const bool value = ipcdetail::is_cv_same<From, To>::value
Chris@16 211 || ipcdetail::is_cv_same<void, To>::value;
Chris@16 212 };
Chris@16 213
Chris@16 214 } //namespace ipcdetail {
Chris@16 215 /// @endcond
Chris@16 216
Chris@16 217 //!A smart pointer that stores the offset between between the pointer and the
Chris@16 218 //!the object it points. This allows offset allows special properties, since
Chris@16 219 //!the pointer is independent from the address address of the pointee, if the
Chris@16 220 //!pointer and the pointee are still separated by the same offset. This feature
Chris@16 221 //!converts offset_ptr in a smart pointer that can be placed in shared memory and
Chris@16 222 //!memory mapped files mapped in different addresses in every process.
Chris@16 223 template <class PointedType, class DifferenceType, class OffsetType, std::size_t OffsetAlignment>
Chris@16 224 class offset_ptr
Chris@16 225 {
Chris@16 226 /// @cond
Chris@16 227 typedef offset_ptr<PointedType, DifferenceType, OffsetType, OffsetAlignment> self_t;
Chris@16 228 void unspecified_bool_type_func() const {}
Chris@16 229 typedef void (self_t::*unspecified_bool_type)() const;
Chris@16 230 /// @endcond
Chris@16 231
Chris@16 232 public:
Chris@16 233 typedef PointedType element_type;
Chris@16 234 typedef PointedType * pointer;
Chris@16 235 typedef typename ipcdetail::
Chris@16 236 add_reference<PointedType>::type reference;
Chris@16 237 typedef typename ipcdetail::
Chris@16 238 remove_volatile<typename ipcdetail::
Chris@16 239 remove_const<PointedType>::type
Chris@16 240 >::type value_type;
Chris@16 241 typedef DifferenceType difference_type;
Chris@16 242 typedef std::random_access_iterator_tag iterator_category;
Chris@16 243 typedef OffsetType offset_type;
Chris@16 244
Chris@16 245 public: //Public Functions
Chris@16 246
Chris@16 247 //!Default constructor (null pointer).
Chris@16 248 //!Never throws.
Chris@16 249 offset_ptr()
Chris@16 250 : internal(1)
Chris@16 251 {}
Chris@16 252
Chris@16 253 //!Constructor from raw pointer (allows "0" pointer conversion).
Chris@16 254 //!Never throws.
Chris@16 255 offset_ptr(pointer ptr)
Chris@16 256 : internal(static_cast<OffsetType>(ipcdetail::offset_ptr_to_offset<0>(ptr, this)))
Chris@16 257 {}
Chris@16 258
Chris@16 259 //!Constructor from other pointer.
Chris@16 260 //!Never throws.
Chris@16 261 template <class T>
Chris@16 262 offset_ptr( T *ptr
Chris@16 263 , typename ipcdetail::enable_if< ipcdetail::is_convertible<T*, PointedType*> >::type * = 0)
Chris@16 264 : internal(static_cast<OffsetType>
Chris@16 265 (ipcdetail::offset_ptr_to_offset<0>(static_cast<PointedType*>(ptr), this)))
Chris@16 266 {}
Chris@16 267
Chris@16 268 //!Constructor from other offset_ptr
Chris@16 269 //!Never throws.
Chris@16 270 offset_ptr(const offset_ptr& ptr)
Chris@16 271 : internal(static_cast<OffsetType>
Chris@16 272 (ipcdetail::offset_ptr_to_offset_from_other<0>(this, &ptr, ptr.internal.m_offset)))
Chris@16 273 {}
Chris@16 274
Chris@16 275 //!Constructor from other offset_ptr. If pointers of pointee types are
Chris@16 276 //!convertible, offset_ptrs will be convertibles. Never throws.
Chris@16 277 template<class T2>
Chris@16 278 offset_ptr( const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr
Chris@16 279 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 280 , typename ipcdetail::enable_if_c< ipcdetail::is_convertible<T2*, PointedType*>::value
Chris@16 281 && ipcdetail::offset_ptr_maintains_address<T2, PointedType>::value
Chris@16 282 >::type * = 0
Chris@16 283 #endif
Chris@16 284 )
Chris@16 285 : internal(static_cast<OffsetType>
Chris@16 286 (ipcdetail::offset_ptr_to_offset_from_other<0>(this, &ptr, ptr.get_offset())))
Chris@16 287 {}
Chris@16 288
Chris@16 289 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 290
Chris@16 291 //!Constructor from other offset_ptr. If pointers of pointee types are
Chris@16 292 //!convertible, offset_ptrs will be convertibles. Never throws.
Chris@16 293 template<class T2>
Chris@16 294 offset_ptr( const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr
Chris@16 295 , typename ipcdetail::enable_if_c< ipcdetail::is_convertible<T2*, PointedType*>::value
Chris@16 296 && !ipcdetail::offset_ptr_maintains_address<T2, PointedType>::value
Chris@16 297 >::type * = 0)
Chris@16 298 : internal(static_cast<OffsetType>
Chris@16 299 (ipcdetail::offset_ptr_to_offset<0>(static_cast<PointedType*>(ptr.get()), this)))
Chris@16 300 {}
Chris@16 301
Chris@16 302 #endif
Chris@16 303
Chris@16 304 //!Emulates static_cast operator.
Chris@16 305 //!Never throws.
Chris@16 306 template<class T2, class P2, class O2, std::size_t A2>
Chris@16 307 offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::static_cast_tag)
Chris@16 308 : internal(static_cast<OffsetType>
Chris@16 309 (ipcdetail::offset_ptr_to_offset<0>(static_cast<PointedType*>(r.get()), this)))
Chris@16 310 {}
Chris@16 311
Chris@16 312 //!Emulates const_cast operator.
Chris@16 313 //!Never throws.
Chris@16 314 template<class T2, class P2, class O2, std::size_t A2>
Chris@16 315 offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::const_cast_tag)
Chris@16 316 : internal(static_cast<OffsetType>
Chris@16 317 (ipcdetail::offset_ptr_to_offset<0>(const_cast<PointedType*>(r.get()), this)))
Chris@16 318 {}
Chris@16 319
Chris@16 320 //!Emulates dynamic_cast operator.
Chris@16 321 //!Never throws.
Chris@16 322 template<class T2, class P2, class O2, std::size_t A2>
Chris@16 323 offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::dynamic_cast_tag)
Chris@16 324 : internal(static_cast<OffsetType>
Chris@16 325 (ipcdetail::offset_ptr_to_offset<0>(dynamic_cast<PointedType*>(r.get()), this)))
Chris@16 326 {}
Chris@16 327
Chris@16 328 //!Emulates reinterpret_cast operator.
Chris@16 329 //!Never throws.
Chris@16 330 template<class T2, class P2, class O2, std::size_t A2>
Chris@16 331 offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::reinterpret_cast_tag)
Chris@16 332 : internal(static_cast<OffsetType>
Chris@16 333 (ipcdetail::offset_ptr_to_offset<0>(reinterpret_cast<PointedType*>(r.get()), this)))
Chris@16 334 {}
Chris@16 335
Chris@16 336 //!Obtains raw pointer from offset.
Chris@16 337 //!Never throws.
Chris@16 338 pointer get() const
Chris@16 339 { return (pointer)ipcdetail::offset_ptr_to_raw_pointer<0>(this, this->internal.m_offset); }
Chris@16 340
Chris@16 341 offset_type get_offset() const
Chris@16 342 { return this->internal.m_offset; }
Chris@16 343
Chris@16 344 //!Pointer-like -> operator. It can return 0 pointer.
Chris@16 345 //!Never throws.
Chris@16 346 pointer operator->() const
Chris@16 347 { return this->get(); }
Chris@16 348
Chris@16 349 //!Dereferencing operator, if it is a null offset_ptr behavior
Chris@16 350 //! is undefined. Never throws.
Chris@16 351 reference operator* () const
Chris@16 352 {
Chris@16 353 pointer p = this->get();
Chris@16 354 reference r = *p;
Chris@16 355 return r;
Chris@16 356 }
Chris@16 357
Chris@16 358 //!Indexing operator.
Chris@16 359 //!Never throws.
Chris@16 360 reference operator[](difference_type idx) const
Chris@16 361 { return this->get()[idx]; }
Chris@16 362
Chris@16 363 //!Assignment from pointer (saves extra conversion).
Chris@16 364 //!Never throws.
Chris@16 365 offset_ptr& operator= (pointer from)
Chris@16 366 {
Chris@16 367 this->internal.m_offset =
Chris@16 368 static_cast<OffsetType>(ipcdetail::offset_ptr_to_offset<0>(from, this));
Chris@16 369 return *this;
Chris@16 370 }
Chris@16 371
Chris@16 372 //!Assignment from other offset_ptr.
Chris@16 373 //!Never throws.
Chris@16 374 offset_ptr& operator= (const offset_ptr & ptr)
Chris@16 375 {
Chris@16 376 this->internal.m_offset =
Chris@16 377 static_cast<OffsetType>(ipcdetail::offset_ptr_to_offset_from_other<0>(this, &ptr, ptr.internal.m_offset));
Chris@16 378 return *this;
Chris@16 379 }
Chris@16 380
Chris@16 381 //!Assignment from related offset_ptr. If pointers of pointee types
Chris@16 382 //! are assignable, offset_ptrs will be assignable. Never throws.
Chris@16 383 template<class T2>
Chris@16 384 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 385 typename ipcdetail::enable_if_c< ipcdetail::is_convertible<T2*, PointedType*>::value
Chris@16 386 && ipcdetail::offset_ptr_maintains_address<T2, PointedType>::value
Chris@16 387 , offset_ptr&>::type
Chris@16 388 #else
Chris@16 389 offset_ptr&
Chris@16 390 #endif
Chris@16 391 operator= (const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr)
Chris@16 392 {
Chris@16 393 this->internal.m_offset =
Chris@16 394 static_cast<OffsetType>(ipcdetail::offset_ptr_to_offset_from_other<0>(this, &ptr, ptr.get_offset()));
Chris@16 395 return *this;
Chris@16 396 }
Chris@16 397
Chris@16 398 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 399 template<class T2>
Chris@16 400 typename ipcdetail::enable_if_c<ipcdetail::is_convertible<T2*, PointedType*>::value
Chris@16 401 && !ipcdetail::offset_ptr_maintains_address<T2, PointedType>::value
Chris@16 402 , offset_ptr&>::type
Chris@16 403 operator= (const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr)
Chris@16 404 {
Chris@16 405 this->internal.m_offset =
Chris@16 406 static_cast<OffsetType>(ipcdetail::offset_ptr_to_offset<0>(static_cast<PointedType*>(ptr.get()), this));
Chris@16 407 return *this;
Chris@16 408 }
Chris@16 409 #endif
Chris@16 410
Chris@16 411 //!offset_ptr += difference_type.
Chris@16 412 //!Never throws.
Chris@16 413 offset_ptr &operator+= (difference_type offset)
Chris@16 414 { this->inc_offset(offset * sizeof (PointedType)); return *this; }
Chris@16 415
Chris@16 416 //!offset_ptr -= difference_type.
Chris@16 417 //!Never throws.
Chris@16 418 offset_ptr &operator-= (difference_type offset)
Chris@16 419 { this->dec_offset(offset * sizeof (PointedType)); return *this; }
Chris@16 420
Chris@16 421 //!++offset_ptr.
Chris@16 422 //!Never throws.
Chris@16 423 offset_ptr& operator++ (void)
Chris@16 424 { this->inc_offset(sizeof (PointedType)); return *this; }
Chris@16 425
Chris@16 426 //!offset_ptr++.
Chris@16 427 //!Never throws.
Chris@16 428 offset_ptr operator++ (int)
Chris@16 429 {
Chris@16 430 offset_ptr tmp(*this);
Chris@16 431 this->inc_offset(sizeof (PointedType));
Chris@16 432 return tmp;
Chris@16 433 }
Chris@16 434
Chris@16 435 //!--offset_ptr.
Chris@16 436 //!Never throws.
Chris@16 437 offset_ptr& operator-- (void)
Chris@16 438 { this->dec_offset(sizeof (PointedType)); return *this; }
Chris@16 439
Chris@16 440 //!offset_ptr--.
Chris@16 441 //!Never throws.
Chris@16 442 offset_ptr operator-- (int)
Chris@16 443 {
Chris@16 444 offset_ptr tmp(*this);
Chris@16 445 this->dec_offset(sizeof (PointedType));
Chris@16 446 return tmp;
Chris@16 447 }
Chris@16 448
Chris@16 449 //!safe bool conversion operator.
Chris@16 450 //!Never throws.
Chris@16 451 operator unspecified_bool_type() const
Chris@16 452 { return this->internal.m_offset != 1? &self_t::unspecified_bool_type_func : 0; }
Chris@16 453
Chris@16 454 //!Not operator. Not needed in theory, but improves portability.
Chris@16 455 //!Never throws
Chris@16 456 bool operator! () const
Chris@16 457 { return this->internal.m_offset == 1; }
Chris@16 458
Chris@16 459 //!Compatibility with pointer_traits
Chris@16 460 //!
Chris@16 461 template <class U>
Chris@16 462 struct rebind
Chris@16 463 { typedef offset_ptr<U, DifferenceType, OffsetType, OffsetAlignment> other; };
Chris@16 464
Chris@16 465 //!Compatibility with pointer_traits
Chris@16 466 //!
Chris@16 467 static offset_ptr pointer_to(reference r)
Chris@16 468 { return offset_ptr(&r); }
Chris@16 469
Chris@16 470 //!difference_type + offset_ptr
Chris@16 471 //!operation
Chris@16 472 friend offset_ptr operator+(difference_type diff, offset_ptr right)
Chris@16 473 { right += diff; return right; }
Chris@16 474
Chris@16 475 //!offset_ptr + difference_type
Chris@16 476 //!operation
Chris@16 477 friend offset_ptr operator+(offset_ptr left, difference_type diff)
Chris@16 478 { left += diff; return left; }
Chris@16 479
Chris@16 480 //!offset_ptr - diff
Chris@16 481 //!operation
Chris@16 482 friend offset_ptr operator-(offset_ptr left, difference_type diff)
Chris@16 483 { left -= diff; return left; }
Chris@16 484
Chris@16 485 //!offset_ptr - diff
Chris@16 486 //!operation
Chris@16 487 friend offset_ptr operator-(difference_type diff, offset_ptr right)
Chris@16 488 { right -= diff; return right; }
Chris@16 489
Chris@16 490 //!offset_ptr - offset_ptr
Chris@16 491 //!operation
Chris@16 492 friend difference_type operator-(const offset_ptr &pt, const offset_ptr &pt2)
Chris@16 493 { return difference_type(pt.get()- pt2.get()); }
Chris@16 494
Chris@16 495 //Comparison
Chris@16 496 friend bool operator== (const offset_ptr &pt1, const offset_ptr &pt2)
Chris@16 497 { return pt1.get() == pt2.get(); }
Chris@16 498
Chris@16 499 friend bool operator!= (const offset_ptr &pt1, const offset_ptr &pt2)
Chris@16 500 { return pt1.get() != pt2.get(); }
Chris@16 501
Chris@16 502 friend bool operator<(const offset_ptr &pt1, const offset_ptr &pt2)
Chris@16 503 { return pt1.get() < pt2.get(); }
Chris@16 504
Chris@16 505 friend bool operator<=(const offset_ptr &pt1, const offset_ptr &pt2)
Chris@16 506 { return pt1.get() <= pt2.get(); }
Chris@16 507
Chris@16 508 friend bool operator>(const offset_ptr &pt1, const offset_ptr &pt2)
Chris@16 509 { return pt1.get() > pt2.get(); }
Chris@16 510
Chris@16 511 friend bool operator>=(const offset_ptr &pt1, const offset_ptr &pt2)
Chris@16 512 { return pt1.get() >= pt2.get(); }
Chris@16 513
Chris@16 514 //Comparison to raw ptr to support literal 0
Chris@16 515 friend bool operator== (pointer pt1, const offset_ptr &pt2)
Chris@16 516 { return pt1 == pt2.get(); }
Chris@16 517
Chris@16 518 friend bool operator!= (pointer pt1, const offset_ptr &pt2)
Chris@16 519 { return pt1 != pt2.get(); }
Chris@16 520
Chris@16 521 friend bool operator<(pointer pt1, const offset_ptr &pt2)
Chris@16 522 { return pt1 < pt2.get(); }
Chris@16 523
Chris@16 524 friend bool operator<=(pointer pt1, const offset_ptr &pt2)
Chris@16 525 { return pt1 <= pt2.get(); }
Chris@16 526
Chris@16 527 friend bool operator>(pointer pt1, const offset_ptr &pt2)
Chris@16 528 { return pt1 > pt2.get(); }
Chris@16 529
Chris@16 530 friend bool operator>=(pointer pt1, const offset_ptr &pt2)
Chris@16 531 { return pt1 >= pt2.get(); }
Chris@16 532
Chris@16 533 //Comparison
Chris@16 534 friend bool operator== (const offset_ptr &pt1, pointer pt2)
Chris@16 535 { return pt1.get() == pt2; }
Chris@16 536
Chris@16 537 friend bool operator!= (const offset_ptr &pt1, pointer pt2)
Chris@16 538 { return pt1.get() != pt2; }
Chris@16 539
Chris@16 540 friend bool operator<(const offset_ptr &pt1, pointer pt2)
Chris@16 541 { return pt1.get() < pt2; }
Chris@16 542
Chris@16 543 friend bool operator<=(const offset_ptr &pt1, pointer pt2)
Chris@16 544 { return pt1.get() <= pt2; }
Chris@16 545
Chris@16 546 friend bool operator>(const offset_ptr &pt1, pointer pt2)
Chris@16 547 { return pt1.get() > pt2; }
Chris@16 548
Chris@16 549 friend bool operator>=(const offset_ptr &pt1, pointer pt2)
Chris@16 550 { return pt1.get() >= pt2; }
Chris@16 551
Chris@16 552 friend void swap(offset_ptr &left, offset_ptr &right)
Chris@16 553 {
Chris@16 554 pointer ptr = right.get();
Chris@16 555 right = left;
Chris@16 556 left = ptr;
Chris@16 557 }
Chris@16 558
Chris@16 559 private:
Chris@16 560 /// @cond
Chris@16 561 void inc_offset(DifferenceType bytes)
Chris@16 562 { internal.m_offset += bytes; }
Chris@16 563
Chris@16 564 void dec_offset(DifferenceType bytes)
Chris@16 565 { internal.m_offset -= bytes; }
Chris@16 566
Chris@16 567 ipcdetail::offset_ptr_internal<OffsetType, OffsetAlignment> internal;
Chris@16 568 /// @endcond
Chris@16 569 };
Chris@16 570
Chris@16 571 //!operator<<
Chris@16 572 //!for offset ptr
Chris@16 573 template<class E, class T, class W, class X, class Y, std::size_t Z>
Chris@16 574 inline std::basic_ostream<E, T> & operator<<
Chris@16 575 (std::basic_ostream<E, T> & os, offset_ptr<W, X, Y, Z> const & p)
Chris@16 576 { return os << p.get_offset(); }
Chris@16 577
Chris@16 578 //!operator>>
Chris@16 579 //!for offset ptr
Chris@16 580 template<class E, class T, class W, class X, class Y, std::size_t Z>
Chris@16 581 inline std::basic_istream<E, T> & operator>>
Chris@16 582 (std::basic_istream<E, T> & is, offset_ptr<W, X, Y, Z> & p)
Chris@16 583 { return is >> p.get_offset(); }
Chris@16 584
Chris@16 585 //!Simulation of static_cast between pointers. Never throws.
Chris@16 586 template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2>
Chris@16 587 inline boost::interprocess::offset_ptr<T1, P1, O1, A1>
Chris@16 588 static_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r)
Chris@16 589 {
Chris@16 590 return boost::interprocess::offset_ptr<T1, P1, O1, A1>
Chris@16 591 (r, boost::interprocess::ipcdetail::static_cast_tag());
Chris@16 592 }
Chris@16 593
Chris@16 594 //!Simulation of const_cast between pointers. Never throws.
Chris@16 595 template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2>
Chris@16 596 inline boost::interprocess::offset_ptr<T1, P1, O1, A1>
Chris@16 597 const_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r)
Chris@16 598 {
Chris@16 599 return boost::interprocess::offset_ptr<T1, P1, O1, A1>
Chris@16 600 (r, boost::interprocess::ipcdetail::const_cast_tag());
Chris@16 601 }
Chris@16 602
Chris@16 603 //!Simulation of dynamic_cast between pointers. Never throws.
Chris@16 604 template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2>
Chris@16 605 inline boost::interprocess::offset_ptr<T1, P1, O1, A1>
Chris@16 606 dynamic_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r)
Chris@16 607 {
Chris@16 608 return boost::interprocess::offset_ptr<T1, P1, O1, A1>
Chris@16 609 (r, boost::interprocess::ipcdetail::dynamic_cast_tag());
Chris@16 610 }
Chris@16 611
Chris@16 612 //!Simulation of reinterpret_cast between pointers. Never throws.
Chris@16 613 template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2>
Chris@16 614 inline boost::interprocess::offset_ptr<T1, P1, O1, A1>
Chris@16 615 reinterpret_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r)
Chris@16 616 {
Chris@16 617 return boost::interprocess::offset_ptr<T1, P1, O1, A1>
Chris@16 618 (r, boost::interprocess::ipcdetail::reinterpret_cast_tag());
Chris@16 619 }
Chris@16 620
Chris@16 621 } //namespace interprocess {
Chris@16 622
Chris@16 623 /// @cond
Chris@16 624
Chris@16 625 //!has_trivial_constructor<> == true_type specialization for optimizations
Chris@16 626 template <class T, class P, class O, std::size_t A>
Chris@16 627 struct has_trivial_constructor< boost::interprocess::offset_ptr<T, P, O, A> >
Chris@16 628 {
Chris@16 629 static const bool value = true;
Chris@16 630 };
Chris@16 631
Chris@16 632 ///has_trivial_destructor<> == true_type specialization for optimizations
Chris@16 633 template <class T, class P, class O, std::size_t A>
Chris@16 634 struct has_trivial_destructor< boost::interprocess::offset_ptr<T, P, O, A> >
Chris@16 635 {
Chris@16 636 static const bool value = true;
Chris@16 637 };
Chris@16 638
Chris@16 639
Chris@16 640 namespace interprocess {
Chris@16 641
Chris@16 642 //!to_raw_pointer() enables boost::mem_fn to recognize offset_ptr.
Chris@16 643 //!Never throws.
Chris@16 644 template <class T, class P, class O, std::size_t A>
Chris@16 645 inline T * to_raw_pointer(boost::interprocess::offset_ptr<T, P, O, A> const & p)
Chris@16 646 { return ipcdetail::to_raw_pointer(p); }
Chris@16 647
Chris@16 648 } //namespace interprocess
Chris@16 649
Chris@16 650
Chris@16 651 /// @endcond
Chris@16 652 } //namespace boost {
Chris@16 653
Chris@16 654 /// @cond
Chris@16 655
Chris@16 656 namespace boost{
Chris@16 657
Chris@16 658 //This is to support embedding a bit in the pointer
Chris@16 659 //for intrusive containers, saving space
Chris@16 660 namespace intrusive {
Chris@16 661
Chris@16 662 //Predeclaration to avoid including header
Chris@16 663 template<class VoidPointer, std::size_t N>
Chris@16 664 struct max_pointer_plus_bits;
Chris@16 665
Chris@16 666 template<std::size_t OffsetAlignment, class P, class O, std::size_t A>
Chris@16 667 struct max_pointer_plus_bits<boost::interprocess::offset_ptr<void, P, O, A>, OffsetAlignment>
Chris@16 668 {
Chris@16 669 //The offset ptr can embed one bit less than the alignment since it
Chris@16 670 //uses offset == 1 to store the null pointer.
Chris@16 671 static const std::size_t value = ::boost::interprocess::ipcdetail::ls_zeros<OffsetAlignment>::value - 1;
Chris@16 672 };
Chris@16 673
Chris@16 674 //Predeclaration
Chris@16 675 template<class Pointer, std::size_t NumBits>
Chris@16 676 struct pointer_plus_bits;
Chris@16 677
Chris@16 678 template<class T, class P, class O, std::size_t A, std::size_t NumBits>
Chris@16 679 struct pointer_plus_bits<boost::interprocess::offset_ptr<T, P, O, A>, NumBits>
Chris@16 680 {
Chris@16 681 typedef boost::interprocess::offset_ptr<T, P, O, A> pointer;
Chris@16 682 typedef ::boost::interprocess::pointer_size_t_caster<T*> caster_t;
Chris@16 683 //Bits are stored in the lower bits of the pointer except the LSB,
Chris@16 684 //because this bit is used to represent the null pointer.
Chris@16 685 static const std::size_t Mask = ((std::size_t(1) << NumBits) - 1) << 1u;
Chris@16 686
Chris@16 687 static pointer get_pointer(const pointer &n)
Chris@16 688 {
Chris@16 689 caster_t caster(n.get());
Chris@16 690 return pointer(caster_t(caster.size() & ~Mask).pointer());
Chris@16 691 }
Chris@16 692
Chris@16 693 static void set_pointer(pointer &n, const pointer &p)
Chris@16 694 {
Chris@16 695 caster_t n_caster(n.get());
Chris@16 696 caster_t p_caster(p.get());
Chris@16 697 BOOST_ASSERT(0 == (p_caster.size() & Mask));
Chris@16 698 n = caster_t(p_caster.size() | (n_caster.size() & Mask)).pointer();
Chris@16 699 }
Chris@16 700
Chris@16 701 static std::size_t get_bits(const pointer &n)
Chris@16 702 { return (caster_t(n.get()).size() & Mask) >> 1u; }
Chris@16 703
Chris@16 704 static void set_bits(pointer &n, std::size_t b)
Chris@16 705 {
Chris@16 706 BOOST_ASSERT(b < (std::size_t(1) << NumBits));
Chris@16 707 caster_t n_caster(n.get());
Chris@16 708 n = caster_t((n_caster.size() & ~Mask) | (b << 1u)).pointer();
Chris@16 709 }
Chris@16 710 };
Chris@16 711
Chris@16 712 } //namespace intrusive
Chris@16 713
Chris@16 714 //Predeclaration
Chris@16 715 template<class T, class U>
Chris@16 716 struct pointer_to_other;
Chris@16 717
Chris@16 718 //Backwards compatibility with pointer_to_other
Chris@16 719 template <class PointedType, class DifferenceType, class OffsetType, std::size_t OffsetAlignment, class U>
Chris@16 720 struct pointer_to_other
Chris@16 721 < ::boost::interprocess::offset_ptr<PointedType, DifferenceType, OffsetType, OffsetAlignment>, U >
Chris@16 722 {
Chris@16 723 typedef ::boost::interprocess::offset_ptr<U, DifferenceType, OffsetType, OffsetAlignment> type;
Chris@16 724 };
Chris@16 725
Chris@16 726 } //namespace boost{
Chris@16 727 /// @endcond
Chris@16 728
Chris@16 729 #include <boost/interprocess/detail/config_end.hpp>
Chris@16 730
Chris@16 731 #endif //#ifndef BOOST_INTERPROCESS_OFFSET_PTR_HPP