Horizon Official Technical Documentation
Reference.hpp
Go to the documentation of this file.
1
2
3#ifndef HORIZON_CORE_STRUCTURES_REFERENCE_HPP
4#define HORIZON_CORE_STRUCTURES_REFERENCE_HPP
5
7#include <cassert>
8
9namespace Horizon
10{
11namespace Structures
12{
13namespace LinkedList
14{
26template <class TO, class FROM>
27class Reference : public Element
28{
29private:
31 FROM *_ref_from;
32
33protected:
34 // Tell our refTo (target) object that we have a link
35 virtual void target_object_build_link() = 0;
36
37 // Tell our refTo (taget) object, that the link is cut
38 virtual void target_object_destroy_link() = 0;
39
40 // Tell our refFrom (source) object, that the link is cut (Target destroyed)
41 virtual void source_object_destroy_link() = 0;
42public:
43 Reference() { _ref_to = nullptr; _ref_from = nullptr; }
44 virtual ~Reference() { }
45
49 void link(TO *toObj, FROM *fromObj)
50 {
51 assert(fromObj);
52
53 // Clear if any previous references to a reference manager.
54 if (is_valid())
55 remove();
56
57 // Build the link.
58 if (toObj != nullptr) {
59 _ref_to = toObj;
60 _ref_from = fromObj;
62 }
63 }
64
65 // We don't need the reference anymore. Call comes from the refFrom object
66 // Tell our refTo object, that the link is cut
67 void remove()
68 {
70 delink();
71 _ref_to = nullptr;
72 _ref_from = nullptr;
73 }
74
75 // Link is invalid due to destruction of referenced target object. Call comes from the refTo object
76 // Tell our refFrom object, that the link is cut
77 void invalidate() // the _ref_from MUST remain!!
78 {
80 delink();
81 _ref_to = nullptr;
82 }
83
84 bool is_valid() const
85 {
86 return _ref_to != nullptr;
87 }
88
90 Reference<TO, FROM> const *next() const { return((Reference<TO, FROM> const*) Element::next()); }
92 Reference<TO, FROM> const *prev() const { return((Reference<TO, FROM> const*) Element::prev()); }
93
98
99 TO *operator->() const { return _ref_to; }
100 TO *target() const { return _ref_to; }
101
102 FROM *source() const { return _ref_from; }
103
104private:
105 Reference(Reference const&) = delete;
106 Reference& operator=(Reference const&) = delete;
107};
108}
109}
110}
111
112#endif
This class is used to manage a linked list of Elements. It is not intended to be used directly....
Definition: Element.hpp:18
Element * next()
Returns the next Element in the list.
Definition: Element.hpp:36
void delink()
Removes this Element from the list.
Definition: Element.hpp:50
Element * nocheck_prev()
Returns the previous Element in the list without checking if it exists.
Definition: Element.hpp:46
Element * prev()
Returns the previous Element in the list.
Definition: Element.hpp:39
Element * nocheck_next()
Returns the next Element in the list without checking if it exists.
Definition: Element.hpp:43
Definition: Reference.hpp:28
void invalidate()
Definition: Reference.hpp:77
void link(TO *toObj, FROM *fromObj)
Links the Reference to the specified object by adding it to the front of the list.
Definition: Reference.hpp:49
TO * target() const
Definition: Reference.hpp:100
Reference< TO, FROM > const * next() const
Definition: Reference.hpp:90
FROM * _ref_from
Definition: Reference.hpp:31
Reference< TO, FROM > * prev()
Definition: Reference.hpp:91
Reference< TO, FROM > const * prev() const
Definition: Reference.hpp:92
FROM * source() const
Definition: Reference.hpp:102
Reference< TO, FROM > * nocheck_next()
Definition: Reference.hpp:94
bool is_valid() const
Definition: Reference.hpp:84
Reference< TO, FROM > const * nocheck_prev() const
Definition: Reference.hpp:97
void remove()
Definition: Reference.hpp:67
TO * _ref_to
Definition: Reference.hpp:30
TO * operator->() const
Definition: Reference.hpp:99
Reference< TO, FROM > * next()
Definition: Reference.hpp:89
virtual ~Reference()
Definition: Reference.hpp:44
Reference()
Definition: Reference.hpp:43
Reference & operator=(Reference const &)=delete
Reference< TO, FROM > const * nocheck_next() const
Definition: Reference.hpp:95
Reference< TO, FROM > * nocheck_prev()
Definition: Reference.hpp:96
Definition: Element.hpp:7