1
2 /*
3 * Copyright (c) 2024 Huawei Device Co., Ltd.
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 #ifndef ENGINE_CALLBACK_MESSAGE_H
17 #define ENGINE_CALLBACK_MESSAGE_H
18
19 #include <functional>
20 #include <map>
21 #include <string>
22 #include <any>
23 #include <optional>
24 #include "nocopyable.h"
25
26 namespace OHOS {
27 namespace IntellVoiceEngine {
28 enum EngineCbMessageId {
29 HANDLE_CLOSE_WAKEUP_SOURCE,
30 HANDLE_HEADSET_HOST_DIE,
31 HANDLE_CLEAR_WAKEUP_ENGINE_CB,
32 HANDLE_RELEASE_ENGINE,
33 HANDLE_ENGINE_SET_SENSIBILITY,
34 HANDLE_UPDATE_COMPLETE,
35 HANDLE_UPDATE_RETRY,
36 RELEASE_ENGINE,
37 QUERY_SWITCH_STATUS,
38 TRIGGERMGR_GET_PARAMETER,
39 TRIGGERMGR_SET_PARAMETER,
40 TRIGGERMGR_UPDATE_MODEL,
41 };
42
43 class EngineCallbackMessage {
44 public:
45 EngineCallbackMessage() = default;
46 ~EngineCallbackMessage() = default;
47
48 using Func = std::function<std::optional<std::any>(const std::vector<std::any>&)>;
49
RegisterFunc(EngineCbMessageId id,Func func)50 static void RegisterFunc(EngineCbMessageId id, Func func)
51 {
52 EngineFuncMap[id] = func;
53 }
54 template <typename... Args>
55 static std::optional<std::any> CallFunc(EngineCbMessageId id, Args &&... args);
56
57 private:
58 static std::map<EngineCbMessageId, Func> EngineFuncMap;
59 DISALLOW_COPY_AND_MOVE(EngineCallbackMessage);
60 };
61
62 template <typename... Args>
CallFunc(EngineCbMessageId id,Args &&...args)63 std::optional<std::any> EngineCallbackMessage::CallFunc(EngineCbMessageId id, Args &&... args)
64 {
65 std::vector<std::any> params = { std::forward<Args>(args)... };
66 if (EngineFuncMap.find(id) != EngineFuncMap.end()) {
67 return EngineFuncMap[id](params);
68 } else {
69 return std::nullopt;
70 }
71 }
72 } // namespace IntellVoice
73 } // namespace OHOS
74 #endif