/* * Copyright (c) 2017 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef IDLC_GENERATOR_H_ #define IDLC_GENERATOR_H_ #include #include #include #include #include #include "idlc/document.h" #include "idlc/parameter.h" #include "idlc/type.h" namespace tidl { class Generator { public: explicit Generator(std::shared_ptr doc); virtual ~Generator() = default; void Run(const std::string& file_name); std::string AddIndent(int indent, std::string lines, bool space = true); std::string GetFileNamespace() const; std::string ReplaceAll(std::string str, const std::string& from, const std::string& to); bool IsDelegateType(const Interface& inf, const BaseType& type); bool IsDelegateType(const BaseType& type); void EnableNamespace(bool enable) { hasNamespace_ = enable; } bool HasNamespace() const { return hasNamespace_; } template void GenTemplate(std::string templ, std::ofstream& stream, T cb, ARGS... args) { size_t f = templ.find("$$"); templ.replace(f, std::string("$$").length(), cb()); GenTemplate(std::move(templ), stream, args...); } template void GenTemplate(std::string templ, std::ofstream& stream, T cb) { size_t f = templ.find("$$"); templ.replace(f, std::string("$$").length(), cb()); stream << templ; } template std::string GenTemplateString(std::string templ, T cb, ARGS... args) { size_t f = templ.find("$$"); templ.replace(f, std::string("$$").length(), cb()); return GenTemplateString(std::move(templ), args...); } template std::string GenTemplateString(std::string templ, T cb) { size_t f = templ.find("$$"); templ.replace(f, std::string("$$").length(), cb()); return templ; } template void GenBrace(std::ofstream& stream, int indent, T cb, bool start_indent = true, bool ended_new_line = true) { if (start_indent) { for (int i = 0; i < indent; i++) stream << " "; } stream << "{" << std::endl; cb(); for (int i = 0; i < indent; i++) stream << " "; stream << "}"; if (ended_new_line) stream << std::endl; } virtual void OnInitGen(std::ofstream& stream) = 0; virtual void OnFiniGen(std::ofstream& stream) = 0; protected: const Document& GetDocument() { return *doc_; } protected: std::string FileName; private: static void CallEmptyCallback(int pos) {} private: std::shared_ptr doc_; std::ofstream out_file_; bool hasNamespace_ = true; }; } // namespace tidl #endif // IDLC_GENERATOR_H_