TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Reference.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef _REFERENCE_H
20 #define _REFERENCE_H
21 
22 #include "Dynamic/LinkedList.h"
23 #include "Errors.h" // for ASSERT
24 
25 //=====================================================
26 
27 template <class TO, class FROM> class Reference : public LinkedListElement
28 {
29  private:
30  TO* iRefTo;
31  FROM* iRefFrom;
32  protected:
33  // Tell our refTo (target) object that we have a link
34  virtual void targetObjectBuildLink() = 0;
35 
36  // Tell our refTo (taget) object, that the link is cut
37  virtual void targetObjectDestroyLink() = 0;
38 
39  // Tell our refFrom (source) object, that the link is cut (Target destroyed)
40  virtual void sourceObjectDestroyLink() = 0;
41  public:
42  Reference() { iRefTo = NULL; iRefFrom = NULL; }
43  virtual ~Reference() { }
44 
45  // Create new link
46  void link(TO* toObj, FROM* fromObj)
47  {
48  ASSERT(fromObj); // fromObj MUST not be NULL
49  if (isValid())
50  unlink();
51  if (toObj != NULL)
52  {
53  iRefTo = toObj;
54  iRefFrom = fromObj;
56  }
57  }
58 
59  // We don't need the reference anymore. Call comes from the refFrom object
60  // Tell our refTo object, that the link is cut
61  void unlink()
62  {
64  delink();
65  iRefTo = NULL;
66  iRefFrom = NULL;
67  }
68 
69  // Link is invalid due to destruction of referenced target object. Call comes from the refTo object
70  // Tell our refFrom object, that the link is cut
71  void invalidate() // the iRefFrom MUST remain!!
72  {
74  delink();
75  iRefTo = NULL;
76  }
77 
78  bool isValid() const // Only check the iRefTo
79  {
80  return iRefTo != NULL;
81  }
82 
87 
92 
93  TO* operator ->() const { return iRefTo; }
94  TO* getTarget() const { return iRefTo; }
95 
96  FROM* GetSource() const { return iRefFrom; }
97 
98  private:
99  Reference(Reference const&);
100  Reference& operator=(Reference const&);
101 };
102 
103 //=====================================================
104 #endif
TO * operator->() const
Definition: Reference.h:93
Definition: LinkedList.h:28
void unlink()
Definition: Reference.h:61
Reference & operator=(Reference const &)
virtual void targetObjectDestroyLink()=0
arena_t NULL
Definition: jemalloc_internal.h:624
virtual void sourceObjectDestroyLink()=0
Reference< TO, FROM > const * nocheck_prev() const
Definition: Reference.h:91
LinkedListElement * next()
Definition: LinkedList.h:43
Reference< TO, FROM > const * nocheck_next() const
Definition: Reference.h:89
void delink()
Definition: LinkedList.h:53
Reference< TO, FROM > const * prev() const
Definition: Reference.h:86
void invalidate()
Definition: Reference.h:71
LinkedListElement * nocheck_next()
Definition: LinkedList.h:48
Reference()
Definition: Reference.h:42
LinkedListElement * nocheck_prev()
Definition: LinkedList.h:50
Definition: Reference.h:27
Reference< TO, FROM > * nocheck_next()
Definition: Reference.h:88
LinkedListElement * prev()
Definition: LinkedList.h:45
Reference< TO, FROM > * next()
Definition: Reference.h:83
Reference< TO, FROM > * nocheck_prev()
Definition: Reference.h:90
void link(TO *toObj, FROM *fromObj)
Definition: Reference.h:46
virtual ~Reference()
Definition: Reference.h:43
TO * iRefTo
Definition: Reference.h:30
FROM * iRefFrom
Definition: Reference.h:31
FROM * GetSource() const
Definition: Reference.h:96
virtual void targetObjectBuildLink()=0
#define ASSERT
Definition: Errors.h:55
bool isValid() const
Definition: Reference.h:78
#define const
Definition: zconf.h:217
Reference< TO, FROM > const * next() const
Definition: Reference.h:84
TO * getTarget() const
Definition: Reference.h:94
Reference< TO, FROM > * prev()
Definition: Reference.h:85