X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/20d4dd0571e75d254444acf755a01b48a691c7c2..refs/heads/master:/include/stg/user_property.h?ds=sidebyside diff --git a/include/stg/user_property.h b/include/stg/user_property.h index 7c65f962..3400abd0 100644 --- a/include/stg/user_property.h +++ b/include/stg/user_property.h @@ -4,10 +4,9 @@ #include "stg/user_ips.h" #include "stg/store.h" #include "stg/admin.h" -#include "stg/notifer.h" +#include "stg/subscriptions.h" #include "stg/admin_conf.h" #include "stg/logger.h" -#include "stg/locker.h" #include "stg/settings.h" #include "stg/scriptexecuter.h" #include "stg/common.h" @@ -22,12 +21,11 @@ #include // access -extern volatile time_t stgTime; - namespace STG { //----------------------------------------------------------------------------- -struct UserPropertyBase { +struct UserPropertyBase +{ virtual ~UserPropertyBase() = default; virtual std::string ToString() const = 0; }; @@ -35,7 +33,8 @@ struct UserPropertyBase { using Registry = std::map; //----------------------------------------------------------------------------- template -class UserProperty : public UserPropertyBase { +class UserProperty : public UserPropertyBase +{ public: explicit UserProperty(T& val); @@ -49,11 +48,10 @@ class UserProperty : public UserPropertyBase { operator const T&() const noexcept { return value; } - void AddBeforeNotifier(PropertyNotifierBase* n); - void DelBeforeNotifier(const PropertyNotifierBase* n); - - void AddAfterNotifier(PropertyNotifierBase* n); - void DelAfterNotifier(const PropertyNotifierBase* n); + template + auto beforeChange(F&& f) { return m_beforeCallbacks.add(std::forward(f)); } + template + auto afterChange(F&& f) { return m_afterCallbacks.add(std::forward(f)); } time_t ModificationTime() const noexcept { return modificationTime; } void ModifyTime() noexcept; @@ -62,13 +60,14 @@ class UserProperty : public UserPropertyBase { private: T& value; time_t modificationTime; - std::set*> beforeNotifiers; - std::set*> afterNotifiers; + Subscriptions m_beforeCallbacks; + Subscriptions m_afterCallbacks; std::mutex mutex; }; //----------------------------------------------------------------------------- template -class UserPropertyLogged: public UserProperty { +class UserPropertyLogged: public UserProperty +{ public: UserPropertyLogged(T& val, const std::string& n, @@ -112,7 +111,8 @@ class UserPropertyLogged: public UserProperty { const Settings& settings; }; //----------------------------------------------------------------------------- -class UserProperties { +class UserProperties +{ /* В этом месте важен порядок следования приватной и открытой частей. Это связано с тем, что часть которая находится в публичной секции @@ -186,9 +186,7 @@ template inline UserProperty::UserProperty(T& val) : value(val), - modificationTime(stgTime), - beforeNotifiers(), - afterNotifiers() + modificationTime(time(NULL)) { } //----------------------------------------------------------------------------- @@ -196,27 +194,23 @@ template inline void UserProperty::ModifyTime() noexcept { - modificationTime = stgTime; + modificationTime = time(NULL); } //----------------------------------------------------------------------------- template inline -void UserProperty::Set(const T& rvalue) +void UserProperty::Set(const T& rhs) { std::lock_guard lock(mutex); T oldVal = value; - auto ni = beforeNotifiers.begin(); - while (ni != beforeNotifiers.end()) - (*ni++)->Notify(oldVal, rvalue); + m_beforeCallbacks.notify(oldVal, rhs); - value = rvalue; - modificationTime = stgTime; + value = rhs; + modificationTime = time(NULL); - ni = afterNotifiers.begin(); - while (ni != afterNotifiers.end()) - (*ni++)->Notify(oldVal, rvalue); + m_afterCallbacks.notify(oldVal, rhs); } //----------------------------------------------------------------------------- template @@ -227,38 +221,6 @@ UserProperty& UserProperty::operator=(const T& newValue) return *this; } //----------------------------------------------------------------------------- -template -inline -void UserProperty::AddBeforeNotifier(PropertyNotifierBase* n) -{ - std::lock_guard lock(mutex); - beforeNotifiers.insert(n); -} -//----------------------------------------------------------------------------- -template -inline -void UserProperty::DelBeforeNotifier(const PropertyNotifierBase* n) -{ - std::lock_guard lock(mutex); - beforeNotifiers.erase(const_cast*>(n)); -} -//----------------------------------------------------------------------------- -template -inline -void UserProperty::AddAfterNotifier(PropertyNotifierBase* n) -{ - std::lock_guard lock(mutex); - afterNotifiers.insert(n); -} -//----------------------------------------------------------------------------- -template -inline -void UserProperty::DelAfterNotifier(const PropertyNotifierBase* n) -{ - std::lock_guard lock(mutex); - afterNotifiers.erase(const_cast*>(n)); -} -//----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- template