Anope IRC Services  Version 2.0
base.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (C) 2008-2011 Adam <Adam@anope.org>
4  * Copyright (C) 2008-2014 Anope Team <team@anope.org>
5  *
6  * Please read COPYING and README for further details.
7  *
8  */
9 
10 #ifndef BASE_H
11 #define BASE_H
12 
13 #include "services.h"
14 
18 {
19  /* References to this base class */
20  std::set<ReferenceBase *> *references;
21  public:
22  Base();
23  virtual ~Base();
24 
29  void AddReference(ReferenceBase *r);
30 
31  void DelReference(ReferenceBase *r);
32 };
33 
35 {
36  protected:
37  bool invalid;
38  public:
39  ReferenceBase() : invalid(false) { }
40  ReferenceBase(const ReferenceBase &other) : invalid(other.invalid) { }
41  virtual ~ReferenceBase() { }
42  inline void Invalidate() { this->invalid = true; }
43 };
44 
48 template<typename T>
49 class Reference : public ReferenceBase
50 {
51  protected:
52  T *ref;
53  public:
54  Reference() : ref(NULL)
55  {
56  }
57 
58  Reference(T *obj) : ref(obj)
59  {
60  if (ref)
61  ref->AddReference(this);
62  }
63 
64  Reference(const Reference<T> &other) : ReferenceBase(other), ref(other.ref)
65  {
66  if (operator bool())
67  ref->AddReference(this);
68  }
69 
70  virtual ~Reference()
71  {
72  if (operator bool())
73  ref->DelReference(this);
74  }
75 
76  inline Reference<T>& operator=(const Reference<T> &other)
77  {
78  if (this != &other)
79  {
80  if (*this)
81  this->ref->DelReference(this);
82 
83  this->ref = other.ref;
84  this->invalid = other.invalid;
85 
86  if (*this)
87  this->ref->AddReference(this);
88  }
89  return *this;
90  }
91 
92  /* We explicitly call operator bool here in several places to prevent other
93  * operators, such operator T*, from being called instead, which will mess
94  * with any class inheriting from this that overloads this operator.
95  */
96  virtual operator bool()
97  {
98  if (!this->invalid)
99  return this->ref != NULL;
100  return false;
101  }
102 
103  inline operator T*()
104  {
105  if (operator bool())
106  return this->ref;
107  return NULL;
108  }
109 
110  inline T* operator->()
111  {
112  if (operator bool())
113  return this->ref;
114  return NULL;
115  }
116 
117  inline T* operator*()
118  {
119  if (operator bool())
120  return this->ref;
121  return NULL;
122  }
123 
136  inline bool operator==(const Reference<T> &other)
137  {
138  if (!this->invalid)
139  return this->ref == other;
140  return false;
141  }
142 };
143 
144 #endif // BASE_H
145 
virtual ~Reference()
Definition: base.h:70
Definition: base.h:17
T * ref
Definition: base.h:52
T * operator->()
Definition: base.h:110
std::set< ReferenceBase * > * references
Definition: base.h:20
Reference()
Definition: base.h:54
Definition: base.h:49
Reference< T > & operator=(const Reference< T > &other)
Definition: base.h:76
ReferenceBase(const ReferenceBase &other)
Definition: base.h:40
void Invalidate()
Definition: base.h:42
Reference(T *obj)
Definition: base.h:58
bool operator==(const Reference< T > &other)
Definition: base.h:136
virtual ~ReferenceBase()
Definition: base.h:41
ReferenceBase()
Definition: base.h:39
#define CoreExport
Definition: services.h:62
T * operator*()
Definition: base.h:117
Reference(const Reference< T > &other)
Definition: base.h:64
bool invalid
Definition: base.h:37