zookeeper-cpp
ZooKeeper Client for C++
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
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 // These tags are used during print_buffer overload resolution. If TBuffer type is not
15 // applicable (ill-formed) for one of the overloads, then SFINAE selects the appropriate overload.
16 // But there are cases (e.g. when TBuffer is std::string) when both of the print_buffer
17 // are well-formed and therefore the call to print_buffer would be ambiguous. These tags
18 // allows to select one of the overloads by using more derived tag (print_buffer_content_tag).
21 
22 template <typename TBuffer>
23 auto print_buffer(std::ostream& os, const TBuffer& buf, struct print_buffer_content_tag)
24  -> decltype((os << buf), void())
25 {
26  os << buf;
27 }
28 
29 template <typename TBuffer>
30 void print_buffer(std::ostream& os, const TBuffer& buf, struct print_buffer_length_tag)
31 {
32  os << "size=" << buf.size();
33 }
34 
35 template <typename TRange>
36 void print_range(std::ostream& os, const TRange& range)
37 {
38  os << '[';
39  bool first = true;
40  for (const auto& x : range)
41  {
42  if (first)
43  first = false;
44  else
45  os << ", ";
46  os << x;
47  }
48  os << ']';
49 }
50 
51 template <typename T>
52 std::string to_string_generic(const T& x)
53 {
54  std::ostringstream os;
55  os << x;
56  return os.str();
57 }
58 
60 // get_result //
62 
63 get_result::get_result(buffer data, const zk::stat& stat) noexcept :
64  _data(std::move(data)),
65  _stat(stat)
66 { }
67 
68 get_result::~get_result() noexcept
69 { }
70 
71 std::ostream& operator<<(std::ostream& os, const get_result& self)
72 {
73  os << "get_result{";
74  print_buffer(os, self.data(), print_buffer_content_tag {});
75  os << ' ' << self.stat();
76  return os << '}';
77 }
78 
79 std::string to_string(const get_result& self)
80 {
81  return to_string_generic(self);
82 }
83 
85 // get_children_result //
87 
88 get_children_result::get_children_result(children_list_type children, const stat& parent_stat) noexcept :
89  _children(std::move(children)),
90  _parent_stat(parent_stat)
91 { }
92 
93 get_children_result::~get_children_result() noexcept
94 { }
95 
96 std::ostream& operator<<(std::ostream& os, const get_children_result& self)
97 {
98  os << "get_children_result{";
99  print_range(os, self.children());
100  os << " parent=" << self.parent_stat();
101  return os << '}';
102 }
103 
104 std::string to_string(const get_children_result& self)
105 {
106  return to_string_generic(self);
107 }
108 
110 // exists_result //
112 
113 exists_result::exists_result(const optional<zk::stat>& stat) noexcept :
114  _stat(stat)
115 { }
116 
117 exists_result::~exists_result() noexcept
118 { }
119 
120 std::ostream& operator<<(std::ostream& os, const exists_result& self)
121 {
122  os << "exists_result{";
123  if (self)
124  os << *self.stat();
125  else
126  os << "(no)";
127  return os << '}';
128 }
129 
130 std::string to_string(const exists_result& self)
131 {
132  return to_string_generic(self);
133 }
134 
136 // create_result //
138 
139 create_result::create_result(std::string name) noexcept :
140  _name(std::move(name))
141 { }
142 
143 create_result::~create_result() noexcept
144 { }
145 
146 std::ostream& operator<<(std::ostream& os, const create_result& self)
147 {
148  return os << "create_result{name=" << self.name() << '}';
149 }
150 
151 std::string to_string(const create_result& self)
152 {
153  return to_string_generic(self);
154 }
155 
157 // set_result //
159 
160 set_result::set_result(const zk::stat& stat) noexcept :
161  _stat(stat)
162 { }
163 
164 set_result::~set_result() noexcept
165 { }
166 
167 std::ostream& operator<<(std::ostream& os, const set_result& self)
168 {
169  return os << "set_result{" << self.stat() << '}';
170 }
171 
172 std::string to_string(const set_result& self)
173 {
174  return to_string_generic(self);
175 }
176 
178 // get_acl_result //
180 
181 get_acl_result::get_acl_result(zk::acl acl, const zk::stat& stat) noexcept :
182  _acl(std::move(acl)),
183  _stat(stat)
184 { }
185 
186 get_acl_result::~get_acl_result() noexcept
187 { }
188 
189 std::ostream& operator<<(std::ostream& os, const get_acl_result& self)
190 {
191  return os << "get_acl_result{" << self.acl() << ' ' << self.stat() << '}';
192 }
193 
194 std::string to_string(const get_acl_result& self)
195 {
196  return to_string_generic(self);
197 }
198 
200 // event //
202 
203 event::event(event_type type, zk::state state) noexcept :
204  _type(type),
205  _state(state)
206 { }
207 
208 std::ostream& operator<<(std::ostream& os, const event& self)
209 {
210  return os << "event{" << self.type() << " | " << self.state() << '}';
211 }
212 
213 std::string to_string(const event& self)
214 {
215  return to_string_generic(self);
216 }
217 
219 // watch_result //
221 
222 watch_result::watch_result(get_result initial, future<event> next) noexcept :
223  _initial(std::move(initial)),
224  _next(std::move(next))
225 { }
226 
227 watch_result::~watch_result() noexcept
228 { }
229 
230 std::ostream& operator<<(std::ostream& os, const watch_result& self)
231 {
232  return os << "watch_result{initial=" << self.initial() << '}';
233 }
234 
235 std::string to_string(const watch_result& self)
236 {
237  return to_string_generic(self);
238 }
239 
241 // watch_children_result //
243 
244 watch_children_result::watch_children_result(get_children_result initial, future<event> next) noexcept :
245  _initial(std::move(initial)),
246  _next(std::move(next))
247 { }
248 
249 watch_children_result::~watch_children_result() noexcept
250 { }
251 
252 std::ostream& operator<<(std::ostream& os, const watch_children_result& self)
253 {
254  return os << "watch_children_result{initial=" << self.initial() << '}';
255 }
256 
257 std::string to_string(const watch_children_result& self)
258 {
259  return to_string_generic(self);
260 }
261 
263 // watch_exists_result //
265 
266 watch_exists_result::watch_exists_result(exists_result initial, future<event> next) noexcept :
267  _initial(std::move(initial)),
268  _next(std::move(next))
269 { }
270 
271 watch_exists_result::~watch_exists_result() noexcept
272 { }
273 
274 std::ostream& operator<<(std::ostream& os, const watch_exists_result& self)
275 {
276  return os << "watch_exists_result{initial=" << self.initial() << '}';
277 }
278 
279 std::string to_string(const watch_exists_result& self)
280 {
281  return to_string_generic(self);
282 }
283 
284 }
Describes the various result types of client operations.
state
Enumeration of states the client may be at when a watch triggers.
Definition: types.hpp:385
Statistics about a ZooKeeper entry, similar to the UNIX stat structure.
Definition: types.hpp:220
event_type
Enumeration of types of events that may occur.
Definition: types.hpp:331
An access control list is a wrapper around acl_rule instances.
Definition: acl.hpp:139
ZKPP_BUFFER_TYPE buffer
The buffer type.
Definition: buffer.hpp:79