Compare commits
3 Commits
master
...
wip/use-an
Author | SHA1 | Date |
---|---|---|
Martchus | 3f6f790849 | |
Martchus | cb98e348f1 | |
Martchus | a6461795a7 |
|
@ -127,6 +127,9 @@ void BinarySerializationCodeGenerator::generate(ostream &os) const
|
|||
os << " // write members\n";
|
||||
auto membersWritten = false;
|
||||
for (const clang::FieldDecl *field : relevantClass.record->fields()) {
|
||||
for (const auto &attr : field->getAttrs()) {
|
||||
cout << " // annotation: " << readAnnotation(attr) << '\n';
|
||||
}
|
||||
if (writePrivateMembers || field->getAccess() == clang::AS_public) {
|
||||
os << " serializer.write(customObject." << field->getName() << ");\n";
|
||||
membersWritten = true;
|
||||
|
|
|
@ -3,8 +3,10 @@
|
|||
|
||||
#include <c++utilities/application/global.h>
|
||||
|
||||
#include <clang/AST/Attr.h>
|
||||
#include <clang/AST/DeclCXX.h>
|
||||
#include <clang/Frontend/CompilerInstance.h>
|
||||
#include <clang/Lex/Lexer.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
@ -24,7 +26,7 @@ void CodeGenerator::addDeclaration(clang::Decl *decl)
|
|||
|
||||
/*!
|
||||
* \brief Lazy initializes the source manager.
|
||||
* \remarks This method must be called in generate() when subclassing to make use of isOnlyIncluded().
|
||||
* \remarks This method must be called in generate() when subclassing to make use of isOnlyIncluded() and readAnnotation().
|
||||
*/
|
||||
void CodeGenerator::lazyInitializeSourceManager() const
|
||||
{
|
||||
|
@ -42,6 +44,21 @@ bool CodeGenerator::isOnlyIncluded(const clang::Decl *declaration) const
|
|||
&& m_sourceManager->getFileID(m_sourceManager->getExpansionLoc(declaration->getSourceRange().getBegin())) != m_sourceManager->getMainFileID();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Returns the specified \a annotation's text.
|
||||
*/
|
||||
std::string_view CodeGenerator::readAnnotation(const clang::Attr *annotation) const
|
||||
{
|
||||
if (!m_sourceManager) {
|
||||
return std::string_view();
|
||||
}
|
||||
auto text = clang::Lexer::getSourceText(sourceManager()->getExpansionRange(annotation->getRange()), *sourceManager(), clang::LangOptions());
|
||||
if (text.size() >= 12 && text.startswith("annotate(\"") && text.endswith("\")")) {
|
||||
text = text.substr(10, text.size() - 12);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Returns whether the specified \a record inherits from an instantiation of the specified \a templateClass.
|
||||
* \remarks The specified \a record must be defined (not only forward-declared).
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include <vector>
|
||||
|
||||
namespace clang {
|
||||
class Attr;
|
||||
class Decl;
|
||||
class CXXRecordDecl;
|
||||
class SourceManager;
|
||||
|
@ -32,6 +33,8 @@ protected:
|
|||
CodeFactory &factory() const;
|
||||
void lazyInitializeSourceManager() const;
|
||||
bool isOnlyIncluded(const clang::Decl *declaration) const;
|
||||
std::string_view readAnnotation(const clang::Attr *annotation) const;
|
||||
const clang::SourceManager *sourceManager() const;
|
||||
static bool inheritsFromInstantiationOf(clang::CXXRecordDecl *record, const char *templateClass);
|
||||
|
||||
private:
|
||||
|
@ -50,6 +53,11 @@ inline CodeFactory &CodeGenerator::factory() const
|
|||
return m_factory;
|
||||
}
|
||||
|
||||
inline const clang::SourceManager *CodeGenerator::sourceManager() const
|
||||
{
|
||||
return m_sourceManager;
|
||||
}
|
||||
|
||||
} // namespace ReflectiveRapidJSON
|
||||
|
||||
#endif // REFLECTIVE_RAPIDJSON_CODE_GENERATOR_H
|
||||
|
|
|
@ -67,4 +67,26 @@ struct PointerStruct : public BinarySerializable<PointerStruct> {
|
|||
std::shared_ptr<PointerTarget> s3;
|
||||
};
|
||||
|
||||
#ifdef REFLECTIVE_RAPIDJSON_GENERATOR
|
||||
#define RR_ATTR(text) __attribute__((annotate(text)))
|
||||
#define RR_V1 RR_ATTR("cond: version >= 1")
|
||||
#else
|
||||
#define RR_ATTR(text)
|
||||
#define RR_V1
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wattributes"
|
||||
#endif
|
||||
|
||||
struct AnnotatedStruct : public BinarySerializable<AnnotatedStruct> {
|
||||
int anyVersion;
|
||||
RR_V1 RR_ATTR("cond: version >= 2") RR_ATTR("foo") __attribute__((annotate("bar"))) int newInVersion1;
|
||||
};
|
||||
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
#endif // REFLECTIVE_RAPIDJSON_TESTS_MORE_STRUCTS_H
|
||||
|
|
|
@ -35,6 +35,10 @@ template <typename T> struct AdaptedBinarySerializable : public Traits::Bool<fal
|
|||
|
||||
template <typename Type> struct BinarySerializable;
|
||||
|
||||
template <typename Type> struct BinarySerializableMeta {
|
||||
static constexpr std::uint64_t version = 0;
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief The BinaryReflector namespace contains BinaryReader and BinaryWriter for automatic binary (de)serialization.
|
||||
*/
|
||||
|
|
|
@ -23,6 +23,7 @@ template <typename Type> struct BinarySerializable {
|
|||
static Type fromBinary(std::istream &inputStream);
|
||||
|
||||
static constexpr const char *qualifiedName = "ReflectiveRapidJSON::BinarySerializable";
|
||||
static constexpr auto version = BinarySerializableMeta<Type>::version;
|
||||
};
|
||||
|
||||
template <typename Type> inline void BinarySerializable<Type>::toBinary(std::ostream &outputStream) const
|
||||
|
@ -53,6 +54,14 @@ template <typename Type> Type BinarySerializable<Type>::fromBinary(std::istream
|
|||
template <> struct ReflectiveRapidJSON::AdaptedBinarySerializable<T> : Traits::Bool<true> { \
|
||||
}
|
||||
|
||||
/*!
|
||||
* \def The REFLECTIVE_RAPIDJSON_DECLARE_BINARY_SERIALIZABLE_VERSION macro allows to declare the version of a BinarySerializable.
|
||||
*/
|
||||
#define REFLECTIVE_RAPIDJSON_DECLARE_BINARY_SERIALIZABLE_VERSION(T, v) \
|
||||
template <> struct ReflectiveRapidJSON::BinarySerializableMeta<T> { \
|
||||
static constexpr std::uint64_t version = v; \
|
||||
}
|
||||
|
||||
} // namespace ReflectiveRapidJSON
|
||||
|
||||
#endif // REFLECTIVE_RAPIDJSON_BINARY_SERIALIZABLE_H
|
||||
|
|
Loading…
Reference in New Issue