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