const T& operator*() // only const access, but see "read-only" above
{
if(d_source->getGeneration() != d_generation) {
- d_source->getState(&d_state, & d_generation);
+ d_source->getState(&d_state, &d_generation);
}
return *d_state;
}
private:
std::shared_ptr<T> d_state;
- unsigned int d_generation;
+ unsigned int d_generation{0};
const GlobalStateHolder<T>* d_source;
};
class GlobalStateHolder
{
public:
- GlobalStateHolder(){}
+ GlobalStateHolder() : d_state(std::make_shared<T>())
+ {}
LocalStateHolder<T> getLocal()
{
return LocalStateHolder<T>(this);
std::shared_ptr<T> getCopy() const
{
std::lock_guard<std::mutex> l(d_lock);
- if(!d_state)
- return std::make_shared<T>();
shared_ptr<T> ret = shared_ptr<T>(new T(*d_state));
return d_state;
}
template<typename F>
void modify(F act) {
std::lock_guard<std::mutex> l(d_lock);
- if(!d_state)
- d_state=std::make_shared<T>();
act(*d_state);
++d_generation;
}
private:
mutable std::mutex d_lock;
std::shared_ptr<T> d_state;
- std::atomic<unsigned int> d_generation{0};
+ std::atomic<unsigned int> d_generation{1};
};