From d59bff8bea30474da4398ee6f8b86f03cf491284 Mon Sep 17 00:00:00 2001 From: silverqx Date: Thu, 6 Jul 2023 10:32:49 +0200 Subject: [PATCH] extracted relations container type --- .../orm/tiny/concerns/hasrelationships.hpp | 101 +++--------------- include/orm/tiny/tinytypes.hpp | 18 ++++ 2 files changed, 35 insertions(+), 84 deletions(-) diff --git a/include/orm/tiny/concerns/hasrelationships.hpp b/include/orm/tiny/concerns/hasrelationships.hpp index 313c64a4a..3698be83a 100644 --- a/include/orm/tiny/concerns/hasrelationships.hpp +++ b/include/orm/tiny/concerns/hasrelationships.hpp @@ -5,15 +5,6 @@ #include "orm/macros/systemheader.hpp" TINY_SYSTEM_HEADER -#include "orm/config.hpp" // IWYU pragma: keep - -#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP -# include -// Leaving only for reference, the unordered_map is included in the modelscollection.hpp -//#else -//# include -#endif - #include #include "orm/exceptions/invalidtemplateargumenterror.hpp" @@ -67,6 +58,9 @@ namespace Concerns using TypeUtils = Orm::Utils::Type; public: + /*! Alias for the RelationsContainer. */ + using RelationsContainerType = RelationsContainer; + /* HasRelationships related */ /*! Get a relationship for Many types relation (load a relationship lazily if not loaded). */ @@ -121,21 +115,9 @@ namespace Concerns Derived &setRelation(const QString &relation, std::optional &&model); /*! Get all the loaded relations for the instance. */ - inline -#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP - const std::map> & -#else - const std::unordered_map> & -#endif - getRelations() const noexcept; + inline const RelationsContainer &getRelations() const noexcept; /*! Get all the loaded relations for the instance. */ - inline -#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP - std::map> & -#else - std::unordered_map> & -#endif - getRelations() noexcept; + inline RelationsContainer &getRelations() noexcept; /*! Unset all the loaded relations for the instance. */ Derived &unsetRelations(); @@ -201,20 +183,9 @@ namespace Concerns getRelationshipFromMethod(const QString &relation); /*! Set the entire relations hash on the model. */ - Derived &setRelations( -#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP - const std::map> &relations); -#else - const std::unordered_map> &relations); -#endif + Derived &setRelations(const RelationsContainer &relations); /*! Set the entire relations hash on the model. */ - Derived &setRelations( -#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP - std::map> &&relations); -#else - std::unordered_map> &&relations); -#endif + Derived &setRelations(RelationsContainer &&relations); /* Relationships factory methods */ /*! Instantiate a new HasOne relationship. */ @@ -267,13 +238,8 @@ namespace Concerns /* The libstdc++ shipped with the GCC <12.1 doesn't allow an incomplete mapped_type (value) in the std::unordered_map. */ -#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP /*! The loaded relationships for the model. */ - std::map> m_relations; -#else - /*! The loaded relationships for the model. */ - std::unordered_map> m_relations; -#endif + RelationsContainer m_relations; /*! The relationships that should be touched on save. */ QStringList u_touches; // CUR1 use sets instead of QStringList where appropriate silverqx @@ -367,21 +333,12 @@ namespace Concerns QVector getLoadedRelationsWithoutPivot() const; /*! Replace relations in the m_relation. */ - void replaceRelations( -#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP - std::map> &relations, -#else - std::unordered_map> &relations, -#endif - const QVector &onlyRelations); + void replaceRelations(RelationsContainer &relations, + const QVector &onlyRelations); /* Serialization - Relations */ /*! Get an map of all serializable relations. */ -#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP - inline const std::map> & -#else - inline const std::unordered_map> & -#endif + inline const RelationsContainer & getSerializableRelations() const; /*! Create and visit the serialize relation store. */ @@ -573,22 +530,14 @@ namespace Concerns } template -#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP - const std::map> & -#else - const std::unordered_map> & -#endif + const RelationsContainer & HasRelationships::getRelations() const noexcept { return m_relations; } template -#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP - std::map> & -#else - std::unordered_map> & -#endif + RelationsContainer & HasRelationships::getRelations() noexcept { return m_relations; @@ -845,11 +794,7 @@ namespace Concerns template Derived & HasRelationships::setRelations( -#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP - const std::map> &relations) -#else - const std::unordered_map> &relations) -#endif + const RelationsContainer &relations) { m_relations = relations; @@ -859,11 +804,7 @@ namespace Concerns template Derived & HasRelationships::setRelations( -#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP - std::map> &&relations) -#else - std::unordered_map> &&relations) -#endif + RelationsContainer &&relations) { m_relations = std::move(relations); @@ -1347,11 +1288,7 @@ namespace Concerns template void HasRelationships::replaceRelations( -#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP - std::map> &relations, -#else - std::unordered_map> &relations, -#endif + RelationsContainer &relations, const QVector &onlyRelations) { /* Replace only relations which was passed to this method, leave other @@ -1384,11 +1321,7 @@ namespace Concerns /* Serialization - Relations */ template -#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP - const std::map> & -#else - const std::unordered_map> & -#endif + const RelationsContainer & HasRelationships::getSerializableRelations() const { // FEATURE HidesAttributes silverqx diff --git a/include/orm/tiny/tinytypes.hpp b/include/orm/tiny/tinytypes.hpp index a73b515cc..d019de482 100644 --- a/include/orm/tiny/tinytypes.hpp +++ b/include/orm/tiny/tinytypes.hpp @@ -7,6 +7,15 @@ TINY_SYSTEM_HEADER #include +#include "orm/config.hpp" // IWYU pragma: keep + +#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP +# include + Leaving only for reference, the unordered_map is included in the modelscollection.hpp +#else +# include +#endif + #include "orm/macros/export.hpp" #include "orm/ormtypes.hpp" // IWYU pragma: export #include "orm/tiny/tinyconcepts.hpp" // IWYU pragma: keep @@ -48,6 +57,15 @@ namespace Types ModelsCollection..., std::optional...>; +#ifdef TINY_NO_INCOMPLETE_UNORDERED_MAP + template + using RelationsContainer = std::map>; +#else + template + using RelationsContainer = std::unordered_map>; +#endif + // TODO pretty print in the debugger silverqx /*! Attribute item used in ORM models. */ struct SHAREDLIB_EXPORT AttributeItem