File indexing completed on 2024-05-19 05:41:57

0001 // ct_lvtcgn_generatecode.h                                       -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #ifndef CT_LVTCGN_CODEGEN_H_INCLUDED
0021 #define CT_LVTCGN_CODEGEN_H_INCLUDED
0022 
0023 #include <lvtcgn_mdl_export.h>
0024 
0025 #include <result/result.hpp>
0026 
0027 #include <memory>
0028 #include <optional>
0029 #include <string>
0030 #include <vector>
0031 
0032 namespace Codethink::lvtcgn::mdl {
0033 
0034 class LVTCGN_MDL_EXPORT IPhysicalEntityInfo {
0035     /**
0036      * Implementations of this class are meant to be a thin, cheaply copiable layer that mostly
0037      * dispatches the calls to an actual underlying model, although having primitive data should be
0038      * fine, as long as it remains cheap to copy.
0039      *
0040      * The reason for that is that we don't have currently a way to avoid direct dependency to database
0041      * data structures.
0042      *
0043      * TODO [#437]: Verify if this interface still makes sense AFTER architecture review.
0044      * _Maybe_ we can remove the thin-layer implementations when task #437 is done.
0045      *
0046      */
0047   public:
0048     virtual ~IPhysicalEntityInfo();
0049     virtual std::string name() const = 0;
0050     virtual std::string type() const = 0;
0051     virtual std::optional<std::reference_wrapper<IPhysicalEntityInfo>> parent() const = 0;
0052     virtual std::vector<std::reference_wrapper<IPhysicalEntityInfo>> children() const = 0;
0053     virtual std::vector<std::reference_wrapper<IPhysicalEntityInfo>> fwdDependencies() const = 0;
0054     virtual bool selectedForCodeGeneration() const = 0;
0055     virtual void setSelectedForCodeGeneration(bool value) = 0;
0056 };
0057 
0058 class LVTCGN_MDL_EXPORT ICodeGenerationDataProvider {
0059   public:
0060     virtual ~ICodeGenerationDataProvider();
0061     virtual std::vector<std::reference_wrapper<IPhysicalEntityInfo>> topLevelEntities() = 0;
0062     virtual int numberOfPhysicalEntities() const = 0;
0063 };
0064 
0065 struct LVTCGN_MDL_EXPORT CodeGenerationError {
0066     enum class Kind { PythonError, ScriptDefinitionError };
0067 
0068     Kind kind;
0069     std::string message;
0070 };
0071 
0072 class LVTCGN_MDL_EXPORT CodeGeneration {
0073   public:
0074     class CodeGenerationStep {
0075       public:
0076         virtual ~CodeGenerationStep() = default;
0077     };
0078 
0079     class ProcessEntityStep : public CodeGenerationStep {
0080       public:
0081         explicit ProcessEntityStep(const std::string& entityName): m_entityName(entityName)
0082         {
0083         }
0084 
0085         std::string entityName() const
0086         {
0087             return m_entityName;
0088         }
0089 
0090       private:
0091         std::string m_entityName;
0092     };
0093 
0094     class BeforeProcessEntitiesStep : public CodeGenerationStep { };
0095     class AfterProcessEntitiesStep : public CodeGenerationStep { };
0096 
0097     static cpp::result<void, CodeGenerationError>
0098     generateCodeFromScript(const std::string& scriptPath,
0099                            const std::string& outputDir,
0100                            ICodeGenerationDataProvider& dataProvider,
0101                            std::optional<std::function<void(CodeGenerationStep const&)>> callback = std::nullopt);
0102 };
0103 
0104 } // namespace Codethink::lvtcgn::mdl
0105 
0106 #endif // CT_LVTCGN_CODEGEN_H_INCLUDED