1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
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 #ifndef WIFI_DIRECT_EVENT_DISPATCHER_H
16 #define WIFI_DIRECT_EVENT_DISPATCHER_H
17 
18 #include <functional>
19 #include "conn_log.h"
20 #include "wifi_direct_event_queue.h"
21 #include "wifi_direct_event_template_dispatcher.h"
22 
23 namespace OHOS::SoftBus {
24 class WifiDirectEventDispatcher {
25 public:
26     WifiDirectEventDispatcher(const WifiDirectEventDispatcher &) = delete;
27     WifiDirectEventDispatcher& operator=(const WifiDirectEventDispatcher &) = delete;
28 
29     explicit WifiDirectEventDispatcher(WifiDirectEventQueue *queue);
30     ~WifiDirectEventDispatcher() noexcept(false);
31 
32     template<typename Content>
33     WifiDirectEventTemplateDispatcher<WifiDirectEventDispatcher, Content>
Handle(std::function<void (Content &)> && func)34     Handle(std::function<void(Content&)> &&func)
35     {
36         return WifiDirectEventTemplateDispatcher<WifiDirectEventDispatcher, Content>(
37             queue_, this, std::forward<std::function<void(Content&)>>(func));
38     }
39 
40 private:
41     template<typename Dispatcher, typename Event>
42     friend class WifiDirectEventTemplateDispatcher;
43 
44     void WaitAndDispatch();
45     static bool Dispatch(const std::shared_ptr<WifiDirectEventBase> &event);
46 
47     WifiDirectEventQueue *queue_;
48     bool chained_;
49 };
50 
51 enum class ProcessorTerminateReason {
52     SUCCESS,
53     FAILURE,
54     RETRY,
55 };
56 
57 struct ProcessorTerminate : public std::exception {
58     ProcessorTerminate(ProcessorTerminateReason reason = ProcessorTerminateReason::SUCCESS) : reason_(reason) {}
59     ProcessorTerminateReason reason_;
60 };
61 }
62 #endif
63