From ce0497145e8a9eb837a70bb35d63803676711bbc Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 16 Jun 2020 22:36:42 +0100 Subject: Clang format and tidy --- Jamroot.jam | 31 +- slicer/Jamfile.jam | 3 +- slicer/db/Jamfile.jam | 7 +- slicer/db/sqlBinder.cpp | 7 +- slicer/db/sqlBinder.h | 44 ++- slicer/db/sqlCommon.cpp | 40 ++- slicer/db/sqlCommon.h | 3 +- slicer/db/sqlInsertSerializer.cpp | 102 ++++--- slicer/db/sqlInsertSerializer.h | 61 ++-- slicer/db/sqlSelectDeserializer.cpp | 69 ++--- slicer/db/sqlSelectDeserializer.h | 27 +- slicer/db/sqlSource.cpp | 6 +- slicer/db/sqlSource.h | 43 ++- slicer/db/sqlTablePatchSerializer.cpp | 16 +- slicer/db/sqlTablePatchSerializer.h | 17 +- slicer/db/sqlUpdateSerializer.cpp | 32 +- slicer/db/sqlUpdateSerializer.h | 23 +- slicer/db/testConversions.cpp | 8 +- slicer/db/testInsert.cpp | 65 ++-- slicer/db/testMockCommon.cpp | 11 +- slicer/db/testMockCommon.h | 13 +- slicer/db/testPatch.cpp | 23 +- slicer/db/testSelect.cpp | 87 +++--- slicer/db/testUpdate.cpp | 34 +-- slicer/ice/serializer.cpp | 30 +- slicer/ice/serializer.h | 60 ++-- slicer/ice/testSpecifics.cpp | 26 +- slicer/json/serializer.cpp | 375 ++++++++++++----------- slicer/json/serializer.h | 74 +++-- slicer/json/testSpecifics.cpp | 17 +- slicer/slicer/metadata.cpp | 3 +- slicer/slicer/metadata.h | 5 +- slicer/slicer/modelParts.cpp | 6 +- slicer/slicer/modelParts.h | 257 ++++++++-------- slicer/slicer/modelPartsTypes.cpp | 253 ++++++++++++---- slicer/slicer/modelPartsTypes.h | 455 ++++++++++++++-------------- slicer/slicer/modelPartsTypes.impl.h | 551 +++++++++++++++++++--------------- slicer/slicer/serializer.cpp | 1 - slicer/slicer/serializer.h | 25 +- slicer/slicer/slicer.cpp | 19 +- slicer/slicer/slicer.h | 19 +- slicer/test/compilation.cpp | 103 ++++--- slicer/test/conversions.cpp | 39 +-- slicer/test/conversions.h | 27 +- slicer/test/helpers.cpp | 11 +- slicer/test/helpers.h | 6 +- slicer/test/preprocessor.cpp | 34 +-- slicer/test/serializers.cpp | 341 ++++++++++----------- slicer/test/streams-mp.cpp | 3 +- slicer/test/streams.cpp | 20 +- slicer/test/streams.h | 5 +- slicer/tool/Jamfile.jam | 2 - slicer/tool/parser.cpp | 329 ++++++++++---------- slicer/tool/parser.h | 108 ++++--- slicer/tool/slicer.cpp | 5 +- slicer/xml/Jamfile.jam | 1 - slicer/xml/serializer.cpp | 315 ++++++++++--------- slicer/xml/serializer.h | 99 +++--- slicer/xml/testSpecifics.cpp | 15 +- 59 files changed, 2248 insertions(+), 2163 deletions(-) diff --git a/Jamroot.jam b/Jamroot.jam index 98e8e4c..9d1aa16 100644 --- a/Jamroot.jam +++ b/Jamroot.jam @@ -23,26 +23,33 @@ project coverage:on tidy:boost-* tidy:bugprone-* + tidy:bugprone-macro-parentheses tidy:clang-* + tidy:clang-analyzer-cplusplus.NewDeleteLeaks tidy:misc-* + tidy:misc-non-private-member-variables-in-classes tidy:modernize-* tidy:modernize-use-trailing-return-type tidy:hicpp-* + tidy:hicpp-no-array-decay + tidy:hicpp-vararg tidy:performance-* - tidy:test/bin/tidy/debug/classes.h - tidy:test/bin/tidy/debug/classtype.h - tidy:test/bin/tidy/debug/collections.h - tidy:test/bin/tidy/debug/enums.h - tidy:test/bin/tidy/debug/inheritance.h - tidy:test/bin/tidy/debug/interfaces.h - tidy:test/bin/tidy/debug/json.h - tidy:test/bin/tidy/debug/locals.h - tidy:test/bin/tidy/debug/optionals.h - tidy:test/bin/tidy/debug/structs.h - tidy:test/bin/tidy/debug/types.h - tidy:test/bin/tidy/debug/xml.h + tidy:test/bin/tidy/debug/cxxstd-17-iso/classes.h + tidy:test/bin/tidy/debug/cxxstd-17-iso/classtype.h + tidy:test/bin/tidy/debug/cxxstd-17-iso/collections.h + tidy:test/bin/tidy/debug/cxxstd-17-iso/enums.h + tidy:test/bin/tidy/debug/cxxstd-17-iso/inheritance.h + tidy:test/bin/tidy/debug/cxxstd-17-iso/interfaces.h + tidy:test/bin/tidy/debug/cxxstd-17-iso/json.h + tidy:test/bin/tidy/debug/cxxstd-17-iso/locals.h + tidy:test/bin/tidy/debug/cxxstd-17-iso/optionals.h + tidy:test/bin/tidy/debug/cxxstd-17-iso/structs.h + tidy:test/bin/tidy/debug/cxxstd-17-iso/types.h + tidy:test/bin/tidy/debug/cxxstd-17-iso/xml.h tidy:slicer/bin/common.h tidy:xml/bin/tidy/debug/cxxstd-17-iso/xmlExceptions.h + tidy:db/bin/tidy/debug/cxxstd-17-iso/sqlExceptions.h + tidy:db/bin/tidy/debug/cxxstd-17-iso/testModels.h ; build-project slicer ; diff --git a/slicer/Jamfile.jam b/slicer/Jamfile.jam index d6f47ff..387a313 100644 --- a/slicer/Jamfile.jam +++ b/slicer/Jamfile.jam @@ -8,8 +8,7 @@ build-project db ; build-project ice ; build-project test ; -lib boost_utf : : boost_unit_test_framework : : - tidy:hicpp-* ; +lib boost_utf : : boost_unit_test_framework ; lib adhocutil : : : : /usr/include/adhocutil ; lib Ice++11 ; lib pthread ; diff --git a/slicer/db/Jamfile.jam b/slicer/db/Jamfile.jam index fcfa4d3..7a5de7e 100644 --- a/slicer/db/Jamfile.jam +++ b/slicer/db/Jamfile.jam @@ -39,7 +39,6 @@ lib testCommon : : ../tool//slicer pure - ../tool//slicer ../test//types ../test//types ../test//common @@ -48,6 +47,12 @@ lib testCommon : ..//adhocutil dbicetypes dbicetypes + gcc:../tool//slicer/gcc + clang:../tool//slicer/clang + tidy:../tool//slicer/gcc + gcc:../tool//slicer/gcc + clang:../tool//slicer/clang + tidy:../tool//slicer/gcc : : ..//adhocutil dbicetypes diff --git a/slicer/db/sqlBinder.cpp b/slicer/db/sqlBinder.cpp index 6969749..8245217 100644 --- a/slicer/db/sqlBinder.cpp +++ b/slicer/db/sqlBinder.cpp @@ -1,11 +1,7 @@ #include "sqlBinder.h" namespace Slicer { - SqlBinder::SqlBinder(DB::Command & c, unsigned int i) : - command(c), - idx(i) - { - } + SqlBinder::SqlBinder(DB::Command & c, unsigned int i) : command(c), idx(i) { } void SqlBinder::get(const boost::posix_time::ptime & b) const @@ -67,4 +63,3 @@ namespace Slicer { command.bindParamS(idx, b); } } - diff --git a/slicer/db/sqlBinder.h b/slicer/db/sqlBinder.h index f7a0be6..8c1ce80 100644 --- a/slicer/db/sqlBinder.h +++ b/slicer/db/sqlBinder.h @@ -1,36 +1,34 @@ #ifndef SLICER_DB_SQLBINDER_H #define SLICER_DB_SQLBINDER_H -#include -#include #include +#include +#include namespace Slicer { - class SqlBinder : public Slicer::ValueTarget, - public Slicer::TValueTarget, - public Slicer::TValueTarget - { - public: - SqlBinder(DB::Command & c, unsigned int idx); + class SqlBinder : + public Slicer::ValueTarget, + public Slicer::TValueTarget, + public Slicer::TValueTarget { + public: + SqlBinder(DB::Command & c, unsigned int idx); - void get(const boost::posix_time::ptime & b) const override; - void get(const boost::posix_time::time_duration & b) const override; - void get(const bool & b) const override; - void get(const Ice::Byte & b) const override; - void get(const Ice::Short & b) const override; - void get(const Ice::Int & b) const override; - void get(const Ice::Long & b) const override; - void get(const Ice::Float & b) const override; - void get(const Ice::Double & b) const override; - void get(const std::string & b) const override; + void get(const boost::posix_time::ptime & b) const override; + void get(const boost::posix_time::time_duration & b) const override; + void get(const bool & b) const override; + void get(const Ice::Byte & b) const override; + void get(const Ice::Short & b) const override; + void get(const Ice::Int & b) const override; + void get(const Ice::Long & b) const override; + void get(const Ice::Float & b) const override; + void get(const Ice::Double & b) const override; + void get(const std::string & b) const override; - private: - DB::Command & command; - const unsigned int idx; + private: + DB::Command & command; + const unsigned int idx; }; typedef std::shared_ptr SqlBinderPtr; } #endif - - diff --git a/slicer/db/sqlCommon.cpp b/slicer/db/sqlCommon.cpp index 193285e..ecce6cc 100644 --- a/slicer/db/sqlCommon.cpp +++ b/slicer/db/sqlCommon.cpp @@ -1,7 +1,7 @@ -#include -#include #include "sqlCommon.h" +#include #include +#include namespace Slicer { const std::string md_pkey = "db:pkey"; @@ -9,53 +9,61 @@ namespace Slicer { const std::string md_ignore = "db:ignore"; const std::string md_global_ignore = "ignore"; - bool isPKey(const HookCommon * h) + bool + isPKey(const HookCommon * h) { return metaDataFlagSet(h->GetMetadata(), md_pkey) && isBind(h); } - bool isAuto(const HookCommon * h) + bool + isAuto(const HookCommon * h) { return metaDataFlagSet(h->GetMetadata(), md_auto) && isBind(h); } - bool isNotAuto(const HookCommon * h) + bool + isNotAuto(const HookCommon * h) { return metaDataFlagNotSet(h->GetMetadata(), md_auto) && isBind(h); } - bool isBind(const HookCommon * h) + bool + isBind(const HookCommon * h) { - return metaDataFlagNotSet(h->GetMetadata(), md_global_ignore) && - metaDataFlagNotSet(h->GetMetadata(), md_ignore); + return metaDataFlagNotSet(h->GetMetadata(), md_global_ignore) + && metaDataFlagNotSet(h->GetMetadata(), md_ignore); } - bool isValue(const HookCommon * h) + bool + isValue(const HookCommon * h) { - return metaDataFlagNotSet(h->GetMetadata(), md_auto) && - metaDataFlagNotSet(h->GetMetadata(), md_pkey) && isBind(h); + return metaDataFlagNotSet(h->GetMetadata(), md_auto) && metaDataFlagNotSet(h->GetMetadata(), md_pkey) + && isBind(h); } - void TooManyRowsReturned::ice_print(std::ostream & s) const + void + TooManyRowsReturned::ice_print(std::ostream & s) const { s << "Too many rows returned"; } - void NoRowsReturned::ice_print(std::ostream & s) const + void + NoRowsReturned::ice_print(std::ostream & s) const { s << "No rows returned"; } - void NoRowsFound::ice_print(std::ostream & s) const + void + NoRowsFound::ice_print(std::ostream & s) const { s << "No rows found"; } AdHocFormatter(UnsuitableIdFieldTypeMsg, "Unsuitable id field type [%?]"); - void UnsuitableIdFieldType::ice_print(std::ostream & s) const + void + UnsuitableIdFieldType::ice_print(std::ostream & s) const { UnsuitableIdFieldTypeMsg::write(s, type); } } - diff --git a/slicer/db/sqlCommon.h b/slicer/db/sqlCommon.h index 954800a..32afd87 100644 --- a/slicer/db/sqlCommon.h +++ b/slicer/db/sqlCommon.h @@ -1,8 +1,8 @@ #ifndef SLICER_DB_SQLCOMMON_H #define SLICER_DB_SQLCOMMON_H -#include #include +#include namespace Slicer { bool isPKey(const HookCommon *); @@ -13,4 +13,3 @@ namespace Slicer { } #endif - diff --git a/slicer/db/sqlInsertSerializer.cpp b/slicer/db/sqlInsertSerializer.cpp index dde8f1f..4061b17 100644 --- a/slicer/db/sqlInsertSerializer.cpp +++ b/slicer/db/sqlInsertSerializer.cpp @@ -1,20 +1,20 @@ -#include "sqlInsertSerializer.h" -#include -#include #include "sqlBinder.h" #include "sqlCommon.h" +#include "sqlInsertSerializer.h" +#include +#include #include +#include #include #include -#include -#include +#include +#include namespace Slicer { using namespace std::placeholders; SqlInsertSerializer::SqlInsertSerializer(DB::Connection * const c, std::string t) : - connection(c), - tableName(std::move(t)) + connection(c), tableName(std::move(t)) { } @@ -22,11 +22,15 @@ namespace Slicer { SqlInsertSerializer::Serialize(Slicer::ModelPartForRootPtr mp) { switch (mp->GetType()) { - case Slicer::mpt_Sequence: - mp->OnEachChild(std::bind(&SqlInsertSerializer::SerializeSequence, this, _2)); + case Slicer::ModelPartType::Sequence: + mp->OnEachChild([this](auto &&, auto && PH2, auto &&) { + SerializeSequence(PH2); + }); return; - case Slicer::mpt_Complex: - mp->OnEachChild(std::bind(&SqlInsertSerializer::SerializeObject, this, _2)); + case Slicer::ModelPartType::Complex: + mp->OnEachChild([this](auto &&, auto && PH2, auto &&) { + SerializeObject(PH2); + }); return; default: throw UnsupportedModelType(); @@ -45,45 +49,52 @@ namespace Slicer { { auto ins = createInsert(mp->GetContainedModelPart()); mp->OnEachChild([&ins, this](const std::string &, const ModelPartPtr & cmp, const HookCommon *) { - bindObjectAndExecute(cmp, ins.get()); - }); + bindObjectAndExecute(cmp, ins.get()); + }); } void SqlInsertSerializer::bindObjectAndExecute(const Slicer::ModelPartPtr & cmp, DB::ModifyCommand * ins) const { int paramNo = 0; - cmp->OnEachChild(std::bind(&SqlInsertSerializer::bindObjectAndExecuteField, this, std::ref(paramNo), ins, _2, _3)); + cmp->OnEachChild([this, ¶mNo, ins](auto &&, auto && PH2, auto && PH3) { + bindObjectAndExecuteField(paramNo, ins, PH2, PH3); + }); ins->execute(); } class IdSave : public Slicer::ValueSource { - public: - explicit IdSave(DB::Connection * const c) : - connection(c) - { - } - -#define NonNumType(T) \ - /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \ - void set(T &) const override { throw UnsuitableIdFieldType(#T); } + public: + explicit IdSave(DB::Connection * const c) : connection(c) { } #define NumType(T) \ - /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \ - void set(T & v) const override { v = boost::numeric_cast(connection->insertId()); } - - NonNumType(bool); - NonNumType(std::string); - - NumType(Ice::Byte); - NumType(Ice::Short); - NumType(Ice::Int); - NumType(Ice::Long); - NumType(Ice::Float); - NumType(Ice::Double); + void set(T & v) const override \ + { \ + doSet(v, #T); \ + } - private: - DB::Connection * const connection; + NumType(bool); + NumType(std::string); + NumType(Ice::Byte); + NumType(Ice::Short); + NumType(Ice::Int); + NumType(Ice::Long); + NumType(Ice::Float); + NumType(Ice::Double); + + private: + DB::Connection * const connection; + template + inline void + doSet([[maybe_unused]] T & v, [[maybe_unused]] const char * Tname) const + { + if constexpr (std::is_integral_v) { + v = boost::numeric_cast(connection->insertId()); + } + else { + throw UnsuitableIdFieldType(Tname); + } + } }; void @@ -98,7 +109,8 @@ namespace Slicer { } void - SqlInsertSerializer::bindObjectAndExecuteField(int & paramNo, DB::ModifyCommand * ins, const Slicer::ModelPartPtr & cmp, const HookCommon * h) const + SqlInsertSerializer::bindObjectAndExecuteField( + int & paramNo, DB::ModifyCommand * ins, const Slicer::ModelPartPtr & cmp, const HookCommon * h) const { if (isBind(h)) { if (!cmp->GetValue(SqlBinder(*ins, paramNo))) { @@ -109,7 +121,8 @@ namespace Slicer { } void - SqlAutoIdInsertSerializer::bindObjectAndExecuteField(int & paramNo, DB::ModifyCommand * ins, const Slicer::ModelPartPtr & cmp, const HookCommon * h) const + SqlAutoIdInsertSerializer::bindObjectAndExecuteField( + int & paramNo, DB::ModifyCommand * ins, const Slicer::ModelPartPtr & cmp, const HookCommon * h) const { if (isNotAuto(h)) { SqlInsertSerializer::bindObjectAndExecuteField(paramNo, ins, cmp, h); @@ -123,7 +136,9 @@ namespace Slicer { std::stringstream insert; "INSERT INTO %?("_fmt(insert, tableName); int fieldNo = 0; - mp->OnEachChild(std::bind(&SqlInsertSerializer::createInsertField, this, std::ref(fieldNo), std::ref(insert), _1, _3)); + mp->OnEachChild([this, &fieldNo, &insert](auto && PH1, auto &&, auto && PH3) { + createInsertField(fieldNo, insert, PH1, PH3); + }); insert << ") VALUES ("; for (; fieldNo > 1; --fieldNo) { insert << "?, "; @@ -133,7 +148,8 @@ namespace Slicer { } void - SqlInsertSerializer::createInsertField(int & fieldNo, std::ostream & insert, const std::string & name, const HookCommon * h) const + SqlInsertSerializer::createInsertField( + int & fieldNo, std::ostream & insert, const std::string & name, const HookCommon * h) const { if (isBind(h)) { if (fieldNo++) { @@ -144,7 +160,8 @@ namespace Slicer { } void - SqlAutoIdInsertSerializer::createInsertField(int & fieldNo, std::ostream & insert, const std::string & name, const HookCommon * h) const + SqlAutoIdInsertSerializer::createInsertField( + int & fieldNo, std::ostream & insert, const std::string & name, const HookCommon * h) const { if (isNotAuto(h)) { if (fieldNo++) { @@ -154,4 +171,3 @@ namespace Slicer { } } } - diff --git a/slicer/db/sqlInsertSerializer.h b/slicer/db/sqlInsertSerializer.h index 175706f..6150d66 100644 --- a/slicer/db/sqlInsertSerializer.h +++ b/slicer/db/sqlInsertSerializer.h @@ -1,49 +1,50 @@ #ifndef SLICER_DB_SQLINSERTSERIALIZER_H #define SLICER_DB_SQLINSERTSERIALIZER_H -#include #include -#include #include +#include +#include namespace Slicer { class DLL_PUBLIC SqlInsertSerializer : public Slicer::Serializer { - public: - SqlInsertSerializer(DB::Connection * const, std::string tableName); - - void Serialize(Slicer::ModelPartForRootPtr) override; - - protected: - void SerializeObject(const Slicer::ModelPartPtr &) const; - void SerializeSequence(const Slicer::ModelPartPtr &) const; - DB::ModifyCommandPtr createInsert(const Slicer::ModelPartPtr &) const; - virtual void createInsertField(int & fieldNo, std::ostream & insert, const std::string & name, const HookCommon * h) const; - virtual void bindObjectAndExecute(const Slicer::ModelPartPtr &, DB::ModifyCommand *) const; - virtual void bindObjectAndExecuteField(int & paramNo, DB::ModifyCommand *, const Slicer::ModelPartPtr &, const HookCommon *) const; - - DB::Connection * const connection; - const std::string tableName; + public: + SqlInsertSerializer(DB::Connection * const, std::string tableName); + + void Serialize(Slicer::ModelPartForRootPtr) override; + + protected: + void SerializeObject(const Slicer::ModelPartPtr &) const; + void SerializeSequence(const Slicer::ModelPartPtr &) const; + DB::ModifyCommandPtr createInsert(const Slicer::ModelPartPtr &) const; + virtual void createInsertField( + int & fieldNo, std::ostream & insert, const std::string & name, const HookCommon * h) const; + virtual void bindObjectAndExecute(const Slicer::ModelPartPtr &, DB::ModifyCommand *) const; + virtual void bindObjectAndExecuteField( + int & paramNo, DB::ModifyCommand *, const Slicer::ModelPartPtr &, const HookCommon *) const; + + DB::Connection * const connection; + const std::string tableName; }; class DLL_PUBLIC SqlAutoIdInsertSerializer : public SqlInsertSerializer { - public: - template - SqlAutoIdInsertSerializer(const P & ... p) : SqlInsertSerializer(p...) { } - - protected: - virtual void createInsertField(int & fieldNo, std::ostream & insert, const std::string & name, const HookCommon * h) const; - virtual void bindObjectAndExecuteField(int & paramNo, DB::ModifyCommand *, const Slicer::ModelPartPtr &, const HookCommon *) const; + public: + template SqlAutoIdInsertSerializer(const P &... p) : SqlInsertSerializer(p...) { } + + protected: + virtual void createInsertField( + int & fieldNo, std::ostream & insert, const std::string & name, const HookCommon * h) const; + virtual void bindObjectAndExecuteField( + int & paramNo, DB::ModifyCommand *, const Slicer::ModelPartPtr &, const HookCommon *) const; }; class DLL_PUBLIC SqlFetchIdInsertSerializer : public SqlAutoIdInsertSerializer { - public: - template - SqlFetchIdInsertSerializer(const P & ... p) : SqlAutoIdInsertSerializer(p...) { } + public: + template SqlFetchIdInsertSerializer(const P &... p) : SqlAutoIdInsertSerializer(p...) { } - protected: - virtual void bindObjectAndExecute(const Slicer::ModelPartPtr &, DB::ModifyCommand *) const; + protected: + virtual void bindObjectAndExecute(const Slicer::ModelPartPtr &, DB::ModifyCommand *) const; }; } #endif - diff --git a/slicer/db/sqlSelectDeserializer.cpp b/slicer/db/sqlSelectDeserializer.cpp index ff16c43..8982958 100644 --- a/slicer/db/sqlSelectDeserializer.cpp +++ b/slicer/db/sqlSelectDeserializer.cpp @@ -1,13 +1,11 @@ #include "sqlSelectDeserializer.h" #include "sqlSource.h" -#include #include +#include namespace Slicer { SqlSelectDeserializer::SqlSelectDeserializer(DB::SelectCommand * c, Ice::optional tc) : - cmd(c), - columnCount(0), - typeIdColName(std::move(tc)) + cmd(c), columnCount(0), typeIdColName(std::move(tc)) { } @@ -20,13 +18,13 @@ namespace Slicer { typeIdColIdx = cmd->getOrdinal(*typeIdColName); } switch (mp->GetType()) { - case Slicer::mpt_Sequence: + case Slicer::ModelPartType::Sequence: DeserializeSequence(mp); return; - case Slicer::mpt_Complex: + case Slicer::ModelPartType::Complex: DeserializeObject(mp); return; - case Slicer::mpt_Simple: + case Slicer::ModelPartType::Simple: DeserializeSimple(mp); return; default: @@ -74,7 +72,7 @@ namespace Slicer { } DeserializeRow(mp); if (cmd->fetch()) { - while (cmd->fetch()) {} + while (cmd->fetch()) { } throw TooManyRowsReturned(); } } @@ -85,43 +83,38 @@ namespace Slicer { auto rmp = mp->GetAnonChild(); if (rmp) { switch (rmp->GetType()) { - case Slicer::mpt_Complex: - { - if (typeIdColIdx) { - std::string subclass; - (*cmd)[*typeIdColIdx] >> subclass; - rmp = rmp->GetSubclassModelPart(subclass); - } - rmp->Create(); - for (auto col = 0U; col < columnCount; col += 1) { - const DB::Column & c = (*cmd)[col]; - if (!c.isNull()) { - auto fmpr = rmp->GetChildRef(c.name, nullptr, false); - if (fmpr) { - auto fmp = fmpr.Child(); - fmp->Create(); - fmp->SetValue(SqlSource(c)); - fmp->Complete(); - } - } - } - rmp->Complete(); + case Slicer::ModelPartType::Complex: { + if (typeIdColIdx) { + std::string subclass; + (*cmd)[*typeIdColIdx] >> subclass; + rmp = rmp->GetSubclassModelPart(subclass); } - break; - case Slicer::mpt_Simple: - { - rmp->Create(); - const DB::Column & c = (*cmd)[0]; + rmp->Create(); + for (auto col = 0U; col < columnCount; col += 1) { + const DB::Column & c = (*cmd)[col]; if (!c.isNull()) { - rmp->SetValue(SqlSource(c)); + auto fmpr = rmp->GetChildRef(c.name, nullptr, false); + if (fmpr) { + auto fmp = fmpr.Child(); + fmp->Create(); + fmp->SetValue(SqlSource(c)); + fmp->Complete(); + } } - rmp->Complete(); } - break; + rmp->Complete(); + } break; + case Slicer::ModelPartType::Simple: { + rmp->Create(); + const DB::Column & c = (*cmd)[0]; + if (!c.isNull()) { + rmp->SetValue(SqlSource(c)); + } + rmp->Complete(); + } break; default: throw UnsupportedModelType(); } } } } - diff --git a/slicer/db/sqlSelectDeserializer.h b/slicer/db/sqlSelectDeserializer.h index e9afd1d..5f34ef4 100644 --- a/slicer/db/sqlSelectDeserializer.h +++ b/slicer/db/sqlSelectDeserializer.h @@ -1,29 +1,28 @@ #ifndef SLICER_DB_SQLSELECTDESERIALIZER_H #define SLICER_DB_SQLSELECTDESERIALIZER_H -#include #include +#include #include namespace Slicer { class DLL_PUBLIC SqlSelectDeserializer : public Slicer::Deserializer { - public: - SqlSelectDeserializer(DB::SelectCommand *, Ice::optional typeIdCol = Ice::optional()); + public: + SqlSelectDeserializer(DB::SelectCommand *, Ice::optional typeIdCol = Ice::optional()); - void Deserialize(Slicer::ModelPartForRootPtr) override; + void Deserialize(Slicer::ModelPartForRootPtr) override; - protected: - void DLL_PRIVATE DeserializeSimple(const Slicer::ModelPartPtr &); - void DLL_PRIVATE DeserializeObject(const Slicer::ModelPartPtr &); - void DLL_PRIVATE DeserializeSequence(const Slicer::ModelPartPtr &); - void DLL_PRIVATE DeserializeRow(const Slicer::ModelPartPtr &); + protected: + void DLL_PRIVATE DeserializeSimple(const Slicer::ModelPartPtr &); + void DLL_PRIVATE DeserializeObject(const Slicer::ModelPartPtr &); + void DLL_PRIVATE DeserializeSequence(const Slicer::ModelPartPtr &); + void DLL_PRIVATE DeserializeRow(const Slicer::ModelPartPtr &); - DB::SelectCommand * cmd; - unsigned int columnCount; - Ice::optional typeIdColName; - Ice::optional typeIdColIdx; + DB::SelectCommand * cmd; + unsigned int columnCount; + Ice::optional typeIdColName; + Ice::optional typeIdColIdx; }; } #endif - diff --git a/slicer/db/sqlSource.cpp b/slicer/db/sqlSource.cpp index eeea99c..84964f2 100644 --- a/slicer/db/sqlSource.cpp +++ b/slicer/db/sqlSource.cpp @@ -2,10 +2,7 @@ #include namespace Slicer { - SqlSource::SqlSource(const DB::Column & c) : - column(c) - { - } + SqlSource::SqlSource(const DB::Column & c) : column(c) { } bool SqlSource::isNull() const @@ -83,4 +80,3 @@ namespace Slicer { column >> b; } } - diff --git a/slicer/db/sqlSource.h b/slicer/db/sqlSource.h index 99ffabb..548f35e 100644 --- a/slicer/db/sqlSource.h +++ b/slicer/db/sqlSource.h @@ -1,35 +1,34 @@ #ifndef SLICER_DB_SQLSOURCE_H #define SLICER_DB_SQLSOURCE_H -#include -#include #include +#include +#include namespace Slicer { - class SqlSource : public Slicer::ValueSource, - public Slicer::TValueSource, - public Slicer::TValueSource - { - public: - SqlSource(const DB::Column & c); + class SqlSource : + public Slicer::ValueSource, + public Slicer::TValueSource, + public Slicer::TValueSource { + public: + SqlSource(const DB::Column & c); - bool isNull() const; - void set(boost::posix_time::ptime & b) const override; - void set(boost::posix_time::time_duration & b) const override; - void set(bool & b) const override; - void set(Ice::Byte & b) const override; - void set(Ice::Short & b) const override; - void set(Ice::Int & b) const override; - void set(Ice::Long & b) const override; - void set(Ice::Float & b) const override; - void set(Ice::Double & b) const override; - void set(std::string & b) const override; + bool isNull() const; + void set(boost::posix_time::ptime & b) const override; + void set(boost::posix_time::time_duration & b) const override; + void set(bool & b) const override; + void set(Ice::Byte & b) const override; + void set(Ice::Short & b) const override; + void set(Ice::Int & b) const override; + void set(Ice::Long & b) const override; + void set(Ice::Float & b) const override; + void set(Ice::Double & b) const override; + void set(std::string & b) const override; - private: - const DB::Column & column; + private: + const DB::Column & column; }; typedef std::shared_ptr SqlSourcePtr; } #endif - diff --git a/slicer/db/sqlTablePatchSerializer.cpp b/slicer/db/sqlTablePatchSerializer.cpp index ec5b59b..7c29be5 100644 --- a/slicer/db/sqlTablePatchSerializer.cpp +++ b/slicer/db/sqlTablePatchSerializer.cpp @@ -1,16 +1,15 @@ -#include "sqlTablePatchSerializer.h" -#include "sqlInsertSerializer.h" #include "sqlCommon.h" -#include +#include "sqlInsertSerializer.h" +#include "sqlTablePatchSerializer.h" #include -#include #include +#include +#include namespace Slicer { AdHocFormatter(ttname, "slicer_tmp_%?"); SqlTablePatchSerializer::SqlTablePatchSerializer(DB::Connection * const db, DB::TablePatch & tp) : - db(db), - tablePatch(tp) + db(db), tablePatch(tp) { tablePatch.src = ttname::get(this); } @@ -22,7 +21,9 @@ namespace Slicer { tablePatch.cols.clear(); createTemporaryTable(); - AdHoc::ScopeExit tidy(std::bind(&SqlTablePatchSerializer::dropTemporaryTable, this)); + AdHoc::ScopeExit tidy([this] { + dropTemporaryTable(); + }); SqlInsertSerializer ins(db, tablePatch.src); ins.Serialize(mpr); @@ -56,4 +57,3 @@ namespace Slicer { db->execute(dropTmpTable::get(tablePatch.src)); } } - diff --git a/slicer/db/sqlTablePatchSerializer.h b/slicer/db/sqlTablePatchSerializer.h index 00ea328..9a82f85 100644 --- a/slicer/db/sqlTablePatchSerializer.h +++ b/slicer/db/sqlTablePatchSerializer.h @@ -6,19 +6,18 @@ namespace Slicer { class DLL_PUBLIC SqlTablePatchSerializer : public Slicer::Serializer { - public: - SqlTablePatchSerializer(DB::Connection * const, DB::TablePatch &); + public: + SqlTablePatchSerializer(DB::Connection * const, DB::TablePatch &); - void Serialize(Slicer::ModelPartForRootPtr) override; + void Serialize(Slicer::ModelPartForRootPtr) override; - private: - void createTemporaryTable(); - void dropTemporaryTable(); + private: + void createTemporaryTable(); + void dropTemporaryTable(); - DB::Connection * const db; - DB::TablePatch & tablePatch; + DB::Connection * const db; + DB::TablePatch & tablePatch; }; } #endif - diff --git a/slicer/db/sqlUpdateSerializer.cpp b/slicer/db/sqlUpdateSerializer.cpp index 64ff1a0..29d9da7 100644 --- a/slicer/db/sqlUpdateSerializer.cpp +++ b/slicer/db/sqlUpdateSerializer.cpp @@ -1,19 +1,18 @@ -#include "sqlUpdateSerializer.h" -#include -#include #include "sqlBinder.h" #include "sqlCommon.h" +#include "sqlUpdateSerializer.h" +#include #include +#include #include #include -#include +#include namespace Slicer { using namespace std::placeholders; SqlUpdateSerializer::SqlUpdateSerializer(DB::Connection * const c, std::string t) : - connection(c), - tableName(std::move(t)) + connection(c), tableName(std::move(t)) { } @@ -21,11 +20,15 @@ namespace Slicer { SqlUpdateSerializer::Serialize(Slicer::ModelPartForRootPtr mp) { switch (mp->GetType()) { - case Slicer::mpt_Sequence: - mp->OnEachChild(std::bind(&SqlUpdateSerializer::SerializeSequence, this, _2)); + case Slicer::ModelPartType::Sequence: + mp->OnEachChild([this](auto &&, auto && PH2, auto &&) { + SerializeSequence(PH2); + }); return; - case Slicer::mpt_Complex: - mp->OnEachChild(std::bind(&SqlUpdateSerializer::SerializeObject, this, _2)); + case Slicer::ModelPartType::Complex: + mp->OnEachChild([this](auto &&, auto && PH2, auto &&) { + SerializeObject(PH2); + }); return; default: throw UnsupportedModelType(); @@ -44,8 +47,8 @@ namespace Slicer { { auto ins = createUpdate(mp->GetContainedModelPart()); mp->OnEachChild([&ins](const std::string &, const ModelPartPtr & cmp, const HookCommon *) { - bindObjectAndExecute(cmp, ins.get()); - }); + bindObjectAndExecute(cmp, ins.get()); + }); } void @@ -77,7 +80,7 @@ namespace Slicer { std::stringstream update; "UPDATE %? SET "_fmt(update, tableName); int fieldNo = 0; - mp->OnEachChild([&update, &fieldNo]( const std::string & name, const ModelPartPtr &, const HookCommon * h) { + mp->OnEachChild([&update, &fieldNo](const std::string & name, const ModelPartPtr &, const HookCommon * h) { if (isValue(h)) { if (fieldNo++) { update << ", "; @@ -87,7 +90,7 @@ namespace Slicer { }); update << " WHERE "; fieldNo = 0; - mp->OnEachChild([&update, &fieldNo]( const std::string & name, const ModelPartPtr &, const HookCommon * h) { + mp->OnEachChild([&update, &fieldNo](const std::string & name, const ModelPartPtr &, const HookCommon * h) { if (isPKey(h)) { if (fieldNo++) { update << " AND "; @@ -98,4 +101,3 @@ namespace Slicer { return connection->modify(update.str()); } } - diff --git a/slicer/db/sqlUpdateSerializer.h b/slicer/db/sqlUpdateSerializer.h index a3462f8..e82015b 100644 --- a/slicer/db/sqlUpdateSerializer.h +++ b/slicer/db/sqlUpdateSerializer.h @@ -1,27 +1,26 @@ #ifndef SLICER_DB_SQLUPDATESERIALIZER_H #define SLICER_DB_SQLUPDATESERIALIZER_H -#include #include +#include #include namespace Slicer { class DLL_PUBLIC SqlUpdateSerializer : public Slicer::Serializer { - public: - SqlUpdateSerializer(DB::Connection * const, std::string tableName); + public: + SqlUpdateSerializer(DB::Connection * const, std::string tableName); - void Serialize(Slicer::ModelPartForRootPtr) override; + void Serialize(Slicer::ModelPartForRootPtr) override; - protected: - void SerializeObject(const Slicer::ModelPartPtr &) const; - void SerializeSequence(const Slicer::ModelPartPtr &) const; - DB::ModifyCommandPtr createUpdate(const Slicer::ModelPartPtr &) const; - static void bindObjectAndExecute(const Slicer::ModelPartPtr &, DB::ModifyCommand *); + protected: + void SerializeObject(const Slicer::ModelPartPtr &) const; + void SerializeSequence(const Slicer::ModelPartPtr &) const; + DB::ModifyCommandPtr createUpdate(const Slicer::ModelPartPtr &) const; + static void bindObjectAndExecute(const Slicer::ModelPartPtr &, DB::ModifyCommand *); - DB::Connection * const connection; - const std::string tableName; + DB::Connection * const connection; + const std::string tableName; }; } #endif - diff --git a/slicer/db/testConversions.cpp b/slicer/db/testConversions.cpp index d19f309..68de17a 100644 --- a/slicer/db/testConversions.cpp +++ b/slicer/db/testConversions.cpp @@ -1,16 +1,17 @@ -#include #include #include +#include #include -#define SHORT(x) boost::numeric_cast< ::Ice::Short , int64_t >(x) +#define SHORT(x) boost::numeric_cast<::Ice::Short, int64_t>(x) namespace Slicer { DLL_PUBLIC ::TestDatabase::TimespanPtr timedurationToTimespan(const boost::posix_time::time_duration & td) { - return std::make_shared<::TestDatabase::Timespan>(SHORT(td.hours() / 24), SHORT(td.hours() % 24), SHORT(td.minutes()), SHORT(td.seconds())); + return std::make_shared<::TestDatabase::Timespan>( + SHORT(td.hours() / 24), SHORT(td.hours() % 24), SHORT(td.minutes()), SHORT(td.seconds())); } DLL_PUBLIC @@ -20,4 +21,3 @@ namespace Slicer { return boost::posix_time::time_duration((ts->days * 24) + ts->hours, ts->minutes, ts->seconds); } } - diff --git a/slicer/db/testInsert.cpp b/slicer/db/testInsert.cpp index 3724679..6bc6acd 100644 --- a/slicer/db/testInsert.cpp +++ b/slicer/db/testInsert.cpp @@ -1,13 +1,13 @@ #define BOOST_TEST_MODULE db_insert -#include -#include -#include "testMockCommon.h" -#include #include "sqlInsertSerializer.h" #include "sqlSelectDeserializer.h" -#include +#include "testMockCommon.h" +#include +#include #include +#include #include +#include using namespace std::literals; @@ -19,16 +19,18 @@ BOOST_TEST_DONT_PRINT_LOG_VALUE(TestDatabase::Timespan); namespace std { template - ostream & operator<<(ostream & s, const Ice::optional &) { + ostream & + operator<<(ostream & s, const Ice::optional &) + { return s; } } -BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); +BOOST_GLOBAL_FIXTURE(StandardMockDatabase); BOOST_FIXTURE_TEST_SUITE(db, ConnectionFixture); -BOOST_AUTO_TEST_CASE( insert_builtins ) +BOOST_AUTO_TEST_CASE(insert_builtins) { TestModule::BuiltInsPtr bi = std::make_shared(true, 4, 16, 64, 128, 1.2, 3.4, "text"); Slicer::SerializeAny(bi, db, "builtins"); @@ -44,12 +46,10 @@ BOOST_AUTO_TEST_CASE( insert_builtins ) BOOST_REQUIRE_EQUAL(bi->mstring, bi2->mstring); } -BOOST_AUTO_TEST_CASE( insert_seq_builtins ) +BOOST_AUTO_TEST_CASE(insert_seq_builtins) { - TestModule::BuiltInSeq bis = { - std::make_shared(true, 5, 17, 65, 129, 2.3, 4.5, "more text"), - std::make_shared(true, 6, 18, 66, 130, 3.4, 5.6, "even more text") - }; + TestModule::BuiltInSeq bis = {std::make_shared(true, 5, 17, 65, 129, 2.3, 4.5, "more text"), + std::make_shared(true, 6, 18, 66, 130, 3.4, 5.6, "even more text")}; Slicer::SerializeAny(bis, db, "builtins"); auto sel = db->select("SELECT * FROM builtins ORDER BY mint"); auto bis2 = Slicer::DeserializeAny(sel.get()); @@ -64,12 +64,10 @@ BOOST_AUTO_TEST_CASE( insert_seq_builtins ) BOOST_REQUIRE_EQUAL(bis.back()->mstring, bis2.back()->mstring); } -BOOST_AUTO_TEST_CASE( autoinsert_seq_builtins ) +BOOST_AUTO_TEST_CASE(autoinsert_seq_builtins) { - TestModule::BuiltInSeq bis = { - std::make_shared(true, 5, 17, 0, 129, 2.3, 4.5, "more text"), - std::make_shared(true, 6, 18, 0, 130, 3.4, 5.6, "even more text") - }; + TestModule::BuiltInSeq bis = {std::make_shared(true, 5, 17, 0, 129, 2.3, 4.5, "more text"), + std::make_shared(true, 6, 18, 0, 130, 3.4, 5.6, "even more text")}; Slicer::SerializeAny(bis, db, "builtins"); auto sel = db->select("SELECT * FROM builtins WHERE mint IN (1, 2) ORDER BY mint"); auto bis2 = Slicer::DeserializeAny(sel.get()); @@ -87,12 +85,10 @@ BOOST_AUTO_TEST_CASE( autoinsert_seq_builtins ) BOOST_REQUIRE_EQUAL(bis.back()->mstring, bis2.back()->mstring); } -BOOST_AUTO_TEST_CASE( fetchinsert_seq_builtins ) +BOOST_AUTO_TEST_CASE(fetchinsert_seq_builtins) { - TestModule::BuiltInSeq bis = { - std::make_shared(true, 5, 17, 0, 129, 2.3, 4.5, "more text"), - std::make_shared(true, 6, 18, 0, 130, 3.4, 5.6, "even more text") - }; + TestModule::BuiltInSeq bis = {std::make_shared(true, 5, 17, 0, 129, 2.3, 4.5, "more text"), + std::make_shared(true, 6, 18, 0, 130, 3.4, 5.6, "even more text")}; Slicer::SerializeAny(bis, db, "builtins"); auto sel = db->select("SELECT * FROM builtins WHERE mint IN (3, 4) ORDER BY mint"); auto bis2 = Slicer::DeserializeAny(sel.get()); @@ -110,12 +106,11 @@ BOOST_AUTO_TEST_CASE( fetchinsert_seq_builtins ) BOOST_REQUIRE_EQUAL(bis.back()->mstring, bis2.back()->mstring); } -BOOST_AUTO_TEST_CASE( fetchinsert_seq_builtinsWithNulls ) +BOOST_AUTO_TEST_CASE(fetchinsert_seq_builtinsWithNulls) { TestDatabase::BuiltInSeq bis = { - std::make_shared(true, IceUtil::None, 17, 0, 129, 2.3, 4.5, "more text"s), - std::make_shared(true, 6, 18, 0, 130, 3.4, IceUtil::None, "even more text"s) - }; + std::make_shared(true, IceUtil::None, 17, 0, 129, 2.3, 4.5, "more text"s), + std::make_shared(true, 6, 18, 0, 130, 3.4, IceUtil::None, "even more text"s)}; Slicer::SerializeAny(bis, db, "builtins"); auto sel = db->select("SELECT * FROM builtins WHERE mint IN (5, 6) ORDER BY mint"); auto bis2 = Slicer::DeserializeAny(sel.get()); @@ -133,13 +128,11 @@ BOOST_AUTO_TEST_CASE( fetchinsert_seq_builtinsWithNulls ) BOOST_REQUIRE_EQUAL(bis.back()->mstring, bis2.back()->mstring); } -BOOST_AUTO_TEST_CASE( insert_converted ) +BOOST_AUTO_TEST_CASE(insert_converted) { - TestDatabase::SpecificTypesPtr st = std::make_shared( - TestModule::DateTime {2015, 10, 16, 19, 12, 34}, - TestModule::IsoDate {2015, 10, 16}, - std::make_shared(1, 2, 3, 4) - ); + TestDatabase::SpecificTypesPtr st + = std::make_shared(TestModule::DateTime {2015, 10, 16, 19, 12, 34}, + TestModule::IsoDate {2015, 10, 16}, std::make_shared(1, 2, 3, 4)); Slicer::SerializeAny(st, db, "converted"); auto sel = db->select("SELECT * FROM converted"); auto st2 = Slicer::DeserializeAny(sel.get()); @@ -151,11 +144,11 @@ BOOST_AUTO_TEST_CASE( insert_converted ) BOOST_REQUIRE_EQUAL(st->ts->seconds, st2->ts->seconds); } -BOOST_AUTO_TEST_CASE( insert_unsupportedModel ) +BOOST_AUTO_TEST_CASE(insert_unsupportedModel) { TestModule::ClassMap cm; - BOOST_REQUIRE_THROW(Slicer::SerializeAny(cm, db, "converted"), Slicer::UnsupportedModelType); + BOOST_REQUIRE_THROW( + Slicer::SerializeAny(cm, db, "converted"), Slicer::UnsupportedModelType); } BOOST_AUTO_TEST_SUITE_END(); - diff --git a/slicer/db/testMockCommon.cpp b/slicer/db/testMockCommon.cpp index ed18563..0d8c2c9 100644 --- a/slicer/db/testMockCommon.cpp +++ b/slicer/db/testMockCommon.cpp @@ -2,15 +2,8 @@ #include StandardMockDatabase::StandardMockDatabase() : -DB::PluginMock("pqmock", { - rootDir.parent_path() / "db" / "slicer.sql" }, - "user=postgres dbname=postgres") -{ -} - -ConnectionFixture::ConnectionFixture() : - _db(DB::MockDatabase::openConnectionTo("pqmock")), - db(_db.get()) + DB::PluginMock("pqmock", {rootDir.parent_path() / "db" / "slicer.sql"}, "user=postgres dbname=postgres") { } +ConnectionFixture::ConnectionFixture() : _db(DB::MockDatabase::openConnectionTo("pqmock")), db(_db.get()) { } diff --git a/slicer/db/testMockCommon.h b/slicer/db/testMockCommon.h index bd9b11f..c775ae0 100644 --- a/slicer/db/testMockCommon.h +++ b/slicer/db/testMockCommon.h @@ -6,17 +6,16 @@ #include class DLL_PUBLIC StandardMockDatabase : public DB::PluginMock { - public: - StandardMockDatabase(); +public: + StandardMockDatabase(); }; class DLL_PUBLIC ConnectionFixture { - public: - ConnectionFixture(); +public: + ConnectionFixture(); - DB::ConnectionPtr _db; - DB::Connection * const db; + DB::ConnectionPtr _db; + DB::Connection * const db; }; #endif - diff --git a/slicer/db/testPatch.cpp b/slicer/db/testPatch.cpp index 2af60b4..70068e2 100644 --- a/slicer/db/testPatch.cpp +++ b/slicer/db/testPatch.cpp @@ -1,13 +1,13 @@ #define BOOST_TEST_MODULE db_patch -#include -#include -#include "testMockCommon.h" -#include -#include "sqlTablePatchSerializer.h" #include "sqlSelectDeserializer.h" -#include +#include "sqlTablePatchSerializer.h" +#include "testMockCommon.h" +#include +#include #include +#include #include +#include // LCOV_EXCL_START BOOST_TEST_DONT_PRINT_LOG_VALUE(TestModule::DateTime); @@ -16,16 +16,14 @@ BOOST_TEST_DONT_PRINT_LOG_VALUE(TestDatabase::Timespan); BOOST_TEST_DONT_PRINT_LOG_VALUE(DB::PrimaryKey); // LCOV_EXCL_STOP -BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); +BOOST_GLOBAL_FIXTURE(StandardMockDatabase); BOOST_FIXTURE_TEST_SUITE(db, ConnectionFixture); -BOOST_AUTO_TEST_CASE( insert_builtins ) +BOOST_AUTO_TEST_CASE(insert_builtins) { - TestModule::BuiltInSeq bis = { - std::make_shared(true, 5, 17, 0, 129, 2.3, 4.5, "more text"), - std::make_shared(true, 6, 18, 0, 130, 3.4, 5.6, "even more text") - }; + TestModule::BuiltInSeq bis = {std::make_shared(true, 5, 17, 0, 129, 2.3, 4.5, "more text"), + std::make_shared(true, 6, 18, 0, 130, 3.4, 5.6, "even more text")}; DB::TablePatch tp; DB::TransactionScope tx(*db); tp.dest = "builtins"; @@ -42,4 +40,3 @@ BOOST_AUTO_TEST_CASE( insert_builtins ) } BOOST_AUTO_TEST_SUITE_END(); - diff --git a/slicer/db/testSelect.cpp b/slicer/db/testSelect.cpp index ce7821c..0e765f2 100644 --- a/slicer/db/testSelect.cpp +++ b/slicer/db/testSelect.cpp @@ -1,59 +1,58 @@ #define BOOST_TEST_MODULE db_select -#include -#include -#include "testMockCommon.h" -#include #include "sqlSelectDeserializer.h" -#include +#include "testMockCommon.h" +#include +#include #include -#include +#include #include +#include +#include using namespace std::literals; -BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); +BOOST_GLOBAL_FIXTURE(StandardMockDatabase); BOOST_FIXTURE_TEST_SUITE(db, ConnectionFixture); -BOOST_AUTO_TEST_CASE( select_simple_int ) +BOOST_AUTO_TEST_CASE(select_simple_int) { auto sel = db->select("SELECT MAX(id) FROM test"); auto bi = Slicer::DeserializeAny(sel.get()); BOOST_REQUIRE_EQUAL(4, bi); } -BOOST_AUTO_TEST_CASE( select_simple_double ) +BOOST_AUTO_TEST_CASE(select_simple_double) { auto sel = db->select("SELECT MAX(fl) FROM test"); auto bi = Slicer::DeserializeAny(sel.get()); BOOST_REQUIRE_CLOSE(1234.1234, bi, 0.0001); } -BOOST_AUTO_TEST_CASE( select_simple_string ) +BOOST_AUTO_TEST_CASE(select_simple_string) { auto sel = db->select("SELECT MAX(string) FROM test"); auto bi = Slicer::DeserializeAny(sel.get()); BOOST_REQUIRE_EQUAL("text two", bi); } -BOOST_AUTO_TEST_CASE( select_simple_true ) +BOOST_AUTO_TEST_CASE(select_simple_true) { auto sel = db->select("SELECT true"); auto bi = Slicer::DeserializeAny(sel.get()); BOOST_REQUIRE_EQUAL(true, bi); } -BOOST_AUTO_TEST_CASE( select_simple_false ) +BOOST_AUTO_TEST_CASE(select_simple_false) { auto sel = db->select("SELECT NOT(true)"); auto bi = Slicer::DeserializeAny(sel.get()); BOOST_REQUIRE_EQUAL(false, bi); } -BOOST_AUTO_TEST_CASE( select_single ) +BOOST_AUTO_TEST_CASE(select_single) { - auto sel = db->select( - "SELECT boolean mbool, \ + auto sel = db->select("SELECT boolean mbool, \ id mbyte, id mshort, id mint, id mlong, \ fl mdouble, fl mfloat, \ string mstring \ @@ -72,10 +71,9 @@ BOOST_AUTO_TEST_CASE( select_single ) BOOST_REQUIRE_EQUAL("text one", bi->mstring); } -BOOST_AUTO_TEST_CASE( select_inherit_single ) +BOOST_AUTO_TEST_CASE(select_inherit_single) { - auto sel = db->select( - "SELECT id a, '::TestModule::D' || CAST(id AS TEXT) tc, 200 b, 300 c, 400 d \ + auto sel = db->select("SELECT id a, '::TestModule::D' || CAST(id AS TEXT) tc, 200 b, 300 c, 400 d \ FROM test \ WHERE id = 2"); auto bi = Slicer::DeserializeAny(sel.get(), "tc"s); @@ -86,10 +84,9 @@ BOOST_AUTO_TEST_CASE( select_inherit_single ) BOOST_REQUIRE_EQUAL(300, d2->c); } -BOOST_AUTO_TEST_CASE( select_simple_sequence ) +BOOST_AUTO_TEST_CASE(select_simple_sequence) { - auto sel = db->select( - "SELECT string \ + auto sel = db->select("SELECT string \ FROM test \ ORDER BY id DESC"); auto bi = Slicer::DeserializeAny(sel.get()); @@ -100,10 +97,9 @@ BOOST_AUTO_TEST_CASE( select_simple_sequence ) BOOST_REQUIRE_EQUAL("text one", bi[3]); } -BOOST_AUTO_TEST_CASE( select_inherit_sequence ) +BOOST_AUTO_TEST_CASE(select_inherit_sequence) { - auto sel = db->select( - "SELECT id a, '::TestModule::D' || CAST(id AS TEXT) tc, 200 b, 300 c, 400 d \ + auto sel = db->select("SELECT id a, '::TestModule::D' || CAST(id AS TEXT) tc, 200 b, 300 c, 400 d \ FROM test \ WHERE id < 4 \ ORDER BY id DESC"); @@ -124,13 +120,13 @@ BOOST_AUTO_TEST_CASE( select_inherit_sequence ) BOOST_REQUIRE_EQUAL(200, d1->b); } -BOOST_AUTO_TEST_CASE( select_inherit_datetime ) +BOOST_AUTO_TEST_CASE(select_inherit_datetime) { - auto sel = db->select( - "SELECT dt, to_char(dt, 'YYYY-MM-DD') date, ts \ + auto sel = db->select("SELECT dt, to_char(dt, 'YYYY-MM-DD') date, ts \ FROM test \ WHERE id = 3"); - TestDatabase::SpecificTypesPtr bi = Slicer::DeserializeAny(sel.get()); + TestDatabase::SpecificTypesPtr bi + = Slicer::DeserializeAny(sel.get()); BOOST_REQUIRE_EQUAL(2015, bi->dt.year); BOOST_REQUIRE_EQUAL(3, bi->dt.month); BOOST_REQUIRE_EQUAL(27, bi->dt.day); @@ -146,45 +142,45 @@ BOOST_AUTO_TEST_CASE( select_inherit_datetime ) BOOST_REQUIRE_EQUAL(12, bi->ts->seconds); } -template +template T -BoostThrowWrapperHelper(P && ... p) +BoostThrowWrapperHelper(P &&... p) { return Slicer::DeserializeAny(p...); } -BOOST_AUTO_TEST_CASE( select_unsupportedModel ) +BOOST_AUTO_TEST_CASE(select_unsupportedModel) { auto sel = db->select("SELECT id FROM test"); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(sel.get()), Slicer::UnsupportedModelType); } -BOOST_AUTO_TEST_CASE( select_tooManyRowsSimple ) +BOOST_AUTO_TEST_CASE(select_tooManyRowsSimple) { auto sel = db->select("SELECT id FROM test"); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(sel.get()), Slicer::TooManyRowsReturned); } -BOOST_AUTO_TEST_CASE( select_noRowsSimple ) +BOOST_AUTO_TEST_CASE(select_noRowsSimple) { auto sel = db->select("SELECT id FROM test WHERE false"); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(sel.get()), Slicer::NoRowsReturned); } -BOOST_AUTO_TEST_CASE( select_noRowsSimpleOptional ) +BOOST_AUTO_TEST_CASE(select_noRowsSimpleOptional) { auto sel = db->select("SELECT id FROM test WHERE false"); auto v = Slicer::DeserializeAny>(sel.get()); BOOST_REQUIRE(!v); } -BOOST_AUTO_TEST_CASE( select_tooManyRowsSimpleOptional ) +BOOST_AUTO_TEST_CASE(select_tooManyRowsSimpleOptional) { auto sel = db->select("SELECT id FROM test"); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper>(sel.get()), Slicer::TooManyRowsReturned); } -BOOST_AUTO_TEST_CASE( select_simpleOptional ) +BOOST_AUTO_TEST_CASE(select_simpleOptional) { auto sel = db->select("SELECT MAX(id) FROM test"); auto v = Slicer::DeserializeAny>(sel.get()); @@ -192,10 +188,9 @@ BOOST_AUTO_TEST_CASE( select_simpleOptional ) BOOST_REQUIRE_EQUAL(4, *v); } -BOOST_AUTO_TEST_CASE( select_noRowsComplexOptional ) +BOOST_AUTO_TEST_CASE(select_noRowsComplexOptional) { - auto sel = db->select( - "SELECT boolean mbool, \ + auto sel = db->select("SELECT boolean mbool, \ id mbyte, id mshort, id mint, id mlong, \ fl mdouble, fl mfloat, \ string mstring \ @@ -205,26 +200,26 @@ BOOST_AUTO_TEST_CASE( select_noRowsComplexOptional ) BOOST_REQUIRE(!v); } -BOOST_AUTO_TEST_CASE( select_tooManyRowsComplex ) +BOOST_AUTO_TEST_CASE(select_tooManyRowsComplex) { auto sel = db->select("SELECT id FROM test"); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(sel.get()), Slicer::TooManyRowsReturned); } -BOOST_AUTO_TEST_CASE( select_noRowsComplex ) +BOOST_AUTO_TEST_CASE(select_noRowsComplex) { auto sel = db->select("SELECT id FROM test WHERE false"); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(sel.get()), Slicer::NoRowsReturned); } -BOOST_AUTO_TEST_CASE( select_emptySequence ) +BOOST_AUTO_TEST_CASE(select_emptySequence) { auto sel = db->select("SELECT id FROM test WHERE false"); auto bi = Slicer::DeserializeAny(sel.get()); BOOST_REQUIRE_EQUAL(0, bi.size()); } -BOOST_AUTO_TEST_CASE( select_null ) +BOOST_AUTO_TEST_CASE(select_null) { db->execute("INSERT INTO test(id) VALUES(NULL)"); @@ -242,12 +237,12 @@ BOOST_AUTO_TEST_CASE( select_null ) BOOST_REQUIRE(!v); } -BOOST_AUTO_TEST_CASE( bulkSelectTest ) +BOOST_AUTO_TEST_CASE(bulkSelectTest) { - auto sel = db->select(R"SQL(select s mint, cast(s as numeric(7,1)) mdouble, cast(s as text) mstring, s % 2 = 0 mbool from generate_series(1, 10000) s)SQL"); + auto sel = db->select( + R"SQL(select s mint, cast(s as numeric(7,1)) mdouble, cast(s as text) mstring, s % 2 = 0 mbool from generate_series(1, 10000) s)SQL"); auto vec = Slicer::DeserializeAny(sel.get()); BOOST_REQUIRE_EQUAL(10000, vec.size()); } BOOST_AUTO_TEST_SUITE_END(); - diff --git a/slicer/db/testUpdate.cpp b/slicer/db/testUpdate.cpp index 48ea903..7dbcba3 100644 --- a/slicer/db/testUpdate.cpp +++ b/slicer/db/testUpdate.cpp @@ -1,29 +1,29 @@ #define BOOST_TEST_MODULE db_update -#include -#include -#include "testMockCommon.h" -#include #include "sqlInsertSerializer.h" #include "sqlSelectDeserializer.h" #include "sqlUpdateSerializer.h" -#include +#include "testMockCommon.h" +#include +#include #include -#include +#include #include +#include +#include using namespace std::literals; -BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); +BOOST_GLOBAL_FIXTURE(StandardMockDatabase); BOOST_FIXTURE_TEST_SUITE(db, ConnectionFixture); -BOOST_AUTO_TEST_CASE( update_builtinsNotFound ) +BOOST_AUTO_TEST_CASE(update_builtinsNotFound) { TestModule::BuiltInsPtr ubi = std::make_shared(false, 5, 17, 64, 129, -1.2, -1.4, "string"); BOOST_REQUIRE_THROW(Slicer::SerializeAny(ubi, db, "builtins"), Slicer::NoRowsFound); } -BOOST_AUTO_TEST_CASE( update_builtins ) +BOOST_AUTO_TEST_CASE(update_builtins) { TestModule::BuiltInsPtr bi1 = std::make_shared(true, 4, 16, 64, 128, 1.2, 3.4, "text1"); TestModule::BuiltInsPtr bi2 = std::make_shared(true, 3, 15, 63, 127, 5.2, 5.4, "text2"); @@ -54,12 +54,10 @@ BOOST_AUTO_TEST_CASE( update_builtins ) BOOST_REQUIRE_EQUAL(bis.back()->mstring, bi2->mstring); } -BOOST_AUTO_TEST_CASE( update_builtins_seq ) +BOOST_AUTO_TEST_CASE(update_builtins_seq) { - TestModule::BuiltInSeq ubis { - TestModule::BuiltInsPtr(std::make_shared(false, 5, 17, 64, 128, -1.2, -1.4, "string")), - TestModule::BuiltInsPtr(std::make_shared(false, 5, 21, 63, 127, -4.2, -5.4, "string updated")) - }; + TestModule::BuiltInSeq ubis {std::make_shared(false, 5, 17, 64, 128, -1.2, -1.4, "string"), + std::make_shared(false, 5, 21, 63, 127, -4.2, -5.4, "string updated")}; Slicer::SerializeAny(ubis, db, "builtins"); auto sel = db->select("SELECT * FROM builtins ORDER BY mint"); @@ -83,7 +81,7 @@ BOOST_AUTO_TEST_CASE( update_builtins_seq ) BOOST_REQUIRE_EQUAL(ubis.back()->mstring, ubis2.front()->mstring); } -BOOST_AUTO_TEST_CASE( update_withNulls ) +BOOST_AUTO_TEST_CASE(update_withNulls) { auto sel = db->select("SELECT * FROM builtins ORDER BY mint"); auto bis = Slicer::DeserializeAny(sel.get()); @@ -107,11 +105,11 @@ BOOST_AUTO_TEST_CASE( update_withNulls ) BOOST_REQUIRE(bis2[1]->mfloat); } -BOOST_AUTO_TEST_CASE( update_unsupportedModel ) +BOOST_AUTO_TEST_CASE(update_unsupportedModel) { TestModule::ClassMap cm; - BOOST_REQUIRE_THROW(Slicer::SerializeAny(cm, db, "converted"), Slicer::UnsupportedModelType); + BOOST_REQUIRE_THROW( + Slicer::SerializeAny(cm, db, "converted"), Slicer::UnsupportedModelType); } BOOST_AUTO_TEST_SUITE_END(); - diff --git a/slicer/ice/serializer.cpp b/slicer/ice/serializer.cpp index a2e6f4a..877f531 100644 --- a/slicer/ice/serializer.cpp +++ b/slicer/ice/serializer.cpp @@ -1,6 +1,6 @@ #include "serializer.h" -#include "Ice/Initialize.h" #include "Ice/Communicator.h" +#include "Ice/Initialize.h" NAMEDFACTORY("application/ice", Slicer::IceStreamSerializer, Slicer::StreamSerializerFactory); NAMEDFACTORY("application/ice", Slicer::IceStreamDeserializer, Slicer::StreamDeserializerFactory); @@ -8,19 +8,13 @@ NAMEDFACTORY("application/ice", Slicer::IceStreamDeserializer, Slicer::StreamDes namespace Slicer { Ice::StringSeq empty; - IceBase::IceBase() : - ic(Ice::initialize(empty)) - { - } + IceBase::IceBase() : ic(Ice::initialize(empty)) { } IceBase::~IceBase() { ic->destroy(); } - IceBlobSerializer::IceBlobSerializer(Ice::ByteSeq & b) : - blob(b) - { - } + IceBlobSerializer::IceBlobSerializer(Ice::ByteSeq & b) : blob(b) { } void IceBlobSerializer::Serialize(ModelPartForRootPtr mp) @@ -30,11 +24,7 @@ namespace Slicer { s.finished(blob); } - IceStreamSerializer::IceStreamSerializer(std::ostream & os) : - IceBlobSerializer(blob), - strm(os) - { - } + IceStreamSerializer::IceStreamSerializer(std::ostream & os) : IceBlobSerializer(blob), strm(os) { } void IceStreamSerializer::Serialize(ModelPartForRootPtr mp) @@ -43,10 +33,7 @@ namespace Slicer { strm.write((const char *)&blob.front(), blob.size()); } - IceBlobDeserializer::IceBlobDeserializer(const Ice::ByteSeq & b) : - blob(b) - { - } + IceBlobDeserializer::IceBlobDeserializer(const Ice::ByteSeq & b) : blob(b) { } void IceBlobDeserializer::Deserialize(ModelPartForRootPtr mp) @@ -55,11 +42,7 @@ namespace Slicer { mp->Read(s); } - IceStreamDeserializer::IceStreamDeserializer(std::istream & is) : - IceBlobDeserializer(blob), - strm(is) - { - } + IceStreamDeserializer::IceStreamDeserializer(std::istream & is) : IceBlobDeserializer(blob), strm(is) { } void IceStreamDeserializer::Deserialize(ModelPartForRootPtr mp) @@ -68,4 +51,3 @@ namespace Slicer { IceBlobDeserializer::Deserialize(mp); } } - diff --git a/slicer/ice/serializer.h b/slicer/ice/serializer.h index c1768c0..05a7752 100644 --- a/slicer/ice/serializer.h +++ b/slicer/ice/serializer.h @@ -1,65 +1,63 @@ #ifndef SLICER_ICE_H #define SLICER_ICE_H +#include +#include #include #include -#include -#include namespace Slicer { class IceBase { - public: - virtual ~IceBase(); + public: + virtual ~IceBase(); - protected: - IceBase(); + protected: + IceBase(); - Ice::CommunicatorPtr ic; + Ice::CommunicatorPtr ic; }; class DLL_PUBLIC IceBlobSerializer : public Serializer, protected IceBase { - public: - IceBlobSerializer(Ice::ByteSeq &); + public: + IceBlobSerializer(Ice::ByteSeq &); - void Serialize(ModelPartForRootPtr) override; + void Serialize(ModelPartForRootPtr) override; - private: - Ice::ByteSeq & blob; + private: + Ice::ByteSeq & blob; }; class DLL_PUBLIC IceStreamSerializer : public IceBlobSerializer { - public: - IceStreamSerializer(std::ostream &); + public: + IceStreamSerializer(std::ostream &); - void Serialize(ModelPartForRootPtr) override; + void Serialize(ModelPartForRootPtr) override; - protected: - std::ostream & strm; - Ice::ByteSeq blob; + protected: + std::ostream & strm; + Ice::ByteSeq blob; }; class DLL_PUBLIC IceBlobDeserializer : public Deserializer, protected IceBase { - public: - IceBlobDeserializer(const Ice::ByteSeq &); + public: + IceBlobDeserializer(const Ice::ByteSeq &); - void Deserialize(ModelPartForRootPtr) override; + void Deserialize(ModelPartForRootPtr) override; - protected: - const Ice::ByteSeq & blob; + protected: + const Ice::ByteSeq & blob; }; class DLL_PUBLIC IceStreamDeserializer : public IceBlobDeserializer { - public: - IceStreamDeserializer(std::istream &); + public: + IceStreamDeserializer(std::istream &); - void Deserialize(ModelPartForRootPtr) override; + void Deserialize(ModelPartForRootPtr) override; - protected: - std::istream & strm; - Ice::ByteSeq blob; + protected: + std::istream & strm; + Ice::ByteSeq blob; }; } #endif - - diff --git a/slicer/ice/testSpecifics.cpp b/slicer/ice/testSpecifics.cpp index 9832352..3a4be15 100644 --- a/slicer/ice/testSpecifics.cpp +++ b/slicer/ice/testSpecifics.cpp @@ -1,7 +1,7 @@ #define BOOST_TEST_MODULE ice_specifics +#include "serializer.h" #include #include -#include "serializer.h" #include // LCOV_EXCL_START @@ -30,7 +30,7 @@ testCompare(const X & x, const std::function & cmp) BOOST_REQUIRE(cmp(x, x2)); } -BOOST_AUTO_TEST_CASE( builtins ) +BOOST_AUTO_TEST_CASE(builtins) { testCompare("some string value"); testCompare(true); @@ -65,7 +65,7 @@ testCompareOptional(const X & d) BOOST_REQUIRE_EQUAL(d, *x3); } -BOOST_AUTO_TEST_CASE( optionalBuiltins ) +BOOST_AUTO_TEST_CASE(optionalBuiltins) { testCompareOptional("some string value"); testCompareOptional(true); @@ -84,9 +84,7 @@ BOOST_AUTO_TEST_CASE( optionalBuiltins ) testCompareOptional(3.14159); } - - -BOOST_AUTO_TEST_CASE( classes ) +BOOST_AUTO_TEST_CASE(classes) { TestModule::BuiltInsPtr x = std::make_shared(); x->mbool = true; @@ -98,21 +96,15 @@ BOOST_AUTO_TEST_CASE( classes ) x->mdouble = 3423423423.42342342343; x->mstring = "sdfsf432423"; testCompare(x, [](const auto & a, const auto & b) { - return a->mbool == b->mbool - && a->mbyte == b->mbyte - && a->mshort == b->mshort - && a->mint == b->mint - && a->mlong == b->mlong - && a->mfloat == b->mfloat - && a->mdouble == b->mdouble - && a->mstring == b->mstring; + return a->mbool == b->mbool && a->mbyte == b->mbyte && a->mshort == b->mshort && a->mint == b->mint + && a->mlong == b->mlong && a->mfloat == b->mfloat && a->mdouble == b->mdouble + && a->mstring == b->mstring; }); } -BOOST_AUTO_TEST_CASE( structes ) +BOOST_AUTO_TEST_CASE(structes) { - TestModule::IsoDate date({ 2016, 10, 3 }); + TestModule::IsoDate date({2016, 10, 3}); testCompare(date); testCompareOptional(date); } - diff --git a/slicer/json/serializer.cpp b/slicer/json/serializer.cpp index b9b0c69..b94ae2e 100644 --- a/slicer/json/serializer.cpp +++ b/slicer/json/serializer.cpp @@ -1,11 +1,11 @@ #include "serializer.h" -#include -#include #include -#include -#include #include +#include #include +#include +#include +#include NAMEDFACTORY(".js", Slicer::JsonFileSerializer, Slicer::FileSerializerFactory); NAMEDFACTORY(".js", Slicer::JsonFileDeserializer, Slicer::FileDeserializerFactory); @@ -24,173 +24,188 @@ namespace Slicer { using namespace std::placeholders; class JsonValueSource : public ValueSource { - public: - explicit JsonValueSource(const json::Value & s) : - value(s) - { - } + public: + explicit JsonValueSource(const json::Value & s) : value(s) { } - void set(bool & v) const override - { - v = std::get(value); - } + void + set(bool & v) const override + { + v = std::get(value); + } - void set(Ice::Byte & v) const override - { - v = boost::numeric_cast(std::get(value)); - } + void + set(Ice::Byte & v) const override + { + v = boost::numeric_cast(std::get(value)); + } - void set(Ice::Short & v) const override - { - v = boost::numeric_cast(std::get(value)); - } + void + set(Ice::Short & v) const override + { + v = boost::numeric_cast(std::get(value)); + } - void set(Ice::Int & v) const override - { - v = boost::numeric_cast(std::get(value)); - } + void + set(Ice::Int & v) const override + { + v = boost::numeric_cast(std::get(value)); + } - void set(Ice::Long & v) const override - { - v = boost::numeric_cast(std::get(value)); - } + void + set(Ice::Long & v) const override + { + v = boost::numeric_cast(std::get(value)); + } - void set(Ice::Float & v) const override - { - v = boost::numeric_cast(std::get(value)); - } + void + set(Ice::Float & v) const override + { + v = boost::numeric_cast(std::get(value)); + } - void set(Ice::Double & v) const override - { - v = boost::numeric_cast(std::get(value)); - } + void + set(Ice::Double & v) const override + { + v = boost::numeric_cast(std::get(value)); + } - void set(std::string & v) const override - { - v = std::get(value); - } + void + set(std::string & v) const override + { + v = std::get(value); + } - private: - const json::Value & value; + private: + const json::Value & value; }; class JsonValueTarget : public ValueTarget { - public: - explicit JsonValueTarget(json::Value & t) : - target(t) - { - target = json::Null(); - } + public: + explicit JsonValueTarget(json::Value & t) : target(t) + { + target = json::Null(); + } - void get(const bool & value) const override - { - target = value; - } + void + get(const bool & value) const override + { + target = value; + } - void get(const Ice::Byte & value) const override - { - target = boost::numeric_cast(value); - } + void + get(const Ice::Byte & value) const override + { + target = boost::numeric_cast(value); + } - void get(const Ice::Short & value) const override - { - target = boost::numeric_cast(value); - } + void + get(const Ice::Short & value) const override + { + target = boost::numeric_cast(value); + } - void get(const Ice::Int & value) const override - { - target = boost::numeric_cast(value); - } + void + get(const Ice::Int & value) const override + { + target = boost::numeric_cast(value); + } - void get(const Ice::Long & value) const override - { - target = boost::numeric_cast(value); - } + void + get(const Ice::Long & value) const override + { + target = boost::numeric_cast(value); + } - void get(const Ice::Float & value) const override - { - target = boost::numeric_cast(value); - } + void + get(const Ice::Float & value) const override + { + target = boost::numeric_cast(value); + } - void get(const Ice::Double & value) const override - { - target = boost::numeric_cast(value); - } + void + get(const Ice::Double & value) const override + { + target = boost::numeric_cast(value); + } - void get(const std::string & value) const override - { - target = value; - } + void + get(const std::string & value) const override + { + target = value; + } - private: - json::Value & target; + private: + json::Value & target; }; class DocumentTreeIterate { - public: - explicit DocumentTreeIterate(ModelPartPtr & mp) : modelPart(mp) - { - } - template - void operator()(const SimpleT & v) const - { - modelPart->Create(); - modelPart->SetValue(JsonValueSource(v)); - modelPart->Complete(); - } - void operator()(const json::Null &) const - { - modelPart->Complete(); - } - void operator()(const json::Object & o) const - { - if (auto typeIdName = modelPart->GetTypeIdProperty()) { - auto typeAttrItr = o.find(*typeIdName); - if (typeAttrItr != o.end() && std::holds_alternative(typeAttrItr->second)) { - modelPart = modelPart->GetSubclassModelPart(std::get(typeAttrItr->second)); - } - } - modelPart->Create(); - if (metaDataFlagSet(modelPart->GetMetadata(), md_object)) { - for (const auto & element : o) { - auto emp = modelPart->GetAnonChild(); - emp->Create(); - auto key = emp->GetChild(keyName); - auto value = emp->GetChild(valueName); - key->Create(); - key->SetValue(JsonValueSource(element.first)); - key->Complete(); - std::visit(DocumentTreeIterate(value), element.second); - emp->Complete(); - } - } - else { - for (const auto & element : o) { - auto emp = modelPart->GetChild(element.first); - if (emp) { - emp->Create(); - std::visit(DocumentTreeIterate(emp), element.second); - emp->Complete(); - } - } - modelPart->Complete(); + public: + explicit DocumentTreeIterate(ModelPartPtr & mp) : modelPart(mp) { } + template + void + operator()(const SimpleT & v) const + { + modelPart->Create(); + modelPart->SetValue(JsonValueSource(v)); + modelPart->Complete(); + } + void + operator()(const json::Null &) const + { + modelPart->Complete(); + } + void + operator()(const json::Object & o) const + { + if (auto typeIdName = modelPart->GetTypeIdProperty()) { + auto typeAttrItr = o.find(*typeIdName); + if (typeAttrItr != o.end() && std::holds_alternative(typeAttrItr->second)) { + modelPart = modelPart->GetSubclassModelPart(std::get(typeAttrItr->second)); } } - void operator()(const json::Array & a) const - { - modelPart->Create(); - for (const auto & element : a) { + modelPart->Create(); + if (metaDataFlagSet(modelPart->GetMetadata(), md_object)) { + for (const auto & element : o) { auto emp = modelPart->GetAnonChild(); + emp->Create(); + auto key = emp->GetChild(keyName); + auto value = emp->GetChild(valueName); + key->Create(); + key->SetValue(JsonValueSource(element.first)); + key->Complete(); + std::visit(DocumentTreeIterate(value), element.second); + emp->Complete(); + } + } + else { + for (const auto & element : o) { + auto emp = modelPart->GetChild(element.first); if (emp) { emp->Create(); - std::visit(DocumentTreeIterate(emp), element); + std::visit(DocumentTreeIterate(emp), element.second); emp->Complete(); } } modelPart->Complete(); } - private: - ModelPartPtr & modelPart; + } + void + operator()(const json::Array & a) const + { + modelPart->Create(); + for (const auto & element : a) { + auto emp = modelPart->GetAnonChild(); + if (emp) { + emp->Create(); + std::visit(DocumentTreeIterate(emp), element); + emp->Complete(); + } + } + modelPart->Complete(); + } + + private: + ModelPartPtr & modelPart; }; void @@ -218,7 +233,7 @@ namespace Slicer { JsonValueSource s(kv); s.set(k); ModelTreeIterateRoot(&v, mp->GetChild(valueName)); - obj->insert({ k, v }); + obj->insert({k, v}); } void @@ -228,18 +243,17 @@ namespace Slicer { return; } switch (mp->GetType()) { - case mpt_Null: + case ModelPartType::Null: std::get(*n).insert({name, json::Null()}); return; - case mpt_Simple: - { - json::Value v; - if (mp->GetValue(JsonValueTarget(v))) { - std::get(*n).insert({ name, v }); - } - break; + case ModelPartType::Simple: { + json::Value v; + if (mp->GetValue(JsonValueTarget(v))) { + std::get(*n).insert({name, v}); } - case mpt_Complex: + break; + } + case ModelPartType::Complex: if (mp->HasValue()) { json::Object nn; if (auto typeIdName = mp->GetTypeIdProperty()) { @@ -248,35 +262,34 @@ namespace Slicer { mp = mp->GetSubclassModelPart(*typeId); } } - mp->OnEachChild( - [capture0 = &std::get(*n).insert({name, nn}).first->second] - (auto && PH1, auto && PH2, auto &&) { - return JsonSerializer::ModelTreeIterate(capture0, PH1, PH2); - }); + mp->OnEachChild([capture0 = &std::get(*n).insert({name, nn}).first->second]( + auto && PH1, auto && PH2, auto &&) { + return JsonSerializer::ModelTreeIterate(capture0, PH1, PH2); + }); } break; - case mpt_Sequence: + case ModelPartType::Sequence: if (mp->HasValue()) { mp->OnEachChild( - [capture0 = &std::get(*n).insert({name, json::Array()}).first->second] - (auto &&, auto && PH2, auto &&) { + [capture0 = &std::get(*n).insert({name, json::Array()}).first->second]( + auto &&, auto && PH2, auto &&) { return JsonSerializer::ModelTreeIterateSeq(capture0, PH2); }); } break; - case mpt_Dictionary: + case ModelPartType::Dictionary: if (mp->HasValue()) { if (metaDataFlagSet(mp->GetMetadata(), md_object)) { mp->OnEachChild( - [capture0 = &std::get(*n).insert({name, json::Object()}).first->second] - (auto &&, auto && PH2, auto &&) { + [capture0 = &std::get(*n).insert({name, json::Object()}).first->second]( + auto &&, auto && PH2, auto &&) { return JsonSerializer::ModelTreeIterateDictObj(capture0, PH2); }); } else { mp->OnEachChild( - [capture0 = &std::get(*n).insert({name, json::Array()}).first->second] - (auto &&, auto && PH2, auto &&) { + [capture0 = &std::get(*n).insert({name, json::Array()}).first->second]( + auto &&, auto && PH2, auto &&) { return JsonSerializer::ModelTreeIterateSeq(capture0, PH2); }); } @@ -290,13 +303,13 @@ namespace Slicer { { if (mp) { switch (mp->GetType()) { - case mpt_Null: + case ModelPartType::Null: *n = json::Null(); return; - case mpt_Simple: + case ModelPartType::Simple: mp->GetValue(JsonValueTarget(*n)); break; - case mpt_Complex: + case ModelPartType::Complex: *n = json::Object(); if (auto typeIdName = mp->GetTypeIdProperty()) { if (auto typeId = mp->GetTypeId()) { @@ -308,13 +321,13 @@ namespace Slicer { return JsonSerializer::ModelTreeIterate(n, PH1, PH2); }); break; - case mpt_Sequence: + case ModelPartType::Sequence: *n = json::Array(); mp->OnEachChild([n](auto &&, auto && PH2, auto &&) { return JsonSerializer::ModelTreeIterateSeq(n, PH2); }); break; - case mpt_Dictionary: + case ModelPartType::Dictionary: if (metaDataFlagSet(mp->GetMetadata(), md_object)) { *n = json::Object(); mp->OnEachChild([n](auto &&, auto && PH2, auto &&) { @@ -332,15 +345,9 @@ namespace Slicer { } } - JsonStreamSerializer::JsonStreamSerializer(std::ostream & s) : - strm(s) - { - } + JsonStreamSerializer::JsonStreamSerializer(std::ostream & s) : strm(s) { } - JsonStreamDeserializer::JsonStreamDeserializer(std::istream & s) : - strm(s) - { - } + JsonStreamDeserializer::JsonStreamDeserializer(std::istream & s) : strm(s) { } void JsonStreamDeserializer::Deserialize(ModelPartForRootPtr modelRoot) @@ -360,15 +367,9 @@ namespace Slicer { json::serializeValue(doc, strm, "utf-8"); } - JsonFileSerializer::JsonFileSerializer(std::filesystem::path p) : - path(std::move(p)) - { - } + JsonFileSerializer::JsonFileSerializer(std::filesystem::path p) : path(std::move(p)) { } - JsonFileDeserializer::JsonFileDeserializer(std::filesystem::path p) : - path(std::move(p)) - { - } + JsonFileDeserializer::JsonFileDeserializer(std::filesystem::path p) : path(std::move(p)) { } void JsonFileDeserializer::Deserialize(ModelPartForRootPtr modelRoot) @@ -390,15 +391,9 @@ namespace Slicer { json::serializeValue(doc, outFile, "utf-8"); } - JsonValueSerializer::JsonValueSerializer(json::Value & v) : - value(v) - { - } + JsonValueSerializer::JsonValueSerializer(json::Value & v) : value(v) { } - JsonValueDeserializer::JsonValueDeserializer(const json::Value & v) : - value(v) - { - } + JsonValueDeserializer::JsonValueDeserializer(const json::Value & v) : value(v) { } void JsonValueDeserializer::Deserialize(ModelPartForRootPtr modelRoot) @@ -415,5 +410,3 @@ namespace Slicer { }); } } - - diff --git a/slicer/json/serializer.h b/slicer/json/serializer.h index ed666aa..c23121b 100644 --- a/slicer/json/serializer.h +++ b/slicer/json/serializer.h @@ -1,80 +1,78 @@ #ifndef SLICER_JSON_H #define SLICER_JSON_H -#include #include +#include #include namespace Slicer { class JsonSerializer : public Serializer { - protected: - static void ModelTreeIterate(json::Value *, const std::string &, ModelPartPtr mp); - static void ModelTreeIterateDictObj(json::Value *, const ModelPartPtr & mp); - static void ModelTreeIterateSeq(json::Value *, const ModelPartPtr & mp); - static void ModelTreeIterateRoot(json::Value *, ModelPartPtr mp); + protected: + static void ModelTreeIterate(json::Value *, const std::string &, ModelPartPtr mp); + static void ModelTreeIterateDictObj(json::Value *, const ModelPartPtr & mp); + static void ModelTreeIterateSeq(json::Value *, const ModelPartPtr & mp); + static void ModelTreeIterateRoot(json::Value *, ModelPartPtr mp); }; class DLL_PUBLIC JsonStreamSerializer : public JsonSerializer { - public: - JsonStreamSerializer(std::ostream &); + public: + explicit JsonStreamSerializer(std::ostream &); - void Serialize(ModelPartForRootPtr) override; + void Serialize(ModelPartForRootPtr) override; - protected: - std::ostream & strm; + protected: + std::ostream & strm; }; class DLL_PUBLIC JsonFileSerializer : public JsonSerializer { - public: - JsonFileSerializer(std::filesystem::path); + public: + explicit JsonFileSerializer(std::filesystem::path); - void Serialize(ModelPartForRootPtr) override; + void Serialize(ModelPartForRootPtr) override; - protected: - const std::filesystem::path path; + protected: + const std::filesystem::path path; }; class DLL_PUBLIC JsonValueSerializer : public JsonSerializer { - public: - JsonValueSerializer(json::Value &); + public: + explicit JsonValueSerializer(json::Value &); - void Serialize(ModelPartForRootPtr) override; + void Serialize(ModelPartForRootPtr) override; - protected: - json::Value & value; + protected: + json::Value & value; }; class DLL_PUBLIC JsonStreamDeserializer : public Deserializer { - public: - JsonStreamDeserializer(std::istream &); + public: + explicit JsonStreamDeserializer(std::istream &); - void Deserialize(ModelPartForRootPtr) override; + void Deserialize(ModelPartForRootPtr) override; - protected: - std::istream & strm; + protected: + std::istream & strm; }; class DLL_PUBLIC JsonFileDeserializer : public Deserializer { - public: - JsonFileDeserializer(std::filesystem::path); + public: + explicit JsonFileDeserializer(std::filesystem::path); - void Deserialize(ModelPartForRootPtr) override; + void Deserialize(ModelPartForRootPtr) override; - protected: - const std::filesystem::path path; + protected: + const std::filesystem::path path; }; class DLL_PUBLIC JsonValueDeserializer : public Deserializer { - public: - JsonValueDeserializer(const json::Value &); + public: + explicit JsonValueDeserializer(const json::Value &); - void Deserialize(ModelPartForRootPtr) override; + void Deserialize(ModelPartForRootPtr) override; - protected: - const json::Value & value; + protected: + const json::Value & value; }; } #endif - - diff --git a/slicer/json/testSpecifics.cpp b/slicer/json/testSpecifics.cpp index c0a54d4..6bfc9d4 100644 --- a/slicer/json/testSpecifics.cpp +++ b/slicer/json/testSpecifics.cpp @@ -1,18 +1,21 @@ #define BOOST_TEST_MODULE json_specifics +#include "serializer.h" #include #include -#include "serializer.h" #include -BOOST_AUTO_TEST_CASE( factories ) +BOOST_AUTO_TEST_CASE(factories) { BOOST_REQUIRE(Slicer::SerializerPtr(Slicer::FileSerializerFactory::createNew(".json", "/some.json"))); BOOST_REQUIRE(Slicer::DeserializerPtr(Slicer::FileDeserializerFactory::createNew(".json", "/some.json"))); BOOST_REQUIRE(Slicer::SerializerPtr(Slicer::FileSerializerFactory::createNew(".js", "/some.js"))); BOOST_REQUIRE(Slicer::DeserializerPtr(Slicer::FileDeserializerFactory::createNew(".js", "/some.js"))); - BOOST_REQUIRE(Slicer::SerializerPtr(Slicer::StreamSerializerFactory::createNew("application/javascript", std::cout))); - BOOST_REQUIRE(Slicer::DeserializerPtr(Slicer::StreamDeserializerFactory::createNew("application/javascript", std::cin))); - BOOST_REQUIRE(Slicer::SerializerPtr(Slicer::StreamSerializerFactory::createNew("application/javascript", std::cout))); - BOOST_REQUIRE(Slicer::DeserializerPtr(Slicer::StreamDeserializerFactory::createNew("application/javascript", std::cin))); + BOOST_REQUIRE( + Slicer::SerializerPtr(Slicer::StreamSerializerFactory::createNew("application/javascript", std::cout))); + BOOST_REQUIRE( + Slicer::DeserializerPtr(Slicer::StreamDeserializerFactory::createNew("application/javascript", std::cin))); + BOOST_REQUIRE( + Slicer::SerializerPtr(Slicer::StreamSerializerFactory::createNew("application/javascript", std::cout))); + BOOST_REQUIRE( + Slicer::DeserializerPtr(Slicer::StreamDeserializerFactory::createNew("application/javascript", std::cin))); } - diff --git a/slicer/slicer/metadata.cpp b/slicer/slicer/metadata.cpp index 4f794fc..6429cc8 100644 --- a/slicer/slicer/metadata.cpp +++ b/slicer/slicer/metadata.cpp @@ -1,7 +1,7 @@ #include "metadata.h" +#include #include #include -#include bool Slicer::metaDataFlagSet(const std::list & md, const std::string & flag) @@ -46,4 +46,3 @@ Slicer::metaDataValue(const std::string & prefix, const std::list & } return std::optional(); } - diff --git a/slicer/slicer/metadata.h b/slicer/slicer/metadata.h index 79baf13..e604658 100644 --- a/slicer/slicer/metadata.h +++ b/slicer/slicer/metadata.h @@ -1,10 +1,10 @@ #ifndef SLICER_METADATA_H #define SLICER_METADATA_H -#include #include -#include #include +#include +#include namespace Slicer { #pragma GCC visibility push(default) @@ -19,4 +19,3 @@ namespace Slicer { } #endif - diff --git a/slicer/slicer/modelParts.cpp b/slicer/slicer/modelParts.cpp index 61cbd25..a6e191a 100644 --- a/slicer/slicer/modelParts.cpp +++ b/slicer/slicer/modelParts.cpp @@ -73,10 +73,7 @@ namespace Slicer { return shared_from_this(); } - HookCommon::HookCommon(std::string n) : - name(std::move(n)) - { - } + HookCommon::HookCommon(std::string n) : name(std::move(n)) { } bool HookCommon::filter(const HookFilter & flt) @@ -90,4 +87,3 @@ namespace Slicer { ch(this->name, modelPart, this); } } - diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index f31a8c0..50ae427 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -1,64 +1,62 @@ #ifndef SLICER_MODELPARTS_H #define SLICER_MODELPARTS_H -#include #include +#include #include -#include #include -#include #include +#include +#include #include namespace Slicer { - template - class TValueTarget { - public: - virtual void get(const T &) const = 0; + template class TValueTarget { + public: + virtual void get(const T &) const = 0; }; class ValueTarget : - public TValueTarget, - public TValueTarget, - public TValueTarget, - public TValueTarget, - public TValueTarget, - public TValueTarget, - public TValueTarget, - public TValueTarget { - public: - using TValueTarget::get; - using TValueTarget::get; - using TValueTarget::get; - using TValueTarget::get; - using TValueTarget::get; - using TValueTarget::get; - using TValueTarget::get; - using TValueTarget::get; + public TValueTarget, + public TValueTarget, + public TValueTarget, + public TValueTarget, + public TValueTarget, + public TValueTarget, + public TValueTarget, + public TValueTarget { + public: + using TValueTarget::get; + using TValueTarget::get; + using TValueTarget::get; + using TValueTarget::get; + using TValueTarget::get; + using TValueTarget::get; + using TValueTarget::get; + using TValueTarget::get; }; - template - class TValueSource { - public: - virtual void set(T &) const = 0; + template class TValueSource { + public: + virtual void set(T &) const = 0; }; class ValueSource : - public TValueSource, - public TValueSource, - public TValueSource, - public TValueSource, - public TValueSource, - public TValueSource, - public TValueSource, - public TValueSource { - public: - using TValueSource::set; - using TValueSource::set; - using TValueSource::set; - using TValueSource::set; - using TValueSource::set; - using TValueSource::set; - using TValueSource::set; - using TValueSource::set; + public TValueSource, + public TValueSource, + public TValueSource, + public TValueSource, + public TValueSource, + public TValueSource, + public TValueSource, + public TValueSource { + public: + using TValueSource::set; + using TValueSource::set; + using TValueSource::set; + using TValueSource::set; + using TValueSource::set; + using TValueSource::set; + using TValueSource::set; + using TValueSource::set; }; class ModelPart; @@ -75,51 +73,51 @@ namespace Slicer { using Metadata = std::list; DLL_PUBLIC extern const Metadata emptyMetadata; - enum ModelPartType { - mpt_Null, - mpt_Simple, - mpt_Complex, - mpt_Sequence, - mpt_Dictionary, + enum class ModelPartType { + Null, + Simple, + Complex, + Sequence, + Dictionary, }; - enum TryConvertResult { - tcr_NoAction = 0, - tcr_NoValue, - tcr_Value, + enum class TryConvertResult { + NoAction, + NoValue, + Value, }; class DLL_PUBLIC ChildRef { - public: - explicit ChildRef(); - explicit ChildRef(ModelPartPtr); - explicit ChildRef(ModelPartPtr, const Metadata &); - - [[nodiscard]] ModelPartPtr Child() const; - [[nodiscard]] const Metadata & ChildMetaData() const; - explicit operator bool() const; - - private: - ModelPartPtr mpp; - const Metadata & mdr; + public: + explicit ChildRef(); + explicit ChildRef(ModelPartPtr); + explicit ChildRef(ModelPartPtr, const Metadata &); + + [[nodiscard]] ModelPartPtr Child() const; + [[nodiscard]] const Metadata & ChildMetaData() const; + explicit operator bool() const; + + private: + ModelPartPtr mpp; + const Metadata & mdr; }; class DLL_PUBLIC HookCommon { - public: - explicit HookCommon(std::string); + public: + explicit HookCommon(std::string); - bool filter(const HookFilter & flt); - void apply(const ChildHandler & ch, const ModelPartPtr & modelPart); + bool filter(const HookFilter & flt); + void apply(const ChildHandler & ch, const ModelPartPtr & modelPart); - [[nodiscard]] virtual const Metadata & GetMetadata() const = 0; + [[nodiscard]] virtual const Metadata & GetMetadata() const = 0; - // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) - const std::string name; + const std::string name; }; struct case_less { template - inline bool operator()(const A & a, const B & b) const + inline bool + operator()(const A & a, const B & b) const { const auto cmp = strncasecmp(a.data(), b.data(), std::min(a.length(), b.length())); return (cmp < 0) || (!cmp && a.length() < b.length()); @@ -127,69 +125,64 @@ namespace Slicer { }; class DLL_PUBLIC ModelPart : public std::enable_shared_from_this { - public: - ModelPart() = default; - ModelPart(const ModelPart &) = delete; - ModelPart(ModelPart &&) = delete; - - virtual ~ModelPart() = default; - - ModelPart & operator=(const ModelPart &) = delete; - ModelPart & operator=(ModelPart &&) = delete; - - template - static ModelPartPtr CreateFor(); - template - static ModelPartPtr CreateFor(T & t); - template - static ModelPartForRootPtr CreateRootFor(T & t); - - virtual void OnEachChild(const ChildHandler &) = 0; - ModelPartPtr GetAnonChild(const HookFilter & = HookFilter()); - ModelPartPtr GetChild(const std::string & memberName, const HookFilter & = HookFilter()); - virtual ChildRef GetAnonChildRef(const HookFilter & = HookFilter()) = 0; - virtual ChildRef GetChildRef(const std::string & memberName, const HookFilter & = HookFilter(), bool matchCase = true) = 0; - virtual ModelPartPtr GetSubclassModelPart(const std::string &); - virtual TypeId GetTypeId() const; - virtual Ice::optional GetTypeIdProperty() const; - virtual ModelPartType GetType() const = 0; - virtual void Create(); - virtual void Complete(); - virtual void SetValue(ValueSource &&); - virtual bool GetValue(ValueTarget &&); - virtual bool HasValue() const = 0; - virtual const Metadata & GetMetadata() const; - virtual bool IsOptional() const; - virtual ModelPartPtr GetContainedModelPart(); + public: + ModelPart() = default; + ModelPart(const ModelPart &) = delete; + ModelPart(ModelPart &&) = delete; + + virtual ~ModelPart() = default; + + ModelPart & operator=(const ModelPart &) = delete; + ModelPart & operator=(ModelPart &&) = delete; + + template static ModelPartPtr CreateFor(); + template static ModelPartPtr CreateFor(T & t); + template static ModelPartForRootPtr CreateRootFor(T & t); + + virtual void OnEachChild(const ChildHandler &) = 0; + ModelPartPtr GetAnonChild(const HookFilter & = HookFilter()); + ModelPartPtr GetChild(const std::string & memberName, const HookFilter & = HookFilter()); + virtual ChildRef GetAnonChildRef(const HookFilter & = HookFilter()) = 0; + virtual ChildRef GetChildRef( + const std::string & memberName, const HookFilter & = HookFilter(), bool matchCase = true) + = 0; + virtual ModelPartPtr GetSubclassModelPart(const std::string &); + virtual TypeId GetTypeId() const; + virtual Ice::optional GetTypeIdProperty() const; + virtual ModelPartType GetType() const = 0; + virtual void Create(); + virtual void Complete(); + virtual void SetValue(ValueSource &&); + virtual bool GetValue(ValueTarget &&); + virtual bool HasValue() const = 0; + virtual const Metadata & GetMetadata() const; + virtual bool IsOptional() const; + virtual ModelPartPtr GetContainedModelPart(); }; - template - class DLL_PUBLIC ModelPartModel { - protected: - explicit ModelPartModel() : Model(nullptr) { } - explicit ModelPartModel(T * m) : Model(m) { } - // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) - T * Model; + template class DLL_PUBLIC ModelPartModel { + protected: + explicit ModelPartModel() : Model(nullptr) { } + explicit ModelPartModel(T * m) : Model(m) { } + T * Model; }; class DLL_PUBLIC ModelPartForRootBase : public ModelPart { - public: - explicit ModelPartForRootBase(ModelPartPtr mp); - - virtual const std::string & GetRootName() const = 0; - ChildRef GetAnonChildRef(const HookFilter &) override; - ChildRef GetChildRef(const std::string & name, const HookFilter &, bool matchCase = true) override; - void OnEachChild(const ChildHandler & ch) override; - ModelPartType GetType() const override; - bool IsOptional() const override; - virtual void Write(::Ice::OutputStream &) const = 0; - virtual void Read(::Ice::InputStream &) = 0; - ModelPartPtr GetContainedModelPart() override; - - // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) - ModelPartPtr mp; + public: + explicit ModelPartForRootBase(ModelPartPtr mp); + + virtual const std::string & GetRootName() const = 0; + ChildRef GetAnonChildRef(const HookFilter &) override; + ChildRef GetChildRef(const std::string & name, const HookFilter &, bool matchCase = true) override; + void OnEachChild(const ChildHandler & ch) override; + ModelPartType GetType() const override; + bool IsOptional() const override; + virtual void Write(::Ice::OutputStream &) const = 0; + virtual void Read(::Ice::InputStream &) = 0; + ModelPartPtr GetContainedModelPart() override; + + ModelPartPtr mp; }; } #endif - diff --git a/slicer/slicer/modelPartsTypes.cpp b/slicer/slicer/modelPartsTypes.cpp index 7d9ea54..73e13bf 100644 --- a/slicer/slicer/modelPartsTypes.cpp +++ b/slicer/slicer/modelPartsTypes.cpp @@ -6,25 +6,29 @@ namespace Slicer { using ClassRefMap = std::map>; using ClassNamePair = std::pair; - using ClassNameMap = boost::multi_index_container< - ClassNamePair, - boost::multi_index::indexed_by< - boost::multi_index::ordered_unique, std::less<>>, - boost::multi_index::ordered_unique, std::less<>> - >>; + using ClassNameMap = boost::multi_index_container, + std::less<>>, + boost::multi_index::ordered_unique< + boost::multi_index::member, + std::less<>>>>; static void createClassMaps() __attribute__((constructor(208))); static void deleteClassMaps() __attribute__((destructor(208))); static ClassNameMap * names; static ClassRefMap * refs; - void createClassMaps() + void + createClassMaps() { names = new ClassNameMap(); refs = new ClassRefMap(); } - static void deleteClassMaps() + static void + deleteClassMaps() { delete names; delete refs; @@ -100,10 +104,7 @@ namespace Slicer { } // ModelPartForRootBase - ModelPartForRootBase::ModelPartForRootBase(ModelPartPtr m) : - mp(std::move(m)) - { - } + ModelPartForRootBase::ModelPartForRootBase(ModelPartPtr m) : mp(std::move(m)) { } ChildRef ModelPartForRootBase::GetAnonChildRef(const HookFilter &) @@ -121,7 +122,8 @@ namespace Slicer { return GetAnonChildRef(hf); } - void ModelPartForRootBase::OnEachChild(const ChildHandler & ch) + void + ModelPartForRootBase::OnEachChild(const ChildHandler & ch) { ch(GetRootName(), mp, nullptr); } @@ -144,37 +146,83 @@ namespace Slicer { return mp->GetContainedModelPart(); } - void ModelPartForSimpleBase::OnEachChild(const ChildHandler &) { } - ChildRef ModelPartForSimpleBase::GetAnonChildRef(const HookFilter &) { return ChildRef(); } - ChildRef ModelPartForSimpleBase::GetChildRef(const std::string &, const HookFilter &, bool) { return ChildRef(); } - bool ModelPartForSimpleBase::HasValue() const { return true; } - ModelPartType ModelPartForSimpleBase::GetType() const { return type; } - const ModelPartType ModelPartForSimpleBase::type = mpt_Simple; + void + ModelPartForSimpleBase::OnEachChild(const ChildHandler &) + { + } + ChildRef + ModelPartForSimpleBase::GetAnonChildRef(const HookFilter &) + { + return ChildRef(); + } + ChildRef + ModelPartForSimpleBase::GetChildRef(const std::string &, const HookFilter &, bool) + { + return ChildRef(); + } + bool + ModelPartForSimpleBase::HasValue() const + { + return true; + } + ModelPartType + ModelPartForSimpleBase::GetType() const + { + return type; + } + const ModelPartType ModelPartForSimpleBase::type = ModelPartType::Simple; - void ModelPartForConvertedBase::OnEachChild(const ChildHandler &) { } - ChildRef ModelPartForConvertedBase::GetAnonChildRef(const HookFilter &) { return ChildRef(); } - ChildRef ModelPartForConvertedBase::GetChildRef(const std::string &, const HookFilter &, bool) { return ChildRef(); } - bool ModelPartForConvertedBase::HasValue() const { return true; } - ModelPartType ModelPartForConvertedBase::GetType() const { return type; } - const ModelPartType ModelPartForConvertedBase::type = mpt_Simple; + void + ModelPartForConvertedBase::OnEachChild(const ChildHandler &) + { + } + ChildRef + ModelPartForConvertedBase::GetAnonChildRef(const HookFilter &) + { + return ChildRef(); + } + ChildRef + ModelPartForConvertedBase::GetChildRef(const std::string &, const HookFilter &, bool) + { + return ChildRef(); + } + bool + ModelPartForConvertedBase::HasValue() const + { + return true; + } + ModelPartType + ModelPartForConvertedBase::GetType() const + { + return type; + } + const ModelPartType ModelPartForConvertedBase::type = ModelPartType::Simple; - ModelPartType ModelPartForComplexBase::GetType() const { return type; } - const ModelPartType ModelPartForComplexBase::type = mpt_Complex; - void ModelPartForComplexBase::registerClass(const std::string & className, const std::string * typeName, const ClassRef & cr) + ModelPartType + ModelPartForComplexBase::GetType() const { - Slicer::classRefMap()->insert({ className, cr }); + return type; + } + const ModelPartType ModelPartForComplexBase::type = ModelPartType::Complex; + void + ModelPartForComplexBase::registerClass( + const std::string & className, const std::string * typeName, const ClassRef & cr) + { + Slicer::classRefMap()->insert({className, cr}); if (typeName) { - Slicer::classNameMap()->insert({ className, *typeName }); + Slicer::classNameMap()->insert({className, *typeName}); } } - void ModelPartForComplexBase::unregisterClass(const std::string & className, const std::string * typeName) + void + ModelPartForComplexBase::unregisterClass(const std::string & className, const std::string * typeName) { Slicer::classRefMap()->erase(className); if (typeName) { classNameMap()->get<0>().erase(className); } } - ModelPartPtr ModelPartForComplexBase::getSubclassModelPart(const std::string & name, void * m) + ModelPartPtr + ModelPartForComplexBase::getSubclassModelPart(const std::string & name, void * m) { auto ref = classRefMap()->find(ToModelTypeName(name)); if (ref == classRefMap()->end()) { @@ -182,32 +230,38 @@ namespace Slicer { } return ref->second(m); } - TypeId ModelPartForComplexBase::GetTypeId(const std::string & id, const std::string & className) + TypeId + ModelPartForComplexBase::GetTypeId(const std::string & id, const std::string & className) { return (id == className) ? TypeId() : ToExchangeTypeName(id); } - std::string ModelPartForComplexBase::demangle(const char * mangled) + std::string + ModelPartForComplexBase::demangle(const char * mangled) { - auto buf = std::unique_ptr(abi::__cxa_demangle(mangled, nullptr, nullptr, nullptr), std::free); + auto buf = std::unique_ptr( + abi::__cxa_demangle(mangled, nullptr, nullptr, nullptr), std::free); return "::" + std::string(buf.get()); } - void ModelPartForOptionalBase::OnEachChild(const ChildHandler & ch) + void + ModelPartForOptionalBase::OnEachChild(const ChildHandler & ch) { if (this->hasModel()) { modelPart->OnEachChild(ch); } } - void ModelPartForOptionalBase::Complete() + void + ModelPartForOptionalBase::Complete() { if (this->hasModel()) { modelPart->Complete(); } } - ChildRef ModelPartForOptionalBase::GetAnonChildRef(const HookFilter & flt) + ChildRef + ModelPartForOptionalBase::GetAnonChildRef(const HookFilter & flt) { if (this->hasModel()) { return modelPart->GetAnonChildRef(flt); @@ -215,7 +269,8 @@ namespace Slicer { return ChildRef(); } - ChildRef ModelPartForOptionalBase::GetChildRef(const std::string & name, const HookFilter & flt, bool matchCase) + ChildRef + ModelPartForOptionalBase::GetChildRef(const std::string & name, const HookFilter & flt, bool matchCase) { if (this->hasModel()) { return modelPart->GetChildRef(name, flt, matchCase); @@ -223,57 +278,127 @@ namespace Slicer { return ChildRef(); } - void ModelPartForOptionalBase::SetValue(ValueSource && s) + void + ModelPartForOptionalBase::SetValue(ValueSource && s) { if (this->hasModel()) { modelPart->SetValue(std::move(s)); } } - bool ModelPartForOptionalBase::HasValue() const + bool + ModelPartForOptionalBase::HasValue() const { return this->hasModel() && modelPart->HasValue(); } - bool ModelPartForOptionalBase::IsOptional() const + bool + ModelPartForOptionalBase::IsOptional() const { return true; }; - const Metadata & ModelPartForOptionalBase::GetMetadata() const + const Metadata & + ModelPartForOptionalBase::GetMetadata() const { return modelPart->GetMetadata(); } - void ModelPartForEnumBase::OnEachChild(const ChildHandler &) { } - ChildRef ModelPartForEnumBase::GetAnonChildRef(const HookFilter &) { return ChildRef(); } - ChildRef ModelPartForEnumBase::GetChildRef(const std::string &, const HookFilter &, bool) { return ChildRef(); } - bool ModelPartForEnumBase::HasValue() const { return true; } - ModelPartType ModelPartForEnumBase::GetType() const { return type; } - const ModelPartType ModelPartForEnumBase::type = mpt_Simple; + void + ModelPartForEnumBase::OnEachChild(const ChildHandler &) + { + } + ChildRef + ModelPartForEnumBase::GetAnonChildRef(const HookFilter &) + { + return ChildRef(); + } + ChildRef + ModelPartForEnumBase::GetChildRef(const std::string &, const HookFilter &, bool) + { + return ChildRef(); + } + bool + ModelPartForEnumBase::HasValue() const + { + return true; + } + ModelPartType + ModelPartForEnumBase::GetType() const + { + return type; + } + const ModelPartType ModelPartForEnumBase::type = ModelPartType::Simple; - bool ModelPartForSequenceBase::HasValue() const { return true; } - ModelPartType ModelPartForSequenceBase::GetType() const { return type; } - const ModelPartType ModelPartForSequenceBase::type = mpt_Sequence; + bool + ModelPartForSequenceBase::HasValue() const + { + return true; + } + ModelPartType + ModelPartForSequenceBase::GetType() const + { + return type; + } + const ModelPartType ModelPartForSequenceBase::type = ModelPartType::Sequence; - bool ModelPartForDictionaryBase::HasValue() const { return true; } - ModelPartType ModelPartForDictionaryBase::GetType() const { return type; } - const ModelPartType ModelPartForDictionaryBase::type = mpt_Dictionary; + bool + ModelPartForDictionaryBase::HasValue() const + { + return true; + } + ModelPartType + ModelPartForDictionaryBase::GetType() const + { + return type; + } + const ModelPartType ModelPartForDictionaryBase::type = ModelPartType::Dictionary; // Streams // NOLINTNEXTLINE(hicpp-no-array-decay) - ChildRef ModelPartForStreamBase::GetAnonChildRef(const Slicer::HookFilter &) { throw InvalidStreamOperation(__FUNCTION__); } + ChildRef + ModelPartForStreamBase::GetAnonChildRef(const Slicer::HookFilter &) + { + throw InvalidStreamOperation(__FUNCTION__); + } // NOLINTNEXTLINE(hicpp-no-array-decay) - ChildRef ModelPartForStreamBase::GetChildRef(const std::string &, const Slicer::HookFilter &, bool) { throw InvalidStreamOperation(__FUNCTION__); } - ModelPartType ModelPartForStreamBase::GetType() const { return mpt_Sequence; } - bool ModelPartForStreamBase::HasValue() const { return true; } + ChildRef + ModelPartForStreamBase::GetChildRef(const std::string &, const Slicer::HookFilter &, bool) + { + throw InvalidStreamOperation(__FUNCTION__); + } + ModelPartType + ModelPartForStreamBase::GetType() const + { + return ModelPartType::Sequence; + } + bool + ModelPartForStreamBase::HasValue() const + { + return true; + } // Stream Roots ModelPartForStreamRootBase::ModelPartForStreamRootBase(const ModelPartPtr & mp) : ModelPartForRootBase(mp) { } // NOLINTNEXTLINE(hicpp-no-array-decay) - void ModelPartForStreamRootBase::Write(Ice::OutputStream&) const { throw InvalidStreamOperation(__FUNCTION__); } + void + ModelPartForStreamRootBase::Write(Ice::OutputStream &) const + { + throw InvalidStreamOperation(__FUNCTION__); + } // NOLINTNEXTLINE(hicpp-no-array-decay) - void ModelPartForStreamRootBase::Read(Ice::InputStream&) { throw InvalidStreamOperation(__FUNCTION__); } - bool ModelPartForStreamRootBase::HasValue() const { return mp->HasValue(); } - void ModelPartForStreamRootBase::OnEachChild(const ChildHandler & ch) { ch(GetRootName(), mp, NULL); } + void + ModelPartForStreamRootBase::Read(Ice::InputStream &) + { + throw InvalidStreamOperation(__FUNCTION__); + } + bool + ModelPartForStreamRootBase::HasValue() const + { + return mp->HasValue(); + } + void + ModelPartForStreamRootBase::OnEachChild(const ChildHandler & ch) + { + ch(GetRootName(), mp, NULL); + } } - diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index 99ccb81..b47353b 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -4,376 +4,367 @@ #include "modelParts.h" namespace Slicer { - template - struct isLocal { + template struct isLocal { static constexpr bool value = false; }; DLL_PUBLIC bool optionalCaseEq(const std::string & a, const std::string & b, bool matchCase); - template - class DLL_PUBLIC ModelPartForRoot : public ModelPartForRootBase { - public: - ModelPartForRoot(T * o); + template class DLL_PUBLIC ModelPartForRoot : public ModelPartForRootBase { + public: + explicit ModelPartForRoot(T * o); - const std::string & GetRootName() const override; - bool HasValue() const override; - void Write(::Ice::OutputStream &) const override; - void Read(::Ice::InputStream &) override; + const std::string & GetRootName() const override; + bool HasValue() const override; + void Write(::Ice::OutputStream &) const override; + void Read(::Ice::InputStream &) override; - static const std::string rootName; + static const std::string rootName; - private: - T * ModelObject; + private: + T * ModelObject; }; class DLL_PUBLIC ModelPartForSimpleBase : public ModelPart { - public: - void OnEachChild(const ChildHandler &) override; - ChildRef GetAnonChildRef(const HookFilter &) override; - ChildRef GetChildRef(const std::string &, const HookFilter &, bool matchCase = true) override; - bool HasValue() const override; - ModelPartType GetType() const override; - static const ModelPartType type; + public: + void OnEachChild(const ChildHandler &) override; + ChildRef GetAnonChildRef(const HookFilter &) override; + ChildRef GetChildRef(const std::string &, const HookFilter &, bool matchCase = true) override; + bool HasValue() const override; + ModelPartType GetType() const override; + static const ModelPartType type; }; template class DLL_PUBLIC ModelPartForSimple : public ModelPartForSimpleBase, protected ModelPartModel { - public: - using element_type = T; + public: + using element_type = T; - ModelPartForSimple(T * h); + explicit ModelPartForSimple(T * h); - void SetValue(ValueSource && s) override; - bool GetValue(ValueTarget && s) override; + void SetValue(ValueSource && s) override; + bool GetValue(ValueTarget && s) override; }; class DLL_PUBLIC ModelPartForConvertedBase : public ModelPart { - public: - void OnEachChild(const ChildHandler &) override; - ChildRef GetAnonChildRef(const HookFilter &) override; - ChildRef GetChildRef(const std::string &, const HookFilter &, bool matchCase = true) override; - bool HasValue() const override; - ModelPartType GetType() const override; - static const ModelPartType type; - - protected: - template - inline static bool tryConvertFrom(ValueSource & vsp, MT * model, const Conv & conv); - template - inline static bool tryConvertFrom(ValueSource & vsp, MT * model); - template - inline static TryConvertResult tryConvertTo(ValueTarget & vsp, const MT * model, const Conv & conv); - template - inline static TryConvertResult tryConvertTo(ValueTarget & vsp, const MT * model); + public: + void OnEachChild(const ChildHandler &) override; + ChildRef GetAnonChildRef(const HookFilter &) override; + ChildRef GetChildRef(const std::string &, const HookFilter &, bool matchCase = true) override; + bool HasValue() const override; + ModelPartType GetType() const override; + static const ModelPartType type; + + protected: + template + inline static bool tryConvertFrom(ValueSource & vsp, MT * model, const Conv & conv); + template inline static bool tryConvertFrom(ValueSource & vsp, MT * model); + template + inline static TryConvertResult tryConvertTo(ValueTarget & vsp, const MT * model, const Conv & conv); + template + inline static TryConvertResult tryConvertTo(ValueTarget & vsp, const MT * model); }; - template + template class DLL_PUBLIC ModelPartForConverted : public ModelPartForConvertedBase, protected ModelPartModel { - public: - using element_type = T; + public: + using element_type = T; - ModelPartForConverted(T * h); + explicit ModelPartForConverted(T * h); - void SetValue(ValueSource && s) override; - bool GetValue(ValueTarget && s) override; + void SetValue(ValueSource && s) override; + bool GetValue(ValueTarget && s) override; }; - template M::* MV> - class DLL_PUBLIC ModelPartForConverted, M, MV> : public ModelPartForConvertedBase, protected ModelPartModel> { - public: - using element_type = Ice::optional; + template M::*MV> + class DLL_PUBLIC ModelPartForConverted, M, MV> : + public ModelPartForConvertedBase, + protected ModelPartModel> { + public: + using element_type = Ice::optional; - ModelPartForConverted(Ice::optional * h); + explicit ModelPartForConverted(Ice::optional * h); - void SetValue(ValueSource && s) override; - bool GetValue(ValueTarget && s) override; - bool HasValue() const override; + void SetValue(ValueSource && s) override; + bool GetValue(ValueTarget && s) override; + bool HasValue() const override; }; class DLL_PUBLIC ModelPartForOptionalBase : public ModelPart { - public: - void OnEachChild(const ChildHandler & ch) override; - void Complete() override; - ChildRef GetAnonChildRef(const HookFilter & flt) override; - ChildRef GetChildRef(const std::string & name, const HookFilter & flt, bool matchCase = true) override; - void SetValue(ValueSource && s) override; - bool HasValue() const override; - bool IsOptional() const override; - const Metadata & GetMetadata() const override; - - protected: - virtual bool hasModel() const = 0; - // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) - ModelPartPtr modelPart; + public: + void OnEachChild(const ChildHandler & ch) override; + void Complete() override; + ChildRef GetAnonChildRef(const HookFilter & flt) override; + ChildRef GetChildRef(const std::string & name, const HookFilter & flt, bool matchCase = true) override; + void SetValue(ValueSource && s) override; + bool HasValue() const override; + bool IsOptional() const override; + const Metadata & GetMetadata() const override; + + protected: + virtual bool hasModel() const = 0; + ModelPartPtr modelPart; }; template - class DLL_PUBLIC ModelPartForOptional : public ModelPartForOptionalBase, protected ModelPartModel > { - public: - ModelPartForOptional(Ice::optional< typename T::element_type > * h); - void Create() override; - bool GetValue(ValueTarget && s) override; - ModelPartType GetType() const override; - - protected: - bool hasModel() const override; + class DLL_PUBLIC ModelPartForOptional : + public ModelPartForOptionalBase, + protected ModelPartModel> { + public: + explicit ModelPartForOptional(Ice::optional * h); + void Create() override; + bool GetValue(ValueTarget && s) override; + ModelPartType GetType() const override; + + protected: + bool hasModel() const override; }; class DLL_PUBLIC ModelPartForComplexBase : public ModelPart { - public: - ModelPartType GetType() const override; - static const ModelPartType type; + public: + ModelPartType GetType() const override; + static const ModelPartType type; - protected: - ModelPartPtr getSubclassModelPart(const std::string & name, void * m); + protected: + ModelPartPtr getSubclassModelPart(const std::string & name, void * m); - static void registerClass(const std::string & className, const std::string * typeName, const ClassRef &); - static void unregisterClass(const std::string & className, const std::string * typeName); - static TypeId GetTypeId(const std::string & id, const std::string & className); - static std::string demangle(const char * mangled); + static void registerClass(const std::string & className, const std::string * typeName, const ClassRef &); + static void unregisterClass(const std::string & className, const std::string * typeName); + static TypeId GetTypeId(const std::string & id, const std::string & className); + static std::string demangle(const char * mangled); - static const std::string & ToExchangeTypeName(const std::string &); - static const std::string & ToModelTypeName(const std::string &); + static const std::string & ToExchangeTypeName(const std::string &); + static const std::string & ToModelTypeName(const std::string &); }; - template - class DLL_PUBLIC ModelPartForComplex : public ModelPartForComplexBase { - public: - class DLL_PRIVATE HookBase; - using HookPtr = std::unique_ptr; + template class DLL_PUBLIC ModelPartForComplex : public ModelPartForComplexBase { + public: + class DLL_PRIVATE HookBase; + using HookPtr = std::unique_ptr; - template - class DLL_PRIVATE Hook; + template class DLL_PRIVATE Hook; - template - class DLL_PRIVATE HookMetadata; + template class DLL_PRIVATE HookMetadata; - void OnEachChild(const ChildHandler & ch) override; + void OnEachChild(const ChildHandler & ch) override; - ChildRef GetAnonChildRef(const HookFilter & flt) override; - ChildRef GetChildRef(const std::string & name, const HookFilter & flt, bool matchCase = true) override; + ChildRef GetAnonChildRef(const HookFilter & flt) override; + ChildRef GetChildRef(const std::string & name, const HookFilter & flt, bool matchCase = true) override; - const Metadata & GetMetadata() const override; + const Metadata & GetMetadata() const override; - virtual T * GetModel() = 0; + virtual T * GetModel() = 0; - protected: - template - DLL_PRIVATE ChildRef GetChildRefFromRange(const R & range, const HookFilter & flt); + protected: + template DLL_PRIVATE ChildRef GetChildRefFromRange(const R & range, const HookFilter & flt); - class DLL_PRIVATE Hooks; + class DLL_PRIVATE Hooks; - template - static void addHook(Hooks &, const P & ...); + template static void addHook(Hooks &, const P &...); - static const Hooks hooks; - static const Metadata metadata; + static const Hooks hooks; + static const Metadata metadata; }; template - class DLL_PUBLIC ModelPartForClass : public ModelPartForComplex, protected ModelPartModel > { - public: - using element_type = std::shared_ptr; + class DLL_PUBLIC ModelPartForClass : public ModelPartForComplex, protected ModelPartModel> { + public: + using element_type = std::shared_ptr; - ModelPartForClass(element_type * h); + explicit ModelPartForClass(element_type * h); - void Create() override; + void Create() override; - T * GetModel() override; + T * GetModel() override; - ModelPartPtr GetSubclassModelPart(const std::string & name) override; + ModelPartPtr GetSubclassModelPart(const std::string & name) override; - [[nodiscard]] bool HasValue() const override; + [[nodiscard]] bool HasValue() const override; - [[nodiscard]] TypeId GetTypeId() const override; - template - const std::string & getTypeId(typename std::enable_if::value>::type * = nullptr) const; - template - std::string getTypeId(typename std::enable_if::value>::type * = nullptr) const; + [[nodiscard]] TypeId GetTypeId() const override; + template + const std::string & getTypeId( + typename std::enable_if::value>::type * = nullptr) const; + template + std::string getTypeId( + typename std::enable_if::value>::type * = nullptr) const; - [[nodiscard]] Ice::optional GetTypeIdProperty() const override; + [[nodiscard]] Ice::optional GetTypeIdProperty() const override; - static const std::string typeIdProperty; - static const std::string * className; - static const std::string * typeName; + static const std::string typeIdProperty; + static const std::string * className; + static const std::string * typeName; - static ModelPartPtr CreateModelPart(void *); + static ModelPartPtr CreateModelPart(void *); - private: - static void initClassName(); - static void deleteClassName(); - static void registerClass() __attribute__ ((constructor(210))); - static void unregisterClass() __attribute__ ((destructor(210))); + private: + static void initClassName(); + static void deleteClassName(); + static void registerClass() __attribute__((constructor(210))); + static void unregisterClass() __attribute__((destructor(210))); }; template class DLL_PUBLIC ModelPartForStruct : public ModelPartForComplex, protected ModelPartModel { - public: - using element_type = T; + public: + using element_type = T; - ModelPartForStruct(T * o); + explicit ModelPartForStruct(T * o); - T * GetModel() override; + T * GetModel() override; - [[nodiscard]] bool HasValue() const override; + [[nodiscard]] bool HasValue() const override; }; class DLL_PUBLIC ModelPartForEnumBase : public ModelPart { - public: - void OnEachChild(const ChildHandler &) override; - ChildRef GetAnonChildRef(const HookFilter &) override; - ChildRef GetChildRef(const std::string &, const HookFilter &, bool matchCase = true) override; - bool HasValue() const override; - ModelPartType GetType() const override; - static const ModelPartType type; + public: + void OnEachChild(const ChildHandler &) override; + ChildRef GetAnonChildRef(const HookFilter &) override; + ChildRef GetChildRef(const std::string &, const HookFilter &, bool matchCase = true) override; + bool HasValue() const override; + ModelPartType GetType() const override; + static const ModelPartType type; }; - template - class DLL_PUBLIC ModelPartForEnum : public ModelPartForEnumBase, protected ModelPartModel { - public: - using element_type = T; - class DLL_PRIVATE Enumerations; + template class DLL_PUBLIC ModelPartForEnum : public ModelPartForEnumBase, protected ModelPartModel { + public: + using element_type = T; + class DLL_PRIVATE Enumerations; - ModelPartForEnum(T * s); + explicit ModelPartForEnum(T * s); - const Metadata & GetMetadata() const override; + const Metadata & GetMetadata() const override; - void SetValue(ValueSource && s) override; + void SetValue(ValueSource && s) override; - bool GetValue(ValueTarget && s) override; + bool GetValue(ValueTarget && s) override; - static const Metadata metadata; - static const Enumerations enumerations; - DLL_PUBLIC static const std::string & lookup(T); - DLL_PUBLIC static T lookup(const std::string_view &); + static const Metadata metadata; + static const Enumerations enumerations; + DLL_PUBLIC static const std::string & lookup(T); + DLL_PUBLIC static T lookup(const std::string_view &); }; class DLL_PUBLIC ModelPartForSequenceBase : public ModelPart { - public: - bool HasValue() const override; - ModelPartType GetType() const override; - static const ModelPartType type; + public: + bool HasValue() const override; + ModelPartType GetType() const override; + static const ModelPartType type; }; template class DLL_PUBLIC ModelPartForSequence : public ModelPartForSequenceBase, protected ModelPartModel { - public: - using element_type = T; + public: + using element_type = T; - ModelPartForSequence(T * s); + explicit ModelPartForSequence(T * s); - void OnEachChild(const ChildHandler & ch) override; + void OnEachChild(const ChildHandler & ch) override; - ChildRef GetAnonChildRef(const HookFilter &) override; + ChildRef GetAnonChildRef(const HookFilter &) override; - ChildRef GetChildRef(const std::string &, const HookFilter &, bool matchCase = true) override; + ChildRef GetChildRef(const std::string &, const HookFilter &, bool matchCase = true) override; - const Metadata & GetMetadata() const override; + const Metadata & GetMetadata() const override; - ModelPartPtr GetContainedModelPart() override; + ModelPartPtr GetContainedModelPart() override; - static const Metadata metadata; - static const std::string elementName; + static const Metadata metadata; + static const std::string elementName; - private: - ModelPartPtr elementModelPart(typename T::value_type &) const; + private: + ModelPartPtr elementModelPart(typename T::value_type &) const; }; template class DLL_PUBLIC ModelPartForDictionaryElementInserter : public ModelPartForStruct { - public: - ModelPartForDictionaryElementInserter(T * d); + public: + explicit ModelPartForDictionaryElementInserter(T * d); - void Complete() override; + void Complete() override; - // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) - typename T::value_type value; + typename T::value_type value; - private: - T * dictionary; + private: + T * dictionary; }; class DLL_PUBLIC ModelPartForDictionaryBase : public ModelPart { - public: - bool HasValue() const override; - ModelPartType GetType() const override; - static const ModelPartType type; + public: + bool HasValue() const override; + ModelPartType GetType() const override; + static const ModelPartType type; }; template class DLL_PUBLIC ModelPartForDictionary : public ModelPartForDictionaryBase, protected ModelPartModel { - public: - using element_type = T; + public: + using element_type = T; - ModelPartForDictionary(T * d); + explicit ModelPartForDictionary(T * d); - void OnEachChild(const ChildHandler & ch) override; + void OnEachChild(const ChildHandler & ch) override; - ChildRef GetAnonChildRef(const HookFilter &) override; + ChildRef GetAnonChildRef(const HookFilter &) override; - ChildRef GetChildRef(const std::string & name, const HookFilter &, bool matchCase = true) override; + ChildRef GetChildRef(const std::string & name, const HookFilter &, bool matchCase = true) override; - const Metadata & GetMetadata() const override; + const Metadata & GetMetadata() const override; - ModelPartPtr GetContainedModelPart() override; + ModelPartPtr GetContainedModelPart() override; - static const Metadata metadata; - static const std::string pairName; + static const Metadata metadata; + static const std::string pairName; }; - template - class DLL_PUBLIC Stream { - public: - using Consumer = std::function; - using element_type = T; + template class DLL_PUBLIC Stream { + public: + using Consumer = std::function; + using element_type = T; - virtual void Produce(const Consumer & c) = 0; + virtual void Produce(const Consumer & c) = 0; }; class DLL_PUBLIC ModelPartForStreamBase : public ModelPart { - public: - ModelPartType GetType() const override; - bool HasValue() const override; - ChildRef GetAnonChildRef(const HookFilter &) override; - ChildRef GetChildRef(const std::string &, const HookFilter &, bool matchCase = true) override; - - ModelPartPtr GetContainedModelPart() override = 0; - void OnEachChild(const ChildHandler & ch) override = 0; + public: + ModelPartType GetType() const override; + bool HasValue() const override; + ChildRef GetAnonChildRef(const HookFilter &) override; + ChildRef GetChildRef(const std::string &, const HookFilter &, bool matchCase = true) override; + + ModelPartPtr GetContainedModelPart() override = 0; + void OnEachChild(const ChildHandler & ch) override = 0; }; template class DLL_PUBLIC ModelPartForStream : public ModelPartForStreamBase, ModelPartModel> { - public: - ModelPartForStream(Stream * s); + public: + explicit ModelPartForStream(Stream * s); - ModelPartPtr GetContainedModelPart() override; - void OnEachChild(const ChildHandler & ch) override; + ModelPartPtr GetContainedModelPart() override; + void OnEachChild(const ChildHandler & ch) override; }; class DLL_PUBLIC ModelPartForStreamRootBase : public ModelPartForRootBase { - public: - ModelPartForStreamRootBase(const ModelPartPtr & mp); - - void Write(Ice::OutputStream&) const override; - void Read(Ice::InputStream&) override; - bool HasValue() const override; - void OnEachChild(const ChildHandler & ch) override; - const std::string & GetRootName() const override = 0; + public: + explicit ModelPartForStreamRootBase(const ModelPartPtr & mp); + + void Write(Ice::OutputStream &) const override; + void Read(Ice::InputStream &) override; + bool HasValue() const override; + void OnEachChild(const ChildHandler & ch) override; + const std::string & GetRootName() const override = 0; }; - template - class DLL_PUBLIC ModelPartForStreamRoot : public ModelPartForStreamRootBase { - public: - ModelPartForStreamRoot(Stream * s); + template class DLL_PUBLIC ModelPartForStreamRoot : public ModelPartForStreamRootBase { + public: + explicit ModelPartForStreamRoot(Stream * s); - const std::string & GetRootName() const override; + const std::string & GetRootName() const override; - private: - Stream * stream; + private: + Stream * stream; }; } #endif - - diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index 872304b..be464d1 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -1,67 +1,98 @@ #ifndef SLICER_MODELPARTSTYPES_IMPL_H #define SLICER_MODELPARTSTYPES_IMPL_H -#include "modelPartsTypes.h" #include "common.h" +#include "modelPartsTypes.h" #include #include -#include -#include -#include +#include #include #include -#include +#include +#include +#include +#include #ifdef __clang__ -#define FINALVISMODELPARTS DLL_PUBLIC +# define FINALVISMODELPARTS DLL_PUBLIC #else -#define FINALVISMODELPARTS +# define FINALVISMODELPARTS #endif #define CUSTOMMODELPARTFOR(Type, BaseModelPart, ModelPartType) \ - template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor() { return std::make_shared(nullptr); } \ - template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor(Type & s) { return std::make_shared(&s); } \ - template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor(const Type & s) { return CreateFor(const_cast(s)); } \ - template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor(Ice::optional & s) { return std::make_shared>(&s); } \ - template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor(const Ice::optional & s) { return CreateFor(const_cast &>(s)); } \ - template<> DLL_PUBLIC ModelPartForRootPtr ModelPart::CreateRootFor(Type & s) { return std::make_shared>(&s); } \ - template<> DLL_PUBLIC ModelPartForRootPtr ModelPart::CreateRootFor(Ice::optional & s) { return std::make_shared>>(&s); } \ - template<> DLL_PUBLIC ModelPartForRootPtr ModelPart::CreateRootFor(const Type & s) { return CreateRootFor(const_cast(s)); } \ - template<> DLL_PUBLIC ModelPartForRootPtr ModelPart::CreateRootFor(const Ice::optional & s) { return CreateRootFor(const_cast &>(s)); } \ + template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor() \ + { \ + return std::make_shared(nullptr); \ + } \ + template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor(Type & s) \ + { \ + return std::make_shared(&s); \ + } \ + template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor(const Type & s) \ + { \ + return CreateFor(const_cast(s)); \ + } \ + template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor(Ice::optional & s) \ + { \ + return std::make_shared>(&s); \ + } \ + template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor(const Ice::optional & s) \ + { \ + return CreateFor(const_cast &>(s)); \ + } \ + template<> DLL_PUBLIC ModelPartForRootPtr ModelPart::CreateRootFor(Type & s) \ + { \ + return std::make_shared>(&s); \ + } \ + template<> DLL_PUBLIC ModelPartForRootPtr ModelPart::CreateRootFor(Ice::optional & s) \ + { \ + return std::make_shared>>(&s); \ + } \ + template<> DLL_PUBLIC ModelPartForRootPtr ModelPart::CreateRootFor(const Type & s) \ + { \ + return CreateRootFor(const_cast(s)); \ + } \ + template<> DLL_PUBLIC ModelPartForRootPtr ModelPart::CreateRootFor(const Ice::optional & s) \ + { \ + return CreateRootFor(const_cast &>(s)); \ + } \ template class FINALVISMODELPARTS BaseModelPart; \ template class ModelPartForRoot; \ - template class ModelPartForRoot< Ice::optional >; \ + template class ModelPartForRoot>; -#define MODELPARTFOR(Type, ModelPartType) \ - CUSTOMMODELPARTFOR(Type, ModelPartType, ModelPartType) +#define MODELPARTFOR(Type, ModelPartType) CUSTOMMODELPARTFOR(Type, ModelPartType, ModelPartType) #define MODELPARTFORSTREAM(StreamImpl) \ namespace Slicer { \ - template<> DLL_PUBLIC ModelPartForRootPtr ModelPart::CreateRootFor(const StreamImpl & stream) { \ - return std::make_shared>(const_cast(&stream)); \ + template<> \ + DLL_PUBLIC ModelPartForRootPtr \ + ModelPart::CreateRootFor(const StreamImpl & stream) \ + { \ + return std::make_shared>( \ + const_cast(&stream)); \ } \ } #ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wundefined-var-template" +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wundefined-var-template" #endif namespace Slicer { // ModelPartForRoot template - ModelPartForRoot::ModelPartForRoot(T * o) : - ModelPartForRootBase(ModelPart::CreateFor(*o)), - ModelObject(o) + ModelPartForRoot::ModelPartForRoot(T * o) : ModelPartForRootBase(ModelPart::CreateFor(*o)), ModelObject(o) { } template - const std::string & ModelPartForRoot::GetRootName() const + const std::string & + ModelPartForRoot::GetRootName() const { return rootName; } template - bool ModelPartForRoot::HasValue() const + bool + ModelPartForRoot::HasValue() const { return ModelObject && mp->HasValue(); } @@ -131,100 +162,144 @@ namespace Slicer { } template - void ModelPartForRoot::Write(::Ice::OutputStream & s) const + void + ModelPartForRoot::Write(::Ice::OutputStream & s) const { typeWrite(s, *ModelObject); } template - void ModelPartForRoot::Read(::Ice::InputStream & s) + void + ModelPartForRoot::Read(::Ice::InputStream & s) { typeRead(s, *ModelObject); } // ModelPartForSimple - template - ModelPartForSimple::ModelPartForSimple(T * h) : - ModelPartModel(h) - { - } + template ModelPartForSimple::ModelPartForSimple(T * h) : ModelPartModel(h) { } template - void ModelPartForSimple::SetValue(ValueSource && s) + void + ModelPartForSimple::SetValue(ValueSource && s) { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); s.set(*this->Model); } template - bool ModelPartForSimple::GetValue(ValueTarget && s) + bool + ModelPartForSimple::GetValue(ValueTarget && s) { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); s.get(*this->Model); return true; } // ModelPartForConverted - template - ModelPartForConverted::ModelPartForConverted(T * h) : - ModelPartModel(h) + template + ModelPartForConverted::ModelPartForConverted(T * h) : ModelPartModel(h) { } - template M::* MV> + template M::*MV> ModelPartForConverted, M, MV>::ModelPartForConverted(Ice::optional * h) : ModelPartModel>(h) { } - template M::* MV> - bool ModelPartForConverted, M, MV>::HasValue() const + template M::*MV> + bool + ModelPartForConverted, M, MV>::HasValue() const { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); return (bool)*this->Model; } // Function traits helpers - template struct function_traits; - template struct function_traits> { + template struct function_traits; + template struct function_traits> { template struct arg { using type = typename std::tuple_element>::type; }; }; - template struct callable_traits : public function_traits::type>> { }; + template + struct callable_traits : public function_traits::type>> { + }; // Converters that remove "optionalness". - template - struct Coerce { + template struct Coerce { using T = typename std::remove_const::type>::type; - T & operator()(T & x) const { return x; } - const T & operator()(const T & x) const { return x; } - template - T & operator()(Ice::optional & x) const { if (!x) x = Y(); return *x; } - template - const T & operator()(const Ice::optional & x) const { return *x; } - static bool valueExists(const T &) { return true; } - static bool valueExists(const Ice::optional & y) { return y.has_value(); } + T & + operator()(T & x) const + { + return x; + } + const T & + operator()(const T & x) const + { + return x; + } + template + T & + operator()(Ice::optional & x) const + { + if (!x) { + x = Y(); + } + return *x; + } + template + const T & + operator()(const Ice::optional & x) const + { + return *x; + } + static bool + valueExists(const T &) + { + return true; + } + static bool + valueExists(const Ice::optional & y) + { + return y.has_value(); + } }; - template - struct Coerce> { + template struct Coerce> { using T = typename std::remove_const::type>::type; - Ice::optional & operator()(Ice::optional & x) const { return x; } - const Ice::optional & operator()(const Ice::optional & x) const { return x; } - template - Ice::optional operator()(Y & y) const { return y; } - static bool valueExists(const T &) { return true; } - static bool valueExists(const Ice::optional &) { return true; } + Ice::optional & + operator()(Ice::optional & x) const + { + return x; + } + const Ice::optional & + operator()(const Ice::optional & x) const + { + return x; + } + template + Ice::optional + operator()(Y & y) const + { + return y; + } + static bool + valueExists(const T &) + { + return true; + } + static bool + valueExists(const Ice::optional &) + { + return true; + } }; template - inline - bool ModelPartForConvertedBase::tryConvertFrom(ValueSource & vsp, MT * model, const Conv & conv) + inline bool + ModelPartForConvertedBase::tryConvertFrom(ValueSource & vsp, MT * model, const Conv & conv) { if (auto vspt = dynamic_cast *>(&vsp)) { using CA = typename callable_traits::template arg<0>::type; @@ -240,8 +315,8 @@ namespace Slicer { } template - inline - bool ModelPartForConvertedBase::tryConvertFrom(ValueSource & vsp, MT * model) + inline bool + ModelPartForConvertedBase::tryConvertFrom(ValueSource & vsp, MT * model) { if (auto vspt = dynamic_cast *>(&vsp)) { if (Coerce::valueExists(*model)) { @@ -253,8 +328,8 @@ namespace Slicer { } template - inline - TryConvertResult ModelPartForConvertedBase::tryConvertTo(ValueTarget & vsp, const MT * model, const Conv & conv) + inline TryConvertResult + ModelPartForConvertedBase::tryConvertTo(ValueTarget & vsp, const MT * model, const Conv & conv) { if (auto vspt = dynamic_cast *>(&vsp)) { using CA = typename callable_traits::template arg<0>::type; @@ -263,32 +338,32 @@ namespace Slicer { auto converted = conv(Coerce()(*model)); if (Coerce::valueExists(converted)) { vspt->get(Coerce()(converted)); - return tcr_Value; + return TryConvertResult::Value; } } - return tcr_NoValue; + return TryConvertResult::NoValue; } - return tcr_NoAction; + return TryConvertResult::NoAction; } template - inline - TryConvertResult ModelPartForConvertedBase::tryConvertTo(ValueTarget & vsp, const MT * model) + inline TryConvertResult + ModelPartForConvertedBase::tryConvertTo(ValueTarget & vsp, const MT * model) { if (auto vspt = dynamic_cast *>(&vsp)) { if (Coerce::valueExists(*model)) { vspt->get(Coerce()(*model)); - return tcr_Value; + return TryConvertResult::Value; } - return tcr_NoValue; + return TryConvertResult::NoValue; } - return tcr_NoAction; + return TryConvertResult::NoAction; } // ModelPartForOptional template - ModelPartForOptional::ModelPartForOptional(Ice::optional< typename T::element_type > * h) : - ModelPartModel >(h) + ModelPartForOptional::ModelPartForOptional(Ice::optional * h) : + ModelPartModel>(h) { if (this->Model && *this->Model) { modelPart = std::make_shared(&**this->Model); @@ -296,17 +371,17 @@ namespace Slicer { } template - bool ModelPartForOptional::hasModel() const + bool + ModelPartForOptional::hasModel() const { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); return (bool)*this->Model; } template - void ModelPartForOptional::Create() + void + ModelPartForOptional::Create() { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); if (!*this->Model) { *this->Model = typename T::element_type(); @@ -316,9 +391,9 @@ namespace Slicer { } template - bool ModelPartForOptional::GetValue(ValueTarget && s) + bool + ModelPartForOptional::GetValue(ValueTarget && s) { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); if (*this->Model) { return modelPart->GetValue(std::move(s)); @@ -327,34 +402,50 @@ namespace Slicer { } template - ModelPartType ModelPartForOptional::GetType() const + ModelPartType + ModelPartForOptional::GetType() const { return T::type; } // ModelPartForComplex template - class ModelPartForComplex::Hooks : public boost::multi_index_container< - HookPtr, - boost::multi_index::indexed_by< - boost::multi_index::sequenced<>, - boost::multi_index::ordered_non_unique, std::less<>>, - boost::multi_index::ordered_non_unique, case_less>>> { + class ModelPartForComplex::Hooks : + public boost::multi_index_container, + boost::multi_index::ordered_non_unique< + boost::multi_index::member, + std::less<>>, + boost::multi_index::ordered_non_unique< + boost::multi_index::member, + case_less>>> { }; template - void ModelPartForComplex::OnEachChild(const ChildHandler & ch) + void + ModelPartForComplex::OnEachChild(const ChildHandler & ch) { for (const auto & h : hooks) { h->apply(ch, h->Get(GetModel())); } } - template auto begin(const P & p) { return p.first; } - template auto end(const P & p) { return p.second; } + template + auto + begin(const P & p) + { + return p.first; + } + template + auto + end(const P & p) + { + return p.second; + } template template - ChildRef ModelPartForComplex::GetChildRefFromRange(const R & range, const HookFilter & flt) + ChildRef + ModelPartForComplex::GetChildRefFromRange(const R & range, const HookFilter & flt) { auto model = GetModel(); for (const auto & h : range) { @@ -366,13 +457,15 @@ namespace Slicer { } template - ChildRef ModelPartForComplex::GetAnonChildRef(const HookFilter & flt) + ChildRef + ModelPartForComplex::GetAnonChildRef(const HookFilter & flt) { return GetChildRefFromRange(hooks.template get<0>(), flt); } template - ChildRef ModelPartForComplex::GetChildRef(const std::string & name, const HookFilter & flt, bool matchCase) + ChildRef + ModelPartForComplex::GetChildRef(const std::string & name, const HookFilter & flt, bool matchCase) { if (matchCase) { return GetChildRefFromRange(hooks.template get<1>().equal_range(name), flt); @@ -383,138 +476,137 @@ namespace Slicer { } template - const Metadata & ModelPartForComplex::GetMetadata() const + const Metadata & + ModelPartForComplex::GetMetadata() const { return metadata; } template - template - void ModelPartForComplex::addHook(Hooks & h, const P & ... p) + template + void + ModelPartForComplex::addHook(Hooks & h, const P &... p) { h.push_back(std::make_unique(p...)); } - template - class DLL_PRIVATE ModelPartForComplex::HookBase : public HookCommon { - public: - HookBase(const std::string & n) : - HookCommon(n) - { - } - virtual ~HookBase() = default; + template class DLL_PRIVATE ModelPartForComplex::HookBase : public HookCommon { + public: + explicit HookBase(const std::string & n) : HookCommon(n) { } + SPECIAL_MEMBERS_DEFAULT(HookBase); + virtual ~HookBase() = default; - virtual ModelPartPtr Get(T * t) const = 0; - const Metadata & GetMetadata() const override - { - return emptyMetadata; - } + virtual ModelPartPtr Get(T * t) const = 0; + [[nodiscard]] const Metadata & + GetMetadata() const override + { + return emptyMetadata; + } }; template template class DLL_PRIVATE ModelPartForComplex::Hook : public ModelPartForComplex::HookBase { - public: - Hook(MT T::* m, const std::string & n) : - HookBase(n), - member(m) - { - } - - ModelPartPtr Get(T * t) const override - { - return std::make_shared(t ? const_cast::type *>(&(t->*member)) : NULL); - } + public: + Hook(MT T::*m, const std::string & n) : HookBase(n), member(m) { } + + ModelPartPtr + Get(T * t) const override + { + return std::make_shared( + t ? const_cast::type *>(&(t->*member)) : nullptr); + } - private: - const MT T::* member; + private: + const MT T::*member; }; template template class DLL_PRIVATE ModelPartForComplex::HookMetadata : public ModelPartForComplex::template Hook { - public: - HookMetadata(MT T::* member, const std::string & n, const Metadata & md) : - Hook(member, n), - metadata(md) - { - } + public: + HookMetadata(MT T::*member, const std::string & n, Metadata md) : + Hook(member, n), metadata(std::move(md)) + { + } - const Metadata & GetMetadata() const override - { - return metadata; - } + [[nodiscard]] const Metadata & + GetMetadata() const override + { + return metadata; + } - const Metadata metadata; + const Metadata metadata; }; // ModelPartForClass - template - ModelPartForClass::ModelPartForClass(element_type * h) : - ModelPartModel(h) - { - } + template ModelPartForClass::ModelPartForClass(element_type * h) : ModelPartModel(h) { } template - void ModelPartForClass::Create() + void + ModelPartForClass::Create() { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); *this->Model = std::make_shared(); } template - T * ModelPartForClass::GetModel() + T * + ModelPartForClass::GetModel() { return this->Model ? this->Model->get() : nullptr; } template - ModelPartPtr ModelPartForClass::GetSubclassModelPart(const std::string & name) + ModelPartPtr + ModelPartForClass::GetSubclassModelPart(const std::string & name) { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); return ModelPartForComplexBase::getSubclassModelPart(name, this->Model); } template - bool ModelPartForClass::HasValue() const + bool + ModelPartForClass::HasValue() const { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); return (bool)*this->Model; } template - Ice::optional ModelPartForClass::GetTypeIdProperty() const + Ice::optional + ModelPartForClass::GetTypeIdProperty() const { return typeIdProperty; } template - ModelPartPtr ModelPartForClass::CreateModelPart(void * p) + ModelPartPtr + ModelPartForClass::CreateModelPart(void * p) { return std::make_shared>(static_cast(p)); } template - void ModelPartForClass::deleteClassName() + void + ModelPartForClass::deleteClassName() { delete className; delete typeName; } template - void ModelPartForClass::registerClass() + void + ModelPartForClass::registerClass() { initClassName(); ModelPartForComplexBase::registerClass(*className, typeName, &ModelPartForClass::CreateModelPart); } template - void ModelPartForClass::unregisterClass() + void + ModelPartForClass::unregisterClass() { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(className); ModelPartForComplexBase::unregisterClass(*className, typeName); deleteClassName(); @@ -524,78 +616,72 @@ namespace Slicer { TypeId ModelPartForClass::GetTypeId() const { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(className); return ModelPartForComplexBase::GetTypeId(getTypeId(), *className); } template template - const std::string & ModelPartForClass::getTypeId(typename std::enable_if::value>::type *) const + const std::string & + ModelPartForClass::getTypeId(typename std::enable_if::value>::type *) const { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); return (*this->Model)->ice_id(); } template template - std::string ModelPartForClass::getTypeId(typename std::enable_if::value>::type *) const + std::string + ModelPartForClass::getTypeId(typename std::enable_if::value>::type *) const { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); return ModelPartForComplexBase::demangle(typeid(*this->Model->get()).name()); } // ModelPartForStruct - template - ModelPartForStruct::ModelPartForStruct(T * o) : - ModelPartModel(o) - { - } + template ModelPartForStruct::ModelPartForStruct(T * o) : ModelPartModel(o) { } template - T * ModelPartForStruct::GetModel() + T * + ModelPartForStruct::GetModel() { return this->Model; } template - bool ModelPartForStruct::HasValue() const + bool + ModelPartForStruct::HasValue() const { return true; } // ModelPartForEnum - template - using EnumPair = std::pair; + template using EnumPair = std::pair; template - class ModelPartForEnum::Enumerations : public boost::multi_index_container< - EnumPair, - boost::multi_index::indexed_by< - boost::multi_index::ordered_unique, const T, &EnumPair::first>>, - boost::multi_index::ordered_unique, const std::string, &EnumPair::second>, std::less<>> - >> { + class ModelPartForEnum::Enumerations : + public boost::multi_index_container, + boost::multi_index::indexed_by, const T, &EnumPair::first>>, + boost::multi_index::ordered_unique< + boost::multi_index::member, const std::string, &EnumPair::second>, + std::less<>>>> { }; - template - ModelPartForEnum::ModelPartForEnum(T * s) : - ModelPartModel(s) - { - } + template ModelPartForEnum::ModelPartForEnum(T * s) : ModelPartModel(s) { } template - const Metadata & ModelPartForEnum::GetMetadata() const + const Metadata & + ModelPartForEnum::GetMetadata() const { return metadata; } template - inline const auto & ModelPartForEnumLookup(const typename ModelPartForEnum::Enumerations & enumerations, - const V & val, R EnumPair::* rv) + inline const auto & + ModelPartForEnumLookup( + const typename ModelPartForEnum::Enumerations & enumerations, const V & val, R EnumPair::*rv) { const auto & side = enumerations.template get(); if (auto i = side.find(val); i != side.end()) { @@ -605,23 +691,25 @@ namespace Slicer { } template - T ModelPartForEnum::lookup(const std::string_view & val) + T + ModelPartForEnum::lookup(const std::string_view & val) { - return ModelPartForEnumLookup<1, InvalidEnumerationSymbol, std::string, T>(enumerations, val, - &EnumPair::first); + return ModelPartForEnumLookup<1, InvalidEnumerationSymbol, std::string, T>( + enumerations, val, &EnumPair::first); } template - const std::string & ModelPartForEnum::lookup(T val) + const std::string & + ModelPartForEnum::lookup(T val) { - return ModelPartForEnumLookup<0, InvalidEnumerationValue, ::Ice::Int, T>(enumerations, val, - &EnumPair::second); + return ModelPartForEnumLookup<0, InvalidEnumerationValue, ::Ice::Int, T>( + enumerations, val, &EnumPair::second); } template - void ModelPartForEnum::SetValue(ValueSource && s) + void + ModelPartForEnum::SetValue(ValueSource && s) { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); std::string val; s.set(val); @@ -629,54 +717,53 @@ namespace Slicer { } template - bool ModelPartForEnum::GetValue(ValueTarget && s) + bool + ModelPartForEnum::GetValue(ValueTarget && s) { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); s.get(lookup(*this->Model)); return true; } // ModelPartForSequence - template - ModelPartForSequence::ModelPartForSequence(T * s) : - ModelPartModel(s) - { - } + template ModelPartForSequence::ModelPartForSequence(T * s) : ModelPartModel(s) { } template - void ModelPartForSequence::OnEachChild(const ChildHandler & ch) + void + ModelPartForSequence::OnEachChild(const ChildHandler & ch) { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); - for(auto & element : *this->Model) { + for (auto & element : *this->Model) { ch(elementName, elementModelPart(element), NULL); } } template - ChildRef ModelPartForSequence::GetAnonChildRef(const HookFilter &) + ChildRef + ModelPartForSequence::GetAnonChildRef(const HookFilter &) { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); this->Model->push_back(typename element_type::value_type()); return ChildRef(ModelPart::CreateFor(this->Model->back())); } template - const Metadata & ModelPartForSequence::GetMetadata() const + const Metadata & + ModelPartForSequence::GetMetadata() const { return metadata; } template - ModelPartPtr ModelPartForSequence::elementModelPart(typename T::value_type & e) const + ModelPartPtr + ModelPartForSequence::elementModelPart(typename T::value_type & e) const { return ModelPart::CreateFor(e); } template - ModelPartPtr ModelPartForSequence::GetContainedModelPart() + ModelPartPtr + ModelPartForSequence::GetContainedModelPart() { return ModelPart::CreateFor(); } @@ -684,28 +771,24 @@ namespace Slicer { // ModelPartForDictionaryElementInserter template ModelPartForDictionaryElementInserter::ModelPartForDictionaryElementInserter(T * d) : - ModelPartForStruct(&value), - dictionary(d) + ModelPartForStruct(&value), dictionary(d) { } template - void ModelPartForDictionaryElementInserter::Complete() + void + ModelPartForDictionaryElementInserter::Complete() { dictionary->insert(value); } // ModelPartForDictionary - template - ModelPartForDictionary::ModelPartForDictionary(T * d) : - ModelPartModel(d) - { - } + template ModelPartForDictionary::ModelPartForDictionary(T * d) : ModelPartModel(d) { } template - void ModelPartForDictionary::OnEachChild(const ChildHandler & ch) + void + ModelPartForDictionary::OnEachChild(const ChildHandler & ch) { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); for (auto & pair : *this->Model) { ch(pairName, std::make_shared>(&pair), NULL); @@ -713,17 +796,17 @@ namespace Slicer { } template - ChildRef ModelPartForDictionary::GetAnonChildRef(const HookFilter &) + ChildRef + ModelPartForDictionary::GetAnonChildRef(const HookFilter &) { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); return ChildRef(std::make_shared>(this->Model)); } template - ChildRef ModelPartForDictionary::GetChildRef(const std::string & name, const HookFilter &, bool matchCase) + ChildRef + ModelPartForDictionary::GetChildRef(const std::string & name, const HookFilter &, bool matchCase) { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); if (!optionalCaseEq(name, pairName, matchCase)) { throw IncorrectElementName(name); @@ -732,23 +815,21 @@ namespace Slicer { } template - const Metadata & ModelPartForDictionary::GetMetadata() const + const Metadata & + ModelPartForDictionary::GetMetadata() const { return metadata; } template - ModelPartPtr ModelPartForDictionary::GetContainedModelPart() + ModelPartPtr + ModelPartForDictionary::GetContainedModelPart() { return std::make_shared>(nullptr); } // ModelPartForStream - template - ModelPartForStream::ModelPartForStream(Stream * s) : - ModelPartModel>(s) - { - } + template ModelPartForStream::ModelPartForStream(Stream * s) : ModelPartModel>(s) { } template ModelPartPtr @@ -761,7 +842,6 @@ namespace Slicer { void ModelPartForStream::OnEachChild(const ChildHandler & ch) { - // NOLINTNEXTLINE(hicpp-no-array-decay,-warnings-as-errors) BOOST_ASSERT(this->Model); this->Model->Produce([&ch](const T & element) { ch(ModelPartForSequence>::elementName, ModelPart::CreateFor(element), NULL); @@ -782,8 +862,7 @@ namespace Slicer { } } #ifdef __clang__ -#pragma clang diagnostic pop +# pragma clang diagnostic pop #endif #endif - diff --git a/slicer/slicer/serializer.cpp b/slicer/slicer/serializer.cpp index ad64fd9..329ef86 100644 --- a/slicer/slicer/serializer.cpp +++ b/slicer/slicer/serializer.cpp @@ -5,4 +5,3 @@ INSTANTIATEFACTORY(Slicer::Serializer, std::ostream &); INSTANTIATEFACTORY(Slicer::Deserializer, std::istream &); INSTANTIATEFACTORY(Slicer::Serializer, const std::filesystem::path &); INSTANTIATEFACTORY(Slicer::Deserializer, const std::filesystem::path &); - diff --git a/slicer/slicer/serializer.h b/slicer/slicer/serializer.h index 90acc18..75df79a 100644 --- a/slicer/slicer/serializer.h +++ b/slicer/slicer/serializer.h @@ -1,32 +1,32 @@ #ifndef SLICER_SERIALIZER_H #define SLICER_SERIALIZER_H +#include +#include #include #include #include -#include -#include namespace Slicer { class DLL_PUBLIC Serializer { - public: - Serializer() = default; - SPECIAL_MEMBERS_DEFAULT(Serializer); + public: + Serializer() = default; + SPECIAL_MEMBERS_DEFAULT(Serializer); - virtual ~Serializer() = default; + virtual ~Serializer() = default; - virtual void Serialize(ModelPartForRootPtr) = 0; + virtual void Serialize(ModelPartForRootPtr) = 0; }; using SerializerPtr = std::shared_ptr; class DLL_PUBLIC Deserializer { - public: - Deserializer() = default; - SPECIAL_MEMBERS_DEFAULT(Deserializer); + public: + Deserializer() = default; + SPECIAL_MEMBERS_DEFAULT(Deserializer); - virtual ~Deserializer() = default; + virtual ~Deserializer() = default; - virtual void Deserialize(ModelPartForRootPtr) = 0; + virtual void Deserialize(ModelPartForRootPtr) = 0; }; using DeserializerPtr = std::shared_ptr; @@ -37,4 +37,3 @@ namespace Slicer { } #endif - diff --git a/slicer/slicer/slicer.cpp b/slicer/slicer/slicer.cpp index 512e025..af55977 100644 --- a/slicer/slicer/slicer.cpp +++ b/slicer/slicer/slicer.cpp @@ -3,23 +3,11 @@ #include namespace Slicer { - Slicer::ChildRef::ChildRef() : - mpp(), - mdr(emptyMetadata) - { - } + Slicer::ChildRef::ChildRef() : mpp(), mdr(emptyMetadata) { } - Slicer::ChildRef::ChildRef(ModelPartPtr m) : - mpp(std::move(m)), - mdr(emptyMetadata) - { - } + Slicer::ChildRef::ChildRef(ModelPartPtr m) : mpp(std::move(m)), mdr(emptyMetadata) { } - Slicer::ChildRef::ChildRef(Slicer::ModelPartPtr mp, const Slicer::Metadata & md) : - mpp(std::move(mp)), - mdr(md) - { - } + Slicer::ChildRef::ChildRef(Slicer::ModelPartPtr mp, const Slicer::Metadata & md) : mpp(std::move(mp)), mdr(md) { } ModelPartPtr Slicer::ChildRef::Child() const @@ -117,4 +105,3 @@ namespace Slicer { InvalidStreamOperationMsg::write(s, method); } } - diff --git a/slicer/slicer/slicer.h b/slicer/slicer/slicer.h index 16d5a46..dcab8b3 100644 --- a/slicer/slicer/slicer.h +++ b/slicer/slicer/slicer.h @@ -5,36 +5,35 @@ #include namespace Slicer { - template + template Object DeserializeAnyWith(const DeserializerPtr & deserializer) { - Object object; + Object object {}; deserializer->Deserialize(ModelPart::CreateRootFor(object)); return object; } - template + template Object - DeserializeAny(SerializerParams && ... sp) + DeserializeAny(SerializerParams &&... sp) { - return DeserializeAnyWith(std::make_shared(sp ...)); + return DeserializeAnyWith(std::make_shared(sp...)); } - template + template void SerializeAnyWith(const Object & object, const SerializerPtr & serializer) { serializer->Serialize(ModelPart::CreateRootFor(object)); } - template + template void - SerializeAny(const Object & object, SerializerParams && ... sp) + SerializeAny(const Object & object, SerializerParams &&... sp) { - SerializeAnyWith(object, std::make_shared(sp ...)); + SerializeAnyWith(object, std::make_shared(sp...)); } } #endif - diff --git a/slicer/test/compilation.cpp b/slicer/test/compilation.cpp index 0475bbe..2ed73e4 100644 --- a/slicer/test/compilation.cpp +++ b/slicer/test/compilation.cpp @@ -1,20 +1,21 @@ #define BOOST_TEST_MODULE compilation #include -#include #include #include #include +#include // LCOV_EXCL_START BOOST_TEST_DONT_PRINT_LOG_VALUE(std::type_info); +BOOST_TEST_DONT_PRINT_LOG_VALUE(Slicer::ModelPartType); // LCOV_EXCL_STOP #define TypeTest(Var, Expr, Explicit, Expected) \ Var obj = Expr; \ Slicer::ModelPartPtr mpp = Slicer::ModelPart::CreateFor(obj); \ BOOST_REQUIRE_EQUAL(Slicer::Expected, mpp->GetType()); \ - \ +\ BOOST_TEST_CONTEXT(#Var) { \ auto mppvalue = mpp.get(); \ auto amppvalue = mpp.get(); \ @@ -24,18 +25,18 @@ BOOST_TEST_DONT_PRINT_LOG_VALUE(std::type_info); BOOST_REQUIRE_EQUAL(typeid(*mppvalue), typeid(*apmppvalue)); \ } -#define StackTypeTest(Var, Explicit, Expected) \ - TypeTest(Var, Var(), Explicit, Expected) +#define StackTypeTest(Var, Explicit, Expected) TypeTest(Var, Var(), Explicit, Expected) -BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_class ) +BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_class) { - TypeTest(TestModule::BuiltInsPtr, std::make_shared(), ModelPartForClass, mpt_Complex); + TypeTest(TestModule::BuiltInsPtr, std::make_shared(), ModelPartForClass, + ModelPartType::Complex); BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } -static -void -hookHandler(std::vector * names, const std::string & name, const Slicer::ModelPartPtr & mpp, const Slicer::HookCommon * h) +static void +hookHandler(std::vector * names, const std::string & name, const Slicer::ModelPartPtr & mpp, + const Slicer::HookCommon * h) { names->push_back(name); BOOST_REQUIRE(mpp); @@ -44,107 +45,110 @@ hookHandler(std::vector * names, const std::string & name, const Sl BOOST_REQUIRE_EQUAL(h->name, name); } - -BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_sequenceclasses ) +BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_sequenceclasses) { - StackTypeTest(TestModule::Classes, ModelPartForSequence, mpt_Sequence); + StackTypeTest(TestModule::Classes, ModelPartForSequence, ModelPartType::Sequence); auto cmpp = mpp->GetContainedModelPart(); BOOST_REQUIRE(cmpp); - BOOST_REQUIRE_EQUAL(Slicer::mpt_Complex, cmpp->GetType()); + BOOST_REQUIRE_EQUAL(Slicer::ModelPartType::Complex, cmpp->GetType()); std::vector names; - cmpp->OnEachChild(std::bind(&hookHandler, &names, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + cmpp->OnEachChild([&](auto && PH1, auto && PH2, auto && PH3) { + hookHandler(&names, PH1, PH2, PH3); + }); BOOST_REQUIRE_EQUAL(2, names.size()); BOOST_REQUIRE_EQUAL("a", names.front()); BOOST_REQUIRE_EQUAL("b", names.back()); } -BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_sequencestructs ) +BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_sequencestructs) { - StackTypeTest(TestModule::Structs, ModelPartForSequence, mpt_Sequence); + StackTypeTest(TestModule::Structs, ModelPartForSequence, ModelPartType::Sequence); auto cmpp = mpp->GetContainedModelPart(); BOOST_REQUIRE(cmpp); - BOOST_REQUIRE_EQUAL(Slicer::mpt_Complex, cmpp->GetType()); + BOOST_REQUIRE_EQUAL(Slicer::ModelPartType::Complex, cmpp->GetType()); std::vector names; - cmpp->OnEachChild(std::bind(&hookHandler, &names, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + cmpp->OnEachChild([&](auto && PH1, auto && PH2, auto && PH3) { + hookHandler(&names, PH1, PH2, PH3); + }); BOOST_REQUIRE_EQUAL(2, names.size()); BOOST_REQUIRE_EQUAL("a", names.front()); BOOST_REQUIRE_EQUAL("b", names.back()); } -BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_mapclasses ) +BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_mapclasses) { - StackTypeTest(TestModule::ClassMap, ModelPartForDictionary, mpt_Dictionary); + StackTypeTest(TestModule::ClassMap, ModelPartForDictionary, ModelPartType::Dictionary); auto cmpp = mpp->GetContainedModelPart(); BOOST_REQUIRE(cmpp); - BOOST_REQUIRE_EQUAL(Slicer::mpt_Complex, cmpp->GetType()); + BOOST_REQUIRE_EQUAL(Slicer::ModelPartType::Complex, cmpp->GetType()); } -BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_mapstructs ) +BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_mapstructs) { - StackTypeTest(TestModule::StructMap, ModelPartForDictionary, mpt_Dictionary); + StackTypeTest(TestModule::StructMap, ModelPartForDictionary, ModelPartType::Dictionary); } -BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_string ) +BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_string) { - StackTypeTest(std::string, ModelPartForSimple, mpt_Simple); + StackTypeTest(std::string, ModelPartForSimple, ModelPartType::Simple); BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } -BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_bool ) +BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_bool) { - StackTypeTest(bool, ModelPartForSimple, mpt_Simple); + StackTypeTest(bool, ModelPartForSimple, ModelPartType::Simple); BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } -BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_float ) +BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_float) { - StackTypeTest(Ice::Float, ModelPartForSimple, mpt_Simple); + StackTypeTest(Ice::Float, ModelPartForSimple, ModelPartType::Simple); BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } -BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_double ) +BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_double) { - StackTypeTest(Ice::Double, ModelPartForSimple, mpt_Simple); + StackTypeTest(Ice::Double, ModelPartForSimple, ModelPartType::Simple); BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } -BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_byte ) +BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_byte) { - StackTypeTest(Ice::Byte, ModelPartForSimple, mpt_Simple); + StackTypeTest(Ice::Byte, ModelPartForSimple, ModelPartType::Simple); BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } -BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_short ) +BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_short) { - StackTypeTest(Ice::Short, ModelPartForSimple, mpt_Simple); + StackTypeTest(Ice::Short, ModelPartForSimple, ModelPartType::Simple); BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } -BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_int ) +BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_int) { - StackTypeTest(Ice::Int, ModelPartForSimple, mpt_Simple); + StackTypeTest(Ice::Int, ModelPartForSimple, ModelPartType::Simple); BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } -BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_long ) +BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_long) { - StackTypeTest(Ice::Long, ModelPartForSimple, mpt_Simple); + StackTypeTest(Ice::Long, ModelPartForSimple, ModelPartType::Simple); BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } -BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_struct ) +BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_struct) { - StackTypeTest(TestModule::StructType, ModelPartForStruct, mpt_Complex); + StackTypeTest(TestModule::StructType, ModelPartForStruct, ModelPartType::Complex); BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } -BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_enum ) +BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_enum) { - StackTypeTest(TestModule::SomeNumbers, ModelPartForEnum, mpt_Simple); + StackTypeTest(TestModule::SomeNumbers, ModelPartForEnum, ModelPartType::Simple); BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } -BOOST_AUTO_TEST_CASE( normalClassTypeId ) +BOOST_AUTO_TEST_CASE(normalClassTypeId) { TestModule::BasePtr base = std::make_shared(1); BOOST_REQUIRE(base); @@ -154,7 +158,7 @@ BOOST_AUTO_TEST_CASE( normalClassTypeId ) BOOST_REQUIRE(!baseType); } -BOOST_AUTO_TEST_CASE( normalSubClassTypeId ) +BOOST_AUTO_TEST_CASE(normalSubClassTypeId) { TestModule::BasePtr base = std::make_shared(1, 2); BOOST_REQUIRE(base); @@ -165,7 +169,7 @@ BOOST_AUTO_TEST_CASE( normalSubClassTypeId ) BOOST_REQUIRE_EQUAL(*baseType, "::TestModule::D1"); } -BOOST_AUTO_TEST_CASE( normalSubSubClassTypeId ) +BOOST_AUTO_TEST_CASE(normalSubSubClassTypeId) { TestModule::BasePtr base = std::make_shared(1, 2, 3); BOOST_REQUIRE(base); @@ -176,7 +180,7 @@ BOOST_AUTO_TEST_CASE( normalSubSubClassTypeId ) BOOST_REQUIRE_EQUAL(*baseType, "::TestModule::D3"); } -BOOST_AUTO_TEST_CASE( localClassTypeId ) +BOOST_AUTO_TEST_CASE(localClassTypeId) { Locals::LocalClassPtr base = std::make_shared(1, "One"); BOOST_REQUIRE(base); @@ -186,7 +190,7 @@ BOOST_AUTO_TEST_CASE( localClassTypeId ) BOOST_REQUIRE(!baseType); } -BOOST_AUTO_TEST_CASE( localSubClassTypeId ) +BOOST_AUTO_TEST_CASE(localSubClassTypeId) { Locals::LocalClassPtr base = std::make_shared(1, "One", 3.1); BOOST_REQUIRE(base); @@ -197,7 +201,7 @@ BOOST_AUTO_TEST_CASE( localSubClassTypeId ) BOOST_REQUIRE_EQUAL(*baseType, "::Locals::LocalSubClass"); } -BOOST_AUTO_TEST_CASE( localSubSubClassTypeId ) +BOOST_AUTO_TEST_CASE(localSubSubClassTypeId) { Locals::LocalClassPtr base = std::make_shared(1, "One", 3.1, 1); BOOST_REQUIRE(base); @@ -207,4 +211,3 @@ BOOST_AUTO_TEST_CASE( localSubSubClassTypeId ) BOOST_REQUIRE(baseType); BOOST_REQUIRE_EQUAL(*baseType, "::Locals::LocalSub2Class"); } - diff --git a/slicer/test/conversions.cpp b/slicer/test/conversions.cpp index 9c148aa..8af0f0f 100644 --- a/slicer/test/conversions.cpp +++ b/slicer/test/conversions.cpp @@ -1,7 +1,7 @@ #include "conversions.h" #include -#define SHORT(x) boost::numeric_cast< ::Ice::Short , int64_t >(x) +#define SHORT(x) boost::numeric_cast<::Ice::Short, int64_t>(x) namespace Slicer { DLL_PUBLIC @@ -16,17 +16,16 @@ namespace Slicer { ::TestModule::DateTime ptimeToDateTime(const boost::posix_time::ptime & pt) { - return ::TestModule::DateTime({ - SHORT(pt.date().year()), SHORT(pt.date().month()), SHORT(pt.date().day()), - SHORT(pt.time_of_day().hours()), SHORT(pt.time_of_day().minutes()), SHORT(pt.time_of_day().seconds()) - }); + return ::TestModule::DateTime({SHORT(pt.date().year()), SHORT(pt.date().month()), SHORT(pt.date().day()), + SHORT(pt.time_of_day().hours()), SHORT(pt.time_of_day().minutes()), SHORT(pt.time_of_day().seconds())}); } DLL_PUBLIC std::string isoDateToString(const ::TestModule::IsoDate & in) { - struct tm tm {}; + struct tm tm { + }; tm.tm_mday = in.day; tm.tm_mon = in.month - 1; tm.tm_year = in.year - 1900; @@ -41,27 +40,28 @@ namespace Slicer { ::TestModule::IsoDate stringToIsoDate(const std::string & in) { - struct tm tm {}; + struct tm tm { + }; auto end = strptime(in.c_str(), "%Y-%m-%d", &tm); if (!end || *end) { // LCOV_EXCL_START throw std::runtime_error("Invalid iso-date string: " + in); // LCOV_EXCL_STOP } - return ::TestModule::IsoDate({ - SHORT(tm.tm_year + 1900), SHORT(tm.tm_mon + 1), SHORT(tm.tm_mday)}); + return ::TestModule::IsoDate({SHORT(tm.tm_year + 1900), SHORT(tm.tm_mon + 1), SHORT(tm.tm_mday)}); } DLL_PUBLIC std::string dateTimeToString(const ::TestModule::DateTime & in) { - struct tm tm {}; + struct tm tm { + }; tm.tm_sec = in.second; tm.tm_min = in.minute; tm.tm_hour = in.hour; tm.tm_mday = in.day; - tm.tm_mon = in.month- 1; + tm.tm_mon = in.month - 1; tm.tm_year = in.year - 1900; tm.tm_isdst = -1; mktime(&tm); @@ -75,15 +75,15 @@ namespace Slicer { ::TestModule::DateTime stringToDateTime(const std::string & in) { - struct tm tm {}; + struct tm tm { + }; auto end = strptime(in.c_str(), "%Y-%b-%d %H:%M:%S", &tm); if (!end || *end) { // LCOV_EXCL_START throw std::runtime_error("Invalid date string: " + in); // LCOV_EXCL_STOP } - return ::TestModule::DateTime({ - SHORT(tm.tm_year + 1900), SHORT(tm.tm_mon + 1), SHORT(tm.tm_mday), + return ::TestModule::DateTime({SHORT(tm.tm_year + 1900), SHORT(tm.tm_mon + 1), SHORT(tm.tm_mday), SHORT(tm.tm_hour), SHORT(tm.tm_min), SHORT(tm.tm_sec)}); } @@ -111,10 +111,7 @@ namespace Slicer { namespace TestModule { int completions = 0; - AbValidator::AbValidator(ClassTypePtr * m) : - Slicer::ModelPartForClass(m) - { - } + AbValidator::AbValidator(ClassTypePtr * m) : Slicer::ModelPartForClass(m) { } void AbValidator::Complete() @@ -129,10 +126,7 @@ namespace TestModule { completions += 1; } - MonthValidator::MonthValidator(::Ice::Short * m) : - Slicer::ModelPartForSimple<::Ice::Short>(m) - { - } + MonthValidator::MonthValidator(::Ice::Short * m) : Slicer::ModelPartForSimple<::Ice::Short>(m) { } void MonthValidator::Complete() @@ -147,4 +141,3 @@ namespace TestModule { completions += 1; } } - diff --git a/slicer/test/conversions.h b/slicer/test/conversions.h index bf70407..dfb18d6 100644 --- a/slicer/test/conversions.h +++ b/slicer/test/conversions.h @@ -2,41 +2,36 @@ #define SLICER_TEST_CONVERSIONS_H #include -#include #include #include +#include namespace TestModule { DLL_PUBLIC extern int completions; class DLL_PUBLIC AbValidator : public Slicer::ModelPartForClass { - public: - explicit AbValidator(ClassTypePtr *); + public: + explicit AbValidator(ClassTypePtr *); - void Complete() override; + void Complete() override; }; class DLL_PUBLIC MonthValidator : public Slicer::ModelPartForSimple<::Ice::Short> { - public: - explicit MonthValidator(::Ice::Short *); + public: + explicit MonthValidator(::Ice::Short *); - void Complete() override; + void Complete() override; }; } namespace Slicer { DLL_PUBLIC - ::TestModule::DateTime - stringToDateTime(const std::string & in); + ::TestModule::DateTime stringToDateTime(const std::string & in); DLL_PUBLIC - std::string - dateTimeToString(const ::TestModule::DateTime & in); + std::string dateTimeToString(const ::TestModule::DateTime & in); DLL_PUBLIC - Ice::optional - str2int(const std::string &); + Ice::optional str2int(const std::string &); DLL_PUBLIC - std::string - int2str(const Ice::optional &); + std::string int2str(const Ice::optional &); } #endif - diff --git a/slicer/test/helpers.cpp b/slicer/test/helpers.cpp index cbc6623..808e9d0 100644 --- a/slicer/test/helpers.cpp +++ b/slicer/test/helpers.cpp @@ -1,6 +1,6 @@ #include "helpers.h" -#include #include +#include void diff(const std::filesystem::path & left, const std::filesystem::path & right) @@ -9,9 +9,12 @@ diff(const std::filesystem::path & left, const std::filesystem::path & right) std::ifstream fr(right.string()); std::string l, r; - std::copy_if(std::istreambuf_iterator(fl), std::istreambuf_iterator(), back_inserter(l), [](char x){ return !isspace(x); }); - std::copy_if(std::istreambuf_iterator(fr), std::istreambuf_iterator(), back_inserter(r), [](char x){ return !isspace(x); }); + std::copy_if(std::istreambuf_iterator(fl), std::istreambuf_iterator(), back_inserter(l), [](char x) { + return !isspace(x); + }); + std::copy_if(std::istreambuf_iterator(fr), std::istreambuf_iterator(), back_inserter(r), [](char x) { + return !isspace(x); + }); BOOST_REQUIRE_EQUAL(l, r); } - diff --git a/slicer/test/helpers.h b/slicer/test/helpers.h index 47754a9..b5db1bf 100644 --- a/slicer/test/helpers.h +++ b/slicer/test/helpers.h @@ -1,14 +1,12 @@ #ifndef SLICER_TEST_HELPERS_H #define SLICER_TEST_HELPERS_H +#include #include #include -#include // These are just thin wrappers that throw exceptions DLL_PUBLIC -void -diff(const std::filesystem::path & left, const std::filesystem::path & right); +void diff(const std::filesystem::path & left, const std::filesystem::path & right); #endif - diff --git a/slicer/test/preprocessor.cpp b/slicer/test/preprocessor.cpp index fe789d0..454d58a 100644 --- a/slicer/test/preprocessor.cpp +++ b/slicer/test/preprocessor.cpp @@ -1,34 +1,23 @@ #define BOOST_TEST_MODULE preprocess #include -#include -#include +#include "helpers.h" #include #include +#include #include -#include "helpers.h" +#include using ComponentsCount = std::map; -ComponentsCount COMPONENTS_IN_TEST_ICE = { - { "classtype.ice", 1 }, - { "classes.ice", 3 }, - { "collections.ice", 6 }, - { "enums.ice", 2 }, - { "inheritance.ice", 12 }, - { "interfaces.ice", 0 }, - { "json.ice", 2 }, - { "locals.ice", 7 }, - { "optionals.ice", 2 }, - { "structs.ice", 4 }, - { "types.ice", 3 }, - { "xml.ice", 5 } -}; +ComponentsCount COMPONENTS_IN_TEST_ICE = {{"classtype.ice", 1}, {"classes.ice", 3}, {"collections.ice", 6}, + {"enums.ice", 2}, {"inheritance.ice", 12}, {"interfaces.ice", 0}, {"json.ice", 2}, {"locals.ice", 7}, + {"optionals.ice", 2}, {"structs.ice", 4}, {"types.ice", 3}, {"xml.ice", 5}}; unsigned int total() { unsigned int t = 0; - for(const auto & c : COMPONENTS_IN_TEST_ICE) { + for (const auto & c : COMPONENTS_IN_TEST_ICE) { t += c.second; } return t; @@ -52,21 +41,21 @@ processAll(Slicer::Slicer & s) { s.includes.push_back(rootDir / "included"); s.includes.push_back(rootDir); - for(const auto & c : COMPONENTS_IN_TEST_ICE) { + for (const auto & c : COMPONENTS_IN_TEST_ICE) { BOOST_TEST_CHECKPOINT(c.first); process(s, c); } BOOST_REQUIRE_EQUAL(total(), s.Components()); } -BOOST_AUTO_TEST_CASE( slicer_test_counts_path ) +BOOST_AUTO_TEST_CASE(slicer_test_counts_path) { Slicer::Slicer s; s.cppPath = "/dev/null"; processAll(s); } -BOOST_AUTO_TEST_CASE( slicer_test_counts_filestar ) +BOOST_AUTO_TEST_CASE(slicer_test_counts_filestar) { FILE * file = fopen("/dev/null", "a"); BOOST_REQUIRE(file); @@ -76,9 +65,8 @@ BOOST_AUTO_TEST_CASE( slicer_test_counts_filestar ) fclose(file); } -BOOST_AUTO_TEST_CASE( slicer_test_counts_nullfilestar ) +BOOST_AUTO_TEST_CASE(slicer_test_counts_nullfilestar) { Slicer::Slicer s; processAll(s); } - diff --git a/slicer/test/serializers.cpp b/slicer/test/serializers.cpp index 0b483b0..3ab54e5 100644 --- a/slicer/test/serializers.cpp +++ b/slicer/test/serializers.cpp @@ -1,126 +1,127 @@ #define BOOST_TEST_MODULE execute_serializers #include -#include -#include -#include -#include -#include -#include -#include +#include "conversions.h" +#include "helpers.h" #include +#include +#include +#include #include -#include #include -#include +#include +#include #include -#include -#include "helpers.h" -#include -#include "conversions.h" +#include +#include +#include +#include +#include +#include #ifdef SLICER_MODELPARTSTYPES_IMPL_H -#error Client code should NOT need to pull in implementation header +# error Client code should NOT need to pull in implementation header #endif namespace fs = std::filesystem; // LCOV_EXCL_START -BOOST_TEST_DONT_PRINT_LOG_VALUE ( TestModule::ClassMap::iterator ) -BOOST_TEST_DONT_PRINT_LOG_VALUE ( TestModule::SomeNumbers ) +BOOST_TEST_DONT_PRINT_LOG_VALUE(TestModule::ClassMap::iterator) +BOOST_TEST_DONT_PRINT_LOG_VALUE(TestModule::SomeNumbers) namespace std { template - ostream & operator<<(ostream & s, const Ice::optional &) { + ostream & + operator<<(ostream & s, const Ice::optional &) + { return s; } } // LCOV_EXCL_STOP class FileBased { - public: - template - void - verifyByFile(const fs::path & infile, const std::function & check = nullptr) - { - verifyByFile(infile, infile, check); +public: + template + void + verifyByFile(const fs::path & infile, const std::function & check = nullptr) + { + verifyByFile(infile, infile, check); + } + + template + void + verifyByFile(const fs::path & infile, const fs::path & expOutFile, + const std::function & check = nullptr) + { + const fs::path input = rootDir / "initial" / infile; + const fs::path expected = rootDir / "initial" / expOutFile; + const fs::path tmpf = binDir / "byFile"; + fs::create_directory(tmpf); + const fs::path output = tmpf / infile; + const fs::path outputJson = tmpf / fs::path(infile).replace_extension("json"); + const fs::path outputXml = tmpf / fs::path(infile).replace_extension("xml"); + + BOOST_TEST_CHECKPOINT("Deserialize: " << input); + T p = Slicer::DeserializeAny(input); + + if (check) { + BOOST_TEST_CHECKPOINT("Check1: " << input); + check(p); } - template - void - verifyByFile(const fs::path & infile, const fs::path & expOutFile, const std::function & check = nullptr) - { - const fs::path input = rootDir / "initial" / infile; - const fs::path expected = rootDir / "initial" / expOutFile; - const fs::path tmpf = binDir / "byFile"; - fs::create_directory(tmpf); - const fs::path output = tmpf / infile; - const fs::path outputJson = tmpf / fs::path(infile).replace_extension("json"); - const fs::path outputXml = tmpf / fs::path(infile).replace_extension("xml"); - - BOOST_TEST_CHECKPOINT("Deserialize: " << input); - T p = Slicer::DeserializeAny(input); - - if (check) { - BOOST_TEST_CHECKPOINT("Check1: " << input); - check(p); - } - - BOOST_TEST_CHECKPOINT("Serialize " << input << " -> " << outputJson); - Slicer::SerializeAny(p, outputJson); - - BOOST_TEST_CHECKPOINT("Serialize " << input << " -> " << outputXml); - Slicer::SerializeAny(p, outputXml); - - if (check) { - BOOST_TEST_CHECKPOINT("Check2: " << input); - check(p); - } - - BOOST_TEST_CHECKPOINT("Checksum: " << input << " === " << output); - diff(expected, output); + BOOST_TEST_CHECKPOINT("Serialize " << input << " -> " << outputJson); + Slicer::SerializeAny(p, outputJson); + + BOOST_TEST_CHECKPOINT("Serialize " << input << " -> " << outputXml); + Slicer::SerializeAny(p, outputXml); + + if (check) { + BOOST_TEST_CHECKPOINT("Check2: " << input); + check(p); } - template - void - verifyByHelper(const fs::path & infile, - const std::function & in, - const std::function & out, - const std::function & ifree, - const std::function & check = nullptr) - { - const fs::path input = rootDir / "initial" / infile; - const fs::path tmph = binDir / "byHandler"; - fs::create_directory(tmph); - const fs::path output = tmph / infile; - - BOOST_TEST_CHECKPOINT("Read: " << input); - Internal docRead = in(input); - - BOOST_TEST_CHECKPOINT("Deserialize: " << input); - T p = Slicer::DeserializeAny(docRead); - ifree(docRead); - - if (check) { - BOOST_TEST_CHECKPOINT("Check1: " << input); - check(p); - } - - BOOST_TEST_CHECKPOINT("Serialize: " << input); - Internal docWrite; - Slicer::SerializeAny(p, docWrite); - - if (check) { - BOOST_TEST_CHECKPOINT("Check2: " << input); - check(p); - } - - BOOST_TEST_CHECKPOINT("Write: " << output); - out(docWrite, output); - ifree(docWrite); - - BOOST_TEST_CHECKPOINT("Checksum: " << input << " === " << output); - diff(input, output); + BOOST_TEST_CHECKPOINT("Checksum: " << input << " === " << output); + diff(expected, output); + } + + template + void + verifyByHelper(const fs::path & infile, const std::function & in, + const std::function & out, + const std::function & ifree, const std::function & check = nullptr) + { + const fs::path input = rootDir / "initial" / infile; + const fs::path tmph = binDir / "byHandler"; + fs::create_directory(tmph); + const fs::path output = tmph / infile; + + BOOST_TEST_CHECKPOINT("Read: " << input); + Internal docRead = in(input); + + BOOST_TEST_CHECKPOINT("Deserialize: " << input); + T p = Slicer::DeserializeAny(docRead); + ifree(docRead); + + if (check) { + BOOST_TEST_CHECKPOINT("Check1: " << input); + check(p); } + + BOOST_TEST_CHECKPOINT("Serialize: " << input); + Internal docWrite; + Slicer::SerializeAny(p, docWrite); + + if (check) { + BOOST_TEST_CHECKPOINT("Check2: " << input); + check(p); + } + + BOOST_TEST_CHECKPOINT("Write: " << output); + out(docWrite, output); + ifree(docWrite); + + BOOST_TEST_CHECKPOINT("Checksum: " << input << " === " << output); + diff(input, output); + } }; void @@ -326,7 +327,7 @@ writeXml(xmlpp::Document * const & doc, const fs::path & path) } void -freeXml(xmlpp::Document * & doc) +freeXml(xmlpp::Document *& doc) { delete doc; } @@ -354,192 +355,194 @@ freeJson(json::Value &) { } -BOOST_FIXTURE_TEST_SUITE ( byFile, FileBased ); +BOOST_FIXTURE_TEST_SUITE(byFile, FileBased); -BOOST_AUTO_TEST_CASE( builtins_xml ) +BOOST_AUTO_TEST_CASE(builtins_xml) { verifyByFile("builtins.xml", checkBuiltIns_valuesCorrect); } -BOOST_AUTO_TEST_CASE( structtype_xml ) +BOOST_AUTO_TEST_CASE(structtype_xml) { verifyByFile("struct.xml", checkStruct); } -BOOST_AUTO_TEST_CASE( structtype_json ) +BOOST_AUTO_TEST_CASE(structtype_json) { verifyByFile("struct2.json", checkStruct); } -BOOST_AUTO_TEST_CASE( simplestring_xml ) +BOOST_AUTO_TEST_CASE(simplestring_xml) { verifyByFile("string.xml", [](const auto & s) { BOOST_REQUIRE_EQUAL("test string", s); }); } -BOOST_AUTO_TEST_CASE( simpleint_xml ) +BOOST_AUTO_TEST_CASE(simpleint_xml) { verifyByFile("int.xml", [](const auto & i) { BOOST_REQUIRE_EQUAL(27, i); }); } -BOOST_AUTO_TEST_CASE( simplestring_json ) +BOOST_AUTO_TEST_CASE(simplestring_json) { verifyByFile("string2.json", [](const auto & s) { BOOST_REQUIRE_EQUAL("test string", s); }); } -BOOST_AUTO_TEST_CASE( simpleint_json ) +BOOST_AUTO_TEST_CASE(simpleint_json) { verifyByFile("int2.json", [](const auto & i) { BOOST_REQUIRE_EQUAL(27, i); }); } -BOOST_AUTO_TEST_CASE( complexClass_xmlattrAndText ) +BOOST_AUTO_TEST_CASE(complexClass_xmlattrAndText) { verifyByFile("entityref.xml", checkEntityRef); } -BOOST_AUTO_TEST_CASE( sequenceOfClass_xml ) +BOOST_AUTO_TEST_CASE(sequenceOfClass_xml) { verifyByFile("seqOfClass.xml", checkSeqOfClass); } -BOOST_AUTO_TEST_CASE( sequenceOfClass_json ) +BOOST_AUTO_TEST_CASE(sequenceOfClass_json) { verifyByFile("seqOfClass2.json", checkSeqOfClass); } -BOOST_AUTO_TEST_CASE( optionals_notset_xml ) +BOOST_AUTO_TEST_CASE(optionals_notset_xml) { verifyByFile("optionals-notset.xml", checkOptionals_notset); } -BOOST_AUTO_TEST_CASE( optionals_areset_xml ) +BOOST_AUTO_TEST_CASE(optionals_areset_xml) { verifyByFile("optionals-areset.xml", checkOptionals_areset); } -BOOST_AUTO_TEST_CASE( inherit_a_xml ) +BOOST_AUTO_TEST_CASE(inherit_a_xml) { verifyByFile("inherit-a.xml"); } -BOOST_AUTO_TEST_CASE( inherit_b_xml ) +BOOST_AUTO_TEST_CASE(inherit_b_xml) { verifyByFile("inherit-b.xml", checkInherits_types); } -BOOST_AUTO_TEST_CASE( conv_datetime_xml ) +BOOST_AUTO_TEST_CASE(conv_datetime_xml) { verifyByFile("conv-datetime.xml"); } -BOOST_AUTO_TEST_CASE( builtins2_json ) +BOOST_AUTO_TEST_CASE(builtins2_json) { verifyByFile("builtins2.json", checkBuiltIns_valuesCorrect); } -BOOST_AUTO_TEST_CASE( builtins3_json ) +BOOST_AUTO_TEST_CASE(builtins3_json) { verifyByFile("builtins3.json", checkBuiltIns3_valuesCorrect); } -BOOST_AUTO_TEST_CASE( optionals_areset2_json ) +BOOST_AUTO_TEST_CASE(optionals_areset2_json) { - verifyByFile("optionals-areset2.json", checkOptionals_areset); + verifyByFile( + "optionals-areset2.json", checkOptionals_areset); } -BOOST_AUTO_TEST_CASE( inherit_c_json ) +BOOST_AUTO_TEST_CASE(inherit_c_json) { verifyByFile("inherit-c.json", checkInherits_types); } -BOOST_AUTO_TEST_CASE( inherit_d_json ) +BOOST_AUTO_TEST_CASE(inherit_d_json) { verifyByFile("inherit-d.json"); } -BOOST_AUTO_TEST_CASE( inherit_mapped_json ) +BOOST_AUTO_TEST_CASE(inherit_mapped_json) { verifyByFile("inherit-mapped.json"); } -BOOST_AUTO_TEST_CASE( xml_attribute_xml ) +BOOST_AUTO_TEST_CASE(xml_attribute_xml) { verifyByFile("xmlattr.xml"); } -BOOST_AUTO_TEST_CASE( xml_barecontainers_xml ) +BOOST_AUTO_TEST_CASE(xml_barecontainers_xml) { verifyByFile("bare.xml", checkBare); } -BOOST_AUTO_TEST_CASE( xml_classOfEnums_xml ) +BOOST_AUTO_TEST_CASE(xml_classOfEnums_xml) { verifyByFile("someenums.xml", checkSomeEnums); } -BOOST_AUTO_TEST_CASE( xml_rootEnums_xml ) +BOOST_AUTO_TEST_CASE(xml_rootEnums_xml) { verifyByFile("enum.xml", checkSomeNumbers); } -BOOST_AUTO_TEST_CASE( xml_attributemap_xml ) +BOOST_AUTO_TEST_CASE(xml_attributemap_xml) { verifyByFile("attributemap.xml", attributeMap); } -BOOST_AUTO_TEST_CASE( xml_elementmap_xml ) +BOOST_AUTO_TEST_CASE(xml_elementmap_xml) { verifyByFile("elementmap.xml", elementMap); } -BOOST_AUTO_TEST_CASE( json_rootEnums_json ) +BOOST_AUTO_TEST_CASE(json_rootEnums_json) { verifyByFile("enum2.json", checkSomeNumbers); } -BOOST_AUTO_TEST_CASE( json_objectmap ) +BOOST_AUTO_TEST_CASE(json_objectmap) { verifyByFile("objectmap.json", checkObjectMap); } -BOOST_AUTO_TEST_CASE( json_objectmapMember ) +BOOST_AUTO_TEST_CASE(json_objectmapMember) { - verifyByFile("objectmapMember.json", checkObjectMapMember); + verifyByFile( + "objectmapMember.json", checkObjectMapMember); } -BOOST_AUTO_TEST_CASE( json_localClass ) +BOOST_AUTO_TEST_CASE(json_localClass) { verifyByFile("localClass.json"); } -BOOST_AUTO_TEST_CASE( json_localSub2Class ) +BOOST_AUTO_TEST_CASE(json_localSub2Class) { verifyByFile("localSub2Class.json"); } -BOOST_AUTO_TEST_CASE( json_localSubClass ) +BOOST_AUTO_TEST_CASE(json_localSubClass) { verifyByFile("localSub2Class.json"); } -BOOST_AUTO_TEST_CASE( json_simpleArray ) +BOOST_AUTO_TEST_CASE(json_simpleArray) { verifyByFile("simpleArray1.json"); } -BOOST_AUTO_TEST_CASE( xml_simpleArray ) +BOOST_AUTO_TEST_CASE(xml_simpleArray) { verifyByFile("simpleArray2.xml"); } -BOOST_AUTO_TEST_CASE( json_emptyToNull ) +BOOST_AUTO_TEST_CASE(json_emptyToNull) { verifyByFile("optionals2.json", [](const auto & o) { BOOST_REQUIRE(o); @@ -548,7 +551,7 @@ BOOST_AUTO_TEST_CASE( json_emptyToNull ) }); } -BOOST_AUTO_TEST_CASE( json_emptyToNull_withValue ) +BOOST_AUTO_TEST_CASE(json_emptyToNull_withValue) { verifyByFile("optionals3.json", [](const auto & o) { BOOST_REQUIRE(o); @@ -558,16 +561,17 @@ BOOST_AUTO_TEST_CASE( json_emptyToNull_withValue ) }); } -BOOST_AUTO_TEST_CASE( json_emptyToNull_omitted ) +BOOST_AUTO_TEST_CASE(json_emptyToNull_omitted) { - verifyByFile("optionals5.json", "optionals2.json", [](const auto & o) { - BOOST_REQUIRE(o); - BOOST_REQUIRE(!o->optConverted); - BOOST_REQUIRE_EQUAL(o->nonOptConverted, 4); - }); + verifyByFile( + "optionals5.json", "optionals2.json", [](const auto & o) { + BOOST_REQUIRE(o); + BOOST_REQUIRE(!o->optConverted); + BOOST_REQUIRE_EQUAL(o->nonOptConverted, 4); + }); } -BOOST_AUTO_TEST_CASE( json_streams ) +BOOST_AUTO_TEST_CASE(json_streams) { const auto tmpf = binDir / "byStream"; const auto inFile = rootDir / "initial" / "inherit-c.json"; @@ -583,7 +587,7 @@ BOOST_AUTO_TEST_CASE( json_streams ) diff(inFile, outFile); } -BOOST_AUTO_TEST_CASE( xml_streams ) +BOOST_AUTO_TEST_CASE(xml_streams) { const auto tmpf = binDir / "byStream"; const auto inFile = rootDir / "initial" / "inherit-b.xml"; @@ -599,50 +603,51 @@ BOOST_AUTO_TEST_CASE( xml_streams ) diff(inFile, outFile); } -BOOST_AUTO_TEST_CASE( invalid_enum ) +BOOST_AUTO_TEST_CASE(invalid_enum) { auto jdeserializer = std::make_shared(rootDir / "initial" / "invalidEnum.json"); - BOOST_REQUIRE_THROW(Slicer::DeserializeAnyWith(jdeserializer), Slicer::InvalidEnumerationSymbol); + BOOST_REQUIRE_THROW( + Slicer::DeserializeAnyWith(jdeserializer), Slicer::InvalidEnumerationSymbol); auto xdeserializer = std::make_shared(rootDir / "initial" / "invalidEnum.xml"); - BOOST_REQUIRE_THROW(Slicer::DeserializeAnyWith(xdeserializer), Slicer::InvalidEnumerationSymbol); + BOOST_REQUIRE_THROW( + Slicer::DeserializeAnyWith(xdeserializer), Slicer::InvalidEnumerationSymbol); } BOOST_AUTO_TEST_SUITE_END(); +BOOST_FIXTURE_TEST_SUITE(byHandler, FileBased); -BOOST_FIXTURE_TEST_SUITE ( byHandler, FileBased ); - -BOOST_AUTO_TEST_CASE( optionals_areset2_json ) +BOOST_AUTO_TEST_CASE(optionals_areset2_json) { - verifyByHelper("optionals-areset2.json", readJson, writeJson, freeJson, checkOptionals_areset); + verifyByHelper( + "optionals-areset2.json", readJson, writeJson, freeJson, checkOptionals_areset); } -BOOST_AUTO_TEST_CASE( optionals_areset_xml ) +BOOST_AUTO_TEST_CASE(optionals_areset_xml) { - verifyByHelper("optionals-areset.xml", readXml, writeXml, freeXml, checkOptionals_areset); + verifyByHelper("optionals-areset.xml", readXml, writeXml, freeXml, checkOptionals_areset); } -BOOST_AUTO_TEST_CASE( simple_complete_validator ) +BOOST_AUTO_TEST_CASE(simple_complete_validator) { - verifyByHelper("isodate.xml", readXml, writeXml, freeXml); + verifyByHelper("isodate.xml", readXml, writeXml, freeXml); } -BOOST_AUTO_TEST_CASE( missingConversion ) +BOOST_AUTO_TEST_CASE(missingConversion) { auto in = json::parseValue(R"J({"conv": "2016-06-30 12:34:56"})J"); - BOOST_REQUIRE_THROW(( - Slicer::DeserializeAny(in) - ), Slicer::NoConversionFound); + BOOST_REQUIRE_THROW((Slicer::DeserializeAny(in)), + Slicer::NoConversionFound); auto obj = std::make_shared("2016-06-30 12:34:56"); json::Value v; - BOOST_REQUIRE_THROW( - Slicer::SerializeAny(obj, v), - Slicer::NoConversionFound); + BOOST_REQUIRE_THROW(Slicer::SerializeAny(obj, v), Slicer::NoConversionFound); } -BOOST_AUTO_TEST_CASE( conversion ) +BOOST_AUTO_TEST_CASE(conversion) { auto in = json::parseValue(R"J({"conv": "2016-06-30 12:34:56"})J"); auto obj = Slicer::DeserializeAny(in); @@ -650,13 +655,12 @@ BOOST_AUTO_TEST_CASE( conversion ) json::Value v; Slicer::SerializeAny(obj, v); - BOOST_REQUIRE_EQUAL("2016-06-30 12:34:56", - std::get(std::get(v)["conv"])); + BOOST_REQUIRE_EQUAL("2016-06-30 12:34:56", std::get(std::get(v)["conv"])); } BOOST_AUTO_TEST_SUITE_END(); -BOOST_AUTO_TEST_CASE( customerModelPartCounters ) +BOOST_AUTO_TEST_CASE(customerModelPartCounters) { BOOST_REQUIRE_EQUAL(21, TestModule::completions); } @@ -666,7 +670,7 @@ BOOST_FIXTURE_TEST_SUITE(l, Slicer::case_less); BOOST_AUTO_TEST_CASE(case_less_test) { using namespace std::literals; - const auto & lc { *this }; + const auto & lc {*this}; BOOST_CHECK(!lc(""sv, ""sv)); BOOST_CHECK(lc("a"sv, "b"sv)); BOOST_CHECK(lc("A"sv, "b"sv)); @@ -686,4 +690,3 @@ BOOST_AUTO_TEST_CASE(enum_lookups) BOOST_CHECK_EQUAL("One", Slicer::ModelPartForEnum::lookup(TestModule::SomeNumbers::One)); BOOST_CHECK_EQUAL(TestModule::SomeNumbers::One, Slicer::ModelPartForEnum::lookup("One")); } - diff --git a/slicer/test/streams-mp.cpp b/slicer/test/streams-mp.cpp index 119ee1f..5764248 100644 --- a/slicer/test/streams-mp.cpp +++ b/slicer/test/streams-mp.cpp @@ -1,5 +1,4 @@ -#include #include "streams.h" +#include MODELPARTFORSTREAM(TestStream); - diff --git a/slicer/test/streams.cpp b/slicer/test/streams.cpp index 3ea4933..e68de0e 100644 --- a/slicer/test/streams.cpp +++ b/slicer/test/streams.cpp @@ -1,15 +1,16 @@ #define BOOST_TEST_MODULE streams #include -#include -#include -#include -#include -#include #include "helpers.h" #include "streams.h" +#include +#include +#include +#include +#include -void TestStream::Produce(const Consumer & c) +void +TestStream::Produce(const Consumer & c) { for (int x = 0; x < 10; x += 1) { auto str = std::to_string(x); @@ -17,9 +18,9 @@ void TestStream::Produce(const Consumer & c) } } -BOOST_FIXTURE_TEST_SUITE( stream, TestStream ); +BOOST_FIXTURE_TEST_SUITE(stream, TestStream); -BOOST_AUTO_TEST_CASE( streamToXml ) +BOOST_AUTO_TEST_CASE(streamToXml) { const auto outputXml = binDir / "streamOut.xml"; Slicer::SerializeAny(*this, outputXml); @@ -30,7 +31,7 @@ BOOST_AUTO_TEST_CASE( streamToXml ) BOOST_REQUIRE_EQUAL("9", seq.back()); } -BOOST_AUTO_TEST_CASE( streamToJson ) +BOOST_AUTO_TEST_CASE(streamToJson) { const auto outputJson = binDir / "streamOut.json"; Slicer::SerializeAny(*this, outputJson); @@ -42,4 +43,3 @@ BOOST_AUTO_TEST_CASE( streamToJson ) } BOOST_AUTO_TEST_SUITE_END(); - diff --git a/slicer/test/streams.h b/slicer/test/streams.h index 28c0c9d..bf80ce2 100644 --- a/slicer/test/streams.h +++ b/slicer/test/streams.h @@ -4,9 +4,8 @@ #include class TestStream : public Slicer::Stream { - public: - void Produce(const Consumer & c) override; +public: + void Produce(const Consumer & c) override; }; #endif - diff --git a/slicer/tool/Jamfile.jam b/slicer/tool/Jamfile.jam index fe45a72..e8ef3e9 100644 --- a/slicer/tool/Jamfile.jam +++ b/slicer/tool/Jamfile.jam @@ -16,8 +16,6 @@ lib slicer-compiler : ../slicer//slicer .. tidy:clang-analyzer-cplusplus.NewDelete - tidy:clang-analyzer-cplusplus.InnerPointer - tidy:clang-analyzer-cplusplus.NewDeleteLeaks : : .. ../slicer//slicer diff --git a/slicer/tool/parser.cpp b/slicer/tool/parser.cpp index 294dd7a..d342c35 100644 --- a/slicer/tool/parser.cpp +++ b/slicer/tool/parser.cpp @@ -1,31 +1,34 @@ #include "parser.h" -#include -#include +#include #include #include #include #include -#include #include -#include +#include +#include #include +#include #include namespace fs = std::filesystem; namespace Slicer { - Slicer::Slicer() : - cpp(nullptr), - headerPrefix("slicer"), - components(0), - classNo(0) + template + bool + ignoreType(const TPtr & t) { + return (!t || t->hasMetaData("slicer:ignore")); } + Slicer::Slicer() : cpp(nullptr), headerPrefix("slicer"), components(0), classNo(0) { } + void Slicer::defineConversions(const Slice::DataMemberPtr & dm) const { - if (!cpp) { return; } + if (!cpp) { + return; + } auto type = dm->type(); auto c = Slice::ContainedPtr::dynamicCast(dm->container()); @@ -33,16 +36,12 @@ namespace Slicer { for (const auto & conversion : conversions) { if (!AdHoc::containerContains(conversion.Options, "nodeclare")) { if (!AdHoc::containerContains(conversion.Options, "nodeclareto")) { - fprintbf(cpp, "DLL_PUBLIC %s %s(const %s &);\n", - conversion.ExchangeType, - conversion.ConvertToExchangeFunc, - Slice::typeToString(type)); + fprintbf(cpp, "DLL_PUBLIC %s %s(const %s &);\n", conversion.ExchangeType, + conversion.ConvertToExchangeFunc, Slice::typeToString(type)); } if (!AdHoc::containerContains(conversion.Options, "nodeclarefrom")) { - fprintbf(cpp, "DLL_PUBLIC %s %s(const %s &);\n\n", - Slice::typeToString(type), - conversion.ConvertToModelFunc, - conversion.ExchangeType); + fprintbf(cpp, "DLL_PUBLIC %s %s(const %s &);\n\n", Slice::typeToString(type), + conversion.ConvertToModelFunc, conversion.ExchangeType); } } } @@ -55,8 +54,7 @@ namespace Slicer { for (const auto & conversion : conversions) { fprintbf(cpp, "\tif (tryConvertFrom< %s >(vsp, Model, &%s)) {\n\t\treturn;\n\t}\n", - conversion.ExchangeType, - conversion.ConvertToModelFunc); + conversion.ExchangeType, conversion.ConvertToModelFunc); } // Default conversion if (!dm->hasMetaData("slicer:nodefaultconversion")) { @@ -64,8 +62,7 @@ namespace Slicer { Slice::typeToString(type)); } // Failed to convert - fprintbf(cpp, "\tthrow NoConversionFound(\"%s\");\n", - Slice::typeToString(type)); + fprintbf(cpp, "\tthrow NoConversionFound(\"%s\");\n", Slice::typeToString(type)); fprintbf(cpp, "}\n\n"); fprintbf(cpp, "template<> DLL_PUBLIC\nbool\n"); @@ -75,18 +72,22 @@ namespace Slicer { fprintbf(cpp, "\tBOOST_ASSERT(Model);\n"); for (const auto & conversion : conversions) { - fprintbf(cpp, "\tif (auto r = tryConvertTo< %s >(vtp, Model, &%s)) {\n\t\treturn (r == tcr_Value);\n\t}\n", - conversion.ExchangeType, - conversion.ConvertToExchangeFunc); + fprintbf(cpp, + "\tif (auto r = tryConvertTo< %s >(vtp, Model, &%s); r != TryConvertResult::NoAction) {\n" + "\t\treturn (r == TryConvertResult::Value);\n" + "\t}\n", + conversion.ExchangeType, conversion.ConvertToExchangeFunc); } // Default conversion if (!dm->hasMetaData("slicer:nodefaultconversion")) { - fprintbf(cpp, "\tif (auto r = tryConvertTo< %s >(vtp, Model)) {\n\t\treturn (r == tcr_Value);\n\t}\n", - Slice::typeToString(type)); + fprintbf(cpp, + "\tif (auto r = tryConvertTo< %s >(vtp, Model); r != TryConvertResult::NoAction) {\n" + "\t\treturn (r == TryConvertResult::Value);\n" + "\t}\n", + Slice::typeToString(type)); } // Failed to convert - fprintbf(cpp, "\tthrow NoConversionFound(\"%s\");\n", - Slice::typeToString(type)); + fprintbf(cpp, "\tthrow NoConversionFound(\"%s\");\n", Slice::typeToString(type)); fprintbf(cpp, "}\n\n"); } } @@ -95,7 +96,9 @@ namespace Slicer { Slicer::visitUnitStart(const Slice::UnitPtr & u) { fs::path topLevelFile(u->topLevelFile()); - if (!cpp) { return true; } + if (!cpp) { + return true; + } fprintbf(cpp, "// Begin Slicer code\n\n"); fprintbf(cpp, "#include <%s>\n\n", (headerPrefix / "modelPartsTypes.impl.h").string()); @@ -111,9 +114,11 @@ namespace Slicer { } void - Slicer::visitUnitEnd(const Slice::UnitPtr&) + Slicer::visitUnitEnd(const Slice::UnitPtr &) { - if (!cpp) { return; } + if (!cpp) { + return; + } fprintbf(cpp, "}\n\n"); fprintbf(cpp, "// End Slicer code\n\n"); @@ -122,7 +127,9 @@ namespace Slicer { bool Slicer::visitModuleStart(const Slice::ModulePtr & m) { - if (!cpp) { return true; } + if (!cpp) { + return true; + } fprintbf(cpp, "// Begin module %s\n\n", m->name()); for (const auto & c : m->structs()) { @@ -143,44 +150,48 @@ namespace Slicer { { if (stype->isLocal()) { fprintbf(cpp, "template<>\n"); - fprintbf(cpp, "struct isLocal< %s > { static constexpr bool value = true; };\n\n", - type); + fprintbf(cpp, "struct isLocal< %s > { static constexpr bool value = true; };\n\n", type); } fprintbf(cpp, "template<> DLL_PUBLIC\n"); - fprintbf(cpp, "const std::string ModelPartForRoot< %s >::rootName(\"%s\");\n\n", - type, name); + fprintbf(cpp, "const std::string ModelPartForRoot< %s >::rootName(\"%s\");\n\n", type, name); fprintbf(cpp, "template<> DLL_PUBLIC\n"); - fprintbf(cpp, "const std::string ModelPartForRoot< Ice::optional< %s > >::rootName(\"Optional%s\");\n\n", - type, name); + fprintbf(cpp, "const std::string ModelPartForRoot< Ice::optional< %s > >::rootName(\"Optional%s\");\n\n", type, + name); } void Slicer::externType(const Slice::TypePtr & type) const { - if (definedTypes.count(type->typeId())) return; - - fprintbf(cpp, "extern template class %s< %s >;\n", - getBasicModelPart(type), Slice::ClassDeclPtr::dynamicCast(type) ? type->typeId() : Slice::typeToString(type)); + if (definedTypes.count(type->typeId())) { + return; + } + fprintbf(cpp, "extern template class %s< %s >;\n", getBasicModelPart(type), + Slice::ClassDeclPtr::dynamicCast(type) ? type->typeId() : Slice::typeToString(type)); } bool Slicer::visitClassDefStart(const Slice::ClassDefPtr & c) { - if (c->isInterface()) { return false; } - if (c->hasMetaData("slicer:ignore")) { return false; } + if (c->isInterface()) { + return false; + } + if (ignoreType(c)) { + return false; + } components += 1; - if (!cpp) { return true; } + if (!cpp) { + return true; + } auto decl = c->declaration(); fprintbf(cpp, "// Class %s\n", c->name()); - visitComplexDataMembers(decl, c->allDataMembers()); + visitComplexDataMembers(decl.get(), c->allDataMembers()); fprintbf(cpp, "template<> DLL_PUBLIC\n"); auto typeId = metaDataValue("slicer:typeid:", c->getMetaData()); - fprintbf(cpp, "const std::string ModelPartForClass< %s >::typeIdProperty(\"%s\");\n\n", - decl->typeId(), + fprintbf(cpp, "const std::string ModelPartForClass< %s >::typeIdProperty(\"%s\");\n\n", decl->typeId(), typeId ? *typeId : "slicer-typeid"); auto name = metaDataValue("slicer:root:", c->getMetaData()); @@ -188,31 +199,28 @@ namespace Slicer { auto typeName = metaDataValue("slicer:typename:", c->getMetaData()); fprintbf(cpp, "template<> DLL_PUBLIC\n"); - fprintbf(cpp, "const std::string * ModelPartForClass< %s >::className = nullptr;\n", - decl->typeId()); + fprintbf(cpp, "const std::string * ModelPartForClass< %s >::className = nullptr;\n", decl->typeId()); fprintbf(cpp, "template<> DLL_PUBLIC\n"); - fprintbf(cpp, "const std::string * ModelPartForClass< %s >::typeName = nullptr;\n", - decl->typeId()); - fprintbf(cpp, "template<>\nvoid ModelPartForClass< %s >::initClassName() {\n\tclassName = new std::string(\"%s\");\n\t", + fprintbf(cpp, "const std::string * ModelPartForClass< %s >::typeName = nullptr;\n", decl->typeId()); + fprintbf(cpp, + "template<>\nvoid ModelPartForClass< %s >::initClassName() {\n\tclassName = new " + "std::string(\"%s\");\n\t", decl->typeId(), c->scoped()); if (typeName) { - fprintbf(cpp, "typeName = new std::string(\"%s\");", - *typeName); + fprintbf(cpp, "typeName = new std::string(\"%s\");", *typeName); } else { fprintbf(cpp, "typeName = nullptr;"); } fprintbf(cpp, "\n}\n"); - fprintbf(cpp, "template<> DLL_PUBLIC\nconst Metadata ModelPartForComplex< %s >::metadata ", - c->scoped()); + fprintbf(cpp, "template<> DLL_PUBLIC\nconst Metadata ModelPartForComplex< %s >::metadata ", c->scoped()); copyMetadata(c->getMetaData()); fprintbf(cpp, ";\n\n"); - fprintbf(cpp, "// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)\n"); if (auto cmp = metaDataValue("slicer:custommodelpart:", c->getMetaData())) { - fprintbf(cpp, "CUSTOMMODELPARTFOR(%s, %s< %s >, %s);\n\n", - Slice::typeToString(decl), getBasicModelPart(decl), c->scoped(), boost::algorithm::replace_all_copy(*cmp, ".", "::")); + fprintbf(cpp, "CUSTOMMODELPARTFOR(%s, %s< %s >, %s);\n\n", Slice::typeToString(decl), + getBasicModelPart(decl), c->scoped(), boost::algorithm::replace_all_copy(*cmp, ".", "::")); } else { fprintbf(cpp, "CUSTOMMODELPARTFOR(%s, ModelPartForClass<%s>, ModelPartForClass<%s>);\n\n", @@ -228,20 +236,23 @@ namespace Slicer { bool Slicer::visitStructStart(const Slice::StructPtr & c) { - if (c->hasMetaData("slicer:ignore")) { return false; } + if (ignoreType(c)) { + return false; + } components += 1; - if (!cpp) { return true; } + if (!cpp) { + return true; + } fprintbf(cpp, "// Struct %s\n", c->name()); - visitComplexDataMembers(c, c->dataMembers()); + visitComplexDataMembers(c.get(), c->dataMembers()); auto name = metaDataValue("slicer:root:", c->getMetaData()); defineRoot(c->scoped(), name ? *name : c->name(), c); - fprintbf(cpp, "template<> DLL_PUBLIC\nconst Metadata ModelPartForComplex< %s >::metadata ", - c->scoped()); + fprintbf(cpp, "template<> DLL_PUBLIC\nconst Metadata ModelPartForComplex< %s >::metadata ", c->scoped()); copyMetadata(c->getMetaData()); fprintbf(cpp, ";\n\n"); @@ -253,20 +264,18 @@ namespace Slicer { void Slicer::visitComplexDataMembers(const Slice::ConstructedPtr & it, const Slice::DataMemberList & dataMembers) const { - if (!cpp) { return; } + if (!cpp) { + return; + } for (const auto & dm : dataMembers) { externType(dm->type()); } - fprintbf(cpp, "using C%d = ModelPartForComplex< %s >;\n", - components, it->scoped()); + fprintbf(cpp, "using C%d = ModelPartForComplex< %s >;\n", components, it->scoped()); fprintbf(cpp, "template<> DLL_PUBLIC\n"); - fprintbf(cpp, "const C%d::Hooks ", - components); - fprintbf(cpp, "C%d::hooks ([](){\n", - components); - fprintbf(cpp, "\t\tC%d::Hooks r;\n", - components); + fprintbf(cpp, "const C%d::Hooks ", components); + fprintbf(cpp, "C%d::hooks ([](){\n", components); + fprintbf(cpp, "\t\tC%d::Hooks r;\n", components); for (const auto & dm : dataMembers) { auto c = Slice::ContainedPtr::dynamicCast(dm->container()); auto t = Slice::TypePtr::dynamicCast(dm->container()); @@ -274,8 +283,7 @@ namespace Slicer { t = Slice::ClassDefPtr::dynamicCast(dm->container())->declaration(); } auto name = metaDataValue("slicer:name:", dm->getMetaData()).value_or(dm->name()); - fprintbf(cpp, "\t\tC%d::addHooktype(); if (hasMetadata(dm->getMetaData())) { fprintbf(cpp, "HookMetadata<"); @@ -283,12 +291,9 @@ namespace Slicer { else { fprintbf(cpp, "Hook<"); } - fprintbf(cpp, " %s, ", - Slice::typeToString(type, dm->optional())); + fprintbf(cpp, " %s, ", Slice::typeToString(type, dm->optional())); createNewModelPartPtrFor(type, dm, getAllMetadata(dm)); - fprintbf(cpp, " > >(r, &%s, \"%s\"", - dm->scoped(), - name); + fprintbf(cpp, " > >(r, &%s, \"%s\"", dm->scoped(), name); if (hasMetadata(dm->getMetaData())) { fprintbf(cpp, ", Metadata "); copyMetadata(dm->getMetaData()); @@ -302,31 +307,34 @@ namespace Slicer { void Slicer::visitEnum(const Slice::EnumPtr & e) { - if (e->hasMetaData("slicer:ignore")) { return; } + if (ignoreType(e)) { + return; + } components += 1; - if (!cpp) { return; } + if (!cpp) { + return; + } fprintbf(cpp, "// Enumeration %s\n", e->name()); - fprintbf(cpp, "template<> DLL_PUBLIC\nconst Metadata ModelPartForEnum< %s >::metadata ", - e->scoped()); + fprintbf(cpp, "template<> DLL_PUBLIC\nconst Metadata ModelPartForEnum< %s >::metadata ", e->scoped()); copyMetadata(e->getMetaData()); fprintbf(cpp, ";\n\n"); - fprintbf(cpp, "template<> DLL_PUBLIC\nconst ModelPartForEnum< %s >::Enumerations\nModelPartForEnum< %s >::enumerations([]() -> ModelPartForEnum< %s >::Enumerations\n", - e->scoped(), - e->scoped(), - e->scoped()); - fprintbf(cpp, "{\n\tModelPartForEnum< %s >::Enumerations e;\n", - e->scoped()); + fprintbf(cpp, + "template<> DLL_PUBLIC\nconst ModelPartForEnum< %s >::Enumerations\nModelPartForEnum< %s " + ">::enumerations([]() -> ModelPartForEnum< %s >::Enumerations\n", + e->scoped(), e->scoped(), e->scoped()); + fprintbf(cpp, "{\n\tModelPartForEnum< %s >::Enumerations e;\n", e->scoped()); for (const auto & ee : e->enumerators()) { fprintbf(cpp, "\te.insert( { %s, \"%s\" } );\n", ee->scoped(), ee->name()); } fprintbf(cpp, "\treturn e;\n}());\n\n"); auto name = metaDataValue("slicer:root:", e->getMetaData()); - defineRoot(e->scoped(), name ? *name : e->name(), e); + const Slice::TypePtr t = e; + defineRoot(e->scoped(), name ? *name : e->name(), t); defineMODELPART(e->scoped(), e, e->getMetaData()); } @@ -334,20 +342,28 @@ namespace Slicer { void Slicer::visitSequence(const Slice::SequencePtr & s) { - if (s->hasMetaData("slicer:ignore")) { return; } + if (ignoreType(s)) { + return; + } components += 1; - if (!cpp) { return; } + if (!cpp) { + return; + } fprintbf(cpp, "// Sequence %s\n", s->name()); externType(s->type()); fprintbf(cpp, "template<> DLL_PUBLIC\n"); - fprintbf(cpp, "ChildRef ModelPartForSequence< %s >::GetChildRef(const std::string & name, const HookFilter & flt, bool matchCase)\n{\n", + fprintbf(cpp, + "ChildRef ModelPartForSequence< %s >::GetChildRef(const std::string & name, const HookFilter & flt, " + "bool matchCase)\n{\n", s->scoped()); auto iname = metaDataValue("slicer:item:", s->getMetaData()); if (iname) { - fprintbf(cpp, "\tif (!name.empty() && !optionalCaseEq(name, \"%s\", matchCase)) { throw IncorrectElementName(name); }\n", + fprintbf(cpp, + "\tif (!name.empty() && !optionalCaseEq(name, \"%s\", matchCase)) { throw " + "IncorrectElementName(name); }\n", *iname); } else { @@ -358,15 +374,13 @@ namespace Slicer { fprintbf(cpp, "template<> DLL_PUBLIC\n"); auto ename = metaDataValue("slicer:element:", s->getMetaData()); - fprintbf(cpp, "const std::string ModelPartForSequence< %s >::elementName(\"%s\");\n\n", - s->scoped(), + fprintbf(cpp, "const std::string ModelPartForSequence< %s >::elementName(\"%s\");\n\n", s->scoped(), ename ? *ename : "element"); auto name = metaDataValue("slicer:root:", s->getMetaData()); defineRoot(s->scoped(), name ? *name : s->name(), s); - fprintbf(cpp, "template<> DLL_PUBLIC\nconst Metadata ModelPartForSequence< %s >::metadata ", - s->scoped()); + fprintbf(cpp, "template<> DLL_PUBLIC\nconst Metadata ModelPartForSequence< %s >::metadata ", s->scoped()); copyMetadata(s->getMetaData()); fprintbf(cpp, ";\n\n"); @@ -376,40 +390,34 @@ namespace Slicer { void Slicer::visitDictionary(const Slice::DictionaryPtr & d) { - if (d->hasMetaData("slicer:ignore")) { return; } + if (ignoreType(d)) { + return; + } components += 1; - if (!cpp) { return; } + if (!cpp) { + return; + } fprintbf(cpp, "// Dictionary %s\n", d->name()); externType(d->keyType()); externType(d->valueType()); auto iname = metaDataValue("slicer:item:", d->getMetaData()); fprintbf(cpp, "template<> DLL_PUBLIC\n"); - fprintbf(cpp, "const std::string ModelPartForDictionary< %s >::pairName(\"%s\");\n\n", - d->scoped(), + fprintbf(cpp, "const std::string ModelPartForDictionary< %s >::pairName(\"%s\");\n\n", d->scoped(), iname ? *iname : "element"); - fprintbf(cpp, "using C%d = ModelPartForComplex< %s::value_type >;\n", - components, d->scoped()); + fprintbf(cpp, "using C%d = ModelPartForComplex< %s::value_type >;\n", components, d->scoped()); fprintbf(cpp, "template<> DLL_PUBLIC\n"); - fprintbf(cpp, "const C%d::Hooks ", - components); - fprintbf(cpp, "C%d::hooks ([](){\n", - components); - fprintbf(cpp, "\t\tC%d::Hooks r;\n", - components); + fprintbf(cpp, "const C%d::Hooks ", components); + fprintbf(cpp, "C%d::hooks ([](){\n", components); + fprintbf(cpp, "\t\tC%d::Hooks r;\n", components); auto addHook = [&](const std::string & name, const char * element, const Slice::TypePtr & t) { fprintbf(cpp, "\t\t"); - fprintbf(cpp, "C%d::addHook< C%d::Hook< const %s, ", - components, components, - Slice::typeToString(t)); + fprintbf(cpp, "C%d::addHook< C%d::Hook< const %s, ", components, components, Slice::typeToString(t)); createNewModelPartPtrFor(t); - fprintbf(cpp, " > >(r, &%s::value_type::%s, \"%s\");\n", - d->scoped(), - element, - name); + fprintbf(cpp, " > >(r, &%s::value_type::%s, \"%s\");\n", d->scoped(), element, name); }; addHook(metaDataValue("slicer:key:", d->getMetaData()).value_or("key"), "first", d->keyType()); addHook(metaDataValue("slicer:value:", d->getMetaData()).value_or("value"), "second", d->valueType()); @@ -420,8 +428,7 @@ namespace Slicer { auto name = metaDataValue("slicer:root:", d->getMetaData()); defineRoot(d->scoped(), name ? *name : d->name(), d); - fprintbf(cpp, "template<> DLL_PUBLIC\nconst Metadata ModelPartForDictionary< %s >::metadata ", - d->scoped()); + fprintbf(cpp, "template<> DLL_PUBLIC\nconst Metadata ModelPartForDictionary< %s >::metadata ", d->scoped()); copyMetadata(d->getMetaData()); fprintbf(cpp, ";\n\n"); @@ -442,41 +449,37 @@ namespace Slicer { } void - Slicer::createModelPartForConverted(const Slice::TypePtr & type, const std::string & container, const Slice::DataMemberPtr & dm) const + Slicer::createModelPartForConverted( + const Slice::TypePtr & type, const std::string & container, const Slice::DataMemberPtr & dm) const { fprintbf(cpp, "ModelPartForConverted< "); if (dm->optional()) { - fprintbf(cpp, "Ice::optional< %s >", - Slice::typeToString(type)); + fprintbf(cpp, "Ice::optional< %s >", Slice::typeToString(type)); } else { - fprintbf(cpp, "%s", - Slice::typeToString(type)); + fprintbf(cpp, "%s", Slice::typeToString(type)); } - fprintbf(cpp, ", %s, &%s >", - container, - dm->scoped()); + fprintbf(cpp, ", %s, &%s >", container, dm->scoped()); } void - Slicer::createNewModelPartPtrFor(const Slice::TypePtr & type, const Slice::DataMemberPtr & dm, const Slice::StringList & md) const + Slicer::createNewModelPartPtrFor( + const Slice::TypePtr & type, const Slice::DataMemberPtr & dm, const Slice::StringList & md) const { auto conversions = getConversions(md); if (dm && !conversions.empty()) { - createModelPartForConverted(type, - boost::algorithm::trim_right_copy_if(dm->container()->thisScope(), ispunct), - dm); + createModelPartForConverted( + type, boost::algorithm::trim_right_copy_if(dm->container()->thisScope(), ispunct), dm); } else if (auto cmp = metaDataValue("slicer:custommodelpart:", md)) { - fprintbf(cpp, "%s", - boost::algorithm::replace_all_copy(*cmp, ".", "::")); + fprintbf(cpp, "%s", boost::algorithm::replace_all_copy(*cmp, ".", "::")); } else { if (dm && dm->optional()) { fprintbf(cpp, "ModelPartForOptional< "); } - fprintbf(cpp, "%s< %s >", - getBasicModelPart(type), Slice::ClassDeclPtr::dynamicCast(type) ? type->typeId() : Slice::typeToString(type)); + fprintbf(cpp, "%s< %s >", getBasicModelPart(type), + Slice::ClassDeclPtr::dynamicCast(type) ? type->typeId() : Slice::typeToString(type)); if (dm && dm->optional()) { fprintbf(cpp, " > "); } @@ -486,24 +489,17 @@ namespace Slicer { std::string Slicer::getBasicModelPart(const Slice::TypePtr & type) const { - if (auto builtin = Slice::BuiltinPtr::dynamicCast(type)) { - return "ModelPartForSimple"; - } - else if (auto complexClass = Slice::ClassDeclPtr::dynamicCast(type)) { - return "ModelPartForClass"; - } - else if (auto complexStruct = Slice::StructPtr::dynamicCast(type)) { - return "ModelPartForStruct"; - } - else if (auto sequence = Slice::SequencePtr::dynamicCast(type)) { - return "ModelPartForSequence"; - } - else if (auto dictionary = Slice::DictionaryPtr::dynamicCast(type)) { - return "ModelPartForDictionary"; - } - else if (auto enumeration = Slice::EnumPtr::dynamicCast(type)) { - return "ModelPartForEnum"; - } +#define CheckTypeAndReturn(Type, ModerlPartClass) \ + if (Type::dynamicCast(type)) { \ + return ModerlPartClass; \ + } + CheckTypeAndReturn(Slice::BuiltinPtr, "ModelPartForSimple"); + CheckTypeAndReturn(Slice::ClassDeclPtr, "ModelPartForClass"); + CheckTypeAndReturn(Slice::StructPtr, "ModelPartForStruct"); + CheckTypeAndReturn(Slice::SequencePtr, "ModelPartForSequence"); + CheckTypeAndReturn(Slice::DictionaryPtr, "ModelPartForDictionary"); + CheckTypeAndReturn(Slice::EnumPtr, "ModelPartForEnum"); +#undef CheckTypeAndReturn throw CompilerError("Unknown basic type"); } @@ -568,14 +564,12 @@ namespace Slicer { void Slicer::defineMODELPART(const std::string & type, const Slice::TypePtr & stype, const Slice::StringList & metadata) { - fprintbf(cpp, "// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)\n"); if (auto cmp = metaDataValue("slicer:custommodelpart:", metadata)) { - fprintbf(cpp, "CUSTOMMODELPARTFOR(%s, %s< %s >, %s);\n\n", - type, getBasicModelPart(stype), type, boost::algorithm::replace_all_copy(*cmp, ".", "::")); + fprintbf(cpp, "CUSTOMMODELPARTFOR(%s, %s< %s >, %s);\n\n", type, getBasicModelPart(stype), type, + boost::algorithm::replace_all_copy(*cmp, ".", "::")); } else { - fprintbf(cpp, "MODELPARTFOR(%s, %s);\n\n", - type, getBasicModelPart(stype)); + fprintbf(cpp, "MODELPARTFOR(%s, %s);\n\n", type, getBasicModelPart(stype)); } definedTypes.insert(stype->typeId()); } @@ -593,8 +587,7 @@ namespace Slicer { throw CompilerError("Both file handle and path provided."); } auto cppfile = std::unique_ptr( - cpp || cppPath.empty() ? cpp : fopen(cppPath.c_str(), "w"), - cppPath.empty() ? fflush : fclose); + cpp || cppPath.empty() ? cpp : fopen(cppPath.c_str(), "w"), cppPath.empty() ? fflush : fclose); if (!cppfile && !cppPath.empty()) { throw CompilerError("Failed to open output file"); } @@ -602,7 +595,7 @@ namespace Slicer { cpp = cppfile.get(); Slicer::Slicer::Args args; // Copy includes to args - for(const auto & include : includes) { + for (const auto & include : includes) { args.push_back("-I" + include.string()); } @@ -646,13 +639,11 @@ namespace Slicer { } Slicer::ConversionSpec::ConversionSpec(const Slicer::Args & s) : - ExchangeType(s[0]), - ConvertToModelFunc(s[1]), - ConvertToExchangeFunc(s[2]) + ExchangeType(s[0]), ConvertToModelFunc(s[1]), ConvertToExchangeFunc(s[2]) { if (s.size() >= 4) { - boost::algorithm::split(Options, s[3], boost::algorithm::is_any_of(","), boost::algorithm::token_compress_off); + boost::algorithm::split( + Options, s[3], boost::algorithm::is_any_of(","), boost::algorithm::token_compress_off); } } }; - diff --git a/slicer/tool/parser.h b/slicer/tool/parser.h index 67aa243..295705d 100644 --- a/slicer/tool/parser.h +++ b/slicer/tool/parser.h @@ -7,81 +7,75 @@ namespace Slicer { class DLL_PUBLIC Slicer : public Slice::ParserVisitor { + public: + typedef std::vector Args; + class ConversionSpec { public: - typedef std::vector Args; - class ConversionSpec { - public: - explicit ConversionSpec(const Args &); - - // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) - std::string ExchangeType; - // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) - std::string ConvertToModelFunc; - // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) - std::string ConvertToExchangeFunc; - // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) - Args Options; - }; - using Conversions = std::vector; - - Slicer(); - - // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) - FILE * cpp; - // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) - std::filesystem::path slicePath; - // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) - std::filesystem::path cppPath; - // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) - std::filesystem::path headerPrefix; - // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) - std::vector includes; - - unsigned int Execute(); - [[nodiscard]] unsigned int Components() const; + explicit ConversionSpec(const Args &); + + std::string ExchangeType; + std::string ConvertToModelFunc; + std::string ConvertToExchangeFunc; + Args Options; + }; + using Conversions = std::vector; + + Slicer(); + + FILE * cpp; + std::filesystem::path slicePath; + std::filesystem::path cppPath; + std::filesystem::path headerPrefix; + std::vector includes; + + unsigned int Execute(); + [[nodiscard]] unsigned int Components() const; #pragma GCC visibility push(hidden) - bool visitUnitStart(const Slice::UnitPtr&) override; + bool visitUnitStart(const Slice::UnitPtr &) override; - void visitUnitEnd(const Slice::UnitPtr&) override; + void visitUnitEnd(const Slice::UnitPtr &) override; - bool visitModuleStart(const Slice::ModulePtr & m) override; + bool visitModuleStart(const Slice::ModulePtr & m) override; - bool visitClassDefStart(const Slice::ClassDefPtr & c) override; + bool visitClassDefStart(const Slice::ClassDefPtr & c) override; - bool visitStructStart(const Slice::StructPtr&) override; + bool visitStructStart(const Slice::StructPtr &) override; - void visitEnum(const Slice::EnumPtr &) override; + void visitEnum(const Slice::EnumPtr &) override; - void visitSequence(const Slice::SequencePtr & s) override; + void visitSequence(const Slice::SequencePtr & s) override; - void visitDictionary(const Slice::DictionaryPtr & d) override; + void visitDictionary(const Slice::DictionaryPtr & d) override; - void visitModuleEnd(const Slice::ModulePtr & m) override; + void visitModuleEnd(const Slice::ModulePtr & m) override; - private: - void createModelPartForConverted(const Slice::TypePtr & type, const std::string & container, const Slice::DataMemberPtr & dm) const; - void createNewModelPartPtrFor(const Slice::TypePtr & type, const Slice::DataMemberPtr & dm = Slice::DataMemberPtr(), const Slice::StringList & md = Slice::StringList()) const; - [[nodiscard]] std::string getBasicModelPart(const Slice::TypePtr & type) const; - void defineMODELPART(const std::string & type, const Slice::TypePtr & stype, const Slice::StringList & metadata); + private: + void createModelPartForConverted( + const Slice::TypePtr & type, const std::string & container, const Slice::DataMemberPtr & dm) const; + void createNewModelPartPtrFor(const Slice::TypePtr & type, + const Slice::DataMemberPtr & dm = Slice::DataMemberPtr(), + const Slice::StringList & md = Slice::StringList()) const; + [[nodiscard]] std::string getBasicModelPart(const Slice::TypePtr & type) const; + void defineMODELPART( + const std::string & type, const Slice::TypePtr & stype, const Slice::StringList & metadata); - void visitComplexDataMembers(const Slice::ConstructedPtr & t, const Slice::DataMemberList &) const; + void visitComplexDataMembers(const Slice::ConstructedPtr & t, const Slice::DataMemberList &) const; - void defineConversions(const Slice::DataMemberPtr & dm) const; - void defineRoot(const std::string & type, const std::string & name, const Slice::TypePtr & stype) const; - void externType(const Slice::TypePtr &) const; + void defineConversions(const Slice::DataMemberPtr & dm) const; + void defineRoot(const std::string & type, const std::string & name, const Slice::TypePtr & stype) const; + void externType(const Slice::TypePtr &) const; - [[nodiscard]] bool hasMetadata(const std::list & metadata) const; - void copyMetadata(const std::list & metadata) const; - static Slice::StringList getAllMetadata(const Slice::DataMemberPtr & dm); - static Conversions getConversions(const Slice::StringList & metadata); - std::set definedTypes; + [[nodiscard]] bool hasMetadata(const std::list & metadata) const; + void copyMetadata(const std::list & metadata) const; + static Slice::StringList getAllMetadata(const Slice::DataMemberPtr & dm); + static Conversions getConversions(const Slice::StringList & metadata); + std::set definedTypes; #pragma GCC visibility pop - unsigned int components; - unsigned int classNo; + unsigned int components; + unsigned int classNo; }; } #endif - diff --git a/slicer/tool/slicer.cpp b/slicer/tool/slicer.cpp index 49d52a6..0145d15 100644 --- a/slicer/tool/slicer.cpp +++ b/slicer/tool/slicer.cpp @@ -1,5 +1,5 @@ -#include #include +#include namespace po = boost::program_options; @@ -9,6 +9,7 @@ main(int argc, char ** argv) Slicer::Slicer slicer; po::options_description opts("Slicer options"); + // clang-format off opts.add_options() ("help,h", "Show this help message") ("include,I", po::value(&slicer.includes), "Add include directory to search path") @@ -16,6 +17,7 @@ main(int argc, char ** argv) ("headerPrefix", po::value(&slicer.headerPrefix)->default_value(slicer.headerPrefix), "Prefix path for Slicer C++ #includes") ("slice,i", po::value(&slicer.slicePath), "Input ICE Slice file") ("cpp,o", po::value(&slicer.cppPath), "Output C++ file"); + // clang-format on po::positional_options_description p; p.add("slice", 1); @@ -35,4 +37,3 @@ main(int argc, char ** argv) return 0; } - diff --git a/slicer/xml/Jamfile.jam b/slicer/xml/Jamfile.jam index c237f93..4150632 100644 --- a/slicer/xml/Jamfile.jam +++ b/slicer/xml/Jamfile.jam @@ -15,7 +15,6 @@ lib slicer-xml : ../slicer//slicer ../slicer//slicer xmlExceptions - tidy:clang-analyzer-optin.cplusplus.VirtualCall : : ../..//libxmlpp xmlExceptions diff --git a/slicer/xml/serializer.cpp b/slicer/xml/serializer.cpp index f31acb0..fbda026 100644 --- a/slicer/xml/serializer.cpp +++ b/slicer/xml/serializer.cpp @@ -1,13 +1,13 @@ #include "serializer.h" -#include -#include -#include -#include #include +#include #include -#include #include -#include +#include +#include +#include +#include +#include NAMEDFACTORY(".xml", Slicer::XmlFileSerializer, Slicer::FileSerializerFactory); NAMEDFACTORY(".xml", Slicer::XmlFileDeserializer, Slicer::FileDeserializerFactory); @@ -24,169 +24,180 @@ namespace Slicer { const std::string md_elements = "xml:elements"; const std::string keyName = "key"; const std::string valueName = "value"; - using ElementCreatorF = xmlpp::Element * (xmlpp::Element::*) (const Glib::ustring &, const Glib::ustring &); - const auto defaultElementCreator = std::bind((ElementCreatorF)&xmlpp::Element::add_child_element, _1, _2, Glib::ustring()); + using ElementCreatorF = xmlpp::Element * (xmlpp::Element::*)(const Glib::ustring &, const Glib::ustring &); + const auto defaultElementCreator = [](auto && element, auto && name) { + return element->add_child_element(name); + }; static const Glib::ustring TrueText("true"); static const Glib::ustring FalseText("false"); class XmlValueSource : public ValueSource { - public: - explicit XmlValueSource(Glib::ustring s) : - value(std::move(s)) - { - } + public: + explicit XmlValueSource(Glib::ustring s) : value(std::move(s)) { } - void set(bool & v) const override - { - if (value == TrueText) { v = true; return; } - if (value == FalseText) { v = false; return; } - throw BadBooleanValue(value); + void + set(bool & v) const override + { + if (value == TrueText) { + v = true; + return; } - - void set(Ice::Byte & v) const override - { - v = boost::numeric_cast(boost::lexical_cast(value)); + if (value == FalseText) { + v = false; + return; } + throw BadBooleanValue(value); + } - void set(Ice::Short & v) const override - { - v = boost::lexical_cast(value); - } + void + set(Ice::Byte & v) const override + { + v = boost::numeric_cast(boost::lexical_cast(value)); + } - void set(Ice::Int & v) const override - { - v = boost::lexical_cast(value); - } + void + set(Ice::Short & v) const override + { + v = boost::lexical_cast(value); + } - void set(Ice::Long & v) const override - { - v = boost::lexical_cast(value); - } + void + set(Ice::Int & v) const override + { + v = boost::lexical_cast(value); + } - void set(Ice::Float & v) const override - { - v = boost::lexical_cast(value); - } + void + set(Ice::Long & v) const override + { + v = boost::lexical_cast(value); + } - void set(Ice::Double & v) const override - { - v = boost::lexical_cast(value); - } + void + set(Ice::Float & v) const override + { + v = boost::lexical_cast(value); + } - void set(std::string & v) const override - { - v = value.raw(); - } + void + set(Ice::Double & v) const override + { + v = boost::lexical_cast(value); + } + + void + set(std::string & v) const override + { + v = value.raw(); + } - private: - const Glib::ustring value; + private: + const Glib::ustring value; }; class XmlContentValueSource : public XmlValueSource { - public: - explicit XmlContentValueSource() : - XmlValueSource(Glib::ustring()) - { - } - explicit XmlContentValueSource(const xmlpp::ContentNode * c) : - XmlValueSource(c->get_content()) - { - } + public: + explicit XmlContentValueSource() : XmlValueSource(Glib::ustring()) { } + explicit XmlContentValueSource(const xmlpp::ContentNode * c) : XmlValueSource(c->get_content()) { } }; class XmlAttributeValueSource : public XmlValueSource { - public: - explicit XmlAttributeValueSource(const xmlpp::Attribute * a) : - XmlValueSource(a->get_value()) - { - } + public: + explicit XmlAttributeValueSource(const xmlpp::Attribute * a) : XmlValueSource(a->get_value()) { } }; class XmlValueTarget : public ValueTarget { - public: - explicit XmlValueTarget(std::function a) : - apply(std::move(a)) - { - } + public: + explicit XmlValueTarget(std::function a) : apply(std::move(a)) { } - void get(const bool & value) const override - { - if (value) { - apply(TrueText); - } - else { - apply(FalseText); - } + void + get(const bool & value) const override + { + if (value) { + apply(TrueText); } - - void get(const Ice::Byte & value) const override - { - apply(boost::lexical_cast((int)value)); + else { + apply(FalseText); } + } - void get(const Ice::Short & value) const override - { - apply(boost::lexical_cast(value)); - } + void + get(const Ice::Byte & value) const override + { + apply(boost::lexical_cast((int)value)); + } - void get(const Ice::Int & value) const override - { - apply(boost::lexical_cast(value)); - } + void + get(const Ice::Short & value) const override + { + apply(boost::lexical_cast(value)); + } - void get(const Ice::Long & value) const override - { - apply(boost::lexical_cast(value)); - } + void + get(const Ice::Int & value) const override + { + apply(boost::lexical_cast(value)); + } - void get(const Ice::Float & value) const override - { - apply(boost::lexical_cast(value)); - } + void + get(const Ice::Long & value) const override + { + apply(boost::lexical_cast(value)); + } - void get(const Ice::Double & value) const override - { - apply(boost::lexical_cast(value)); - } + void + get(const Ice::Float & value) const override + { + apply(boost::lexical_cast(value)); + } - void get(const std::string & value) const override - { - apply(value); - } + void + get(const Ice::Double & value) const override + { + apply(boost::lexical_cast(value)); + } - private: - const std::function apply; - }; + void + get(const std::string & value) const override + { + apply(value); + } + private: + const std::function apply; + }; class XmlAttributeValueTarget : public XmlValueTarget { - public: - explicit XmlAttributeValueTarget(xmlpp::Element * p, const std::string & n) : - XmlValueTarget([p, n](auto && PH1) { - p->set_attribute(n, PH1); - }) - { - } + public: + explicit XmlAttributeValueTarget(xmlpp::Element * p, const std::string & n) : + XmlValueTarget([p, n](auto && PH1) { + p->set_attribute(n, PH1); + }) + { + } }; class XmlContentValueTarget : public XmlValueTarget { - public: - explicit XmlContentValueTarget(xmlpp::Element * p) : - XmlValueTarget([p](auto && PH1) { - p->set_first_child_text(PH1); - }) - { - } + public: + explicit XmlContentValueTarget(xmlpp::Element * p) : + XmlValueTarget([p](auto && PH1) { + p->set_first_child_text(PH1); + }) + { + } - explicit XmlContentValueTarget(const CurrentElementCreator & cec) : - XmlValueTarget(std::bind(&xmlpp::Element::set_first_child_text, std::bind(&CurrentElementCreator::deref, &cec), _1)) - { - } + explicit XmlContentValueTarget(const CurrentElementCreator & cec) : + XmlValueTarget([&](auto && PH1) { + cec->set_first_child_text(PH1); + }) + { + } }; void - XmlDeserializer::DocumentTreeIterateDictAttrs(const xmlpp::Element::const_AttributeList & attrs, const ModelPartPtr & dict) + XmlDeserializer::DocumentTreeIterateDictAttrs( + const xmlpp::Element::const_AttributeList & attrs, const ModelPartPtr & dict) { for (const auto & attr : attrs) { auto emp = dict->GetAnonChild(); @@ -307,7 +318,8 @@ namespace Slicer { } void - XmlSerializer::ModelTreeIterate(xmlpp::Element * n, const std::string & name, const ModelPartPtr & mp, const HookCommon * hp, const ElementCreator & ec) + XmlSerializer::ModelTreeIterate(xmlpp::Element * n, const std::string & name, const ModelPartPtr & mp, + const HookCommon * hp, const ElementCreator & ec) { if (name.empty()) { return; @@ -331,7 +343,9 @@ namespace Slicer { }); } else { - CurrentElementCreator cec([ec, n, name] { return ec(n, name); }); + CurrentElementCreator cec([ec, n, name] { + return ec(n, name); + }); ModelTreeProcessElement(cec, mp, defaultElementCreator); } } @@ -342,7 +356,7 @@ namespace Slicer { { dict->OnEachChild([element](const auto &, const auto & mp, const auto &) { if (mp->HasValue()) { - mp->GetChild(keyName)->GetValue(XmlValueTarget([&mp,element](const auto & name) { + mp->GetChild(keyName)->GetValue(XmlValueTarget([&mp, element](const auto & name) { mp->GetChild(valueName)->GetValue(XmlAttributeValueTarget(element, name)); })); } @@ -354,8 +368,10 @@ namespace Slicer { { dict->OnEachChild([element](const auto &, const auto & mp, const auto &) { if (mp->HasValue()) { - mp->GetChild(keyName)->GetValue(XmlValueTarget([&mp,element](const auto & name) { - CurrentElementCreator cec([&element, &name]() { return element->add_child_element(name); }); + mp->GetChild(keyName)->GetValue(XmlValueTarget([&mp, element](const auto & name) { + CurrentElementCreator cec([&element, &name]() { + return element->add_child_element(name); + }); ModelTreeProcessElement(cec, mp->GetChild(valueName), defaultElementCreator); })); } @@ -363,9 +379,10 @@ namespace Slicer { } void - XmlSerializer::ModelTreeProcessElement(const CurrentElementCreator & cec, ModelPartPtr mp, const ElementCreator & ec) + XmlSerializer::ModelTreeProcessElement( + const CurrentElementCreator & cec, ModelPartPtr mp, const ElementCreator & ec) { - if (mp->GetType() == mpt_Simple) { + if (mp->GetType() == ModelPartType::Simple) { mp->GetValue(XmlContentValueTarget(cec)); } else if (mp->HasValue()) { @@ -388,15 +405,9 @@ namespace Slicer { ModelTreeProcessElement(doc->create_root_node(name), mp, defaultElementCreator); } - XmlStreamSerializer::XmlStreamSerializer(std::ostream & s) : - strm(s) - { - } + XmlStreamSerializer::XmlStreamSerializer(std::ostream & s) : strm(s) { } - XmlStreamDeserializer::XmlStreamDeserializer(std::istream & s) : - strm(s) - { - } + XmlStreamDeserializer::XmlStreamDeserializer(std::istream & s) : strm(s) { } void XmlStreamDeserializer::Deserialize(ModelPartForRootPtr modelRoot) @@ -417,15 +428,9 @@ namespace Slicer { doc.write_to_stream(strm); } - XmlFileSerializer::XmlFileSerializer(std::filesystem::path p) : - path(std::move(p)) - { - } + XmlFileSerializer::XmlFileSerializer(std::filesystem::path p) : path(std::move(p)) { } - XmlFileDeserializer::XmlFileDeserializer(std::filesystem::path p) : - path(std::move(p)) - { - } + XmlFileDeserializer::XmlFileDeserializer(std::filesystem::path p) : path(std::move(p)) { } void XmlFileDeserializer::Deserialize(ModelPartForRootPtr modelRoot) @@ -445,15 +450,9 @@ namespace Slicer { doc.write_to_file_formatted(path); } - XmlDocumentSerializer::XmlDocumentSerializer(xmlpp::Document * & d) : - doc(d) - { - } + XmlDocumentSerializer::XmlDocumentSerializer(xmlpp::Document *& d) : doc(d) { } - XmlDocumentDeserializer::XmlDocumentDeserializer(const xmlpp::Document * d) : - doc(d) - { - } + XmlDocumentDeserializer::XmlDocumentDeserializer(const xmlpp::Document * d) : doc(d) { } void XmlDocumentDeserializer::Deserialize(ModelPartForRootPtr modelRoot) @@ -471,9 +470,9 @@ namespace Slicer { } AdHocFormatter(BadBooleanValueMsg, "Bad boolean value [%?]"); - void BadBooleanValue::ice_print(std::ostream & s) const + void + BadBooleanValue::ice_print(std::ostream & s) const { BadBooleanValueMsg::write(s, text); } } - diff --git a/slicer/xml/serializer.h b/slicer/xml/serializer.h index f41e5a7..99b03dd 100644 --- a/slicer/xml/serializer.h +++ b/slicer/xml/serializer.h @@ -1,96 +1,97 @@ #ifndef SLICER_XML_H #define SLICER_XML_H -#include +#include #include #include +#include #include -#include namespace Slicer { - typedef ::AdHoc::LazyPointer CurrentElementCreator; + using CurrentElementCreator = ::AdHoc::LazyPointer; class DLL_PUBLIC XmlSerializer : public Serializer { - protected: - typedef std::function ElementCreator; - static void ModelTreeIterate(xmlpp::Element *, const std::string &, const ModelPartPtr & mp, const HookCommon * hp, const ElementCreator &); - static void ModelTreeIterateRoot(xmlpp::Document *, const std::string &, const ModelPartPtr & mp); - - protected: - static void ModelTreeProcessElement(const CurrentElementCreator &, ModelPartPtr mp, const ElementCreator &); - static void ModelTreeIterateDictAttrs(xmlpp::Element * element, const ModelPartPtr & dict); - static void ModelTreeIterateDictElements(xmlpp::Element * element, const ModelPartPtr & dict); + protected: + using ElementCreator = std::function; + static void ModelTreeIterate(xmlpp::Element *, const std::string &, const ModelPartPtr & mp, + const HookCommon * hp, const ElementCreator &); + static void ModelTreeIterateRoot(xmlpp::Document *, const std::string &, const ModelPartPtr & mp); + + protected: + static void ModelTreeProcessElement(const CurrentElementCreator &, ModelPartPtr mp, const ElementCreator &); + static void ModelTreeIterateDictAttrs(xmlpp::Element * element, const ModelPartPtr & dict); + static void ModelTreeIterateDictElements(xmlpp::Element * element, const ModelPartPtr & dict); }; class DLL_PUBLIC XmlStreamSerializer : public XmlSerializer { - public: - XmlStreamSerializer(std::ostream &); + public: + explicit XmlStreamSerializer(std::ostream &); - void Serialize(ModelPartForRootPtr) override; + void Serialize(ModelPartForRootPtr) override; - protected: - std::ostream & strm; + protected: + std::ostream & strm; }; class DLL_PUBLIC XmlFileSerializer : public XmlSerializer { - public: - XmlFileSerializer(std::filesystem::path); + public: + explicit XmlFileSerializer(std::filesystem::path); - void Serialize(ModelPartForRootPtr) override; + void Serialize(ModelPartForRootPtr) override; - protected: - const std::filesystem::path path; + protected: + const std::filesystem::path path; }; class DLL_PUBLIC XmlDocumentSerializer : public XmlSerializer { - public: - XmlDocumentSerializer(xmlpp::Document * &); + public: + explicit XmlDocumentSerializer(xmlpp::Document *&); - void Serialize(ModelPartForRootPtr) override; + void Serialize(ModelPartForRootPtr) override; - protected: - xmlpp::Document * & doc; + protected: + xmlpp::Document *& doc; }; class DLL_PUBLIC XmlDeserializer : public Deserializer { - protected: - static void DocumentTreeIterate(const xmlpp::Node * node, const ModelPartPtr & mp); - static void DocumentTreeIterateElement(const xmlpp::Element * element, ModelPartPtr mp, const ChildRef & c); - static void DocumentTreeIterate(const xmlpp::Document * doc, const ModelPartPtr & mp); - static void DocumentTreeIterateDictAttrs(const xmlpp::Element::const_AttributeList & attrs, const ModelPartPtr & dict); - static void DocumentTreeIterateDictElements(const xmlpp::Element * parent, const ModelPartPtr & dict); + protected: + static void DocumentTreeIterate(const xmlpp::Node * node, const ModelPartPtr & mp); + static void DocumentTreeIterateElement(const xmlpp::Element * element, ModelPartPtr mp, const ChildRef & c); + static void DocumentTreeIterate(const xmlpp::Document * doc, const ModelPartPtr & mp); + static void DocumentTreeIterateDictAttrs( + const xmlpp::Element::const_AttributeList & attrs, const ModelPartPtr & dict); + static void DocumentTreeIterateDictElements(const xmlpp::Element * parent, const ModelPartPtr & dict); }; class DLL_PUBLIC XmlStreamDeserializer : public XmlDeserializer { - public: - XmlStreamDeserializer(std::istream &); + public: + explicit XmlStreamDeserializer(std::istream &); - void Deserialize(ModelPartForRootPtr) override; + void Deserialize(ModelPartForRootPtr) override; - protected: - std::istream & strm; + protected: + std::istream & strm; }; class DLL_PUBLIC XmlFileDeserializer : public XmlDeserializer { - public: - XmlFileDeserializer(std::filesystem::path); + public: + explicit XmlFileDeserializer(std::filesystem::path); - void Deserialize(ModelPartForRootPtr) override; + void Deserialize(ModelPartForRootPtr) override; - protected: - const std::filesystem::path path; + protected: + const std::filesystem::path path; }; class DLL_PUBLIC XmlDocumentDeserializer : public XmlDeserializer { - public: - XmlDocumentDeserializer(const xmlpp::Document *); + public: + explicit XmlDocumentDeserializer(const xmlpp::Document *); - void Deserialize(ModelPartForRootPtr) override; + void Deserialize(ModelPartForRootPtr) override; - protected: - const xmlpp::Document * doc; + protected: + const xmlpp::Document * doc; }; } #endif - diff --git a/slicer/xml/testSpecifics.cpp b/slicer/xml/testSpecifics.cpp index d20ea09..3d05979 100644 --- a/slicer/xml/testSpecifics.cpp +++ b/slicer/xml/testSpecifics.cpp @@ -1,19 +1,19 @@ #define BOOST_TEST_MODULE xml_specifics -#include -#include #include "serializer.h" +#include #include +#include #include #include -template +template T -BoostThrowWrapperHelper(P && ... p) +BoostThrowWrapperHelper(P &&... p) { return Slicer::DeserializeAny(p...); } -BOOST_AUTO_TEST_CASE( boolean_values ) +BOOST_AUTO_TEST_CASE(boolean_values) { xmlpp::DomParser doc; doc.parse_memory("true"); @@ -28,7 +28,7 @@ BOOST_AUTO_TEST_CASE( boolean_values ) BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(doc.get_document()), Slicer::BadBooleanValue); } -BOOST_AUTO_TEST_CASE( int_values ) +BOOST_AUTO_TEST_CASE(int_values) { xmlpp::DomParser doc; doc.parse_memory("13"); @@ -47,11 +47,10 @@ BOOST_AUTO_TEST_CASE( int_values ) BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(doc.get_document()), std::bad_cast); } -BOOST_AUTO_TEST_CASE( factories ) +BOOST_AUTO_TEST_CASE(factories) { BOOST_REQUIRE(Slicer::SerializerPtr(Slicer::FileSerializerFactory::createNew(".xml", "/some.xml"))); BOOST_REQUIRE(Slicer::DeserializerPtr(Slicer::FileDeserializerFactory::createNew(".xml", "/some.xml"))); BOOST_REQUIRE(Slicer::SerializerPtr(Slicer::StreamSerializerFactory::createNew("application/xml", std::cout))); BOOST_REQUIRE(Slicer::DeserializerPtr(Slicer::StreamDeserializerFactory::createNew("application/xml", std::cin))); } - -- cgit v1.2.3