-
Notifications
You must be signed in to change notification settings - Fork 2
/
Object.h
54 lines (43 loc) · 1.19 KB
/
Object.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
#include <cassert>
#include "to_string.h" // Remove on c++11
#include "types.h"
namespace dawn
{
class Object
{
public:
Object() : m_id(to_string(this)), m_etag(now()) { }
Object(std::string id) : m_id(id), m_etag(now()) { }
virtual bool isChanged(etag_t *etag, bool recursive) const {
assert(etag);
if (*etag < m_etag) {
*etag = m_etag;
return true;
}
return false;
}
etag_t lastChange(bool recursive) const {
etag_t etag = 0;
isChanged(&etag, recursive);
return etag;
}
std::string id() const { return m_id; }
protected:
void setChanged(etag_t etag) {
m_etag = etag;
}
void setChanged(bool changed = true) {
if (changed) {
setChanged(now());
}
}
private:
// TODO Move elsewhere?
etag_t now() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}
std::string m_id;
etag_t m_etag;
};
}