Hazelcast C++ Client
Hazelcast C++ Client Library
reliable_listener.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 <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 
58  class HAZELCAST_API reliable_listener final {
59  friend class client::reliable_topic;
60  public:
71  explicit reliable_listener(bool loss_tolerant, int64_t initial_sequence_id = -1);
72 
82  template<typename Handler,
83  typename = util::enable_if_rvalue_ref_t<Handler &&>>
84  reliable_listener &on_received(Handler &&h) & {
85  received_ = std::move(h);
86  return *this;
87  }
88 
92  template<typename Handler,
93  typename = util::enable_if_rvalue_ref_t<Handler &&>>
94  reliable_listener &&on_received(Handler &&h) && {
95  on_received(std::move(h));
96  return std::move(*this);
97  }
98 
106  template<typename Handler,
107  typename = util::enable_if_rvalue_ref_t<Handler &&>>
109  store_sequence_id_ = std::move(h);
110  return *this;
111  }
112 
116  template<typename Handler,
117  typename = util::enable_if_rvalue_ref_t<Handler &&>>
119  on_store_sequence_id(std::move(h));
120  return std::move(*this);
121  }
122 
130  template<typename Handler,
131  typename = util::enable_if_rvalue_ref_t<Handler &&>>
133  terminal_ = std::move(h);
134  return *this;
135  }
136 
140  template<typename Handler,
141  typename = util::enable_if_rvalue_ref_t<Handler &&>>
143  terminate_on_exception(std::move(h));
144  return std::move(*this);
145  }
146 
147  private:
148  using received_handler_t = std::function<void(message &&)>;
149  using store_sequence_id_handler_t = std::function<void(int64_t)>;
150  using exception_handler_t = std::function<bool(const exception::iexception &)>;
151 
152  bool loss_tolerant_;
153  int64_t initial_sequence_id_;
154 
155  received_handler_t received_{ util::noop<message &&> };
156  store_sequence_id_handler_t store_sequence_id_ { util::noop<int64_t> };
157  exception_handler_t terminal_{
158  [](const exception::iexception &){
159  return false;
160  }
161  };
162  };
163 
164  }
165  }
166 }
167 
168 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
169 #pragma warning(pop)
170 #endif
171 
172 
173 
Base class for all exception originated from Hazelcast methods.
Definition: iexception.h:48
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.