diff options
Diffstat (limited to 'include/base.h')
-rw-r--r-- | include/base.h | 88 |
1 files changed, 50 insertions, 38 deletions
diff --git a/include/base.h b/include/base.h index ff3fee203..8c0546e2f 100644 --- a/include/base.h +++ b/include/base.h @@ -1,13 +1,23 @@ /* + * Anope IRC Services * - * (C) 2008-2011 Adam <Adam@anope.org> - * (C) 2008-2017 Anope Team <team@anope.org> + * Copyright (C) 2012-2017 Anope Team <team@anope.org> * - * Please read COPYING and README for further details. + * This file is part of Anope. Anope is free software; you can + * redistribute it and/or modify it under the terms of the GNU + * General Public License as published by the Free Software + * Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see see <http://www.gnu.org/licenses/>. */ -#ifndef BASE_H -#define BASE_H +#pragma once #include "services.h" @@ -32,13 +42,14 @@ class CoreExport Base class ReferenceBase { - protected: - bool invalid; + static std::set<ReferenceBase *> *references; public: - ReferenceBase() : invalid(false) { } - ReferenceBase(const ReferenceBase &other) : invalid(other.invalid) { } - virtual ~ReferenceBase() { } - inline void Invalidate() { this->invalid = true; } + static void ResetAll(); + + ReferenceBase(); + virtual ~ReferenceBase(); + virtual void Invalidate() anope_abstract; + virtual void Reset() { } }; /** Used to hold pointers to objects that may be deleted. A Reference will @@ -49,8 +60,11 @@ class Reference : public ReferenceBase { protected: T *ref; + + virtual void Check() { } + public: - Reference() : ref(NULL) + Reference() : ref(nullptr) { } @@ -60,19 +74,24 @@ class Reference : public ReferenceBase ref->AddReference(this); } - Reference(const Reference<T> &other) : ReferenceBase(other), ref(other.ref) + Reference(const Reference<T> &other) : ref(other.ref) { - if (operator bool()) + if (ref) ref->AddReference(this); } virtual ~Reference() { - if (operator bool()) - ref->DelReference(this); + if (*this) + this->ref->DelReference(this); + } + + void Invalidate() override + { + ref = nullptr; } - inline Reference<T>& operator=(const Reference<T> &other) + Reference<T>& operator=(const Reference<T> &other) { if (this != &other) { @@ -80,7 +99,6 @@ class Reference : public ReferenceBase this->ref->DelReference(this); this->ref = other.ref; - this->invalid = other.invalid; if (*this) this->ref->AddReference(this); @@ -88,36 +106,31 @@ class Reference : public ReferenceBase return *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() + explicit operator bool() { - if (!this->invalid) - return this->ref != NULL; - return false; + Check(); + return this->ref != nullptr; } - inline operator T*() + operator T*() { - if (operator bool()) + if (*this) return this->ref; - return NULL; + return nullptr; } - inline T* operator->() + T* operator->() { - if (operator bool()) + if (*this) return this->ref; - return NULL; + return nullptr; } - inline T* operator*() + T* operator*() { - if (operator bool()) + if (*this) return this->ref; - return NULL; + return nullptr; } /** Note that we can't have an operator< that returns this->ref < other.ref @@ -132,12 +145,11 @@ class Reference : public ReferenceBase * actually referred to. */ - inline bool operator==(const Reference<T> &other) + bool operator==(const Reference<T> &other) { - if (!this->invalid) + if (*this) return this->ref == other; return false; } }; -#endif // BASE_H |