QCAD
Open Source 2D CAD
Loading...
Searching...
No Matches
PoolPointer.h
Go to the documentation of this file.
1/******************************************************************************
2 * Project: libspatialindex - A C++ library for spatial indexing
3 * Author: Marios Hadjieleftheriou, [email protected]
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
32namespace Tools
33{
34 template <class X> class PointerPool;
35
36 template <class X> class PoolPointer
37 {
38 public:
39 explicit PoolPointer(X* p = 0) : m_pointer(p), m_pPool(0) { m_prev = m_next = this; }
40 explicit PoolPointer(X* p, PointerPool<X>* pPool) throw() : m_pointer(p), m_pPool(pPool) { m_prev = m_next = this; }
42 PoolPointer(const PoolPointer& p) throw() { acquire(p); }
44 {
45 if (this != &p)
46 {
47 release();
48 acquire(p);
49 }
50 return *this;
51 }
52
53 X& operator*() const throw() { return *m_pointer; }
54 X* operator->() const throw() { return m_pointer; }
55 X* get() const throw() { return m_pointer; }
56 bool unique() const throw() { return m_prev ? m_prev == this : true; }
57 void relinquish() throw()
58 {
59 m_pPool = 0;
60 m_pointer = 0;
61 release();
62 }
63
64 private:
66 mutable const PoolPointer* m_prev;
67 mutable const PoolPointer* m_next;
69
70 void acquire(const PoolPointer& p) throw()
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 != 0) m_pPool->release(m_pointer);
89 else delete m_pointer;
90 }
91 else
92 {
95 m_prev = m_next = 0;
96 }
97 m_pointer = 0;
98 m_pPool = 0;
99 }
100 };
101}
102
Definition PointerPool.h:35
Definition PoolPointer.h:37
X * operator->() const
Definition PoolPointer.h:54
void relinquish()
Definition PoolPointer.h:57
PoolPointer & operator=(const PoolPointer &p)
Definition PoolPointer.h:43
PointerPool< X > * m_pPool
Definition PoolPointer.h:68
bool unique() const
Definition PoolPointer.h:56
void release()
Definition PoolPointer.h:84
X * m_pointer
Definition PoolPointer.h:65
PoolPointer(const PoolPointer &p)
Definition PoolPointer.h:42
X * get() const
Definition PoolPointer.h:55
const PoolPointer * m_prev
Definition PoolPointer.h:66
~PoolPointer()
Definition PoolPointer.h:41
X & operator*() const
Definition PoolPointer.h:53
const PoolPointer * m_next
Definition PoolPointer.h:67
void acquire(const PoolPointer &p)
Definition PoolPointer.h:70
PoolPointer(X *p=0)
Definition PoolPointer.h:39
PoolPointer(X *p, PointerPool< X > *pPool)
Definition PoolPointer.h:40
Definition PointerPool.h:33