Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
Reference.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#ifndef _REFERENCE_H
7#define _REFERENCE_H
8
10#include "Errors.h" // for ASSERT
11
12//=====================================================
13
14template <class TO, class FROM> class Reference : public LinkedListElement
15{
16private:
17 TO* iRefTo;
18 FROM* iRefFrom;
19protected:
20 // Tell our refTo (target) object that we have a link
21 virtual void targetObjectBuildLink() = 0;
22
23 // Tell our refTo (taget) object, that the link is cut
24 virtual void targetObjectDestroyLink() = 0;
25
26 // Tell our refFrom (source) object, that the link is cut (Target destroyed)
27 virtual void sourceObjectDestroyLink() = 0;
28public:
29 Reference() { iRefTo = NULL; iRefFrom = NULL; }
30 virtual ~Reference() { }
31
32 // Create new link
33 void link(TO* toObj, FROM* fromObj)
34 {
35 ASSERT(fromObj); // fromObj MUST not be NULL
36 if (isValid())
37 unlink();
38 if (toObj != NULL)
39 {
40 iRefTo = toObj;
41 iRefFrom = fromObj;
43 }
44 }
45
46 // We don't need the reference anymore. Call comes from the refFrom object
47 // Tell our refTo object, that the link is cut
48 void unlink()
49 {
51 delink();
52 iRefTo = NULL;
53 iRefFrom = NULL;
54 }
55
56 // Link is invalid due to destruction of referenced target object. Call comes from the refTo object
57 // Tell our refFrom object, that the link is cut
58 void invalidate() // the iRefFrom MUST remain!!
59 {
61 delink();
62 iRefTo = NULL;
63 }
64
65 bool isValid() const // Only check the iRefTo
66 {
67 return iRefTo != NULL;
68 }
69
74
79
80 TO* operator ->() const { return iRefTo; }
81 TO* getTarget() const { return iRefTo; }
82
83 FROM* GetSource() const { return iRefFrom; }
84private:
87};
88
89//=====================================================
90#endif
#define ASSERT
Definition Errors.h:29
LinkedListElement * nocheck_next()
Definition LinkedList.h:35
LinkedListElement * prev()
Definition LinkedList.h:32
LinkedListElement * nocheck_prev()
Definition LinkedList.h:37
LinkedListElement * next()
Definition LinkedList.h:30
TO * getTarget() const
Definition Reference.h:81
void unlink()
Definition Reference.h:48
Reference< TO, FROM > const * prev() const
Definition Reference.h:73
Reference< TO, FROM > * prev()
Definition Reference.h:72
Reference< TO, FROM > const * next() const
Definition Reference.h:71
Reference< TO, FROM > const * nocheck_next() const
Definition Reference.h:76
virtual void targetObjectDestroyLink()=0
virtual ~Reference()
Definition Reference.h:30
virtual void sourceObjectDestroyLink()=0
void link(TO *toObj, FROM *fromObj)
Definition Reference.h:33
bool isValid() const
Definition Reference.h:65
Reference< TO, FROM > * nocheck_prev()
Definition Reference.h:77
Reference< TO, FROM > * nocheck_next()
Definition Reference.h:75
FROM * GetSource() const
Definition Reference.h:83
Reference< TO, FROM > * next()
Definition Reference.h:70
virtual void targetObjectBuildLink()=0
Reference & operator=(Reference const &)
void invalidate()
Definition Reference.h:58
TO * iRefTo
Definition Reference.h:17
Reference(Reference const &)
TO * operator->() const
Definition Reference.h:80
FROM * iRefFrom
Definition Reference.h:18
Reference< TO, FROM > const * nocheck_prev() const
Definition Reference.h:78