summaryrefslogtreecommitdiff
path: root/include/base.h
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2012-02-14 15:13:27 -0500
committerAdam <Adam@anope.org>2012-02-14 15:13:27 -0500
commita9772cde21407c89abd161d51aff45267f87b1fb (patch)
tree9e57ba6c121d3843888917d968dd4f5d030b57cf /include/base.h
parent086790d6331357022f4da17c76b26b9fc6e2ad90 (diff)
Clean up and reorganize our header files
Diffstat (limited to 'include/base.h')
-rw-r--r--include/base.h106
1 files changed, 106 insertions, 0 deletions
diff --git a/include/base.h b/include/base.h
new file mode 100644
index 000000000..2a7cb5147
--- /dev/null
+++ b/include/base.h
@@ -0,0 +1,106 @@
+/*
+ *
+ * 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) { }
+ 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(T *obj) : ref(obj)
+ {
+ if (ref)
+ ref->AddReference(this);
+ }
+
+ dynamic_reference(const dynamic_reference<T> &obj) : ref(obj.ref)
+ {
+ if (ref)
+ ref->AddReference(this);
+ }
+
+ virtual ~dynamic_reference()
+ {
+ if (this->invalid)
+ {
+ this->invalid = false;
+ this->ref = NULL;
+ }
+ else if (this->operator bool())
+ ref->DelReference(this);
+ }
+
+ virtual operator bool()
+ {
+ if (this->invalid)
+ {
+ this->invalid = false;
+ this->ref = NULL;
+ }
+ return this->ref != NULL;
+ }
+
+ virtual inline operator T*()
+ {
+ if (this->operator bool())
+ return this->ref;
+ return NULL;
+ }
+
+ virtual inline T *operator->()
+ {
+ if (this->operator bool())
+ return this->ref;
+ return NULL;
+ }
+
+ virtual inline void operator=(T *newref)
+ {
+ if (this->invalid)
+ {
+ this->invalid = false;
+ this->ref = NULL;
+ }
+ else if (this->operator bool())
+ this->ref->DelReference(this);
+ this->ref = newref;
+ if (this->operator bool())
+ this->ref->AddReference(this);
+ }
+};
+
+#endif // BASE_H
+