TrinityCore
Loading...
Searching...
No Matches
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 Derived> class Reference : public LinkedListElement
27{
28 private:
29 TO* iRefTo;
30 FROM* iRefFrom;
31
32 protected:
33 // Notification functions are found by CRTP
34 // Tell our refTo (target) object that we have a link
35 //virtual void targetObjectBuildLink() = 0;
36
37 // Tell our refTo (taget) object, that the link is cut
38 //virtual void targetObjectDestroyLink() = 0;
39
40 // Tell our refFrom (source) object, that the link is cut (Target destroyed)
41 //virtual void sourceObjectDestroyLink() = 0;
42 public:
43 Reference() { iRefTo = nullptr; iRefFrom = nullptr; }
45
46 // Create new link
47 void link(TO* toObj, FROM* fromObj)
48 {
49 ASSERT(fromObj); // fromObj MUST not be NULL
50 if (isValid())
51 unlink();
52 if (toObj != nullptr)
53 {
54 iRefTo = toObj;
55 iRefFrom = fromObj;
56 static_cast<Derived*>(this)->targetObjectBuildLink();
57 }
58 }
59
60 // We don't need the reference anymore. Call comes from the refFrom object
61 // Tell our refTo object, that the link is cut
62 void unlink()
63 {
64 static_cast<Derived*>(this)->targetObjectDestroyLink();
65 delink();
66 iRefTo = nullptr;
67 iRefFrom = nullptr;
68 }
69
70 // Link is invalid due to destruction of referenced target object. Call comes from the refTo object
71 // Tell our refFrom object, that the link is cut
72 void invalidate() // the iRefFrom MUST remain!!
73 {
74 static_cast<Derived*>(this)->sourceObjectDestroyLink();
75 delink();
76 iRefTo = nullptr;
77 }
78
79 bool isValid() const // Only check the iRefTo
80 {
81 return iRefTo != nullptr;
82 }
83
84 TO* operator->() const { return iRefTo; }
85 TO* getTarget() const { return iRefTo; }
86
87 FROM* GetSource() const { return iRefFrom; }
88
89 private:
90 Reference(Reference const&) = delete;
91 Reference& operator=(Reference const&) = delete;
92};
93
94//=====================================================
95#endif
#define ASSERT
Definition Errors.h:80
bool isValid() const
Definition Reference.h:79
FROM * iRefFrom
Definition Reference.h:30
TO * getTarget() const
Definition Reference.h:85
Reference & operator=(Reference const &)=delete
void link(TO *toObj, FROM *fromObj)
Definition Reference.h:47
void invalidate()
Definition Reference.h:72
FROM * GetSource() const
Definition Reference.h:87
Reference(Reference const &)=delete
TO * iRefTo
Definition Reference.h:29
TO * operator->() const
Definition Reference.h:84
void unlink()
Definition Reference.h:62