7 #ifndef __VERSIONED_H__
8 #define __VERSIONED_H__
23 virtual void Release(std::shared_ptr<Segment> release) = 0;
24 virtual void Collapse(std::shared_ptr<Revision> main, std::shared_ptr<Segment> parent) = 0;
25 virtual void Merge(std::shared_ptr<Revision> main, std::shared_ptr<Revision> joinRev, std::shared_ptr<Segment> join) = 0;
52 template <
class T,
typename _Strategy = DefaultMergeStrategy<T>>
96 bool Set(
const T& v,
const std::function<
bool(T&)>& updater =
nullptr);
104 void Release(std::shared_ptr<Segment> release)
override;
112 void Collapse(std::shared_ptr<Revision> main, std::shared_ptr<Segment> parent)
override;
121 void Merge(std::shared_ptr<Revision> main, std::shared_ptr<Revision> joinRev, std::shared_ptr<Segment> join)
override;
131 const T&
Get(std::shared_ptr<Revision> r)
const;
145 bool Set(std::shared_ptr<Revision> r,
const T& value,
const std::function<
bool(T&)>& updater =
nullptr);
151 bool SetMerge(std::shared_ptr<Revision> r, T& value);
156 _Strategy merge_strategy;
162 template <
class T,
typename _Strategy>
167 template <
class T,
typename _Strategy>
173 if (versions.find(s->version) != versions.end()) {
174 for (
auto it = s->written.begin(); it != s->written.end();){
176 it = s->written.erase(it);
185 template <
class T,
typename _Strategy>
190 template <
class T,
typename _Strategy>
192 std::shared_ptr<Segment> s = r->current;
194 while (versions.find(s->version) == versions.end()) {
201 return versions.at(s->version);
204 template <
class T,
typename _Strategy>
209 template <
class T,
typename _Strategy>
211 if (versions.find(r->current->version) == versions.end()) {
212 r->current->written.push_back(
this);
213 versions[r->current->version] = value;
215 return updater(versions[r->current->version]);
219 return updater(versions[r->current->version]);
221 versions[r->current->version] = value;
227 template <
class T,
typename _Strategy>
229 if (versions.find(r->current->version) == versions.end()) {
230 r->current->written.push_back(
this);
231 versions[r->current->version] = value;
233 merge_strategy.merge(versions[r->current->version], value);
238 template <
class T,
typename _Strategy>
240 versions.erase(release->version);
243 template <
class T,
typename _Strategy>
245 if (versions.find(main->current->version) == versions.end()) {
246 Set(main, versions[parent->version]);
251 template <
class T,
typename _Strategy>
253 std::shared_ptr<Segment> s = joinRev->current;
254 while (versions.find(s->version) == versions.end()) {
261 SetMerge(main, versions[join->version]);
void merge(T &dst, T &src)
static thread_local std::shared_ptr< Revision > currentRevision
The current Revision for current thread.
Interface for all Versioned classes.
virtual void Merge(std::shared_ptr< Revision > main, std::shared_ptr< Revision > joinRev, std::shared_ptr< Segment > join)=0
virtual void Release(std::shared_ptr< Segment > release)=0
virtual void Collapse(std::shared_ptr< Revision > main, std::shared_ptr< Segment > parent)=0
Wrapper to make any class Versioned.
void Collapse(std::shared_ptr< Revision > main, std::shared_ptr< Segment > parent) override
Collapse all changes made in Revision into one.
void Release(std::shared_ptr< Segment > release) override
Forget version that was changed in some Segment.
Versioned(const T &val)
Construct a new Versioned object from your object.
~Versioned()
Destroy the Versioned object.
std::map< int, T > versions
Map of all versions of specified object.
const T & Get() const
Get the current value of the object in the current Revision.
void Merge(std::shared_ptr< Revision > main, std::shared_ptr< Revision > joinRev, std::shared_ptr< Segment > join) override
Merge changes from two Revisions.
bool Set(const T &v, const std::function< bool(T &)> &updater=nullptr)
Set new value of the object.
Implementation of the class Revision.
Implementation of the class Segment.