summaryrefslogtreecommitdiff
path: root/include/base.h
blob: c09b97400efcc74cb6b09ef7557143953298fb33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 *
 * Copyright (C) 2008-2011 Adam <Adam@anope.org>
 * Copyright (C) 2008-2012 Anope Team <team@anope.org>
 *
 * Please read COPYING and README for further details.
 */

#ifndef BASE_H
#define BASE_H

#include "services.h"

/** The base class that most classes in Anope inherit from
 */
class CoreExport Base
{
	/* References to this base class */
	std::set<dynamic_reference_base *> References;
 public:
	Base();
	virtual ~Base();
	void AddReference(dynamic_reference_base *r);
	void DelReference(dynamic_reference_base *r);
};

class dynamic_reference_base
{
 protected:
	bool invalid;
 public:
	dynamic_reference_base() : invalid(false) { }
	dynamic_reference_base(const dynamic_reference_base &other) : invalid(other.invalid) { }
	virtual ~dynamic_reference_base() { }
	inline void Invalidate() { this->invalid = true; }
};

template<typename T>
class dynamic_reference : public dynamic_reference_base
{
 protected:
	T *ref;
 public:
 	dynamic_reference() : ref(NULL)
	{
	}

	dynamic_reference(T *obj) : ref(obj)
	{
		if (ref)
			ref->AddReference(this);
	}

	dynamic_reference(const dynamic_reference<T> &other) : dynamic_reference_base(other), ref(other.ref)
	{
		if (operator bool())
			ref->AddReference(this);
	}

	virtual ~dynamic_reference()
	{
		if (operator bool())
			ref->DelReference(this);
	}

	/* We explicitly call operator bool here in several places to prevent other
	 * operators, such operator T*, from being called instead, which will mess
	 * with any class inheriting from this that overloads this operator.
	 */
	virtual operator bool()
	{
		if (!this->invalid)
			return this->ref != NULL;
		return false;
	}

	inline operator T*()
	{
		if (operator bool())
			return this->ref;
		return NULL;
	}

	inline T* operator->()
	{
		if (operator bool())
			return this->ref;
		return NULL;
	}

	inline T* operator*()
	{
		if (operator bool())
			return this->ref;
		return NULL;
	}

	inline void operator=(T *newref)
	{
		if (operator bool())
			this->ref->DelReference(this);
		this->ref = newref;
		this->invalid = false;
		if (operator bool())
			this->ref->AddReference(this);
	}

	inline bool operator<(const dynamic_reference<T> &other) const
	{
		return this < &other;
	}

	inline bool operator==(const dynamic_reference<T> &other)
	{
		if (!this->invalid)
			return this->ref == other;
		return false;
	}
};

#endif // BASE_H