From 76142b1970d6fd68d001e9972833046c79483a49 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 3 Dec 2017 14:40:01 -0500 Subject: serialize: set and cache new value before calling OnSet and pass old value to OnSet --- include/serialize.h | 16 +++++++++------- include/xline.h | 2 +- modules/chanserv/main/channeltype.cpp | 3 +-- modules/chanserv/main/channeltype.h | 2 +- modules/nickserv/main/accounttype.cpp | 3 +-- modules/nickserv/main/accounttype.h | 2 +- modules/nickserv/main/nicktype.cpp | 4 ++-- modules/nickserv/main/nicktype.h | 2 +- src/xline.cpp | 2 +- 9 files changed, 18 insertions(+), 18 deletions(-) diff --git a/include/serialize.h b/include/serialize.h index af10fe856..ecaf2609f 100644 --- a/include/serialize.h +++ b/include/serialize.h @@ -602,8 +602,8 @@ class Serialize::Field : public CommonFieldBase T t2 = this->Unserialize(value); // set & cache - OnSet(s, t2); this->Set_(s, t2); + OnSet(s, t, t2); return t2; } @@ -620,14 +620,16 @@ class Serialize::Field : public CommonFieldBase * Override to hook to changes in the internally * cached value */ - virtual void OnSet(TypeImpl *s, const T &value) { } + virtual void OnSet(TypeImpl *s, T *old, const T &value) { } void SetField(TypeImpl *s, const T &value) { + T* t = this->Get_(s); + Anope::string strvalue = this->Serialize(value); EventManager::Get()->Dispatch(&Event::SerializeEvents::OnSerializeSet, s, this, strvalue); - OnSet(s, value); + OnSet(s, t, value); this->Set_(s, value); } @@ -739,8 +741,8 @@ class Serialize::ObjectField : public CommonFieldBase T t2 = result == EVENT_ALLOW ? static_cast(base->Require(sid)) : nullptr; - OnSet(s, t2); - this->Set_(s, t2); + this->Set_(s, t2); // internally set & cache + OnSet(s, t != nullptr ? *t : nullptr, t2); // now trigger set event with old and new values return t2; } @@ -753,7 +755,7 @@ class Serialize::ObjectField : public CommonFieldBase SetField(this->Upcast(s), value); } - virtual void OnSet(TypeImpl *s, T value) { } + virtual void OnSet(TypeImpl *s, T old, T value) { } void SetField(TypeImpl *s, T value) { @@ -763,8 +765,8 @@ class Serialize::ObjectField : public CommonFieldBase if (old != nullptr && *old != nullptr) s->RemoveEdge(*old, this); - OnSet(s, value); this->Set_(s, value); + OnSet(s, old != nullptr ? *old : nullptr, value); if (value != nullptr) s->AddEdge(value, this); diff --git a/include/xline.h b/include/xline.h index 59f151864..0ed39946c 100644 --- a/include/xline.h +++ b/include/xline.h @@ -89,7 +89,7 @@ class XLineType : public Serialize::Type { using Serialize::Field::Field; - void OnSet(XLine *s, const Anope::string &value) override; + void OnSet(XLine *s, Anope::string *old, const Anope::string &value) override; } mask; Serialize::Field by, reason, id; Serialize::Field created, expires; diff --git a/modules/chanserv/main/channeltype.cpp b/modules/chanserv/main/channeltype.cpp index e3de67357..4cbda871e 100644 --- a/modules/chanserv/main/channeltype.cpp +++ b/modules/chanserv/main/channeltype.cpp @@ -53,10 +53,9 @@ ChannelType::ChannelType(Module *me) : Serialize::Type(me) } -void ChannelType::Name::OnSet(ChannelImpl *c, const Anope::string &value) +void ChannelType::Name::OnSet(ChannelImpl *c, Anope::string *old, const Anope::string &value) { ChanServ::registered_channel_map& map = ChanServ::service->GetChannels(); - Anope::string *old = this->Get_(c); if (old != nullptr) map.erase(*old); diff --git a/modules/chanserv/main/channeltype.h b/modules/chanserv/main/channeltype.h index c8515b289..437eba133 100644 --- a/modules/chanserv/main/channeltype.h +++ b/modules/chanserv/main/channeltype.h @@ -27,7 +27,7 @@ class ChannelType : public Serialize::Type { using Serialize::Field::Field; - void OnSet(ChannelImpl *c, const Anope::string &value) override; + void OnSet(ChannelImpl *c, Anope::string *old, const Anope::string &value) override; } name; Serialize::Field desc; Serialize::Field time_registered; diff --git a/modules/nickserv/main/accounttype.cpp b/modules/nickserv/main/accounttype.cpp index 0f4ebbd16..d8d4da108 100644 --- a/modules/nickserv/main/accounttype.cpp +++ b/modules/nickserv/main/accounttype.cpp @@ -47,11 +47,10 @@ AccountType::AccountType(Module *me) : Serialize::Type(me) } -void AccountType::Display::OnSet(AccountImpl *acc, const Anope::string &disp) +void AccountType::Display::OnSet(AccountImpl *acc, Anope::string *old, const Anope::string &disp) { NickServ::nickcore_map& map = NickServ::service->GetAccountMap(); - Anope::string *old = this->Get_(acc); if (old != nullptr) map.erase(*old); diff --git a/modules/nickserv/main/accounttype.h b/modules/nickserv/main/accounttype.h index 1a6ab37cd..9ac91cd2e 100644 --- a/modules/nickserv/main/accounttype.h +++ b/modules/nickserv/main/accounttype.h @@ -27,7 +27,7 @@ class AccountType : public Serialize::Type { using Serialize::Field::Field; - void OnSet(AccountImpl *s, const Anope::string &) override; + void OnSet(AccountImpl *s, Anope::string *, const Anope::string &) override; } display; /* User password in form of hashm:data */ Serialize::Field pass; diff --git a/modules/nickserv/main/nicktype.cpp b/modules/nickserv/main/nicktype.cpp index c732fe74a..4480169a1 100644 --- a/modules/nickserv/main/nicktype.cpp +++ b/modules/nickserv/main/nicktype.cpp @@ -34,11 +34,11 @@ NickType::NickType(Module *me) : Serialize::Type(me) } -void NickType::Nick::OnSet(NickImpl *na, const Anope::string &value) +void NickType::Nick::OnSet(NickImpl *na, Anope::string *old, const Anope::string &value) { /* Remove us from the aliases list */ NickServ::nickalias_map &map = NickServ::service->GetNickMap(); - Anope::string *old = this->Get_(na); + if (old != nullptr) map.erase(*old); diff --git a/modules/nickserv/main/nicktype.h b/modules/nickserv/main/nicktype.h index 187cf463e..b804c2762 100644 --- a/modules/nickserv/main/nicktype.h +++ b/modules/nickserv/main/nicktype.h @@ -26,7 +26,7 @@ class NickType : public Serialize::Type { using Serialize::Field::Field; - void OnSet(NickImpl *s, const Anope::string &value) override; + void OnSet(NickImpl *s, Anope::string *old, const Anope::string &value) override; } nick; Serialize::Field last_quit; Serialize::Field last_realname; diff --git a/src/xline.cpp b/src/xline.cpp index 02afa597e..6e19e3602 100644 --- a/src/xline.cpp +++ b/src/xline.cpp @@ -229,7 +229,7 @@ XLineManager *XLine::GetManager() return ServiceManager::Get()->FindService(GetType()); } -void XLineType::Mask::OnSet(XLine *s, const Anope::string &value) +void XLineType::Mask::OnSet(XLine *s, Anope::string *old, const Anope::string &value) { s->Recache(value); } -- cgit