TrinityCore
Reference.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef _REFERENCE_H
19#define _REFERENCE_H
20
21#include "Dynamic/LinkedList.h"
22#include "Errors.h" // for ASSERT
23
24//=====================================================
25
26template <class TO, class FROM> class Reference : public LinkedListElement
27{
28 private:
29 TO* iRefTo;
30 FROM* iRefFrom;
31
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 = nullptr; iRefFrom = nullptr; }
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 != nullptr)
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 = nullptr;
66 iRefFrom = nullptr;
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 = nullptr;
76 }
77
78 bool isValid() const // Only check the iRefTo
79 {
80 return iRefTo != nullptr;
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&) = delete;
100 Reference& operator=(Reference const&) = delete;
101};
102
103//=====================================================
104#endif
#define ASSERT
Definition: Errors.h:68
LinkedListElement * nocheck_next()
Definition: LinkedList.h:47
LinkedListElement * prev()
Definition: LinkedList.h:44
LinkedListElement * nocheck_prev()
Definition: LinkedList.h:49
LinkedListElement * next()
Definition: LinkedList.h:42
TO * getTarget() const
Definition: Reference.h:94
void unlink()
Definition: Reference.h:61
Reference< TO, FROM > const * prev() const
Definition: Reference.h:86
Reference< TO, FROM > * prev()
Definition: Reference.h:85
Reference(Reference const &)=delete
Reference< TO, FROM > const * next() const
Definition: Reference.h:84
Reference< TO, FROM > const * nocheck_next() const
Definition: Reference.h:89
virtual void targetObjectDestroyLink()=0
virtual ~Reference()
Definition: Reference.h:43
virtual void sourceObjectDestroyLink()=0
void link(TO *toObj, FROM *fromObj)
Definition: Reference.h:46
bool isValid() const
Definition: Reference.h:78
Reference & operator=(Reference const &)=delete
Reference< TO, FROM > * nocheck_prev()
Definition: Reference.h:90
Reference< TO, FROM > * nocheck_next()
Definition: Reference.h:88
FROM * GetSource() const
Definition: Reference.h:96
Reference< TO, FROM > * next()
Definition: Reference.h:83
virtual void targetObjectBuildLink()=0
void invalidate()
Definition: Reference.h:71
TO * iRefTo
Definition: Reference.h:29
Reference()
Definition: Reference.h:42
TO * operator->() const
Definition: Reference.h:93
FROM * iRefFrom
Definition: Reference.h:30
Reference< TO, FROM > const * nocheck_prev() const
Definition: Reference.h:91