Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : //
3 : : // Licensed under the Apache License, Version 2.0 (the "License");
4 : : // you may not use this file except in compliance with the License.
5 : : // You may obtain a copy of the License at
6 : : //
7 : : // http://www.apache.org/licenses/LICENSE-2.0
8 : : //
9 : : // Unless required by applicable law or agreed to in writing, software
10 : : // distributed under the License is distributed on an "AS IS" BASIS,
11 : : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : // See the License for the specific language governing permissions and
13 : : // limitations under the License.
14 : :
15 : : #pragma once
16 : :
17 : : #include <functional>
18 : :
19 : : namespace hpactor {
20 : :
21 : : class TypedMessage;
22 : :
23 : : // -----------------------------------------------------------------------------
24 : : // overloaded - helper for std::visit with lambdas (C++20 backport)
25 : : // -----------------------------------------------------------------------------
26 : : template <class... Ts> struct overloaded : Ts... {
27 : : using Ts::operator()...;
28 : : };
29 : : template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
30 : :
31 : : // -----------------------------------------------------------------------------
32 : : // Behavior - handler function for message processing
33 : : // -----------------------------------------------------------------------------
34 : : class Behavior {
35 : : public:
36 : : using handler_type = std::function<void(TypedMessage&)>;
37 : :
38 : 123 : Behavior() = default;
39 : :
40 : 50 : explicit Behavior(handler_type handler) : handler_(std::move(handler)) {}
41 : :
42 : 338 : explicit operator bool() const {
43 : 338 : return handler_ != nullptr;
44 : : }
45 : :
46 : 331 : void operator()(TypedMessage& msg) const {
47 : 331 : if (handler_) {
48 : 331 : handler_(msg);
49 : : }
50 : 331 : }
51 : :
52 : : private:
53 : : handler_type handler_;
54 : : };
55 : :
56 : : } // namespace hpactor
|