diff --git a/src/libs/utils/CMakeLists.txt b/src/libs/utils/CMakeLists.txt index ca5e88b66c4..c7e0a87b0f2 100644 --- a/src/libs/utils/CMakeLists.txt +++ b/src/libs/utils/CMakeLists.txt @@ -19,6 +19,7 @@ add_qtc_library(Utils basetreeview.cpp basetreeview.h benchmarker.cpp benchmarker.h buildablehelperlibrary.cpp buildablehelperlibrary.h + builderutils.h camelcasecursor.cpp camelcasecursor.h categorysortfiltermodel.cpp categorysortfiltermodel.h changeset.cpp changeset.h diff --git a/src/libs/utils/builderutils.h b/src/libs/utils/builderutils.h new file mode 100644 index 00000000000..51f65bd6b6e --- /dev/null +++ b/src/libs/utils/builderutils.h @@ -0,0 +1,111 @@ +// Copyright (C) 2020 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#pragma once + +#include + +#if defined(UTILS_LIBRARY) +# define QTCREATOR_UTILS_EXPORT Q_DECL_EXPORT +#elif defined(UTILS_STATIC_LIBRARY) +# define QTCREATOR_UTILS_EXPORT +#else +# define QTCREATOR_UTILS_EXPORT Q_DECL_IMPORT +#endif + +namespace Building { + +class NestId {}; + +template +class IdAndArg +{ +public: + IdAndArg(Id, const Arg &arg) : arg(arg) {} + const Arg arg; // FIXME: Could be const &, but this would currently break bindTo(). +}; + +template +struct Arg2 +{ + Arg2(const T1 &a1, const T2 &a2) + : p1(a1) + , p2(a2) + {} + const T1 p1; + const T2 p2; +}; + +template +struct Arg3 +{ + Arg3(const T1 &a1, const T2 &a2, const T3 &a3) + : p1(a1) + , p2(a2) + , p3(a3) + {} + const T1 p1; + const T2 p2; + const T3 p3; +}; + +template +struct Arg4 +{ + Arg4(const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4) + : p1(a1) + , p2(a2) + , p3(a3) + , p4(a4) + {} + const T1 p1; + const T2 p2; + const T3 p3; + const T4 p4; +}; + +// The main dispatcher + +void doit(auto x, auto id, auto p); + +template class BuilderItem +{ +public: + // Property setter + template + BuilderItem(IdAndArg && idarg) + { + apply = [&idarg](X *x) { doit(x, Id{}, idarg.arg); }; + } + + // Nested child object + template + BuilderItem(Inner && p) + { + apply = [&p](X *x) { doit(x, NestId{}, std::forward(p)); }; + } + + std::function apply; +}; + +#define QTC_DEFINE_BUILDER_SETTER(name, setter) \ + class name##_TAG {}; \ + inline auto name(auto p) { return Building::IdAndArg{name##_TAG{}, p}; } \ + inline void doit(auto x, name##_TAG, auto p) { x->setter(p); } + +#define QTC_DEFINE_BUILDER_SETTER2(name, setter) \ + class name##_TAG {}; \ + inline auto name(auto p1, auto p2) { return Building::IdAndArg{name##_TAG{}, Building::Arg2{p1, p2}}; } \ + inline void doit(auto x, name##_TAG, auto p) { x->setter(p.p1, p.p2); } + +#define QTC_DEFINE_BUILDER_SETTER3(name, setter) \ + class name##_TAG {}; \ + inline auto name(auto p1, auto p2, auto p3) { return Building::IdAndArg{name##_TAG{}, Building::Arg3{p1, p2, p3}}; } \ + inline void doit(auto x, name##_TAG, auto p) { x->setter(p.p1, p.p2, p.p3); } + +#define QTC_DEFINE_BUILDER_SETTER4(name, setter) \ + class name##_TAG {}; \ + inline auto name(auto p1, auto p2, auto p3, auto p4) { return Building::IdAndArg{name##_TAG{}, Building::Arg4{p1, p2, p3, p4}}; } \ + inline void doit(auto x, name##_TAG, auto p) { x->setter(p.p1, p.p2, p.p3, p.p4); } + +} // Building diff --git a/src/libs/utils/layoutbuilder.h b/src/libs/utils/layoutbuilder.h index a411ea47633..9c9d9b2e2c8 100644 --- a/src/libs/utils/layoutbuilder.h +++ b/src/libs/utils/layoutbuilder.h @@ -3,9 +3,10 @@ #pragma once +#include "builderutils.h" + #include -#include #include #include @@ -39,80 +40,6 @@ QT_END_NAMESPACE namespace Layouting { -class NestId {}; - -template -class IdAndArg -{ -public: - IdAndArg(Id, const Arg &arg) : arg(arg) {} - const Arg arg; // FIXME: Could be const &, but this would currently break bindTo(). -}; - -template -struct Arg2 -{ - Arg2(const T1 &a1, const T2 &a2) - : p1(a1) - , p2(a2) - {} - const T1 p1; - const T2 p2; -}; - -template -struct Arg3 -{ - Arg3(const T1 &a1, const T2 &a2, const T3 &a3) - : p1(a1) - , p2(a2) - , p3(a3) - {} - const T1 p1; - const T2 p2; - const T3 p3; -}; - -template -struct Arg4 -{ - Arg4(const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4) - : p1(a1) - , p2(a2) - , p3(a3) - , p4(a4) - {} - const T1 p1; - const T2 p2; - const T3 p3; - const T4 p4; -}; - -// The main dispatcher - -void doit(auto x, auto id, auto p); - -template class BuilderItem -{ -public: - // Nested child object - template - BuilderItem(Inner && p) - { - apply = [&p](X *x) { doit(x, NestId{}, std::forward(p)); }; - } - - // Property setter - template - BuilderItem(IdAndArg && idarg) - { - apply = [&idarg](X *x) { doit(x, Id{}, idarg.arg); }; - } - - std::function apply; -}; - - ////////////////////////////////////////////// // @@ -129,7 +56,7 @@ class QTCREATOR_UTILS_EXPORT Object : public Thing { public: using Implementation = QObject; - using I = BuilderItem; + using I = Building::BuilderItem; Object() = default; Object(std::initializer_list ps); @@ -165,7 +92,7 @@ class QTCREATOR_UTILS_EXPORT Layout : public Object { public: using Implementation = QLayout; - using I = BuilderItem; + using I = Building::BuilderItem; Layout() = default; Layout(Implementation *w) { ptr = w; } @@ -210,7 +137,7 @@ class QTCREATOR_UTILS_EXPORT Column : public Layout { public: using Implementation = QVBoxLayout; - using I = BuilderItem; + using I = Building::BuilderItem; Column(std::initializer_list ps); }; @@ -219,7 +146,7 @@ class QTCREATOR_UTILS_EXPORT Row : public Layout { public: using Implementation = QHBoxLayout; - using I = BuilderItem; + using I = Building::BuilderItem; Row(std::initializer_list ps); }; @@ -228,7 +155,7 @@ class QTCREATOR_UTILS_EXPORT Form : public Layout { public: using Implementation = QFormLayout; - using I = BuilderItem
; + using I = Building::BuilderItem; Form(); Form(std::initializer_list ps); @@ -238,7 +165,7 @@ class QTCREATOR_UTILS_EXPORT Grid : public Layout { public: using Implementation = QGridLayout; - using I = BuilderItem; + using I = Building::BuilderItem; Grid(); Grid(std::initializer_list ps); @@ -285,7 +212,7 @@ class QTCREATOR_UTILS_EXPORT Widget : public Object { public: using Implementation = QWidget; - using I = BuilderItem; + using I = Building::BuilderItem; Widget() = default; Widget(std::initializer_list ps); @@ -307,7 +234,7 @@ class QTCREATOR_UTILS_EXPORT Label : public Widget { public: using Implementation = QLabel; - using I = BuilderItem