zookeeper-cpp
ZooKeeper Client for C++
multi.hpp
1 #pragma once
2 
3 #include <zk/config.hpp>
4 
5 #include <initializer_list>
6 #include <iosfwd>
7 #include <string>
8 #include <vector>
9 
10 #include "acl.hpp"
11 #include "buffer.hpp"
12 #include "forwards.hpp"
13 #include "results.hpp"
14 #include "types.hpp"
15 
16 namespace zk
17 {
18 
23 enum class op_type : int
24 {
25  check,
26  create,
27  erase,
28  set,
29 };
30 
31 std::ostream& operator<<(std::ostream&, const op_type&);
32 
33 std::string to_string(const op_type&);
34 
35 class op final
36 {
37 public:
38  struct check_data
39  {
40  std::string path;
41  version check;
42 
43  explicit check_data(std::string path, version check);
44  };
45 
47  static op check(std::string path, version check = version::any());
48 
49  struct create_data
50  {
51  std::string path;
52  buffer data;
53  acl rules;
54  create_mode mode;
55 
56  explicit create_data(std::string path, buffer data, acl rules, create_mode mode);
57  };
58 
60  static op create(std::string path, buffer data, acl rules, create_mode mode = create_mode::normal);
61  static op create(std::string path, buffer data, create_mode mode = create_mode::normal);
62 
63  struct erase_data
64  {
65  std::string path;
66  version check;
67 
68  explicit erase_data(std::string path, version check);
69  };
70 
72  static op erase(std::string path, version check = version::any());
73 
74  struct set_data
75  {
76  std::string path;
77  buffer data;
78  version check;
79 
80  explicit set_data(std::string path, buffer data, version check);
81  };
82 
84  static op set(std::string path, buffer data, version check = version::any());
85 
86 public:
87  op(const op&);
88  op(op&&) noexcept;
89 
90  op& operator=(const op&) = delete;
91  op& operator=(op&&) = delete;
92 
93  ~op() noexcept;
94 
95  op_type type() const { return _type; }
96 
97  const check_data& as_check() const;
98 
99  const create_data& as_create() const;
100 
101  const erase_data& as_erase() const;
102 
103  const set_data& as_set() const;
104 
105 private:
106  union any_data
107  {
108  check_data check;
109  create_data create;
110  erase_data erase;
111  set_data set;
112 
113  any_data(std::nullptr_t) noexcept;
114  any_data(check_data&&) noexcept;
115  any_data(create_data&&) noexcept;
116  any_data(erase_data&&) noexcept;
117  any_data(set_data&&) noexcept;
118 
119  ~any_data() noexcept;
120  };
121 
122  explicit op(check_data&&) noexcept;
123  explicit op(create_data&&) noexcept;
124  explicit op(erase_data&&) noexcept;
125  explicit op(set_data&&) noexcept;
126 
127 private:
128  op_type _type;
129  any_data _storage;
130 };
131 
132 std::ostream& operator<<(std::ostream&, const op&);
133 
134 std::string to_string(const op&);
135 
136 class multi_op final
137 {
138 public:
139  using iterator = std::vector<op>::iterator;
140  using const_iterator = std::vector<op>::const_iterator;
141  using size_type = std::vector<op>::size_type;
142 
143 public:
144  multi_op() noexcept
145  { }
146 
147  multi_op(std::vector<op> ops) noexcept;
148 
149  multi_op(std::initializer_list<op> ops) :
150  multi_op(std::vector<op>(ops))
151  { }
152 
153  ~multi_op() noexcept;
154 
155  size_type size() const { return _ops.size(); }
156 
157  op& operator[](size_type idx) { return _ops[idx]; }
158  const op& operator[](size_type idx) const { return _ops[idx]; }
159 
160  iterator begin() { return _ops.begin(); }
161  const_iterator begin() const { return _ops.begin(); }
162  const_iterator cbegin() const { return _ops.begin(); }
163 
164  iterator end() { return _ops.end(); }
165  const_iterator end() const { return _ops.end(); }
166  const_iterator cend() const { return _ops.end(); }
167 
168  void reserve(size_type capacity) { _ops.reserve(capacity); }
169 
170  template <typename... TArgs>
171  void emplace_back(TArgs&&... args)
172  {
173  _ops.emplace_back(std::forward<TArgs>(args)...);
174  }
175 
176  void push_back(op&& x)
177  {
178  emplace_back(std::move(x));
179  }
180 
181  void push_back(const op& x)
182  {
183  emplace_back(x);
184  }
185 
186 private:
187  std::vector<op> _ops;
188 };
189 
190 std::ostream& operator<<(std::ostream&, const multi_op&);
191 
192 std::string to_string(const multi_op&);
193 
194 class multi_result final
195 {
196 private:
197  union any_result
198  {
200  set_result set;
201 
202  any_result(std::nullptr_t) noexcept;
203  any_result(create_result&&) noexcept;
204  any_result(set_result&&) noexcept;
205 
206  ~any_result() noexcept;
207  };
208 
209 public:
211  class part final
212  {
213  public:
214  explicit part(op_type, std::nullptr_t) noexcept;
215  explicit part(create_result) noexcept;
216  explicit part(set_result) noexcept;
217 
218  part(const part&);
219  part(part&&) noexcept;
220 
221  part& operator=(const part&) = delete;
222  part& operator=(part&&) = delete;
223 
224  ~part() noexcept;
225 
227  op_type type() const { return _type; }
228 
229  const create_result& as_create() const;
230 
231  const set_result& as_set() const;
232 
233  private:
234  op_type _type;
235  any_result _storage;
236  };
237 
238  using iterator = std::vector<part>::iterator;
239  using const_iterator = std::vector<part>::const_iterator;
240  using size_type = std::vector<part>::size_type;
241 
242 public:
243  multi_result() noexcept
244  { }
245 
246  multi_result(std::vector<part> parts) noexcept;
247 
248  ~multi_result() noexcept;
249 
250  size_type size() const { return _parts.size(); }
251 
252  part& operator[](size_type idx) { return _parts[idx]; }
253  const part& operator[](size_type idx) const { return _parts[idx]; }
254 
255  iterator begin() { return _parts.begin(); }
256  const_iterator begin() const { return _parts.begin(); }
257  const_iterator cbegin() const { return _parts.begin(); }
258 
259  iterator end() { return _parts.end(); }
260  const_iterator end() const { return _parts.end(); }
261  const_iterator cend() const { return _parts.end(); }
262 
263  void reserve(size_type capacity) { _parts.reserve(capacity); }
264 
265  template <typename... TArgs>
266  void emplace_back(TArgs&&... args)
267  {
268  _parts.emplace_back(std::forward<TArgs>(args)...);
269  }
270 
271  void push_back(part&& x)
272  {
273  emplace_back(std::move(x));
274  }
275 
276  void push_back(const part& x)
277  {
278  emplace_back(x);
279  }
280 
281 private:
282  std::vector<part> _parts;
283 };
284 
285 std::ostream& operator<<(std::ostream&, const multi_result::part&);
286 std::ostream& operator<<(std::ostream&, const multi_result&);
287 
288 std::string to_string(const multi_result::part&);
289 std::string to_string(const multi_result&);
290 
293 }
Describes the various result types of client operations.
Definition: acl.cpp:8
Definition: multi.hpp:35
The result type of client::create.
Definition: results.hpp:101
create_mode
When used in client::set, this value determines how the znode is created on the server.
Definition: types.hpp:252
Standard behavior of a znode – the opposite of doing any of the options.
You can create a child node.
The result type of client::set.
Definition: results.hpp:123
An access control list is a wrapper around acl_rule instances.
Definition: acl.hpp:124
Controls the buffer type.
op_type type() const
The op_type of the op that caused this result.
Definition: multi.hpp:227
You can erase a child node (but not necessarily this one).
static constexpr version any()
When specified in an operation, this version specifier will always pass.
Definition: types.hpp:122
A part of a result.
Definition: multi.hpp:211
Represents a version of the data.
Definition: types.hpp:131
ZKPP_BUFFER_TYPE buffer
The buffer type.
Definition: buffer.hpp:67