zookeeper-cpp
ZooKeeper Client for C++
results.cpp
1 #include "results.hpp"
2 
3 #include <ostream>
4 #include <sstream>
5 #include <utility>
6 
7 namespace zk
8 {
9 
11 // Utilities //
13 
14 template <typename TBuffer>
15 auto print_buffer(std::ostream& os, const TBuffer& buf)
16  -> decltype((os << buf), void())
17 {
18  os << buf;
19 }
20 
21 template <typename TBuffer>
22 void print_buffer(std::ostream& os, const TBuffer& buf, ...)
23 {
24  os << "size=" << buf.size();
25 }
26 
27 template <typename TRange>
28 void print_range(std::ostream& os, const TRange& range)
29 {
30  os << '[';
31  bool first = true;
32  for (const auto& x : range)
33  {
34  if (first)
35  first = false;
36  else
37  os << ", ";
38  os << x;
39  }
40  os << ']';
41 }
42 
43 template <typename T>
44 std::string to_string_generic(const T& x)
45 {
46  std::ostringstream os;
47  os << x;
48  return os.str();
49 }
50 
52 // get_result //
54 
55 get_result::get_result(buffer data, const zk::stat& stat) noexcept :
56  _data(std::move(data)),
57  _stat(stat)
58 { }
59 
60 get_result::~get_result() noexcept
61 { }
62 
63 std::ostream& operator<<(std::ostream& os, const get_result& self)
64 {
65  os << "get_result{";
66  print_buffer(os, self.data());
67  os << ' ' << self.stat();
68  return os << '}';
69 }
70 
71 std::string to_string(const get_result& self)
72 {
73  return to_string_generic(self);
74 }
75 
77 // get_children_result //
79 
80 get_children_result::get_children_result(children_list_type children, const stat& parent_stat) noexcept :
81  _children(std::move(children)),
82  _parent_stat(parent_stat)
83 { }
84 
85 get_children_result::~get_children_result() noexcept
86 { }
87 
88 std::ostream& operator<<(std::ostream& os, const get_children_result& self)
89 {
90  os << "get_children_result{";
91  print_range(os, self.children());
92  os << " parent=" << self.parent_stat();
93  return os << '}';
94 }
95 
96 std::string to_string(const get_children_result& self)
97 {
98  return to_string_generic(self);
99 }
100 
102 // exists_result //
104 
105 exists_result::exists_result(const optional<zk::stat>& stat) noexcept :
106  _stat(stat)
107 { }
108 
109 exists_result::~exists_result() noexcept
110 { }
111 
112 std::ostream& operator<<(std::ostream& os, const exists_result& self)
113 {
114  os << "exists_result{";
115  if (self)
116  os << *self.stat();
117  else
118  os << "(no)";
119  return os << '}';
120 }
121 
122 std::string to_string(const exists_result& self)
123 {
124  return to_string_generic(self);
125 }
126 
128 // create_result //
130 
131 create_result::create_result(std::string name) noexcept :
132  _name(std::move(name))
133 { }
134 
135 create_result::~create_result() noexcept
136 { }
137 
138 std::ostream& operator<<(std::ostream& os, const create_result& self)
139 {
140  return os << "create_result{name=" << self.name() << '}';
141 }
142 
143 std::string to_string(const create_result& self)
144 {
145  return to_string_generic(self);
146 }
147 
149 // set_result //
151 
152 set_result::set_result(const zk::stat& stat) noexcept :
153  _stat(stat)
154 { }
155 
156 set_result::~set_result() noexcept
157 { }
158 
159 std::ostream& operator<<(std::ostream& os, const set_result& self)
160 {
161  return os << "set_result{" << self.stat() << '}';
162 }
163 
164 std::string to_string(const set_result& self)
165 {
166  return to_string_generic(self);
167 }
168 
170 // get_acl_result //
172 
173 get_acl_result::get_acl_result(zk::acl acl, const zk::stat& stat) noexcept :
174  _acl(std::move(acl)),
175  _stat(stat)
176 { }
177 
178 get_acl_result::~get_acl_result() noexcept
179 { }
180 
181 std::ostream& operator<<(std::ostream& os, const get_acl_result& self)
182 {
183  return os << "get_acl_result{" << self.acl() << ' ' << self.stat() << '}';
184 }
185 
186 std::string to_string(const get_acl_result& self)
187 {
188  return to_string_generic(self);
189 }
190 
192 // event //
194 
195 event::event(event_type type, zk::state state) noexcept :
196  _type(type),
197  _state(state)
198 { }
199 
200 std::ostream& operator<<(std::ostream& os, const event& self)
201 {
202  return os << "event{" << self.type() << " | " << self.state() << '}';
203 }
204 
205 std::string to_string(const event& self)
206 {
207  return to_string_generic(self);
208 }
209 
211 // watch_result //
213 
214 watch_result::watch_result(get_result initial, future<event> next) noexcept :
215  _initial(std::move(initial)),
216  _next(std::move(next))
217 { }
218 
219 watch_result::~watch_result() noexcept
220 { }
221 
222 std::ostream& operator<<(std::ostream& os, const watch_result& self)
223 {
224  return os << "watch_result{initial=" << self.initial() << '}';
225 }
226 
227 std::string to_string(const watch_result& self)
228 {
229  return to_string_generic(self);
230 }
231 
233 // watch_children_result //
235 
236 watch_children_result::watch_children_result(get_children_result initial, future<event> next) noexcept :
237  _initial(std::move(initial)),
238  _next(std::move(next))
239 { }
240 
241 watch_children_result::~watch_children_result() noexcept
242 { }
243 
244 std::ostream& operator<<(std::ostream& os, const watch_children_result& self)
245 {
246  return os << "watch_children_result{initial=" << self.initial() << '}';
247 }
248 
249 std::string to_string(const watch_children_result& self)
250 {
251  return to_string_generic(self);
252 }
253 
255 // watch_exists_result //
257 
258 watch_exists_result::watch_exists_result(exists_result initial, future<event> next) noexcept :
259  _initial(std::move(initial)),
260  _next(std::move(next))
261 { }
262 
263 watch_exists_result::~watch_exists_result() noexcept
264 { }
265 
266 std::ostream& operator<<(std::ostream& os, const watch_exists_result& self)
267 {
268  return os << "watch_exists_result{initial=" << self.initial() << '}';
269 }
270 
271 std::string to_string(const watch_exists_result& self)
272 {
273  return to_string_generic(self);
274 }
275 
276 }
Describes the various result types of client operations.
state
Enumeration of states the client may be at when a watch triggers.
Definition: types.hpp:320
Definition: acl.cpp:8
Statistics about a znode, similar to the UNIX stat structure.
Definition: types.hpp:191
event_type
Enumeration of types of events that may occur.
Definition: types.hpp:298
An access control list is a wrapper around acl_rule instances.
Definition: acl.hpp:124
ZKPP_BUFFER_TYPE buffer
The buffer type.
Definition: buffer.hpp:67