Helper to define synchronized, lazy constructed, module initialization (intended to work with DataExchange::OptionFile) More...
#include <ModuleGetterSetter.h>
Public Member Functions | |
nonvirtual T | Get () const |
nonvirtual void | Set (const T &v) |
shared_ptr< const T > | operator-> () const |
nonvirtual optional< T > | Update (const function< optional< T >(const T &)> &updaterFunction) |
Call this with a lambda that will update the associated value (INSIDE a lock (synchronized)) | |
Helper to define synchronized, lazy constructed, module initialization (intended to work with DataExchange::OptionFile)
Features: o Simple API - get/set o automatically intrinsically threadsafe o Init underling object on first access, so easy to declare globally (static init) and less worry about running before main o IMPL need not worry about thread safety. Just init on CTOR, and implement Get/Set methods.
Why use this instead of std c++ static inline object initialization? o This provides internal synchronization for reads/writes (std c++ object initialization only guarantees threadsafe initialization across modules) o Lazy construction - so underlying object construction (intended for the case where this is costly, like read/parse files)
Definition at line 97 of file ModuleGetterSetter.h.
T Stroika::Foundation::Execution::ModuleGetterSetter< T, IMPL >::Get | ( | ) | const |
Grab the global value, performing any necessary read-locks automatically.
Definition at line 13 of file ModuleGetterSetter.inl.
void Stroika::Foundation::Execution::ModuleGetterSetter< T, IMPL >::Set | ( | const T & | v | ) |
Set the global value, performing any necessary write-locks automatically.
Definition at line 29 of file ModuleGetterSetter.inl.
shared_ptr< const T > Stroika::Foundation::Execution::ModuleGetterSetter< T, IMPL >::operator-> | ( | ) | const |
Experimental API as of 2024-02-22 - if works well - store shared_ptr internally - now SharedByValue - and then can return its shared_ptr - safely - always readonly? Anyhow - COULD be done so returned shared_ptr not constructed all the time... But still lock safety stuff...
Definition at line 123 of file ModuleGetterSetter.h.
optional< T > Stroika::Foundation::Execution::ModuleGetterSetter< T, IMPL >::Update | ( | const function< optional< T >(const T &)> & | updaterFunction | ) |
Call this with a lambda that will update the associated value (INSIDE a lock (synchronized))
Call this with a lambda that will update the associated value. The update will happen INSIDE a lock (synchronized). {{ reconsider if we should make that explicit promise or upgradelock so we can do update and check for change inside a shared_lock and never write lock if no change}}
updaterFunction should return nullopt if no change, or the new value if changed.
Update () assures atomic update of your global data, and returns copy of the new value set (optional - this can be ignored). But since it returns an optional<> you can test the result to see if any update was made, and trigger a wakeup or further processing (without the global lock held).
Definition at line 38 of file ModuleGetterSetter.inl.