zookeeper-cpp
ZooKeeper Client for C++
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
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 
14 
27 template <typename TReal, typename TId>
28 struct strong_id
29 {
31  using value_type = TId;
32 
35 
38  strong_id() noexcept = default;
39 
41  constexpr explicit strong_id(value_type value) noexcept :
42  value(value)
43  { }
44 
46  constexpr value_type get() const noexcept { return value; }
47 
51  explicit constexpr operator value_type() noexcept { return value; }
52 
57  TReal& operator++()
58  {
59  ++this->value;
60  return *static_cast<TReal*>(this);
61  }
62 
63  TReal operator++(int)
64  {
65  TReal copy(*this);
66  operator++();
67  return copy;
68  }
70 
74  TReal& operator--()
75  {
76  --this->value;
77  return *static_cast<TReal*>(this);
78  }
79 
80  TReal operator--(int)
81  {
82  TReal copy(*this);
83  operator--();
84  return copy;
85  }
87 };
88 
89 template <typename TReal, typename TId>
90 constexpr bool operator==(const strong_id<TReal, TId>& a, const strong_id<TReal, TId>& b)
91 {
92  return a.value == b.value;
93 }
94 
95 template <typename TReal, typename TId>
96 constexpr bool operator!=(const strong_id<TReal, TId>& a, const strong_id<TReal, TId>& b)
97 {
98  return a.value != b.value;
99 }
100 
101 template <typename TReal, typename TId>
102 constexpr bool operator<(const strong_id<TReal, TId>& a, const strong_id<TReal, TId>& b)
103 {
104  return a.value < b.value;
105 }
106 
107 template <typename TReal, typename TId>
108 constexpr bool operator<=(const strong_id<TReal, TId>& a, const strong_id<TReal, TId>& b)
109 {
110  return a.value <= b.value;
111 }
112 
113 template <typename TReal, typename TId>
114 constexpr bool operator>(const strong_id<TReal, TId>& a, const strong_id<TReal, TId>& b)
115 {
116  return a.value > b.value;
117 }
118 
119 template <typename TReal, typename TId>
120 constexpr bool operator>=(const strong_id<TReal, TId>& a, const strong_id<TReal, TId>& b)
121 {
122  return a.value >= b.value;
123 }
124 
126 template <typename TReal, typename TId>
127 inline std::size_t hash(const strong_id<TReal, TId>& x)
128 {
129  return std::hash<TId>()(x.value);
130 }
131 
139 template <typename TReal>
141  public strong_id<TReal, std::int32_t>
142 {
145  static constexpr TReal invalid() { return TReal(-42); };
146 
150  static constexpr TReal any() { return TReal(-1); };
151 
153 };
154 
158 struct version final :
159  public basic_version<version>
160 {
162 };
163 
164 std::ostream& operator<<(std::ostream&, const version&);
165 
166 std::string to_string(const version&);
167 
171 struct acl_version final :
172  public basic_version<acl_version>
173 {
175 };
176 
177 std::ostream& operator<<(std::ostream&, const acl_version&);
178 
179 std::string to_string(const acl_version&);
180 
184 struct child_version final :
185  public basic_version<child_version>
186 {
188 };
189 
190 std::ostream& operator<<(std::ostream&, const child_version&);
191 
192 std::string to_string(const child_version&);
193 
199 struct transaction_id final :
200  public strong_id<transaction_id, std::size_t>
201 {
202  using strong_id::strong_id;
203 };
204 
205 std::ostream& operator<<(std::ostream&, const transaction_id&);
206 
207 std::string to_string(const transaction_id&);
208 
220 struct stat final
221 {
222 public:
223  using time_point = std::chrono::system_clock::time_point;
224 
225 public:
228 
231 
234 
243  time_point create_time;
244 
246  time_point modified_time;
247 
251 
254 
257 
262  std::uint64_t ephemeral_owner;
263 
265  std::size_t data_size;
266 
268  std::size_t children_count;
269 
271  bool is_ephemeral() const
272  {
273  return ephemeral_owner == 0U;
274  }
275 };
276 
277 std::ostream& operator<<(std::ostream&, const stat&);
278 
279 std::string to_string(const stat&);
280 
283 enum class create_mode : unsigned int
284 {
286  normal = 0b0000,
288  ephemeral = 0b0001,
293  sequential = 0b0010,
298  container = 0b0100,
299 };
300 
303 {
304  return create_mode(static_cast<unsigned int>(a) | static_cast<unsigned int>(b));
305 }
306 
309 {
310  return create_mode(static_cast<unsigned int>(a) & static_cast<unsigned int>(b));
311 }
312 
316 {
317  return create_mode(~static_cast<unsigned int>(a) & 0b0111);
318 }
319 
321 constexpr bool is_set(create_mode self, create_mode flags)
322 {
323  return (self & flags) == flags;
324 }
325 
326 std::ostream& operator<<(std::ostream&, const create_mode&);
327 
328 std::string to_string(const create_mode&);
329 
331 enum class event_type : int
332 {
333  error = 0,
334  created = 1,
335  erased = 2,
336  changed = 3,
337  child = 4,
340  session = -1,
342  not_watching = -2,
343 };
345 
346 std::ostream& operator<<(std::ostream&, const event_type&);
347 
348 std::string to_string(const event_type&);
349 
385 enum class state : int
386 {
387  closed = 0,
388  connecting = 1,
389  connected = 3,
390  read_only = 5,
393  expired_session = -112,
397  authentication_failed = -113,
400 };
402 
403 std::ostream& operator<<(std::ostream&, const state&);
404 
406 std::string to_string(const state& state);
407 
409 
410 }
411 
412 namespace std
413 {
414 
415 template <>
416 struct hash<zk::version>
417 {
418  using argument_type = zk::version;
419  using result_type = std::size_t;
420 
421  result_type operator()(const argument_type& x) const
422  {
423  return zk::hash(x);
424  }
425 };
426 
427 template <>
428 struct hash<zk::acl_version>
429 {
431  using result_type = std::size_t;
432 
433  result_type operator()(const argument_type& x) const
434  {
435  return zk::hash(x);
436  }
437 };
438 
439 template <>
440 struct hash<zk::child_version>
441 {
443  using result_type = std::size_t;
444 
445  result_type operator()(const argument_type& x) const
446  {
447  return zk::hash(x);
448  }
449 };
450 
451 template <>
452 struct hash<zk::transaction_id>
453 {
455  using result_type = std::size_t;
456 
457  result_type operator()(const argument_type& x) const
458  {
459  return zk::hash(x);
460  }
461 };
462 
463 }
time_point modified_time
Last time the entry was last modified. Like create_time, this is not a reliable source.
Definition: types.hpp:246
constexpr create_mode operator&(create_mode a, create_mode b)
Set intersection operation of create_mode.
Definition: types.hpp:308
transaction_id child_modified_transaction
The transaction ID that last modified the children of the entry.
Definition: types.hpp:233
std::size_t children_count
The number of children this entry has.
Definition: types.hpp:268
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 an entry at a given path is erased.
Base type for creating strong ID types.
Definition: types.hpp:28
constexpr bool is_set(create_mode self, create_mode flags)
Check that self has flags set.
Definition: types.hpp:321
transaction_id modified_transaction
The last transaction that modified the entry.
Definition: types.hpp:230
state
Enumeration of states the client may be at when a watch triggers.
Definition: types.hpp:385
The client is connecting.
zk::version data_version
The number of changes to the data of the entry.
Definition: types.hpp:250
std::uint64_t ephemeral_owner
The session ID of the owner of this entry, if it is an ephemeral entry.
Definition: types.hpp:262
strong_id() noexcept=default
Default construct the ID.
The connection is closed.
Definition: error.hpp:291
Represents a version of the ACL of an entry.
Definition: types.hpp:171
Base error type for all errors raised by this library.
Definition: error.hpp:107
constexpr create_mode operator~(create_mode a)
Set inverse operation of create_mode.
Definition: types.hpp:315
create_mode
When used in client::set, this value determines how the entry is created on the server.
Definition: types.hpp:283
std::size_t data_size
The size of the data field of the entry.
Definition: types.hpp:265
Standard behavior of an entry – the opposite of doing any of the options.
Statistics about a ZooKeeper entry, similar to the UNIX stat structure.
Definition: types.hpp:220
std::size_t hash(const strong_id< TReal, TId > &x)
Compute the std::hash of the given x.
Definition: types.hpp:127
constexpr create_mode operator|(create_mode a, create_mode b)
Set union operation of create_mode.
Definition: types.hpp:302
The server rejected the connection due to invalid authentication information.
Definition: error.hpp:195
TReal & operator++()
Definition: types.hpp:57
bool is_ephemeral() const
Is the entry an ephemeral entry?
Definition: types.hpp:271
TReal & operator--()
Definition: types.hpp:74
std::int32_t value_type
The representation type of this ID.
Definition: types.hpp:31
The client is in the connected state – it is connected to a server in the ensemble (one of the serve...
The entry will be deleted when the client session expires.
Issued when an entry for a given path is created.
Issued when the data of a watched entry is altered.
The serving cluster has expired this session.
time_point create_time
Time the entry was created.
Definition: types.hpp:243
Watch has been forcefully removed.
event_type
Enumeration of types of events that may occur.
Definition: types.hpp:331
The name of the entry will be appended with a monotonically increasing number.
transaction_id create_transaction
The transaction ID that created the entry.
Definition: types.hpp:227
Represents a version of the children of an entry.
Definition: types.hpp:184
static constexpr TReal any()
When specified in an operation, this version specifier will always pass.
Definition: types.hpp:150
Represents a version of the data.
Definition: types.hpp:158
value_type value
Underlying value of this ID.
Definition: types.hpp:34
Base type for version types.
Definition: types.hpp:140
static constexpr TReal invalid()
An invalid version specifier.
Definition: types.hpp:145
Represents the ZooKeeper transaction ID in which an event happened to an entry.
Definition: types.hpp:199
zk::child_version child_version
The number of changes to the children of the entry.
Definition: types.hpp:253
Container entries are special purpose entries useful for recipes such as leader, lock, etc.
Issued when the children of a watched entry are created or deleted.
zk::acl_version acl_version
The number of changes to the ACL of the entry.
Definition: types.hpp:256