Hazelcast C++ Client
Hazelcast C++ Client Library
iqueue.h
1 /*
2  * Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include "hazelcast/client/item_listener.h"
19 #include "hazelcast/client/proxy/IQueueImpl.h"
20 #include "hazelcast/client/impl/ItemEventHandler.h"
21 #include "hazelcast/client/protocol/codec/codecs.h"
22 
23 namespace hazelcast {
24  namespace client {
29  class iqueue : public proxy::IQueueImpl {
30  friend class spi::ProxyManager;
31  public:
32  static constexpr const char *SERVICE_NAME = "hz:impl:queueService";
33 
48  boost::future<boost::uuids::uuid> add_item_listener(item_listener &&listener, bool include_value) {
49  std::unique_ptr<impl::item_event_handler<protocol::codec::queue_addlistener_handler>> itemEventHandler(
50  new impl::item_event_handler<protocol::codec::queue_addlistener_handler>(
51  get_name(), get_context().get_client_cluster_service(),
52  get_context().get_serialization_service(),
53  std::move(listener),
54  include_value));
55 
56  return proxy::IQueueImpl::add_item_listener(std::move(itemEventHandler), include_value);
57  }
58 
66  template<typename E>
67  boost::future<bool> offer(const E &element) {
68  return offer(element, std::chrono::milliseconds(0));
69  }
70 
75  template<typename E>
76  boost::future<void> put(const E &element) {
77  return proxy::IQueueImpl::put(to_data(element));
78  }
79 
89  template<typename E>
90  boost::future<bool> offer(const E &element, std::chrono::milliseconds timeout) {
91  return proxy::IQueueImpl::offer(to_data(element), timeout);
92  }
93 
98  template<typename E>
99  boost::future<boost::optional<E>> take() {
100  return to_object<E>(proxy::IQueueImpl::take_data());
101  }
102 
108  template<typename E>
109  boost::future<boost::optional<E>> poll(std::chrono::milliseconds timeout) {
110  return to_object<E>(proxy::IQueueImpl::poll_data(timeout));
111  }
112 
118  template<typename E>
119  boost::future<bool> remove(const E &element) {
120  return proxy::IQueueImpl::remove(to_data(element));
121  }
122 
128  template<typename E>
129  boost::future<bool> contains(const E &element) {
130  return proxy::IQueueImpl::contains(to_data(element));
131  }
132 
139  template<typename E>
140  boost::future<size_t> drain_to(std::vector<E> &elements) {
141  return proxy::IQueueImpl::drain_to_data().then(boost::launch::sync,
142  [&](boost::future<std::vector<serialization::pimpl::data>> f) {
143  return drain_items(std::move(f), elements);
144  });
145  }
146 
154  template<typename E>
155  boost::future<size_t> drain_to(std::vector<E> &elements, size_t max_elements) {
156  return proxy::IQueueImpl::drain_to_data(max_elements).then(boost::launch::sync,
157  [&](boost::future<std::vector<serialization::pimpl::data>> f) {
158  return drain_items(std::move(f),
159  elements);
160  });
161  }
162 
168  template<typename E>
169  boost::future<boost::optional<E>> poll() {
170  return poll<E>(std::chrono::milliseconds(0));
171  }
172 
178  template<typename E>
179  boost::future<boost::optional<E>> peek() {
180  return to_object<E>(proxy::IQueueImpl::peek_data());
181  }
182 
187  template<typename E>
188  boost::future<std::vector<E>> to_array() {
189  return to_object_vector<E>(proxy::IQueueImpl::to_array_data());
190  }
191 
197  template<typename E>
198  boost::future<bool> contains_all(const std::vector<E> &elements) {
199  return proxy::IQueueImpl::contains_all_data(to_data_collection(elements));
200  }
201 
207  template<typename E>
208  boost::future<bool> add_all(const std::vector<E> &elements) {
209  return proxy::IQueueImpl::add_all_data(to_data_collection(elements));
210  }
211 
217  template<typename E>
218  boost::future<bool> remove_all(const std::vector<E> &elements) {
219  return proxy::IQueueImpl::remove_all_data(to_data_collection(elements));
220  }
221 
228  template<typename E>
229  boost::future<bool> retain_all(const std::vector<E> &elements) {
230  return proxy::IQueueImpl::retain_all_data(to_data_collection(elements));
231  }
232 
233  private:
234  iqueue(const std::string &instance_name, spi::ClientContext *context) : proxy::IQueueImpl(instance_name,
235  context) {}
236 
237  template<typename E>
238  size_t drain_items(boost::future<std::vector<serialization::pimpl::data>> f, std::vector<E> &elements) {
239  auto datas = f.get();
240  auto size = datas.size();
241  elements.reserve(size);
242  auto &ss = get_context().get_serialization_service();
243  for (auto &data : datas) {
244  elements.push_back(ss.template to_object<E>(data).value());
245  }
246  return size;
247  }
248  };
249  }
250 }
251 
Concurrent, blocking, distributed, observable, client queue.
Definition: iqueue.h:29
boost::future< boost::optional< E > > poll(std::chrono::milliseconds timeout)
Definition: iqueue.h:109
boost::future< bool > remove(const E &element)
Definition: iqueue.h:119
boost::future< bool > contains(const E &element)
Definition: iqueue.h:129
boost::future< boost::optional< E > > peek()
Returns immediately without waiting.
Definition: iqueue.h:179
boost::future< bool > add_all(const std::vector< E > &elements)
Definition: iqueue.h:208
boost::future< size_t > drain_to(std::vector< E > &elements, size_t max_elements)
Note that elements will be pushed_back to vector.
Definition: iqueue.h:155
boost::future< bool > offer(const E &element, std::chrono::milliseconds timeout)
Inserts the specified element into this queue.
Definition: iqueue.h:90
boost::future< boost::optional< E > > poll()
Returns immediately without waiting.
Definition: iqueue.h:169
boost::future< bool > remove_all(const std::vector< E > &elements)
Definition: iqueue.h:218
boost::future< bool > contains_all(const std::vector< E > &elements)
Definition: iqueue.h:198
boost::future< std::vector< E > > to_array()
Definition: iqueue.h:188
boost::future< void > put(const E &element)
Puts the element into queue.
Definition: iqueue.h:76
boost::future< bool > offer(const E &element)
Inserts the specified element into this queue.
Definition: iqueue.h:67
boost::future< boost::uuids::uuid > add_item_listener(item_listener &&listener, bool include_value)
Adds an item listener for this collection.
Definition: iqueue.h:48
boost::future< bool > retain_all(const std::vector< E > &elements)
Removes the elements from this queue that are not available in given "elements" vector.
Definition: iqueue.h:229
boost::future< size_t > drain_to(std::vector< E > &elements)
Note that elements will be pushed_back to vector.
Definition: iqueue.h:140
boost::future< boost::optional< E > > take()
Definition: iqueue.h:99
Item listener for IQueue, ISet and IList.
Definition: item_listener.h:51