File: nnweakrefptr.h

package info (click to toggle)
museek%2B 1%3A0.2%2Bsvn20100315.r1208-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 7,628 kB
  • sloc: cpp: 28,853; python: 28,014; ansic: 538; makefile: 122; sh: 117
file content (137 lines) | stat: -rw-r--r-- 4,052 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*  NewNet - A networking framework in C++
    Copyright (C) 2006-2007 Ingmar K. Steen (iksteen@gmail.com)
    Copyright 2008 little blue poney <lbponey@users.sourceforge.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 */

#ifndef NEWNET_WEAKREFPTR_H
#define NEWNET_WEAKREFPTR_H

#include "nnbaseptr.h"
#include "nnguardobject.h"

namespace NewNet
{
  class Object;

  //! A weak reference pointer class.
  /*! A weak reference pointer as implemented in this class points at NULL
      after the object it once pointed as is deleted. This is done by
      registering a callback to the object's guard object so that the
      pointer class will be notified when the object is deleted. */
  template<class T> class WeakRefPtr : public BasePtr<T>
  {
  private:
#ifndef DOXYGEN_UNDOCUMENTED
    class GuardObjectCallback : public GuardObject::Callback
    {
    public:
      GuardObjectCallback(WeakRefPtr * ptr) : m_Ptr(ptr)
      {
      }

      void operator()(Object * p)
      {
        m_Ptr->objectDestroyed(p);
      }

    private:
      WeakRefPtr * m_Ptr;
    };

    GuardObjectCallback * m_GuardObjectCallback;
#endif // DOXYGEN_UNDOCUMENTED

  public:
    //! Create a new weak reference pointer that points at NULL.
    /*! Create a new weak reference pointer that points at NULL. */
    WeakRefPtr() : BasePtr<T>()
    {
      m_GuardObjectCallback = new GuardObjectCallback(this);
    }

    //! Create a new weak reference pointer that points at t.
    /*! Create a new weak reference pointer that points at t. */
    WeakRefPtr(T * t) : BasePtr<T>(t)
    {
      m_GuardObjectCallback = new GuardObjectCallback(this);
      if(t)
        t->guardObject() += m_GuardObjectCallback;
    }

    //! Create a new weak reference pointer that points at t.
    /*! Create a new weak reference pointer that points at t. */
    WeakRefPtr(const WeakRefPtr& t) : BasePtr<T>(t.m_Ptr)
    {
      m_GuardObjectCallback = new GuardObjectCallback(this);
      if(t.m_Ptr)
        t.m_Ptr->guardObject() += m_GuardObjectCallback;
    }

    //! Assign t's object to this pointer.
    /*! Assign t's object to this pointer. */
    WeakRefPtr& operator=(const WeakRefPtr& t)
    {
      if(BasePtr<T>::m_Ptr == t.m_Ptr)
        return *this;
      if(BasePtr<T>::m_Ptr)
        BasePtr<T>::m_Ptr->guardObject() -= m_GuardObjectCallback;
      BasePtr<T>::m_Ptr = t.m_Ptr;
      if(t.m_Ptr)
        BasePtr<T>::m_Ptr->guardObject() += m_GuardObjectCallback;
      return *this;
    }

    //! Assign object t to this pointer.
    /*! Assign object t to this pointer. */
    WeakRefPtr& operator=(T * t)
    {
      if(BasePtr<T>::m_Ptr == t)
        return *this;
      if(BasePtr<T>::m_Ptr)
        BasePtr<T>::m_Ptr->guardObject() -= m_GuardObjectCallback;
      BasePtr<T>::m_Ptr = t;
      if(t)
        t->guardObject() += m_GuardObjectCallback;
      return *this;
    }

#ifndef DOXYGEN_UNDOCUMENTED
    ~WeakRefPtr()
    {
      if(BasePtr<T>::m_Ptr)
        BasePtr<T>::m_Ptr->guardObject() -= m_GuardObjectCallback;
      delete m_GuardObjectCallback;
    }
#endif // DOXYGEN_UNDOCUMENTED

  protected:
#ifndef DOXYGEN_UNDOCUMENTED
    void objectDestroyed(Object * p)
    {
#ifdef NN_PTR_DEBUG
      assert(p == BasePtr<T>::m_Ptr);
#endif
      BasePtr<T>::m_Ptr = 0;
    }
#endif // DOXYGEN_UNDOCUMENTED
  };
}

#undef NN_PTR_CHECK

#endif // NEWNET_REFPTR_H