1 /*
2  * Copyright (c) 2021-2023 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 
16 #ifndef ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_COMMAND_FACTORY_H
17 #define ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_COMMAND_FACTORY_H
18 
19 #include <parcel.h>
20 #include <unordered_map>
21 
22 #include "common/rs_common_def.h"
23 #include "common/rs_macros.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 class RSCommand;
28 using UnmarshallingFunc = RSCommand* (*)(Parcel& parcel);
29 
30 class RSB_EXPORT RSCommandFactory {
31 public:
32     static RSCommandFactory& Instance();
33 
34     void Register(uint16_t type, uint16_t subtype, UnmarshallingFunc func);
35     UnmarshallingFunc GetUnmarshallingFunc(uint16_t type, uint16_t subtype);
36 
37 private:
38     RSCommandFactory() = default;
39     ~RSCommandFactory() = default;
40 
41     std::unordered_map<uint32_t, UnmarshallingFunc> unmarshallingFuncLUT_;
42 };
43 
44 // Helper class for automatically registry
45 template<uint16_t commandType, uint16_t commandSubType, UnmarshallingFunc func>
46 class RSCommandRegister {
47 public:
RSCommandRegister()48     RSCommandRegister()
49     {
50         RSCommandFactory::Instance().Register(commandType, commandSubType, func);
51     }
52 };
53 
54 } // namespace Rosen
55 } // namespace OHOS
56 
57 #endif // ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_COMMAND_FACTORY_H
58