Hazelcast C++ Client
IQueue.h
1 /*
2  * Copyright (c) 2008-2019, 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 #ifndef HAZELCAST_IQUEUE
17 #define HAZELCAST_IQUEUE
18 
19 #include <stdexcept>
20 
21 #include "hazelcast/client/proxy/IQueueImpl.h"
22 #include "hazelcast/client/ItemListener.h"
23 #include "hazelcast/client/impl/ItemEventHandler.h"
24 #include "hazelcast/client/protocol/codec/QueueAddListenerCodec.h"
25 
26 namespace hazelcast {
27  namespace client {
28  namespace adaptor {
29  template <typename E>
30  class RawPointerQueue;
31  }
32 
38  template<typename E>
39  class IQueue : public proxy::IQueueImpl {
40  friend class impl::HazelcastClientInstanceImpl;
41  friend class adaptor::RawPointerQueue<E>;
42 
43  public:
58  std::string addItemListener(ItemListener<E>& listener, bool includeValue) {
59  spi::ClientClusterService &cs = getContext().getClientClusterService();
60  serialization::pimpl::SerializationService& ss = getContext().getSerializationService();
61  impl::ItemEventHandler<E, protocol::codec::QueueAddListenerCodec::AbstractEventHandler> *itemEventHandler =
62  new impl::ItemEventHandler<E, protocol::codec::QueueAddListenerCodec::AbstractEventHandler>(getName(), cs, ss, listener, includeValue);
63  return proxy::IQueueImpl::addItemListener(itemEventHandler, includeValue);
64  }
65 
74  bool removeItemListener(const std::string& registrationId) {
75  return proxy::IQueueImpl::removeItemListener(registrationId);
76  }
77 
86  bool offer(const E& element) {
87  return offer(element, 0);
88  }
89 
94  void put(const E& element) {
95  proxy::IQueueImpl::put(toData(element));
96  }
97 
107  bool offer(const E& element, long timeoutInMillis) {
108  return proxy::IQueueImpl::offer(toData(element), timeoutInMillis);
109  }
110 
115  boost::shared_ptr<E> take() {
116  return poll(-1);
117  }
118 
124  boost::shared_ptr<E> poll(long timeoutInMillis) {
125  return boost::shared_ptr<E>(boost::shared_ptr<E>(toObject<E>(
126  proxy::IQueueImpl::pollData(timeoutInMillis))));
127  }
128 
134  return proxy::IQueueImpl::remainingCapacity();
135  }
136 
142  bool remove(const E& element) {
143  return proxy::IQueueImpl::remove(toData(element));
144  }
145 
151  bool contains(const E& element) {
152  return proxy::IQueueImpl::contains(toData(element));
153  }
154 
161  size_t drainTo(std::vector<E>& elements) {
162  std::vector<serialization::pimpl::Data> coll = proxy::IQueueImpl::drainToData();
163  for (std::vector<serialization::pimpl::Data>::const_iterator it = coll.begin(); it != coll.end(); ++it) {
164  std::auto_ptr<E> e = getContext().getSerializationService().template toObject<E>(*it);
165  elements.push_back(*e);
166  }
167  return coll.size();
168  }
169 
177  size_t drainTo(std::vector<E>& elements, size_t maxElements) {
178  std::vector<serialization::pimpl::Data> coll = proxy::IQueueImpl::drainToData(maxElements);
179  for (std::vector<serialization::pimpl::Data>::const_iterator it = coll.begin(); it != coll.end(); ++it) {
180  std::auto_ptr<E> e = getContext().getSerializationService().template toObject<E>(*it);
181  elements.push_back(*e);
182  }
183  return coll.size();
184  }
185 
191  boost::shared_ptr<E> poll() {
192  return poll(0);
193  }
194 
200  boost::shared_ptr<E> peek() {
201  return boost::shared_ptr<E>(toObject<E>(proxy::IQueueImpl::peekData()));
202  }
203 
208  int size() {
209  return proxy::IQueueImpl::size();
210  }
211 
216  bool isEmpty() {
217  return proxy::IQueueImpl::isEmpty();
218  }
219 
224  std::vector<E> toArray() {
225  return toObjectCollection<E>(proxy::IQueueImpl::toArrayData());
226  }
227 
234  bool containsAll(const std::vector<E>& elements) {
235  std::vector<serialization::pimpl::Data> list = toDataCollection(elements);
236  return proxy::IQueueImpl::containsAll(list);
237  }
238 
245  bool addAll(const std::vector<E>& elements) {
246  std::vector<serialization::pimpl::Data> dataList = toDataCollection(elements);
247  return proxy::IQueueImpl::addAll(dataList);
248  }
249 
256  bool removeAll(const std::vector<E>& elements) {
257  std::vector<serialization::pimpl::Data> dataList = toDataCollection(elements);
258  return proxy::IQueueImpl::removeAll(dataList);
259  }
260 
268  bool retainAll(const std::vector<E>& elements) {
269  std::vector<serialization::pimpl::Data> dataList = toDataCollection(elements);
270  return proxy::IQueueImpl::retainAll(dataList);
271  }
272 
276  void clear() {
277  proxy::IQueueImpl::clear();
278  }
279  private:
280  IQueue(const std::string& instanceName, spi::ClientContext *context)
281  : proxy::IQueueImpl(instanceName, context) {
282  }
283  };
284  }
285 }
286 
287 #endif /* HAZELCAST_IQUEUE */
288 
void clear()
Removes all elements from queue.
Definition: IQueue.h:276
bool offer(const E &element, long timeoutInMillis)
Inserts the specified element into this queue.
Definition: IQueue.h:107
Concurrent, blocking, distributed, observable, client queue.
Definition: IQueue.h:39
bool offer(const E &element)
Inserts the specified element into this queue.
Definition: IQueue.h:86
std::vector< E > toArray()
Definition: IQueue.h:224
Concurrent, blocking, distributed, observable, client queue.
Definition: RawPointerQueue.h:33
size_t drainTo(std::vector< E > &elements, size_t maxElements)
Note that elements will be pushed_back to vector.
Definition: IQueue.h:177
bool contains(const E &element)
Definition: IQueue.h:151
bool removeAll(const std::vector< E > &elements)
Definition: IQueue.h:256
size_t drainTo(std::vector< E > &elements)
Note that elements will be pushed_back to vector.
Definition: IQueue.h:161
int remainingCapacity()
Definition: IQueue.h:133
bool removeItemListener(const std::string &registrationId)
Removes the specified item listener.
Definition: IQueue.h:74
std::string addItemListener(ItemListener< E > &listener, bool includeValue)
Adds an item listener for this collection.
Definition: IQueue.h:58
boost::shared_ptr< E > poll(long timeoutInMillis)
Definition: IQueue.h:124
int size()
Definition: IQueue.h:208
void put(const E &element)
Puts the element into queue.
Definition: IQueue.h:94
bool isEmpty()
Definition: IQueue.h:216
bool retainAll(const std::vector< E > &elements)
Removes the elements from this queue that are not available in given "elements" vector.
Definition: IQueue.h:268
boost::shared_ptr< E > peek()
Returns immediately without waiting.
Definition: IQueue.h:200
Item listener for IQueue, ISet and IList.
Definition: ItemListener.h:41
bool containsAll(const std::vector< E > &elements)
Definition: IQueue.h:234
PN (Positive-Negative) CRDT counter.
Definition: MapEntryView.h:32
bool addAll(const std::vector< E > &elements)
Definition: IQueue.h:245
boost::shared_ptr< E > poll()
Returns immediately without waiting.
Definition: IQueue.h:191
boost::shared_ptr< E > take()
Definition: IQueue.h:115