Hazelcast C++ Client
Hazelcast C++ Client Library
reliable_listener.h
1 /*
2  * Copyright (c) 2008-2022, 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 <cstdint>
19 
20 #include "hazelcast/client/exception/iexception.h"
21 #include "hazelcast/util/noop.h"
22 #include "hazelcast/util/type_traits.h"
23 
24 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
25 #pragma warning(push)
26 #pragma warning(disable : 4251) // for dll export
27 #endif
28 
29 namespace hazelcast {
30 namespace client {
31 class reliable_topic;
32 namespace topic {
33 class message;
34 
65 class HAZELCAST_API reliable_listener final
66 {
67  friend class client::reliable_topic;
68 
69 public:
83  explicit reliable_listener(bool loss_tolerant,
84  int64_t initial_sequence_id = -1);
85 
96  template<typename Handler,
97  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
99  {
100  received_ = std::move(h);
101  return *this;
102  }
103 
107  template<typename Handler,
108  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
109  reliable_listener&& on_received(Handler&& h) &&
110  {
111  on_received(std::move(h));
112  return std::move(*this);
113  }
114 
123  template<typename Handler,
124  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
126  {
127  store_sequence_id_ = std::move(h);
128  return *this;
129  }
130 
134  template<typename Handler,
135  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
137  {
138  on_store_sequence_id(std::move(h));
139  return std::move(*this);
140  }
141 
150  template<typename Handler,
151  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
153  {
154  terminal_ = std::move(h);
155  return *this;
156  }
157 
161  template<typename Handler,
162  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
164  {
165  terminate_on_exception(std::move(h));
166  return std::move(*this);
167  }
168 
169 private:
170  using received_handler_t = std::function<void(message&&)>;
171  using store_sequence_id_handler_t = std::function<void(int64_t)>;
172  using exception_handler_t =
173  std::function<bool(const exception::iexception&)>;
174 
175  bool loss_tolerant_;
176  int64_t initial_sequence_id_;
177 
178  received_handler_t received_{ util::noop<message&&> };
179  store_sequence_id_handler_t store_sequence_id_{ util::noop<int64_t> };
180  exception_handler_t terminal_{ [](const exception::iexception&) {
181  return false;
182  } };
183 };
184 
185 } // namespace topic
186 } // namespace client
187 } // namespace hazelcast
188 
189 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
190 #pragma warning(pop)
191 #endif
Base class for all exception originated from Hazelcast methods.
Definition: iexception.h:49
Hazelcast provides distribution mechanism for publishing messages that are delivered to multiple subs...
Listen to messages from a reliable_topic.
reliable_listener & terminate_on_exception(Handler &&h) &
Set an handler function that checks if the listener should be terminated based on an exception thrown...
reliable_listener && terminate_on_exception(Handler &&h) &&
Set an handler function that checks if the listener should be terminated based on an exception thrown...
reliable_listener && on_store_sequence_id(Handler &&h) &&
reliable_listener & on_store_sequence_id(Handler &&h) &
Set an handler function to be invoked to informs the listener that it should store the sequence.
reliable_listener && on_received(Handler &&h) &&
Set an handler function to be invoked when a message is received for the added topic.
reliable_listener & on_received(Handler &&h) &
Set an handler function to be invoked when a message is received for the added topic.