Hazelcast C++ Client
Hazelcast C++ Client Library
lifecycle_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 <functional>
19 
20 #include "hazelcast/util/export.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 lifecycle_event;
32 namespace spi {
33 class lifecycle_service;
34 }
35 
47 class HAZELCAST_API lifecycle_listener final
48 {
49 public:
55  template<typename Handler,
56  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
58  {
59  starting_ = std::forward<Handler>(h);
60  return *this;
61  }
62 
66  template<typename Handler,
67  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
68  lifecycle_listener&& on_starting(Handler&& h) &&
69  {
70  on_starting(std::forward<Handler>(h));
71  return std::move(*this);
72  }
73 
79  template<typename Handler,
80  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
82  {
83  started_ = std::forward<Handler>(h);
84  return *this;
85  }
86 
90  template<typename Handler,
91  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
92  lifecycle_listener&& on_started(Handler&& h) &&
93  {
94  on_started(std::forward<Handler>(h));
95  return std::move(*this);
96  }
97 
103  template<typename Handler,
104  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
106  {
107  shutting_down_ = std::forward<Handler>(h);
108  return *this;
109  }
110 
114  template<typename Handler,
115  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
117  {
118  on_shutting_down(std::forward<Handler>(h));
119  return std::move(*this);
120  }
121 
127  template<typename Handler,
128  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
130  {
131  shutdown_ = std::forward<Handler>(h);
132  return *this;
133  }
134 
138  template<typename Handler,
139  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
140  lifecycle_listener&& on_shutdown(Handler&& h) &&
141  {
142  on_shutdown(std::forward<Handler>(h));
143  return std::move(*this);
144  }
145 
151  template<typename Handler,
152  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
154  {
155  connected_ = std::forward<Handler>(h);
156  return *this;
157  }
158 
162  template<typename Handler,
163  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
165  {
166  on_connected(std::forward<Handler>(h));
167  return std::move(*this);
168  }
169 
175  template<typename Handler,
176  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
178  {
179  disconnected_ = std::forward<Handler>(h);
180  return *this;
181  }
182 
186  template<typename Handler,
187  typename = util::enable_if_rvalue_ref_trait<Handler&&>>
189  {
190  on_disconnected(std::forward<Handler>(h));
191  return std::move(*this);
192  }
193 
194 private:
195  using HandlerType = std::function<void()>;
196  HandlerType starting_{ util::noop<> }, started_{ util::noop<> },
197  shutting_down_{ util::noop<> }, shutdown_{ util::noop<> },
198  connected_{ util::noop<> }, disconnected_{ util::noop<> };
199 
200  friend class spi::lifecycle_service;
201 };
202 } // namespace client
203 } // namespace hazelcast
204 
205 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
206 #pragma warning(pop)
207 #endif
Listener object for listening lifecycle events of hazelcast instance.
lifecycle_listener & on_shutdown(Handler &&h) &
Set an handler function to be invoked when the client's shutdown has completed.
lifecycle_listener && on_disconnected(Handler &&h) &&
Set an handler function to be invoked when client is disconnected from the cluster.
lifecycle_listener & on_started(Handler &&h) &
Set an handler function to be invoked when the client has started.
lifecycle_listener && on_connected(Handler &&h) &&
Set an handler function to be invoked when the client is connected to the cluster.
lifecycle_listener && on_shutdown(Handler &&h) &&
Set an handler function to be invoked when the client's shutdown has completed.
lifecycle_listener && on_shutting_down(Handler &&h) &&
Set an handler function to be invoked when the client is shutting down.
lifecycle_listener && on_started(Handler &&h) &&
Set an handler function to be invoked when the client has started.
lifecycle_listener & on_connected(Handler &&h) &
Set an handler function to be invoked when the client is connected to the cluster.
lifecycle_listener & on_starting(Handler &&h) &
Set an handler function to be invoked when the client is starting.
lifecycle_listener & on_disconnected(Handler &&h) &
Set an handler function to be invoked when client is disconnected from the cluster.
lifecycle_listener && on_starting(Handler &&h) &&
Set an handler function to be invoked when the client is starting.
lifecycle_listener & on_shutting_down(Handler &&h) &
Set an handler function to be invoked when the client is shutting down.