- /** Extend an Extensible class.
- *
- * @param key The key parameter is an arbitary string which identifies the extension data
- * @param p This parameter is a pointer to any data you wish to associate with the object
- *
- * You must provide a key to store the data as via the parameter 'key' and store the data
- * in the templated parameter 'p'.
- * The data will be inserted into the map. If the data already exists, you may not insert it
- * twice, Extensible::Extend will return false in this case.
- *
- * @return Returns true on success, false if otherwise
- */
- template<typename T> bool Extend(const std::string &key, T* p)
+/** The base class for inspircd classes that support reference counting.
+ * Any objects that do not have a well-defined lifetime should inherit from
+ * this, and should be assigned to a reference<type> object to establish their
+ * lifetime.
+ *
+ * Reference objects should not hold circular references back to themselves,
+ * even indirectly; this will cause a memory leak because the count will never
+ * drop to zero.
+ *
+ * Using a normal pointer for the object is recommended if you can assure that
+ * at least one reference<> will remain as long as that pointer is used; this
+ * will avoid the slight overhead of changing the reference count.
+ */
+class CoreExport refcountbase
+{
+ mutable unsigned int refcount;
+ public:
+ refcountbase();
+ virtual ~refcountbase();
+ inline unsigned int GetReferenceCount() const { return refcount; }
+ static inline void* operator new(size_t, void* m) { return m; }
+ static void* operator new(size_t);
+ static void operator delete(void*);
+ inline void refcount_inc() const { refcount++; }
+ inline bool refcount_dec() const { refcount--; return !refcount; }
+ private:
+ // uncopyable
+ refcountbase(const refcountbase&);
+ void operator=(const refcountbase&);
+};
+
+/** Base class for use count tracking. Uses reference<>, but does not
+ * cause object deletion when the last user is removed.
+ *
+ * Safe for use as a second parent class; will not add a second vtable.
+ */
+class CoreExport usecountbase
+{
+ mutable unsigned int usecount;
+ public:
+ usecountbase() : usecount(0) { }
+ ~usecountbase();
+ inline unsigned int GetUseCount() const { return usecount; }
+ inline void refcount_inc() const { usecount++; }
+ inline bool refcount_dec() const { usecount--; return false; }
+ private:
+ // uncopyable
+ usecountbase(const usecountbase&);
+ void operator=(const usecountbase&);
+};
+
+template <typename T>
+class reference
+{
+ T* value;
+ public:
+ reference() : value(0) { }
+ reference(T* v) : value(v) { if (value) value->refcount_inc(); }
+ reference(const reference<T>& v) : value(v.value) { if (value) value->refcount_inc(); }
+ reference<T>& operator=(const reference<T>& other)