#pragma once #include #include #include #include namespace NBus { template class TGranUp { public: TGranUp(TDuration gran) : Gran(gran) , Next(TInstant::MicroSeconds(0)) { } template void Update(TFunctor functor, TInstant now, bool force = false) { if (force || now > Next) Set(functor(), now); } void Update(const TItem& item, TInstant now, bool force = false) { if (force || now > Next) Set(item, now); } TItem Get() const noexcept { TGuard guard(Lock); return Item; } protected: void Set(const TItem& item, TInstant now) { TGuard guard(Lock); Item = item; Next = now + Gran; } private: const TDuration Gran; TLocker Lock; TItem Item; TInstant Next; }; }