From 1ba242179fee46583098f48421af39ce9a8985a1 Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 15 Oct 2016 16:25:57 -0400 Subject: Various improvements/bugfixes to extdb stuff --- include/config.h | 8 +++++- include/modules/sql.h | 4 +-- include/serialize.h | 76 +++++++++++++++++++++++++++++++++------------------ include/users.h | 1 - include/xline.h | 2 +- 5 files changed, 59 insertions(+), 32 deletions(-) (limited to 'include') diff --git a/include/config.h b/include/config.h index dccadbe79..b6b36b726 100644 --- a/include/config.h +++ b/include/config.h @@ -102,8 +102,9 @@ namespace Configuration struct Usermode; struct Channelmode; - struct CoreExport Conf : Block + class CoreExport Conf : public Block { + public: /* options:readtimeout */ time_t ReadTimeout; /* options:useprivmsg */ @@ -162,6 +163,11 @@ namespace Configuration ServiceBot *GetClient(const Anope::string &name); Block *GetCommand(CommandSource &); + + void LoadBots(); + void ApplyBots(); + + void LoadOpers(); }; struct Uplink diff --git a/include/modules/sql.h b/include/modules/sql.h index 2e84db152..6770f407e 100644 --- a/include/modules/sql.h +++ b/include/modules/sql.h @@ -202,8 +202,8 @@ namespace SQL virtual std::vector InitSchema(const Anope::string &prefix) anope_abstract; virtual std::vector Replace(const Anope::string &table, const Query &, const std::set &) anope_abstract; - virtual std::vector CreateTable(const Anope::string &prefix, const Anope::string &table) anope_abstract; - virtual std::vector AlterTable(const Anope::string &, const Anope::string &table, const Anope::string &field, bool object) anope_abstract; + virtual std::vector CreateTable(const Anope::string &prefix, Serialize::TypeBase *) anope_abstract; + virtual std::vector AlterTable(const Anope::string &, Serialize::TypeBase *, Serialize::FieldBase *) anope_abstract; virtual std::vector CreateIndex(const Anope::string &table, const Anope::string &field) anope_abstract; virtual Query BeginTransaction() anope_abstract; diff --git a/include/serialize.h b/include/serialize.h index 1d0fb9400..8f35e61c6 100644 --- a/include/serialize.h +++ b/include/serialize.h @@ -48,6 +48,9 @@ namespace Serialize template inline T GetObject(); + template + inline std::vector GetObjects_(TypeBase *); + template inline std::vector GetObjects(const Anope::string &name); @@ -235,29 +238,6 @@ class CoreExport Serialize::TypeBase : public Service */ const Anope::string &GetName() { return this->name; } - /** - * Get all objects of the given type - */ - template - std::vector List() - { - std::vector ids; - EventReturn result = EventManager::Get()->Dispatch(&Event::SerializeEvents::OnSerializeList, this, ids); - if (result == EVENT_ALLOW) - { - std::vector o; - for (ID id : ids) - { - Object *s = Require(id); - if (s) - o.push_back(anope_dynamic_static_cast(s)); - } - return o; - } - - return GetObjects(); - } - /** Create a new object of this type. */ virtual Object *Create() anope_abstract; @@ -306,7 +286,10 @@ class Serialize::Type : public Base return new T(this, id); if (s->GetSerializableType() != this) + { + Log(LOG_DEBUG) << "Mismatch for required id " << id << ", is of type " << s->GetSerializableType()->GetName() << " but wants " << this->GetName(); return nullptr; + } return static_cast(s); } @@ -387,6 +370,8 @@ class Serialize::FieldBase : public Service */ bool depends; + bool object = false; + FieldBase(Module *, const Anope::string &, const Anope::string &, bool); virtual ~FieldBase(); void Unregister(); @@ -592,7 +577,10 @@ class Serialize::Field : public CommonFieldBase // module returned us data, so we unserialize it T t2 = this->Unserialize(value); + // should cache the default value somehow, but can't differentiate not cached from cached and default + // Cache + OnSet(s, t2); this->Set_(s, t2); return t2; @@ -609,11 +597,18 @@ class Serialize::Field : public CommonFieldBase SetField(this->Upcast(s), value); } - virtual void SetField(TypeImpl *s, const T &value) + /** + * Override to hook to changes in the internally + * cached value + */ + virtual void OnSet(TypeImpl *s, const T &value) { } + + void SetField(TypeImpl *s, const T &value) { Anope::string strvalue = this->Serialize(value); EventManager::Get()->Dispatch(&Event::SerializeEvents::OnSerializeSet, s, this, strvalue); + OnSet(s, value); this->Set_(s, value); } @@ -697,10 +692,12 @@ class Serialize::ObjectField : public CommonFieldBase public: ObjectField(TypeBase *t, const Anope::string &n, bool d = false) : CommonFieldBase(t, n, d) { + this->object = true; } ObjectField(TypeBase *t, const Anope::string &n, T TypeImpl::*field, bool d = false) : CommonFieldBase(t, n, field, d) { + this->object = true; } T GetField(TypeImpl *s) @@ -723,6 +720,7 @@ class Serialize::ObjectField : public CommonFieldBase T t2 = result == EVENT_ALLOW ? static_cast(base->Require(sid)) : nullptr; + OnSet(s, t2); this->Set_(s, t2); return t2; @@ -739,7 +737,9 @@ class Serialize::ObjectField : public CommonFieldBase SetField(this->Upcast(s), value); } - virtual void SetField(TypeImpl *s, T value) + virtual void OnSet(TypeImpl *s, T value) { } + + void SetField(TypeImpl *s, T value) { EventManager::Get()->Dispatch(&Event::SerializeEvents::OnSerializeSetSerializable, s, this, value); @@ -747,6 +747,7 @@ class Serialize::ObjectField : public CommonFieldBase if (old != nullptr && *old != nullptr) s->RemoveEdge(*old, this); + OnSet(s, value); this->Set_(s, value); if (value != nullptr) @@ -811,14 +812,35 @@ T Serialize::GetObject() return v.empty() ? nullptr : v[0]; } +template +std::vector Serialize::GetObjects_(TypeBase *type) +{ + std::vector o; + std::vector ids; + EventReturn result = EventManager::Get()->Dispatch(&Event::SerializeEvents::OnSerializeList, type, ids); + if (result == EVENT_ALLOW) + { + for (ID id : ids) + { + Object *s = type->Require(id); + if (s) + o.push_back(anope_dynamic_static_cast(s)); + } + return o; + } + + std::transform(type->objects.begin(), type->objects.end(), std::back_inserter(o), [](Object *e) { return anope_dynamic_static_cast(e); }); + return o; +} + template std::vector Serialize::GetObjects(const Anope::string &name) { std::vector objs; for (TypeBase *t : GetTypes(name)) - for (Object *s : t->objects) - objs.push_back(anope_dynamic_static_cast(s)); + for (T obj : GetObjects_(t)) + objs.push_back(obj); return objs; } diff --git a/include/users.h b/include/users.h index dacb7045e..be4ee2a39 100644 --- a/include/users.h +++ b/include/users.h @@ -38,7 +38,6 @@ extern CoreExport int OperCount; enum class UserType { USER, - //LOCAL_USER, BOT }; diff --git a/include/xline.h b/include/xline.h index 87b64d1da..05ddbe2d3 100644 --- a/include/xline.h +++ b/include/xline.h @@ -87,7 +87,7 @@ class XLineType : public Serialize::Type { using Serialize::Field::Field; - void SetField(XLine *s, const Anope::string &value) override; + void OnSet(XLine *s, const Anope::string &value) override; } mask; Serialize::Field by, reason, id; Serialize::Field created, expires; -- cgit