Hazelcast C++ Client
 All Classes Functions Variables Enumerations Enumerator Pages
IQueue.h
1 /*
2  * Copyright (c) 2008-2017, 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 "hazelcast/client/proxy/IQueueImpl.h"
20 #include "hazelcast/client/ItemListener.h"
21 #include "hazelcast/client/impl/ItemEventHandler.h"
22 #include "hazelcast/client/protocol/codec/QueueAddListenerCodec.h"
23 #include <stdexcept>
24 
25 namespace hazelcast {
26  namespace client {
27  namespace adaptor {
28  template <typename E>
29  class RawPointerQueue;
30  }
31 
37  template<typename E>
38  class IQueue : public proxy::IQueueImpl {
39  friend class HazelcastClient;
40  friend class adaptor::RawPointerQueue<E>;
41 
42  public:
57  std::string addItemListener(ItemListener<E>& listener, bool includeValue) {
58  spi::ClusterService& cs = context->getClusterService();
59  serialization::pimpl::SerializationService& ss = context->getSerializationService();
60  impl::ItemEventHandler<E, protocol::codec::QueueAddListenerCodec::AbstractEventHandler> *itemEventHandler =
61  new impl::ItemEventHandler<E, protocol::codec::QueueAddListenerCodec::AbstractEventHandler>(getName(), cs, ss, listener, includeValue);
62  return proxy::IQueueImpl::addItemListener(itemEventHandler, includeValue);
63  }
64 
73  bool removeItemListener(const std::string& registrationId) {
74  return proxy::IQueueImpl::removeItemListener(registrationId);
75  }
76 
85  bool offer(const E& element) {
86  return offer(element, 0);
87  }
88 
93  void put(const E& element) {
94  proxy::IQueueImpl::put(toData(element));
95  }
96 
106  bool offer(const E& element, long timeoutInMillis) {
107  return proxy::IQueueImpl::offer(toData(element), timeoutInMillis);
108  }
109 
114  boost::shared_ptr<E> take() {
115  return poll(-1);
116  }
117 
123  boost::shared_ptr<E> poll(long timeoutInMillis) {
124  return boost::shared_ptr<E>(boost::shared_ptr<E>(toObject<E>(
125  proxy::IQueueImpl::pollData(timeoutInMillis))));
126  }
127 
133  return proxy::IQueueImpl::remainingCapacity();
134  }
135 
141  bool remove(const E& element) {
142  return proxy::IQueueImpl::remove(toData(element));
143  }
144 
150  bool contains(const E& element) {
151  return proxy::IQueueImpl::contains(toData(element));
152  }
153 
160  size_t drainTo(std::vector<E>& elements) {
161  return drainTo(elements, -1);
162  }
163 
171  size_t drainTo(std::vector<E>& elements, size_t maxElements) {
172  std::vector<serialization::pimpl::Data> coll = proxy::IQueueImpl::drainToData(maxElements);
173  for (std::vector<serialization::pimpl::Data>::const_iterator it = coll.begin(); it != coll.end(); ++it) {
174  std::auto_ptr<E> e = context->getSerializationService().template toObject<E>(*it);
175  elements.push_back(*e);
176  }
177  return coll.size();
178  }
179 
185  boost::shared_ptr<E> poll() {
186  return poll(0);
187  }
188 
194  boost::shared_ptr<E> peek() {
195  return boost::shared_ptr<E>(toObject<E>(proxy::IQueueImpl::peekData()));
196  }
197 
202  int size() {
203  return proxy::IQueueImpl::size();
204  }
205 
210  bool isEmpty() {
211  return size() == 0;
212  }
213 
218  std::vector<E> toArray() {
219  return toObjectCollection<E>(proxy::IQueueImpl::toArrayData());
220  }
221 
228  bool containsAll(const std::vector<E>& elements) {
229  std::vector<serialization::pimpl::Data> list = toDataCollection(elements);
230  return proxy::IQueueImpl::containsAll(list);
231  }
232 
239  bool addAll(const std::vector<E>& elements) {
240  std::vector<serialization::pimpl::Data> dataList = toDataCollection(elements);
241  return proxy::IQueueImpl::addAll(dataList);
242  }
243 
250  bool removeAll(const std::vector<E>& elements) {
251  std::vector<serialization::pimpl::Data> dataList = toDataCollection(elements);
252  return proxy::IQueueImpl::removeAll(dataList);
253  }
254 
262  bool retainAll(const std::vector<E>& elements) {
263  std::vector<serialization::pimpl::Data> dataList = toDataCollection(elements);
264  return proxy::IQueueImpl::retainAll(dataList);
265  }
266 
270  void clear() {
271  proxy::IQueueImpl::clear();
272  }
273  private:
274  IQueue(const std::string& instanceName, spi::ClientContext *context)
275  : proxy::IQueueImpl(instanceName, context) {
276  }
277  };
278  }
279 }
280 
281 #endif /* HAZELCAST_IQUEUE */
282 
void clear()
Removes all elements from queue.
Definition: IQueue.h:270
bool offer(const E &element, long timeoutInMillis)
Inserts the specified element into this queue.
Definition: IQueue.h:106
Concurrent, blocking, distributed, observable, client queue.
Definition: IQueue.h:38
bool offer(const E &element)
Inserts the specified element into this queue.
Definition: IQueue.h:85
std::vector< E > toArray()
Definition: IQueue.h:218
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:171
bool contains(const E &element)
Definition: IQueue.h:150
bool removeAll(const std::vector< E > &elements)
Definition: IQueue.h:250
size_t drainTo(std::vector< E > &elements)
Note that elements will be pushed_back to vector.
Definition: IQueue.h:160
int remainingCapacity()
Definition: IQueue.h:132
bool removeItemListener(const std::string &registrationId)
Removes the specified item listener.
Definition: IQueue.h:73
std::string addItemListener(ItemListener< E > &listener, bool includeValue)
Adds an item listener for this collection.
Definition: IQueue.h:57
boost::shared_ptr< E > poll(long timeoutInMillis)
Definition: IQueue.h:123
int size()
Definition: IQueue.h:202
void put(const E &element)
Puts the element into queue.
Definition: IQueue.h:93
bool isEmpty()
Definition: IQueue.h:210
bool retainAll(const std::vector< E > &elements)
Removes the elements from this queue that are not available in given "elements" vector.
Definition: IQueue.h:262
boost::shared_ptr< E > peek()
Returns immediately without waiting.
Definition: IQueue.h:194
Item listener for IQueue, ISet and IList.
Definition: ItemListener.h:40
bool containsAll(const std::vector< E > &elements)
Definition: IQueue.h:228
bool addAll(const std::vector< E > &elements)
Definition: IQueue.h:239
boost::shared_ptr< E > poll()
Returns immediately without waiting.
Definition: IQueue.h:185
boost::shared_ptr< E > take()
Definition: IQueue.h:114
Hazelcast Client enables you to do all Hazelcast operations without being a member of the cluster...
Definition: HazelcastClient.h:458