QCAD
Open Source 2D CAD
Loading...
Searching...
No Matches
SmartPointer.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
30namespace Tools
31{
32 template <class X> class SmartPointer
33 {
34 public:
35 explicit SmartPointer(X* p = 0) throw() : m_pointer(p) { m_prev = m_next = this; }
37 SmartPointer(const SmartPointer& p) throw() { acquire(p); }
39 {
40 if (this != &p)
41 {
42 release();
43 acquire(p);
44 }
45 return *this;
46 }
47
48 X& operator*() const throw() { return *m_pointer; }
49 X* operator->() const throw() { return m_pointer; }
50 X* get() const throw() { return m_pointer; }
51 bool unique() const throw() { return m_prev ? m_prev == this : true; }
52
53 private:
55 mutable const SmartPointer* m_prev;
56 mutable const SmartPointer* m_next;
57
58 void acquire(const SmartPointer& p) throw()
59 {
60 m_pointer = p.m_pointer;
61 m_next = p.m_next;
62 m_next->m_prev = this;
63 m_prev = &p;
64 #ifndef mutable
65 p.m_next = this;
66 #else
67 (const_cast<linked_ptr<X>*>(&p))->m_next = this;
68 #endif
69 }
70
71 void release()
72 {
73 if (unique()) delete m_pointer;
74 else
75 {
78 m_prev = m_next = 0;
79 }
80 m_pointer = 0;
81 }
82 };
83}
84
Definition SmartPointer.h:33
void release()
Definition SmartPointer.h:71
void acquire(const SmartPointer &p)
Definition SmartPointer.h:58
X * m_pointer
Definition SmartPointer.h:54
X * get() const
Definition SmartPointer.h:50
X * operator->() const
Definition SmartPointer.h:49
const SmartPointer * m_next
Definition SmartPointer.h:56
~SmartPointer()
Definition SmartPointer.h:36
const SmartPointer * m_prev
Definition SmartPointer.h:55
X & operator*() const
Definition SmartPointer.h:48
bool unique() const
Definition SmartPointer.h:51
SmartPointer & operator=(const SmartPointer &p)
Definition SmartPointer.h:38
SmartPointer(X *p=0)
Definition SmartPointer.h:35
SmartPointer(const SmartPointer &p)
Definition SmartPointer.h:37
Definition PointerPool.h:33