summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authordanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>2009-09-30 21:55:21 +0000
committerdanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>2009-09-30 21:55:21 +0000
commit3dc81ee1331d1b37ee85be9bf0d843e3b6827a2d (patch)
tree431e66fceef85d577d360865b3b878dc9132afc2 /include
parent7aa5e059a8f66d91bd8b69c58c657ceb70b4baff (diff)
Add explicit reference-counting base class
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11785 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'include')
-rw-r--r--include/base.h110
-rw-r--r--include/inspsocket.h2
-rw-r--r--include/users.h15
3 files changed, 59 insertions, 68 deletions
diff --git a/include/base.h b/include/base.h
index b66051caf..f41ea07d2 100644
--- a/include/base.h
+++ b/include/base.h
@@ -28,71 +28,69 @@ class CoreExport classbase
public:
classbase();
- // Called just prior to destruction via cull list
- virtual void cull();
+ /**
+ * Called just prior to destruction via cull list.
+ *
+ * @return true to allow the delete, or false to halt the delete
+ */
+ virtual bool cull();
virtual ~classbase();
};
-/** BoolSet is a utility class designed to hold eight bools in a bitmask.
- * Use BoolSet::Set and BoolSet::Get to set and get bools in the bitmask,
- * and Unset and Invert for special operations upon them.
+/** The base class for inspircd classes that support reference counting.
+ * Any objects that do not have a well-defined lifetime should inherit from
+ * this
*/
-class CoreExport BoolSet : public classbase
+class CoreExport refcountbase : public classbase
{
- /** Actual bit values */
- char bits;
-
+ unsigned int refcount;
public:
+ refcountbase();
+ virtual bool cull();
+ virtual ~refcountbase();
+ inline unsigned int GetReferenceCount() const { return refcount; }
+ friend class reference_base;
+};
- /** The default constructor initializes the BoolSet to all values unset.
- */
- BoolSet();
-
- /** This constructor copies the default bitmask from a char
- */
- BoolSet(char bitmask);
-
- /** The Set method sets one bool in the set.
- *
- * @param number The number of the item to set. This must be between 0 and 7.
- */
- void Set(int number);
-
- /** The Get method returns the value of one bool in the set
- *
- * @param number The number of the item to retrieve. This must be between 0 and 7.
- *
- * @return True if the item is set, false if it is unset.
- */
- bool Get(int number);
-
- /** The Unset method unsets one value in the set
- *
- * @param number The number of the item to set. This must be between 0 and 7.
- */
- void Unset(int number);
-
- /** The Unset method inverts (flips) one value in the set
- *
- * @param number The number of the item to invert. This must be between 0 and 7.
- */
- void Invert(int number);
-
- /** Compare two BoolSets
- */
- bool operator==(BoolSet other);
-
- /** OR two BoolSets together
- */
- BoolSet operator|(BoolSet other);
+class CoreExport reference_base
+{
+ protected:
+ static inline unsigned int inc(refcountbase* v) { return ++(v->refcount); }
+ static inline unsigned int dec(refcountbase* v) { return --(v->refcount); }
+};
- /** AND two BoolSets together
- */
- BoolSet operator&(BoolSet other);
+template <typename T>
+class CoreExport reference : public reference_base
+{
+ T* value;
+ public:
+ reference() : value(0) { }
+ reference(T* v) : value(v) { if (value) inc(value); }
+ reference(const reference& v) : value(v.value) { if (value) inc(value); }
+ reference<T>& operator=(const reference<T>& other)
+ {
+ if (other.value)
+ inc(other.value);
+ this->reference::~reference();
+ value = other.value;
+ return *this;
+ }
- /** Assign one BoolSet to another
- */
- bool operator=(BoolSet other);
+ ~reference()
+ {
+ if (value)
+ {
+ int rc = dec(value);
+ if (rc == 0 && value->cull())
+ delete value;
+ }
+ }
+ inline const T* operator->() const { return value; }
+ inline const T& operator*() const { return *value; }
+ inline T* operator->() { return value; }
+ inline T& operator*() { return *value; }
+ operator bool() const { return value; }
+ operator T*() const { return value; }
};
/** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
diff --git a/include/inspsocket.h b/include/inspsocket.h
index 73aa748a0..84716eae6 100644
--- a/include/inspsocket.h
+++ b/include/inspsocket.h
@@ -146,7 +146,7 @@ class CoreExport StreamSocket : public EventHandler
*/
virtual void Close();
/** This ensures that close is called prior to destructor */
- virtual void cull();
+ virtual bool cull();
};
/**
* BufferedSocket is an extendable socket class which modules
diff --git a/include/users.h b/include/users.h
index 76337b2c3..31c1c641d 100644
--- a/include/users.h
+++ b/include/users.h
@@ -62,7 +62,7 @@ class UserResolver;
/** Holds information relevent to &lt;connect allow&gt; and &lt;connect deny&gt; tags in the config file.
*/
-struct CoreExport ConnectClass : public classbase
+struct CoreExport ConnectClass : public refcountbase
{
/** Type of line, either CC_ALLOW or CC_DENY
*/
@@ -127,12 +127,6 @@ struct CoreExport ConnectClass : public classbase
*/
unsigned long limit;
- /** Reference counter.
- * This will be 1 if no users are connected, as long as it is a valid connect block
- * When it reaches 0, the object should be deleted
- */
- unsigned long RefCount;
-
/** Create a new connect class with no settings.
*/
ConnectClass(char type, const std::string& mask);
@@ -206,7 +200,7 @@ typedef std::vector< std::pair<irc::string, time_t> > InvitedList;
/** Holds a complete list of all allow and deny tags from the configuration file (connection classes)
*/
-typedef std::vector<ConnectClass*> ClassVector;
+typedef std::vector<reference<ConnectClass> > ClassVector;
/** Typedef for the list of user-channel records for a user
*/
@@ -274,9 +268,8 @@ class CoreExport User : public StreamSocket
static LocalStringExt OperQuit;
/** Contains a pointer to the connect class a user is on from - this will be NULL for remote connections.
- * The pointer is guarenteed to *always* be valid. :)
*/
- ConnectClass *MyClass;
+ reference<ConnectClass> MyClass;
/** Hostname of connection.
* This should be valid as per RFC1035.
@@ -869,7 +862,7 @@ class CoreExport User : public StreamSocket
/** Default destructor
*/
virtual ~User();
- virtual void cull();
+ virtual bool cull();
};
/** Derived from Resolver, and performs user forward/reverse lookups.