#ifndef REFLECTIVE_RAPIDJSON_GENERATOR_H #define REFLECTIVE_RAPIDJSON_GENERATOR_H #include #include #include #include namespace clang { class Decl; class NamedDecl; class CXXRecordDecl; } // namespace clang namespace ReflectiveRapidJSON { /*! * \brief The CodeGenerator class is the base for generators used by the CodeFactory class. */ class CodeGenerator { public: CodeGenerator(); virtual ~CodeGenerator(); virtual void addDeclaration(clang::Decl *decl); /// \brief Generates code based on the previously added declarations. The code is written to \a os. virtual void generate(std::ostream &os) const = 0; protected: static bool inheritsFromInstantiationOf(clang::CXXRecordDecl *record, const char *templateClass); }; inline CodeGenerator::CodeGenerator() { } /*! * \brief The JSONSerializationCodeGenerator class generates code for JSON (de)serialization * of objects inheriting from an instantiation of JSONSerializable. */ class JSONSerializationCodeGenerator : public CodeGenerator { public: JSONSerializationCodeGenerator(); void addDeclaration(clang::Decl *decl) override; void generate(std::ostream &os) const override; private: struct RelevantClass { explicit RelevantClass(const std::string &qualifiedName, clang::CXXRecordDecl *record); std::string qualifiedName; clang::CXXRecordDecl *record; }; std::vector m_relevantClasses; }; inline JSONSerializationCodeGenerator::JSONSerializationCodeGenerator() { } inline JSONSerializationCodeGenerator::RelevantClass::RelevantClass(const std::string &qualifiedName, clang::CXXRecordDecl *record) : qualifiedName(qualifiedName) , record(record) { } /*! * \brief The CodeFactory class produces additional (reflection) code for a specified list of C++ source files. * \remarks * - The code is written to a specified std::ostream instance. * - The CodeFactory class is constituted by its underlying CodeGenerator instances. */ class CodeFactory { public: CodeFactory(const char *applicationPath, const std::vector &sourceFiles, std::ostream &os); ~CodeFactory(); std::vector> &generators(); const std::vector> &generators() const; void addDeclaration(clang::Decl *decl); bool readAST(); bool generate() const; private: struct ToolInvocation; std::vector makeClangArgs() const; const char *const m_applicationPath; const std::vector &m_sourceFiles; std::ostream &m_os; std::vector> m_generators; std::unique_ptr m_toolInvocation; }; inline std::vector> &CodeFactory::generators() { return m_generators; } inline const std::vector> &CodeFactory::generators() const { return m_generators; } } // namespace ReflectiveRapidJSON #endif // REFLECTIVE_RAPIDJSON_GENERATOR_H