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: m_pointer(p)39 explicit PoolPointer(X* p = 0) : m_pointer(p), m_pPool(0) { m_prev = m_next = this; } PoolPointer(X * p,PointerPool<X> * pPool)40 explicit PoolPointer(X* p, PointerPool<X>* pPool) throw() : m_pointer(p), m_pPool(pPool) { m_prev = m_next = this; } ~PoolPointer()41 ~PoolPointer() { release(); } throw()42 PoolPointer(const PoolPointer& p) throw() { acquire(p); } 43 PoolPointer& operator=(const PoolPointer& p) 44 { 45 if (this != &p) 46 { 47 release(); 48 acquire(p); 49 } 50 return *this; 51 } 52 throw()53 X& operator*() const throw() { return *m_pointer; } 54 X* operator->() const throw() { return m_pointer; } get()55 X* get() const throw() { return m_pointer; } unique()56 bool unique() const throw() { return m_prev ? m_prev == this : true; } relinquish()57 void relinquish() throw() 58 { 59 m_pPool = 0; 60 m_pointer = 0; 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 acquire(const PoolPointer & p)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 release()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 { 93 m_prev->m_next = m_next; 94 m_next->m_prev = m_prev; 95 m_prev = m_next = 0; 96 } 97 m_pointer = 0; 98 m_pPool = 0; 99 } 100 }; 101 } 102 103