annotate DEPENDENCIES/generic/include/boost/interprocess/offset_ptr.hpp @ 133:4acb5d8d80b6 tip

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