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 
16 #ifndef OHOS_IDL_CODEGENERATOR_H
17 #define OHOS_IDL_CODEGENERATOR_H
18 
19 #include <functional>
20 #include <map>
21 #include <string>
22 
23 #include "codegen/code_emitter.h"
24 
25 namespace OHOS {
26 namespace Idl {
27 
28 using CodeEmitMap = std::unordered_map<std::string, AutoPtr<CodeEmitter>>;
29 using CodeGenFunc = std::function<void(const AutoPtr<AST>&, const std::string&)>;
30 using StrAstMap = std::unordered_map<std::string, AutoPtr<AST>>;
31 
32 class CodeGenerator : public LightRefCountBase {
33 public:
34     virtual bool DoGenerate(const StrAstMap &allAst);
35 };
36 
37 class CodegenBuilder : public LightRefCountBase {
38 public:
39     using GeneratorEntry = std::map<InterfaceType, AutoPtr<CodeGenerator>>;
40 
41     static CodegenBuilder &GetInstance();
42     CodegenBuilder(const CodegenBuilder &other) = delete;
43     CodegenBuilder operator=(const CodegenBuilder &other) = delete;
44     ~CodegenBuilder() = default;
45 
46     bool Generate(const StrAstMap &allAst);
47     void GeneratorRegister(InterfaceType type, AutoPtr<CodeGenerator> generator);
48 
49 private:
CodegenBuilder()50     CodegenBuilder() {}
51     GeneratorEntry generators;
52 };
53 } // namespace Idl
54 } // namespace OHOS
55 
56 #endif // OHOS_IDL_CODEGENERATOR_H