source: soft/giet_vm/memo/include/libelfpp/dpp/weak_ptr @ 790

Last change on this file since 790 was 163, checked in by karaoui, 12 years ago

changing mover to memo
changing soft.bin to soft.elf
...

File size: 3.5 KB
Line 
1/* -*- c++ -*-
2
3   C++ empty pointer classes
4
5   This file is part of the dpp library of C++ template classes
6
7   doc: http://diaxen.ssji.net/dpp/index.html
8   repo: https://www.ssji.net/svn/projets/trunk/libdpp
9
10   This program is free software: you can redistribute it and/or
11   modify it under the terms of the GNU Lesser General Public License
12   as published by the Free Software Foundation, either version 3 of
13   the License, or (at your option) any later version.
14
15   This program is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public
21   License along with this program.  If not, see
22   <http://www.gnu.org/licenses/>.
23
24   (c) 2008-2011 Alexandre Becoulet <alexandre.becoulet@free.fr>
25
26*/
27
28#ifndef DPP_EMPTY_REF_HH_
29#define DPP_EMPTY_REF_HH_
30
31/** @file @module{Empty pointer} */
32
33namespace dpp {
34
35  template <class X> class weak_ptr_base;
36  template <class X> class weak_ptr;
37
38  /**
39     @internal
40   */
41  class weak_ptr_node
42  {
43    template <class> friend class weak_ptr_base;
44    template <class> friend class weak_ptr;
45
46    void init()
47    {
48      m_prev = m_next = this;
49    }
50
51    void add(weak_ptr_node *n)
52    {
53      n->m_prev = this;
54      m_next->m_prev = n;
55      n->m_next = m_next;
56      m_next = n;
57    }
58
59    void remove()
60    {
61      m_prev->m_next = m_next;
62      m_next->m_prev = m_prev;
63    }
64
65    weak_ptr_node *m_next;
66    weak_ptr_node *m_prev;
67  };
68
69  /**
70      @short Empty pointer object base class
71      @module{Empty pointer}
72      @header dpp/weak_ptr
73
74      @This is the referenced object base class, any class which
75      inherits from this class can be pointed to by an @ref weak_ptr
76      pointer.
77   */
78  template <class X>
79  class weak_ptr_base : public weak_ptr_node
80  {
81    template <class> friend class weak_ptr;
82    typedef X _dpp_weak_ptr_type;
83
84  public:
85
86    weak_ptr_base()
87    {
88      weak_ptr_node::init();
89    }
90
91    ~weak_ptr_base()
92    {
93      for (weak_ptr_node *n = weak_ptr_node::m_next;
94           n != (weak_ptr_node*)this; n = n->m_next)
95        static_cast<weak_ptr<X>*>(n)->_obj = 0;
96    }
97  };
98
99  /**
100      @short Empty pointer class
101      @module{Empty pointer}
102      @header dpp/weak_ptr
103      @main
104
105      @This implements a pointer class which is automatically set to 0
106      when the associated object is deleted.
107
108      All pointers to the same object are linked together and
109      this list is used to clear all pointers from object
110      destructor. Object class must inherit from the @ref
111      weak_ptr_base class.
112
113      @example test/test_weak_ptr.cc:1
114  */
115  template <class X>
116  class weak_ptr : public weak_ptr_node
117  {
118    template <class> friend class weak_ptr_base;
119
120  public:
121    weak_ptr()
122      : _obj(0)
123    {
124    }
125
126    weak_ptr(X *o)
127      : _obj(o)
128    {
129      const_cast<typename X::_dpp_weak_ptr_type*>(_obj)->add(this);
130    }
131
132    ~weak_ptr()
133    {
134      if (_obj)
135        weak_ptr_node::remove();
136    }
137
138    weak_ptr & operator=(X *o)
139    {
140      if (_obj)
141        weak_ptr_node::remove();
142      _obj = o;
143      if (_obj)
144        const_cast<typename X::_dpp_weak_ptr_type*>(_obj)->add(this);
145      return *this;
146    }
147
148    X & operator*() const
149    {
150      return *_obj;
151    }
152
153    X * operator->() const
154    {
155      return _obj;
156    }
157
158    operator X* () const
159    {
160      return _obj;
161    }
162
163  private:
164    X *_obj;
165  };
166
167}
168
169#endif
170
Note: See TracBrowser for help on using the repository browser.