libspatialindex API Reference  (git-trunk)
PoolPointer.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Project: libspatialindex - A C++ library for spatial indexing
3  * Author: Marios Hadjieleftheriou, mhadji@gmail.com
4  ******************************************************************************
5  * Copyright (c) 2004, Marios Hadjieleftheriou
6  *
7  * All rights reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26 ******************************************************************************/
27 
28 #pragma once
29 
30 #include "PointerPool.h"
31 
32 namespace Tools
33 {
34  template <class X> class PointerPool;
35 
36  template <class X> class PoolPointer
37  {
38  public:
39  explicit PoolPointer(X* p = nullptr) : m_pointer(p), m_pPool(nullptr) { m_prev = m_next = this; }
40  explicit PoolPointer(X* p, PointerPool<X>* pPool) noexcept : m_pointer(p), m_pPool(pPool) { m_prev = m_next = this; }
41  ~PoolPointer() { release(); }
42  PoolPointer(const PoolPointer& p) noexcept { acquire(p); }
44  {
45  if (this != &p)
46  {
47  release();
48  acquire(p);
49  }
50  return *this;
51  }
52 
53  X& operator*() const noexcept { return *m_pointer; }
54  X* operator->() const noexcept { return m_pointer; }
55  X* get() const noexcept { return m_pointer; }
56  bool unique() const noexcept { return m_prev ? m_prev == this : true; }
57  void relinquish() noexcept
58  {
59  m_pPool = nullptr;
60  m_pointer = nullptr;
61  release();
62  }
63 
64  private:
65  X* m_pointer;
66  mutable const PoolPointer* m_prev;
67  mutable const PoolPointer* m_next;
68  PointerPool<X>* m_pPool;
69 
70  void acquire(const PoolPointer& p) noexcept
71  {
72  m_pPool = p.m_pPool;
73  m_pointer = p.m_pointer;
74  m_next = p.m_next;
75  m_next->m_prev = this;
76  m_prev = &p;
77  #ifndef mutable
78  p.m_next = this;
79  #else
80  (const_cast<linked_ptr<X>*>(&p))->m_next = this;
81  #endif
82  }
83 
84  void release()
85  {
86  if (unique())
87  {
88  if (m_pPool != nullptr) m_pPool->release(m_pointer);
89  else delete m_pointer;
90  }
91  else
92  {
93  m_prev->m_next = m_next;
94  m_next->m_prev = m_prev;
95  m_prev = m_next = nullptr;
96  }
97  m_pointer = nullptr;
98  m_pPool = nullptr;
99  }
100  };
101 }
102 
X * operator->() const noexcept
Definition: PoolPointer.h:54
PoolPointer(X *p=nullptr)
Definition: PoolPointer.h:39
X & operator*() const noexcept
Definition: PoolPointer.h:53
bool unique() const noexcept
Definition: PoolPointer.h:56
PoolPointer(X *p, PointerPool< X > *pPool) noexcept
Definition: PoolPointer.h:40
PoolPointer & operator=(const PoolPointer &p)
Definition: PoolPointer.h:43
PoolPointer(const PoolPointer &p) noexcept
Definition: PoolPointer.h:42
void release(X *p)
Definition: PointerPool.h:87
void relinquish() noexcept
Definition: PoolPointer.h:57