1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef _REFERENCE_H |
---|
22 | #define _REFERENCE_H |
---|
23 | |
---|
24 | #include "Utilities/LinkedList.h" |
---|
25 | |
---|
26 | //===================================================== |
---|
27 | |
---|
28 | template <class TO, class FROM> class Reference : public LinkedListElement |
---|
29 | { |
---|
30 | private: |
---|
31 | TO* iRefTo; |
---|
32 | FROM* iRefFrom; |
---|
33 | protected: |
---|
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 = NULL; iRefFrom = NULL; } |
---|
44 | virtual ~Reference() {} |
---|
45 | |
---|
46 | // Create new link |
---|
47 | inline void link(TO* toObj, FROM* fromObj) |
---|
48 | { |
---|
49 | assert(fromObj); // fromObj MUST not be NULL |
---|
50 | if(isValid()) |
---|
51 | unlink(); |
---|
52 | if(toObj != NULL) |
---|
53 | { |
---|
54 | iRefTo = toObj; |
---|
55 | iRefFrom = fromObj; |
---|
56 | 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 | inline void unlink() { targetObjectDestroyLink(); delink(); iRefTo = NULL; iRefFrom = NULL; } |
---|
63 | |
---|
64 | // Link is invalid due to destruction of referenced target object. Call comes from the refTo object |
---|
65 | // Tell our refFrom object, that the link is cut |
---|
66 | inline void invalidate() // the iRefFrom MUST remain!! |
---|
67 | { |
---|
68 | sourceObjectDestroyLink(); delink(); iRefTo = NULL; |
---|
69 | } |
---|
70 | |
---|
71 | inline bool isValid() const // Only check the iRefTo |
---|
72 | { |
---|
73 | return iRefTo != NULL; |
---|
74 | } |
---|
75 | |
---|
76 | Reference<TO,FROM>* next() { return((Reference<TO,FROM>*)LinkedListElement::next()); } |
---|
77 | Reference<TO,FROM>const* next() const { return((Reference<TO,FROM> const*)LinkedListElement::next()); } |
---|
78 | Reference<TO,FROM>* prev() { return((Reference<TO,FROM>*)LinkedListElement::prev()); } |
---|
79 | |
---|
80 | inline TO* operator ->() const { return iRefTo; } |
---|
81 | inline TO* getTarget() const { return iRefTo; } |
---|
82 | |
---|
83 | inline FROM* getSource() const { return iRefFrom; } |
---|
84 | }; |
---|
85 | |
---|
86 | //===================================================== |
---|
87 | #endif |
---|