blob: 797547e7e688f98e7d390d9882c3f3cfc5ff2329 [file] [log] [blame]
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SETTINGSD_VERSION_STAMP_H_
#define SETTINGSD_VERSION_STAMP_H_
#include <map>
#include <string>
namespace settingsd {
// Implementation of a vector clock. See also:
// http://en.wikipedia.org/wiki/Vector_clock for an explanation of vector
// clocks.
class VersionStamp {
public:
VersionStamp();
// Sets the entry for the clock with key |name|. |value| must be >= 1.
void Set(const std::string& name, int value);
// If |clocks_| contains a clock entry with key |name|, this method returns
// the corresponding clock value, otherwise it returns 0.
const int& Get(const std::string& name) const;
// Returns true, if there is a causal relationship to the vector clock |rhs|
// and |rhs| is later than this vector clock.
bool IsAfter(const VersionStamp& rhs) const;
// Returns true, if there is a causal relationship to the vector clock |rhs|
// and |rhs| is earlier than this vector clock.
bool IsBefore(const VersionStamp& rhs) const;
// Returns true, if there is no causal relationship between this vector clock
// and |rhs|. The events have thus to be considered concurrent.
bool IsConcurrent(const VersionStamp& rhs) const;
private:
using VersionMap = std::map<std::string, int>;
VersionMap clocks_;
static bool isBefore(const VersionStamp::VersionMap& lhs,
const VersionStamp::VersionMap& rhs);
};
} // namespace settingsd
#endif // SETTINGSD_VERSION_STAMP_H_