zookeeper-cpp
ZooKeeper Client for C++
types.hpp
1 #pragma once
2 
3 #include <zk/config.hpp>
4 
5 #include <chrono>
6 #include <iosfwd>
7 #include <string>
8 
9 namespace zk
10 {
11 
17 template <typename TReal, typename TId>
18 struct strong_id
19 {
20  using value_type = TId;
21 
22  value_type value;
23 
24  strong_id() noexcept = default;
25 
26  constexpr explicit strong_id(value_type val) noexcept :
27  value(val)
28  { }
29 
30  constexpr value_type get() const noexcept { return value; }
31 
32  explicit constexpr operator value_type() noexcept { return value; }
33 
34  TReal& operator++()
35  {
36  ++this->value;
37  return *static_cast<TReal*>(this);
38  }
39 
40  TReal operator++(int)
41  {
42  TReal copy(*this);
43  operator++();
44  return copy;
45  }
46 
47  TReal& operator--()
48  {
49  --this->value;
50  return *static_cast<TReal*>(this);;
51  }
52 
53  TReal operator--(int)
54  {
55  TReal copy(*this);
56  operator--();
57  return copy;
58  }
59 };
60 
61 template <typename TReal, typename TId>
62 constexpr bool operator==(const strong_id<TReal, TId>& a, const strong_id<TReal, TId>& b)
63 {
64  return a.value == b.value;
65 }
66 
67 template <typename TReal, typename TId>
68 constexpr bool operator!=(const strong_id<TReal, TId>& a, const strong_id<TReal, TId>& b)
69 {
70  return a.value != b.value;
71 }
72 
73 template <typename TReal, typename TId>
74 constexpr bool operator<(const strong_id<TReal, TId>& a, const strong_id<TReal, TId>& b)
75 {
76  return a.value < b.value;
77 }
78 
79 template <typename TReal, typename TId>
80 constexpr bool operator<=(const strong_id<TReal, TId>& a, const strong_id<TReal, TId>& b)
81 {
82  return a.value <= b.value;
83 }
84 
85 template <typename TReal, typename TId>
86 constexpr bool operator>(const strong_id<TReal, TId>& a, const strong_id<TReal, TId>& b)
87 {
88  return a.value > b.value;
89 }
90 
91 template <typename TReal, typename TId>
92 constexpr bool operator>=(const strong_id<TReal, TId>& a, const strong_id<TReal, TId>& b)
93 {
94  return a.value >= b.value;
95 }
96 
97 template <typename TReal, typename TId>
98 inline std::size_t hash(const strong_id<TReal, TId>& x)
99 {
100  return std::hash<TId>()(x.value);
101 }
102 
110 template <typename TReal>
112  public strong_id<TReal, std::int32_t>
113 {
117  static constexpr TReal invalid() { return TReal(-42); };
118 
122  static constexpr TReal any() { return TReal(-1); };
123 
125 };
126 
131 struct version final :
132  public basic_version<version>
133 {
135 };
136 
137 std::ostream& operator<<(std::ostream&, const version&);
138 
139 std::string to_string(const version&);
140 
145 struct acl_version final :
146  public basic_version<acl_version>
147 {
149 };
150 
151 std::ostream& operator<<(std::ostream&, const acl_version&);
152 
153 std::string to_string(const acl_version&);
154 
159 struct child_version final :
160  public basic_version<child_version>
161 {
163 };
164 
165 std::ostream& operator<<(std::ostream&, const child_version&);
166 
167 std::string to_string(const child_version&);
168 
169 struct transaction_id final :
170  public strong_id<transaction_id, std::size_t>
171 {
172  using strong_id::strong_id;
173 };
174 
175 std::ostream& operator<<(std::ostream&, const transaction_id&);
176 
177 std::string to_string(const transaction_id&);
178 
191 struct stat final
192 {
193 public:
194  using time_point = std::chrono::system_clock::time_point;
195 
196 public:
199 
202 
205 
215  time_point create_time;
216 
218  time_point modified_time;
219 
222 
225 
228 
230  std::uint64_t ephemeral_owner;
231 
233  std::size_t data_size;
234 
236  std::size_t children_count;
237 
239  bool is_ephemeral() const
240  {
241  return ephemeral_owner == 0U;
242  }
243 };
244 
245 std::ostream& operator<<(std::ostream&, const stat&);
246 
247 std::string to_string(const stat&);
248 
252 enum class create_mode : unsigned int
253 {
255  normal = 0b0000,
257  ephemeral = 0b0001,
263  sequential = 0b0010,
269  container = 0b0100,
270 };
271 
272 constexpr create_mode operator|(create_mode a, create_mode b)
273 {
274  return create_mode(static_cast<unsigned int>(a) | static_cast<unsigned int>(b));
275 }
276 
277 constexpr create_mode operator&(create_mode a, create_mode b)
278 {
279  return create_mode(static_cast<unsigned int>(a) & static_cast<unsigned int>(b));
280 }
281 
282 constexpr create_mode operator~(create_mode a)
283 {
284  return create_mode(~static_cast<unsigned int>(a));
285 }
286 
288 constexpr bool is_set(create_mode self, create_mode flags)
289 {
290  return (self & flags) == flags;
291 }
292 
293 std::ostream& operator<<(std::ostream&, const create_mode&);
294 
295 std::string to_string(const create_mode&);
296 
298 enum class event_type : int
299 {
300  error = 0,
301  created = 1,
302  erased = 2,
303  changed = 3,
304  child = 4,
307  session = -1,
309  not_watching = -2,
310 };
312 
313 std::ostream& operator<<(std::ostream&, const event_type&);
314 
315 std::string to_string(const event_type&);
316 
320 enum class state : int
321 {
322  closed = 0,
323  connecting = 1,
324  associating = 2,
325  connected = 3,
326  read_only = 5,
329  not_connected = 999,
333  expired_session = -112,
334  authentication_failed = -113,
337 };
339 
340 std::ostream& operator<<(std::ostream&, const state&);
341 
342 std::string to_string(const state&);
343 
346 }
347 
348 namespace std
349 {
350 
351 template <>
352 struct hash<zk::version>
353 {
354  using argument_type = zk::version;
355  using result_type = std::size_t;
356 
357  result_type operator()(const argument_type& x) const
358  {
359  return zk::hash(x);
360  }
361 };
362 
363 template <>
364 struct hash<zk::transaction_id>
365 {
367  using result_type = std::size_t;
368 
369  result_type operator()(const argument_type& x) const
370  {
371  return zk::hash(x);
372  }
373 };
374 
375 }
time_point modified_time
Last time the znode was last modified.
Definition: types.hpp:218
transaction_id child_modified_transaction
The transaction ID that last modified the children of the znode.
Definition: types.hpp:204
std::size_t children_count
The number of children this znode has.
Definition: types.hpp:236
This value is issued as part of an event when the state changes.
The client is connected to a read-only server, that is the server which is not currently connected to...
Issued when a znode at a given path is erased.
Base type for creating strong ID types.
Definition: types.hpp:18
constexpr bool is_set(create_mode self, create_mode flags)
Check that self has flags set.
Definition: types.hpp:288
The client is not connected to any server in the ensemble.
transaction_id modified_transaction
The last transaction that modified the znode.
Definition: types.hpp:201
state
Enumeration of states the client may be at when a watch triggers.
Definition: types.hpp:320
Definition: acl.cpp:8
The client is connecting.
zk::version data_version
The number of changes to the data of the znode.
Definition: types.hpp:221
std::uint64_t ephemeral_owner
The session ID of the owner of this znode, if it is an ephemeral entry.
Definition: types.hpp:230
STL namespace.
Client is attempting to associate a session.
Represents a version of the ACL of a ZNode.
Definition: types.hpp:145
create_mode
When used in client::set, this value determines how the znode is created on the server.
Definition: types.hpp:252
std::size_t data_size
The size of the data field of the znode.
Definition: types.hpp:233
Standard behavior of a znode – the opposite of doing any of the options.
Statistics about a znode, similar to the UNIX stat structure.
Definition: types.hpp:191
bool is_ephemeral() const
Is the znode an ephemeral entry?
Definition: types.hpp:239
The client is in the connected state – it is connected to a server in the ensemble (one of the serve...
The znode will be deleted upon the client&#39;s disconnect.
Issued when a znode at a given path is created.
Issued when the data of a watched znode are altered.
The serving cluster has expired this session.
time_point create_time
Time the znode was created.
Definition: types.hpp:215
Watch has been forcefully removed.
event_type
Enumeration of types of events that may occur.
Definition: types.hpp:298
The name of the znode will be appended with a monotonically increasing number.
transaction_id create_transaction
The transaction ID that created the znode.
Definition: types.hpp:198
Represents a version of the children of a ZNode.
Definition: types.hpp:159
static constexpr TReal any()
When specified in an operation, this version specifier will always pass.
Definition: types.hpp:122
Represents a version of the data.
Definition: types.hpp:131
Base type for version types.
Definition: types.hpp:111
static constexpr TReal invalid()
An invalid version specifier.
Definition: types.hpp:117
zk::child_version child_version
The number of changes to the children of the znode.
Definition: types.hpp:224
Container nodes are special purpose nodes useful for recipes such as leader, lock, etc.
Issued when the children of a watched znode are created or deleted.
zk::acl_version acl_version
The number of changes to the ACL of the znode.
Definition: types.hpp:227