1
0
mirror of https://github.com/anope/anope.git synced 2026-06-12 19:14:47 +02:00

Added a classbase for the major classes, makes dynamic_reference invalidation really work.

This also cleans up a bit of the code in the modestacker.
This commit is contained in:
Adam
2010-08-22 00:34:02 -04:00
parent 8a4c6ae618
commit ada65a3baf
12 changed files with 144 additions and 215 deletions
+1 -1
View File
@@ -57,7 +57,7 @@ template<typename T> class ExtensibleItemPointerArray : public ExtensibleItemBas
T *GetItem() const { return Item; }
};
class CoreExport Extensible
class CoreExport Extensible : public virtual Base
{
private:
typedef std::map<Anope::string, ExtensibleItemBase *> extensible_map;
+7 -61
View File
@@ -74,7 +74,7 @@ enum ModeClass
/** This class is the basis of all modes in Anope
*/
class CoreExport Mode
class CoreExport Mode : public virtual Base
{
public:
/* Class of mode this is */
@@ -350,9 +350,9 @@ class StackerInfo
{
public:
/* Modes to be added */
std::list<std::pair<void *, Anope::string> > AddModes;
std::list<std::pair<Base *, Anope::string> > AddModes;
/* Modes to be deleted */
std::list<std::pair<void *, Anope::string> > DelModes;
std::list<std::pair<Base *, Anope::string> > DelModes;
/* The type of object this stacker info is for */
StackerType Type;
/* Bot this is sent from */
@@ -363,7 +363,7 @@ class StackerInfo
* @param Set true if setting, false if unsetting
* @param Param The param for the mode
*/
void AddMode(void *Mode, bool Set, const Anope::string &Param);
void AddMode(Base *Mode, bool Set, const Anope::string &Param);
};
/** This is mode manager
@@ -376,13 +376,13 @@ class CoreExport ModeManager
{
protected:
/* List of pairs of user/channels and their stacker info */
static std::list<std::pair<void *, StackerInfo *> > StackerObjects;
static std::list<std::pair<Base *, StackerInfo *> > StackerObjects;
/** Get the stacker info for an item, if one doesnt exist it is created
* @param Item The user/channel etc
* @return The stacker info
*/
static StackerInfo *GetInfo(void *Item);
static StackerInfo *GetInfo(Base *Item);
/** Build a list of mode strings to send to the IRCd from the mode stacker
* @param info The stacker info for a channel or user
@@ -390,24 +390,6 @@ class CoreExport ModeManager
*/
static std::list<Anope::string> BuildModeStrings(StackerInfo *info);
/** Add a mode to the stacker, internal use only
* @param bi The client to set the modes from
* @param u The user
* @param um The user mode
* @param Set Adding or removing?
* @param Param A param, if there is one
*/
static void StackerAddInternal(BotInfo *bi, User *u, UserMode *um, bool Set, const Anope::string &Param);
/** Add a mode to the stacker, internal use only
* @param bi The client to set the modes from
* @param c The channel
* @param cm The channel mode
* @param Set Adding or removing?
* @param Param A param, if there is one
*/
static void StackerAddInternal(BotInfo *bi, Channel *c, ChannelMode *cm, bool Set, const Anope::string &Param);
/** Really add a mode to the stacker, internal use only
* @param bi The client to set the modes from
* @param Object The object, user/channel
@@ -416,7 +398,7 @@ class CoreExport ModeManager
* @param Param A param, if there is one
* @param Type The type this is, user or channel
*/
static void StackerAddInternal(BotInfo *bi, void *Object, void *Mode, bool Set, const Anope::string &Param, StackerType Type);
static void StackerAddInternal(BotInfo *bi, Base *Object, Base *Mode, bool Set, const Anope::string &Param, StackerType Type);
public:
/* List of all modes Anope knows about */
@@ -489,24 +471,6 @@ class CoreExport ModeManager
*/
static void StackerAdd(BotInfo *bi, Channel *c, ChannelMode *cm, bool Set, const Anope::string &Param = "");
/** Add a mode to the stacker to be set on a channel
* @param bi The client to set the modes from
* @param c The channel
* @param Name The channel mode name
* @param Set true for setting, false for removing
* @param Param The param, if there is one
*/
static void StackerAdd(BotInfo *bi, Channel *c, ChannelModeName Name, bool Set, const Anope::string &Param = "");
/** Add a mode to the stacker to be set on a channel
* @param bi The client to set the modes from
* @param c The channel
* @param Mode The mode char
* @param Set true for setting, false for removing
* @param Param The param, if there is one
*/
static void StackerAdd(BotInfo *bi, Channel *c, const char Mode, bool Set, const Anope::string &Param = "");
/** Add a mode to the stacker to be set on a user
* @param bi The client to set the modes from
* @param u The user
@@ -516,24 +480,6 @@ class CoreExport ModeManager
*/
static void StackerAdd(BotInfo *bi, User *u, UserMode *um, bool Set, const Anope::string &Param = "");
/** Add a mode to the stacker to be set on a user
* @param bi The client to set the modes from
* @param u The user
* @param Name The user mode name
* @param Set true for setting, false for removing
* @param Param The param, if there is one
*/
static void StackerAdd(BotInfo *bi, User *u, UserModeName Name, bool Set, const Anope::string &Param = "");
/** Add a mode to the stacker to be set on a user
* @param bi The client to set the modes from
* @param u The user
* @param Mode The mode to be set
* @param Set true for setting, false for removing
* @param Param The param, if there is one
*/
static void StackerAdd(BotInfo *bi, User *u, const char Mode, bool Set, const Anope::string &Param = "");
/** Process all of the modes in the stacker and send them to the IRCd to be set on channels/users
*/
static void ProcessModes();
+64 -35
View File
@@ -64,7 +64,7 @@ if (true) \
{ \
(*_i)->x ; \
} \
catch (const CoreException &modexcept) \
catch (const ModuleException &modexcept) \
{ \
Alog() << "Exception caught: " << modexcept.GetReason(); \
} \
@@ -96,7 +96,7 @@ if (true) \
break; \
} \
} \
catch (const CoreException &modexcept) \
catch (const ModuleException &modexcept) \
{ \
Alog() << "Exception caught: " << modexcept.GetReason(); \
} \
@@ -201,7 +201,7 @@ class CallBack;
/** Every module in Anope is actually a class.
*/
class CoreExport Module
class CoreExport Module : public virtual Base
{
private:
bool permanent;
@@ -1249,7 +1249,7 @@ class CallBack : public Timer
}
};
class Service
class Service : public virtual Base
{
public:
Module *owner;
@@ -1260,56 +1260,67 @@ class Service
virtual ~Service();
};
class dynamic_reference_base
class dynamic_reference_base : public virtual Base
{
protected:
bool invalid;
public:
dynamic_reference_base();
virtual ~dynamic_reference_base();
dynamic_reference_base() : invalid(false) { }
virtual ~dynamic_reference_base() { }
inline void Invalidate() { this->invalid = true; }
};
extern std::list<dynamic_reference_base *> dyn_references;
template<typename T>
class dynamic_reference : public dynamic_reference_base
{
protected:
T *ref;
public:
dynamic_reference() : dynamic_reference_base(), ref(NULL) { }
dynamic_reference(T *obj) : ref(obj)
{
if (ref)
ref->AddReference(this);
}
dynamic_reference(T *obj) : dynamic_reference_base(), ref(obj) { }
virtual ~dynamic_reference() { }
virtual ~dynamic_reference()
{
if (this->invalid)
{
this->invalid = false;
this->ref = NULL;
}
else if (ref)
ref->DelReference(this);
}
virtual operator bool()
{
if (this->invalid)
{
this->invalid = false;
this->ref = NULL;
}
return this->ref;
}
virtual inline void operator=(T *newref)
{
if (this->invalid)
{
this->invalid = false;
this->ref = NULL;
}
else if (this->ref)
this->ref->DelReference(this);
this->ref = newref;
if (this->ref)
this->ref->AddReference(this);
}
virtual inline T *operator->()
{
return this->ref;
}
void Invalidate()
{
this->ref = NULL;
}
static void Invalidate(T *obj)
{
for (std::list<dynamic_reference_base *>::iterator it = dyn_references.begin(), it_end = dyn_references.end(); it != it_end;)
{
dynamic_reference<void *> *d = static_cast<dynamic_reference<void *> *>(*it);
++it;
if (d && d->ref == obj)
{
d->Invalidate();
}
}
}
};
template<typename T>
@@ -1319,25 +1330,43 @@ class service_reference : public dynamic_reference<T>
Anope::string name;
public:
service_reference(Module *o, const Anope::string &n) : dynamic_reference<T>(), owner(o), name(n)
service_reference(Module *o, const Anope::string &n) : dynamic_reference<T>(static_cast<T *>(ModuleManager::GetService(this->name))), owner(o), name(n)
{
}
~service_reference()
virtual ~service_reference()
{
}
operator bool()
{
if (this->invalid)
{
this->invalid = false;
this->ref = NULL;
}
if (!this->ref)
{
this->ref = static_cast<T *>(ModuleManager::GetService(this->name));
if (this->ref)
this->ref->AddReference(this);
}
return this->ref;
}
inline T *operator->()
{
if (this->invalid)
{
this->invalid = false;
this->ref = NULL;
}
if (!this->ref)
{
this->ref = static_cast<T *>(ModuleManager::GetService(this->name));
if (this->ref)
this->ref->AddReference(this);
}
return this->ref;
}
};
+1 -1
View File
@@ -10,7 +10,7 @@
#include "hashcomp.h"
class CoreExport OperType
class CoreExport OperType : public virtual Base
{
private:
/** The name of this opertype, e.g. "sra".
+1 -1
View File
@@ -74,7 +74,7 @@ enum ServerFlag
/** Class representing a server
*/
class CoreExport Server : public Flags<ServerFlag>
class CoreExport Server : public virtual Base, public Flags<ServerFlag>
{
private:
/* Server name */
+15
View File
@@ -193,6 +193,21 @@ extern "C" void __pfnBkCheck() {}
#include "anope.h"
class dynamic_reference_base;
/** 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);
};
/** 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
+3 -3
View File
@@ -251,17 +251,17 @@ class ModuleSQL : public Module
for (unsigned i = this->QueryRequests.size(); i > 0; --i)
{
QueryRequest &r = this->QueryRequests[i];
QueryRequest &r = this->QueryRequests[i - 1];
if (r.interface && r.interface->owner == m)
{
if (i == 0)
if (i == 1)
{
r.service->Lock.Lock();
r.service->Lock.Unlock();
}
this->QueryRequests.erase(this->QueryRequests.begin() + i);
this->QueryRequests.erase(this->QueryRequests.begin() + i - 1);
}
}
+25
View File
@@ -0,0 +1,25 @@
#include "services.h"
#include "modules.h"
Base::Base()
{
}
Base::~Base()
{
for (std::set<dynamic_reference_base *>::iterator it = this->References.begin(), it_end = this->References.end(); it != it_end; ++it)
{
(*it)->Invalidate();
}
}
void Base::AddReference(dynamic_reference_base *r)
{
this->References.insert(r);
}
void Base::DelReference(dynamic_reference_base *r)
{
this->References.erase(r);
}
+21 -93
View File
@@ -10,7 +10,7 @@
#include "modules.h"
/* List of pairs of user/channels and their stacker info */
std::list<std::pair<void *, StackerInfo *> > ModeManager::StackerObjects;
std::list<std::pair<Base *, StackerInfo *> > ModeManager::StackerObjects;
/* List of all modes Anope knows about */
std::map<Anope::string, Mode *> ModeManager::Modes;
@@ -420,7 +420,7 @@ void ChannelModeInvex::DelMask(Channel *chan, const Anope::string &mask)
}
}
void StackerInfo::AddMode(void *Mode, bool Set, const Anope::string &Param)
void StackerInfo::AddMode(Base *Mode, bool Set, const Anope::string &Param)
{
ChannelMode *cm = NULL;
UserMode *um = NULL;
@@ -428,17 +428,17 @@ void StackerInfo::AddMode(void *Mode, bool Set, const Anope::string &Param)
if (Type == ST_CHANNEL)
{
cm = static_cast<ChannelMode *>(Mode);
cm = debug_cast<ChannelMode *>(Mode);
if (cm->Type == MODE_PARAM)
IsParam = true;
}
else if (Type == ST_USER)
{
um = static_cast<UserMode *>(Mode);
um = debug_cast<UserMode *>(Mode);
if (um->Type == MODE_PARAM)
IsParam = true;
}
std::list<std::pair<void *, Anope::string> > *list, *otherlist;
std::list<std::pair<Base *, Anope::string> > *list, *otherlist;
if (Set)
{
list = &AddModes;
@@ -451,7 +451,7 @@ void StackerInfo::AddMode(void *Mode, bool Set, const Anope::string &Param)
}
/* Loop through the list and find if this mode is already on here */
std::list<std::pair<void *, Anope::string > >::iterator it, it_end;
std::list<std::pair<Base *, Anope::string > >::iterator it, it_end;
for (it = list->begin(), it_end = list->end(); it != it_end; ++it)
{
/* The param must match too (can have multiple status or list modes), but
@@ -489,11 +489,11 @@ void StackerInfo::AddMode(void *Mode, bool Set, const Anope::string &Param)
* @param Item The user/channel etc
* @return The stacker info
*/
StackerInfo *ModeManager::GetInfo(void *Item)
StackerInfo *ModeManager::GetInfo(Base *Item)
{
for (std::list<std::pair<void *, StackerInfo *> >::const_iterator it = StackerObjects.begin(), it_end = StackerObjects.end(); it != it_end; ++it)
for (std::list<std::pair<Base *, StackerInfo *> >::const_iterator it = StackerObjects.begin(), it_end = StackerObjects.end(); it != it_end; ++it)
{
const std::pair<void *, StackerInfo *> &PItem = *it;
const std::pair<Base *, StackerInfo *> &PItem = *it;
if (PItem.first == Item)
return PItem.second;
}
@@ -510,7 +510,7 @@ StackerInfo *ModeManager::GetInfo(void *Item)
std::list<Anope::string> ModeManager::BuildModeStrings(StackerInfo *info)
{
std::list<Anope::string> ret;
std::list<std::pair<void *, Anope::string> >::iterator it, it_end;
std::list<std::pair<Base *, Anope::string> >::iterator it, it_end;
Anope::string buf = "+", parambuf;
ChannelMode *cm = NULL;
UserMode *um = NULL;
@@ -528,12 +528,12 @@ std::list<Anope::string> ModeManager::BuildModeStrings(StackerInfo *info)
if (info->Type == ST_CHANNEL)
{
cm = static_cast<ChannelMode *>(it->first);
cm = debug_cast<ChannelMode *>(it->first);
buf += cm->ModeChar;
}
else if (info->Type == ST_USER)
{
um = static_cast<UserMode *>(it->first);
um = debug_cast<UserMode *>(it->first);
buf += um->ModeChar;
}
@@ -557,12 +557,12 @@ std::list<Anope::string> ModeManager::BuildModeStrings(StackerInfo *info)
if (info->Type == ST_CHANNEL)
{
cm = static_cast<ChannelMode *>(it->first);
cm = debug_cast<ChannelMode *>(it->first);
buf += cm->ModeChar;
}
else if (info->Type == ST_USER)
{
um = static_cast<UserMode *>(it->first);
um = debug_cast<UserMode *>(it->first);
buf += um->ModeChar;
}
@@ -579,30 +579,6 @@ std::list<Anope::string> ModeManager::BuildModeStrings(StackerInfo *info)
return ret;
}
/** Add a mode to the stacker, internal use only
* @param bi The client to set the modes from
* @param u The user
* @param um The user mode
* @param Set Adding or removing?
* @param Param A param, if there is one
*/
void ModeManager::StackerAddInternal(BotInfo *bi, User *u, UserMode *um, bool Set, const Anope::string &Param)
{
StackerAddInternal(bi, u, um, Set, Param, ST_USER);
}
/** Add a mode to the stacker, internal use only
* @param bi The client to set the modes from
* @param c The channel
* @param cm The channel mode
* @param Set Adding or removing?
* @param Param A param, if there is one
*/
void ModeManager::StackerAddInternal(BotInfo *bi, Channel *c, ChannelMode *cm, bool Set, const Anope::string &Param)
{
StackerAddInternal(bi, c, cm, Set, Param, ST_CHANNEL);
}
/** Really add a mode to the stacker, internal use only
* @param bi The client to set the modes from
* @param Object The object, user/channel
@@ -611,7 +587,7 @@ void ModeManager::StackerAddInternal(BotInfo *bi, Channel *c, ChannelMode *cm, b
* @param Param A param, if there is one
* @param Type The type this is, user or channel
*/
void ModeManager::StackerAddInternal(BotInfo *bi, void *Object, void *Mode, bool Set, const Anope::string &Param, StackerType Type)
void ModeManager::StackerAddInternal(BotInfo *bi, Base *Object, Base *Mode, bool Set, const Anope::string &Param, StackerType Type)
{
StackerInfo *s = GetInfo(Object);
s->Type = Type;
@@ -619,7 +595,7 @@ void ModeManager::StackerAddInternal(BotInfo *bi, void *Object, void *Mode, bool
if (bi)
s->bi = bi;
else if (Type == ST_CHANNEL)
s->bi = whosends(static_cast<Channel *>(Object)->ci);
s->bi = whosends(debug_cast<Channel *>(Object)->ci);
else if (Type == ST_USER)
s->bi = NULL;
}
@@ -778,31 +754,7 @@ char ModeManager::GetStatusChar(char Value)
*/
void ModeManager::StackerAdd(BotInfo *bi, Channel *c, ChannelMode *cm, bool Set, const Anope::string &Param)
{
StackerAddInternal(bi, c, cm, Set, Param);
}
/** Add a mode to the stacker to be set on a channel
* @param bi The client to set the modes from
* @param c The channel
* @param Name The channel mode name
* @param Set true for setting, false for removing
* @param Param The param, if there is one
*/
void ModeManager::StackerAdd(BotInfo *bi, Channel *c, ChannelModeName Name, bool Set, const Anope::string &Param)
{
StackerAdd(bi, c, FindChannelModeByName(Name), Set, Param);
}
/** Add a mode to the stacker to be set on a channel
* @param bi The client to set the modes from
* @param c The channel
* @param Mode The mode char
* @param Set true for setting, false for removing
* @param Param The param, if there is one
*/
void ModeManager::StackerAdd(BotInfo *bi, Channel *c, const char Mode, bool Set, const Anope::string &Param)
{
StackerAdd(bi, c, FindChannelModeByChar(Mode), Set, Param);
StackerAddInternal(bi, c, cm, Set, Param, ST_CHANNEL);
}
/** Add a mode to the stacker to be set on a user
@@ -814,31 +766,7 @@ void ModeManager::StackerAdd(BotInfo *bi, Channel *c, const char Mode, bool Set,
*/
void ModeManager::StackerAdd(BotInfo *bi, User *u, UserMode *um, bool Set, const Anope::string &Param)
{
StackerAddInternal(bi, u, um, Set, Param);
}
/** Add a mode to the stacker to be set on a user
* @param bi The client to set the modes from
* @param u The user
* @param Name The user mode name
* @param Set true for setting, false for removing
* @param Param The param, if there is one
*/
void ModeManager::StackerAdd(BotInfo *bi, User *u, UserModeName Name, bool Set, const Anope::string &Param)
{
StackerAdd(bi, u, FindUserModeByName(Name), Set, Param);
}
/** Add a mode to the stacker to be set on a user
* @param bi The client to set the modes from
* @param u The user
* @param Mode The mode to be set
* @param Set true for setting, false for removing
* @param Param The param, if there is one
*/
void ModeManager::StackerAdd(BotInfo *bi, User *u, const char Mode, bool Set, const Anope::string &Param)
{
StackerAdd(bi, u, FindUserModeByChar(Mode), Set, Param);
StackerAddInternal(bi, u, um, Set, Param, ST_USER);
}
/** Process all of the modes in the stacker and send them to the IRCd to be set on channels/users
@@ -847,16 +775,16 @@ void ModeManager::ProcessModes()
{
if (!StackerObjects.empty())
{
for (std::list<std::pair<void *, StackerInfo *> >::const_iterator it = StackerObjects.begin(), it_end = StackerObjects.end(); it != it_end; ++it)
for (std::list<std::pair<Base *, StackerInfo *> >::const_iterator it = StackerObjects.begin(), it_end = StackerObjects.end(); it != it_end; ++it)
{
StackerInfo *s = it->second;
User *u = NULL;
Channel *c = NULL;
if (s->Type == ST_USER)
u = static_cast<User *>(it->first);
u = debug_cast<User *>(it->first);
else if (s->Type == ST_CHANNEL)
c = static_cast<Channel *>(it->first);
c = debug_cast<Channel *>(it->first);
else
throw CoreException("ModeManager::ProcessModes got invalid Stacker Info type");
+2 -10
View File
@@ -37,6 +37,8 @@ Module::~Module()
for (i = 0; i < NUM_LANGS; ++i)
this->DeleteLanguage(i);
/* Detach all event hooks for this module */
ModuleManager::DetachAll(this);
/* Clear any active callbacks this module has */
ModuleManager::ClearCallBacks(this);
@@ -93,13 +95,3 @@ unsigned Version::GetBuild() const
return this->Build;
}
Service::Service(Module *o, const Anope::string &n) : owner(o), name(n)
{
ModuleManager::RegisterService(this);
}
Service::~Service()
{
ModuleManager::UnregisterService(this);
}
-1
View File
@@ -249,7 +249,6 @@ void ModuleManager::DeleteModule(Module *m)
if (!m || !m->handle)
return;
DetachAll(m);
ano_module_t handle = m->handle;
Anope::string filename = m->filename;
+4 -9
View File
@@ -382,18 +382,13 @@ Version Module::GetVersion() const
return Version(VERSION_MAJOR, VERSION_MINOR, VERSION_BUILD);
}
std::list<dynamic_reference_base *> dyn_references;
dynamic_reference_base::dynamic_reference_base()
Service::Service(Module *o, const Anope::string &n) : owner(o), name(n)
{
dyn_references.push_back(this);
ModuleManager::RegisterService(this);
}
dynamic_reference_base::~dynamic_reference_base()
Service::~Service()
{
std::list<dynamic_reference_base *>::iterator it = std::find(dyn_references.begin(), dyn_references.end(), this);
if (it != dyn_references.end())
dyn_references.erase(it);
ModuleManager::UnregisterService(this);
}