zookeeper-cpp
ZooKeeper Client for C++
types.cpp
1 #include "types.hpp"
2 
3 #include <ostream>
4 #include <sstream>
5 #include <utility>
6 
7 namespace zk
8 {
9 
11 // event_type //
13 
14 std::ostream& operator<<(std::ostream& os, const event_type& self)
15 {
16  switch (self)
17  {
18  case event_type::error: return os << "error";
19  case event_type::created: return os << "created";
20  case event_type::erased: return os << "erased";
21  case event_type::changed: return os << "changed";
22  case event_type::child: return os << "child";
23  case event_type::session: return os << "session";
24  case event_type::not_watching: return os << "not_watching";
25  default: return os << "event_type(" << static_cast<int>(self) << ')';
26  };
27 }
28 
29 std::string to_string(const event_type& self)
30 {
31  std::ostringstream os;
32  os << self;
33  return os.str();
34 }
35 
37 // version //
39 
40 std::ostream& operator<<(std::ostream& os, const version& self)
41 {
42  os << "version(";
43  if (self == version::any())
44  os << "any";
45  else if (self == version::invalid())
46  os << "invalid";
47  else
48  os << self.value;
49  return os << ')';
50 }
51 
52 std::string to_string(const version& self)
53 {
54  std::ostringstream os;
55  os << self;
56  return os.str();
57 }
58 
60 // acl_version //
62 
63 std::ostream& operator<<(std::ostream& os, const acl_version& self)
64 {
65  os << "acl_version(";
66  if (self == acl_version::any())
67  os << "any";
68  else if (self == acl_version::invalid())
69  os << "invalid";
70  else
71  os << self.value;
72  return os << ')';
73 }
74 
75 std::string to_string(const acl_version& self)
76 {
77  std::ostringstream os;
78  os << self;
79  return os.str();
80 }
81 
83 // child_version //
85 
86 std::ostream& operator<<(std::ostream& os, const child_version& self)
87 {
88  os << "child_version(";
89  if (self == child_version::any())
90  os << "any";
91  else if (self == child_version::invalid())
92  os << "invalid";
93  else
94  os << self.value;
95  return os << ')';
96 }
97 
98 std::string to_string(const child_version& self)
99 {
100  std::ostringstream os;
101  os << self;
102  return os.str();
103 }
104 
106 // transaction_id //
108 
109 std::ostream& operator<<(std::ostream& os, const transaction_id& self)
110 {
111  return os << "transaction_id(" << self.value << ')';
112 }
113 
114 std::string to_string(const transaction_id& self)
115 {
116  std::ostringstream os;
117  os << self;
118  return os.str();
119 }
120 
122 // stat //
124 
125 std::ostream& operator<<(std::ostream& os, const stat& self)
126 {
127  os << "{data_version=" << self.data_version.value;
128  os << " child_version=" << self.child_version.value;
129  os << " acl_version=" << self.acl_version.value;
130  os << " data_size=" << self.data_size;
131  os << " children_count=" << self.children_count;
132  os << " ephemeral=" << (self.is_ephemeral() ? "true" : "false");
133  return os << '}';
134 }
135 
136 std::string to_string(const stat& self)
137 {
138  std::ostringstream os;
139  os << self;
140  return os.str();
141 }
142 
144 // state //
146 
147 std::ostream& operator<<(std::ostream& os, const state& self)
148 {
149  switch (self)
150  {
151  case state::closed: return os << "closed";
152  case state::connecting: return os << "connecting";
153  case state::associating: return os << "associating";
154  case state::connected: return os << "connected";
155  case state::read_only: return os << "read_only";
156  case state::not_connected: return os << "not_connected";
157  case state::expired_session: return os << "expired_session";
158  case state::authentication_failed: return os << "authentication_failed";
159  default: return os << "state(" << static_cast<int>(self) << ')';
160  }
161 }
162 
163 std::string to_string(const state& self)
164 {
165  std::ostringstream os;
166  os << self;
167  return os.str();
168 }
169 
171 // create_mode //
173 
174 std::ostream& operator<<(std::ostream& os, const create_mode& mode)
175 {
176  if (mode == create_mode::normal)
177  return os << "normal";
178 
179  bool first = true;
180  auto tick = [&] { return std::exchange(first, false) ? "" : "|"; };
181  if (is_set(mode, create_mode::ephemeral)) os << tick() << "ephemeral";
182  if (is_set(mode, create_mode::sequential)) os << tick() << "sequential";
183  if (is_set(mode, create_mode::container)) os << tick() << "container";
184 
185  return os;
186 }
187 
188 std::string to_string(const create_mode& self)
189 {
190  std::ostringstream os;
191  os << self;
192  return os.str();
193 }
194 
195 }
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.
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.
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.
Client is attempting to associate a session.
create_mode
When used in client::set, this value determines how the znode is created on the server.
Definition: types.hpp:252
Authentication has failed – connection requires a new connection instance with different credentials...
Standard behavior of a znode – the opposite of doing any of the options.
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.
Invalid event (this should never be issued).
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.
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.
static constexpr version any()
When specified in an operation, this version specifier will always pass.
Definition: types.hpp:122
static constexpr version invalid()
An invalid version specifier.
Definition: types.hpp:117
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.