2007-07-16 17:30:04 +00:00
|
|
|
/* +------------------------------------+
|
|
|
|
* | Inspire Internet Relay Chat Daemon |
|
|
|
|
* +------------------------------------+
|
|
|
|
*
|
2009-01-02 18:16:05 +00:00
|
|
|
* InspIRCd: (C) 2002-2009 InspIRCd Development Team
|
2009-03-15 12:42:35 +00:00
|
|
|
* See: http://wiki.inspircd.org/Credits
|
2007-07-16 17:30:04 +00:00
|
|
|
*
|
|
|
|
* This program is free but copyrighted software; see
|
|
|
|
* the file COPYING for details.
|
|
|
|
*
|
|
|
|
* ---------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2009-02-14 21:14:36 +00:00
|
|
|
#ifndef __BASE_H__
|
|
|
|
#define __BASE_H__
|
2007-07-16 17:30:04 +00:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <deque>
|
|
|
|
#include <string>
|
|
|
|
|
2009-10-17 18:52:39 +00:00
|
|
|
/** Dummy class to help enforce culls being parent-called up to classbase */
|
|
|
|
class CullResult
|
|
|
|
{
|
|
|
|
CullResult();
|
|
|
|
friend class classbase;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** The base class for all inspircd classes with a well-defined lifetime.
|
|
|
|
* Classes that inherit from this may be destroyed through GlobalCulls,
|
|
|
|
* and may rely on cull() being called prior to their deletion.
|
|
|
|
*/
|
2007-07-16 17:30:04 +00:00
|
|
|
class CoreExport classbase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
classbase();
|
|
|
|
|
2009-09-30 21:55:21 +00:00
|
|
|
/**
|
|
|
|
* Called just prior to destruction via cull list.
|
|
|
|
*
|
|
|
|
* @return true to allow the delete, or false to halt the delete
|
|
|
|
*/
|
2009-10-17 18:52:39 +00:00
|
|
|
virtual CullResult cull();
|
2009-09-21 13:26:31 +00:00
|
|
|
virtual ~classbase();
|
2009-10-17 18:52:39 +00:00
|
|
|
private:
|
|
|
|
// uncopyable
|
|
|
|
classbase(const classbase&);
|
|
|
|
void operator=(const classbase&);
|
2007-07-16 17:30:04 +00:00
|
|
|
};
|
|
|
|
|
2009-09-30 21:55:21 +00:00
|
|
|
/** The base class for inspircd classes that support reference counting.
|
|
|
|
* Any objects that do not have a well-defined lifetime should inherit from
|
2009-10-17 18:52:39 +00:00
|
|
|
* 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.
|
2007-07-16 17:30:04 +00:00
|
|
|
*/
|
2009-10-17 18:52:39 +00:00
|
|
|
class CoreExport refcountbase
|
2007-07-16 17:30:04 +00:00
|
|
|
{
|
2009-09-30 21:55:21 +00:00
|
|
|
unsigned int refcount;
|
2007-07-16 17:30:04 +00:00
|
|
|
public:
|
2009-09-30 21:55:21 +00:00
|
|
|
refcountbase();
|
|
|
|
virtual ~refcountbase();
|
|
|
|
inline unsigned int GetReferenceCount() const { return refcount; }
|
|
|
|
friend class reference_base;
|
2009-10-17 18:52:39 +00:00
|
|
|
private:
|
|
|
|
// uncopyable
|
|
|
|
refcountbase(const refcountbase&);
|
|
|
|
void operator=(const refcountbase&);
|
2009-09-30 21:55:21 +00:00
|
|
|
};
|
2007-07-16 17:30:04 +00:00
|
|
|
|
2009-09-30 21:55:21 +00:00
|
|
|
class CoreExport reference_base
|
|
|
|
{
|
|
|
|
protected:
|
2009-10-14 22:12:55 +00:00
|
|
|
template<typename T> static inline unsigned int inc(T* v) { return ++(v->refcount); }
|
|
|
|
template<typename T> static inline unsigned int dec(T* v) { return --(v->refcount); }
|
2009-09-30 21:55:21 +00:00
|
|
|
};
|
2007-07-16 17:30:04 +00:00
|
|
|
|
2009-09-30 21:55:21 +00:00
|
|
|
template <typename T>
|
2009-10-12 20:50:23 +00:00
|
|
|
class reference : public reference_base
|
2009-09-30 21:55:21 +00:00
|
|
|
{
|
|
|
|
T* value;
|
|
|
|
public:
|
|
|
|
reference() : value(0) { }
|
|
|
|
reference(T* v) : value(v) { if (value) inc(value); }
|
2009-10-17 19:12:12 +00:00
|
|
|
reference<T>& operator=(T* v)
|
2009-09-30 21:55:21 +00:00
|
|
|
{
|
2009-10-17 19:12:12 +00:00
|
|
|
if (v)
|
|
|
|
inc(v);
|
2009-09-30 21:55:21 +00:00
|
|
|
this->reference::~reference();
|
2009-10-17 19:12:12 +00:00
|
|
|
value = v;
|
2009-09-30 21:55:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~reference()
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
int rc = dec(value);
|
2009-10-17 18:52:39 +00:00
|
|
|
if (rc == 0)
|
2009-09-30 21:55:21 +00:00
|
|
|
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; }
|
2009-10-12 20:23:26 +00:00
|
|
|
inline operator bool() const { return value; }
|
|
|
|
inline operator T*() const { return value; }
|
2007-07-16 17:30:04 +00:00
|
|
|
};
|
|
|
|
|
2007-08-28 18:47:55 +00:00
|
|
|
/** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
|
|
|
|
* When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
|
|
|
|
* a class derived from ModuleException. If a module throws an exception during its constructor, the module will not
|
|
|
|
* be loaded. If this happens, the error message returned by ModuleException::GetReason will be displayed to the user
|
|
|
|
* attempting to load the module, or dumped to the console if the ircd is currently loading for the first time.
|
|
|
|
*/
|
|
|
|
class CoreExport CoreException : public std::exception
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
/** Holds the error message to be displayed
|
|
|
|
*/
|
|
|
|
const std::string err;
|
|
|
|
/** Source of the exception
|
|
|
|
*/
|
|
|
|
const std::string source;
|
|
|
|
public:
|
|
|
|
/** Default constructor, just uses the error mesage 'Core threw an exception'.
|
|
|
|
*/
|
|
|
|
CoreException() : err("Core threw an exception"), source("The core") {}
|
|
|
|
/** This constructor can be used to specify an error message before throwing.
|
|
|
|
*/
|
|
|
|
CoreException(const std::string &message) : err(message), source("The core") {}
|
|
|
|
/** This constructor can be used to specify an error message before throwing,
|
|
|
|
* and to specify the source of the exception.
|
|
|
|
*/
|
|
|
|
CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
|
|
|
|
/** This destructor solves world hunger, cancels the world debt, and causes the world to end.
|
|
|
|
* Actually no, it does nothing. Never mind.
|
|
|
|
* @throws Nothing!
|
|
|
|
*/
|
|
|
|
virtual ~CoreException() throw() {};
|
|
|
|
/** Returns the reason for the exception.
|
|
|
|
* The module should probably put something informative here as the user will see this upon failure.
|
|
|
|
*/
|
|
|
|
virtual const char* GetReason()
|
|
|
|
{
|
|
|
|
return err.c_str();
|
|
|
|
}
|
2007-07-16 17:30:04 +00:00
|
|
|
|
2007-08-28 18:47:55 +00:00
|
|
|
virtual const char* GetSource()
|
|
|
|
{
|
|
|
|
return source.c_str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CoreExport ModuleException : public CoreException
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Default constructor, just uses the error mesage 'Module threw an exception'.
|
|
|
|
*/
|
|
|
|
ModuleException() : CoreException("Module threw an exception", "A Module") {}
|
|
|
|
|
|
|
|
/** This constructor can be used to specify an error message before throwing.
|
|
|
|
*/
|
|
|
|
ModuleException(const std::string &message) : CoreException(message, "A Module") {}
|
|
|
|
/** This destructor solves world hunger, cancels the world debt, and causes the world to end.
|
|
|
|
* Actually no, it does nothing. Never mind.
|
|
|
|
* @throws Nothing!
|
|
|
|
*/
|
|
|
|
virtual ~ModuleException() throw() {};
|
|
|
|
};
|
2007-07-16 17:30:04 +00:00
|
|
|
|
2007-08-28 18:47:55 +00:00
|
|
|
#endif
|