Reflection for RapidJSON 0.0.17
Reflection for serializing/deserializing with RapidJSON
Loading...
Searching...
No Matches
codefactory.h
Go to the documentation of this file.
1#ifndef REFLECTIVE_RAPIDJSON_CODE_FACTORY_H
2#define REFLECTIVE_RAPIDJSON_CODE_FACTORY_H
3
4#include "./codegenerator.h"
5
6#include <functional>
7#include <iosfwd>
8#include <memory>
9#include <string>
10#include <vector>
11
12namespace clang {
13class CompilerInstance;
14} // namespace clang
15
16namespace ReflectiveRapidJSON {
17
18class Consumer;
19class Visitor;
20
28 friend class Consumer;
29 friend class Visitor;
30
31public:
32 CodeFactory(std::string_view applicationPath, const std::vector<const char *> &sourceFiles, const std::vector<std::string_view> &clangOptions,
33 std::ostream &os);
35
36 const std::vector<std::unique_ptr<CodeGenerator>> &generators() const;
37 template <typename GeneratorType, typename... Args> void addGenerator(Args &&...args);
38 template <typename GeneratorType, typename... Args> auto bindGenerator(Args &&...args);
39
40 bool run();
41 clang::CompilerInstance *compilerInstance();
42 void setCompilerInstance(clang::CompilerInstance *compilerInstance);
43 bool isErrorResilient() const;
44 void setErrorResilient(bool errorResilient);
45
46private:
47 struct ToolInvocation;
48
49 void addDeclaration(clang::Decl *decl);
50 bool generate() const;
51 std::vector<std::string> makeClangArgs() const;
52
53 std::string_view m_applicationPath;
54 const std::vector<const char *> &m_sourceFiles;
55 const std::vector<std::string_view> &m_clangOptions;
56 std::ostream &m_os;
57 std::vector<std::unique_ptr<CodeGenerator>> m_generators;
58 std::unique_ptr<ToolInvocation> m_toolInvocation;
59 clang::CompilerInstance *m_compilerInstance;
60 bool m_errorResilient;
61};
62
67template <typename GeneratorType, typename... Args> void CodeFactory::addGenerator(Args &&...args)
68{
69 m_generators.emplace_back(std::make_unique<GeneratorType>(*this, std::forward<Args>(args)...));
70}
71
72namespace Detail {
76template <typename T> std::reference_wrapper<const T> wrapReferences(const T &val)
77{
78 return std::cref(val);
79}
80
84template <typename T> std::reference_wrapper<T> wrapReferences(T &val)
85{
86 return std::ref(val);
87}
88
92template <typename T> T &&wrapReferences(T &&val)
93{
94 return std::forward<T>(val);
95}
96} // namespace Detail
97
104template <typename GeneratorType, typename... Args> auto CodeFactory::bindGenerator(Args &&...args)
105{
106 return std::bind(&CodeFactory::addGenerator<GeneratorType, Args...>, this, Detail::wrapReferences(std::forward<Args>(args)...));
107}
108
112inline const std::vector<std::unique_ptr<CodeGenerator>> &CodeFactory::generators() const
113{
114 return m_generators;
115}
116
121inline clang::CompilerInstance *CodeFactory::compilerInstance()
122{
123 return m_compilerInstance;
124}
125
130inline void CodeFactory::setCompilerInstance(clang::CompilerInstance *compilerInstance)
131{
132 m_compilerInstance = compilerInstance;
133}
134
139{
140 return m_errorResilient;
141}
142
146inline void CodeFactory::setErrorResilient(bool errorResilient)
147{
148 m_errorResilient = errorResilient;
149}
150
151} // namespace ReflectiveRapidJSON
152
153#endif // REFLECTIVE_RAPIDJSON_CODE_FACTORY_H
bool isErrorResilient() const
Returns whether most errors will be turned into warnings (by default false).
const std::vector< std::unique_ptr< CodeGenerator > > & generators() const
Returns the added generators.
bool run()
Reads (relevant) AST elements using Clang and generates code.
auto bindGenerator(Args &&...args)
Returns a function which instantiates a code generator of the specified type and adds it to the curre...
void setCompilerInstance(clang::CompilerInstance *compilerInstance)
Assigns the compiler instance.
void addGenerator(Args &&...args)
Instantiates a code generator of the specified type and adds it to the current instance.
Definition codefactory.h:67
clang::CompilerInstance * compilerInstance()
Returns the compiler instance.
void setErrorResilient(bool errorResilient)
Sets whether most errors will be turned into warnings (by default false).
CodeFactory(std::string_view applicationPath, const std::vector< const char * > &sourceFiles, const std::vector< std::string_view > &clangOptions, std::ostream &os)
Constructs a new instance.
The Consumer class is passed to FrontendAction for handling occurrences of different elements of the ...
Definition consumer.h:24
The Visitor class is used to traverse the elements of a translation unit.
Definition visitor.h:15
std::reference_wrapper< const T > wrapReferences(const T &val)
Wraps const references using std::cref() for use with std::bind().
Definition codefactory.h:76