1 /**
2  * (C) 2016 - 2017 KISTLER INSTRUMENTE AG, Winterthur, Switzerland
3  * (C) 2016 - 2019 Stanislav Angelovic <angelovic.s@gmail.com>
4  *
5  * @file BaseGenerator.h
6  *
7  * Created on: Feb 1, 2017
8  * Project: sdbus-c++
9  * Description: High-level D-Bus IPC C++ library based on sd-bus
10  *
11  * This file is part of sdbus-c++.
12  *
13  * sdbus-c++ is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation, either version 2.1 of the License, or
16  * (at your option) any later version.
17  *
18  * sdbus-c++ is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with sdbus-c++. If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 #ifndef __SDBUSCPP_TOOLS_BASE_GENERATOR_H
28 #define __SDBUSCPP_TOOLS_BASE_GENERATOR_H
29 
30 // Own headers
31 #include "xml.h"
32 
33 // STL
34 #include <string>
35 #include <tuple>
36 
37 class BaseGenerator
38 {
39 
40 public:
41     int transformXmlToFile(const sdbuscpp::xml::Document& doc, const char* filename) const;
42 
43 protected:
44     enum class StubType
45     {
46         ADAPTOR,
47         PROXY
48     };
49 
50     constexpr static const char *tab = "    ";
51 
~BaseGenerator()52     virtual ~BaseGenerator() {}
53 
54     /**
55      * Implementation of public function that is provided by inherited class
56      * @param doc
57      * @param filename
58      * @return
59      */
60     virtual int transformXmlToFileImpl(const sdbuscpp::xml::Document& doc, const char* filename) const = 0;
61 
62 
63     /**
64      * Write data to file
65      * @param filename Written file
66      * @param data Data to write
67      * @return 0 if ok
68      */
69     int writeToFile(const char* filename, const std::string& data) const;
70 
71     /**
72      * Crete header of file - include guard, includes
73      * @param filename
74      * @param stubType
75      * @return
76      */
77     std::string createHeader(const char* filename, const StubType& stubType) const;
78 
79     /**
80      * Namespaces according to the interface name
81      * @param ifaceName
82      * @return tuple: count of namespaces, string with code
83      */
84     std::tuple<unsigned, std::string> generateNamespaces(const std::string& ifaceName) const;
85 
86     /**
87      * Transform arguments into source code
88      * @param args
89      * @return tuple: argument names, argument types and names, argument types
90      */
91     std::tuple<std::string, std::string, std::string, std::string> argsToNamesAndTypes(const sdbuscpp::xml::Nodes& args, bool async = false) const;
92 
93     /**
94      * Output arguments to return type
95      * @param args
96      * @return return type
97      */
98     std::string outArgsToType(const sdbuscpp::xml::Nodes& args, bool bareList = false) const;
99 
100 };
101 
102 
103 
104 #endif //__SDBUSCPP_TOOLS_BASE_GENERATOR_H
105