~ubuntu-branches/ubuntu/trusty/qgis/trusty

« back to all changes in this revision

Viewing changes to src/core/spatialindex/include/PoolPointer.h

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Tools Library
 
2
//
 
3
// Copyright (C) 2004  Navel Ltd.
 
4
//
 
5
// This library is free software; you can redistribute it and/or
 
6
// modify it under the terms of the GNU Lesser General Public
 
7
// License as published by the Free Software Foundation; either
 
8
// version 2.1 of the License, or (at your option) any later version.
 
9
//
 
10
// This library is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
// Lesser General Public License for more details.
 
14
//
 
15
// You should have received a copy of the GNU Lesser General Public
 
16
// License along with this library; if not, write to the Free Software
 
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
//
 
19
//  Email:
 
20
//    mhadji@gmail.com
 
21
 
 
22
#ifndef __tools_pool_pointer_h
 
23
#define __tools_pool_pointer_h
 
24
 
 
25
#include "PointerPool.h"
 
26
 
 
27
namespace Tools
 
28
{
 
29
  template <class X> class PointerPool;
 
30
 
 
31
  template <class X> class PoolPointer
 
32
  {
 
33
    public:
 
34
      explicit PoolPointer( X* p = 0 ) : m_pointer( p ), m_pPool( 0 ) { m_prev = m_next = this; }
 
35
      explicit PoolPointer( X* p, PointerPool<X>* pPool ) throw() : m_pointer( p ), m_pPool( pPool ) { m_prev = m_next = this; }
 
36
      ~PoolPointer() { release(); }
 
37
      PoolPointer( const PoolPointer& p ) throw() { acquire( p ); }
 
38
      PoolPointer& operator=( const PoolPointer& 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
      void relinquish() throw()
 
53
      {
 
54
        m_pPool = 0;
 
55
        m_pointer = 0;
 
56
        release();
 
57
      }
 
58
 
 
59
    private:
 
60
      X* m_pointer;
 
61
      mutable const PoolPointer* m_prev;
 
62
      mutable const PoolPointer* m_next;
 
63
      PointerPool<X>* m_pPool;
 
64
 
 
65
      void acquire( const PoolPointer& p ) throw()
 
66
      {
 
67
        m_pPool = p.m_pPool;
 
68
        m_pointer = p.m_pointer;
 
69
        m_next = p.m_next;
 
70
        m_next->m_prev = this;
 
71
        m_prev = &p;
 
72
#ifndef mutable
 
73
        p.m_next = this;
 
74
#else
 
75
        ( const_cast<linked_ptr<X>*>( &p ) )->m_next = this;
 
76
#endif
 
77
      }
 
78
 
 
79
      void release()
 
80
      {
 
81
        if ( unique() )
 
82
        {
 
83
          if ( m_pPool != 0 ) m_pPool->release( m_pointer );
 
84
          else delete m_pointer;
 
85
        }
 
86
        else
 
87
        {
 
88
          m_prev->m_next = m_next;
 
89
          m_next->m_prev = m_prev;
 
90
          m_prev = m_next = 0;
 
91
        }
 
92
        m_pointer = 0;
 
93
        m_pPool = 0;
 
94
      }
 
95
  };
 
96
}
 
97
 
 
98
#endif /* __tools_pool_pointer_h */