2024-07-03 13:23:08 +02:00
|
|
|
// 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 <functional>
|
|
|
|
|
|
|
|
|
|
namespace Building {
|
|
|
|
|
|
|
|
|
|
class NestId {};
|
|
|
|
|
|
|
|
|
|
template <typename Id, typename Arg>
|
|
|
|
|
class IdAndArg
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
IdAndArg(Id, const Arg &arg) : arg(arg) {}
|
|
|
|
|
const Arg arg; // FIXME: Could be const &, but this would currently break bindTo().
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// The main dispatcher
|
|
|
|
|
|
|
|
|
|
void doit(auto x, auto id, auto p);
|
|
|
|
|
|
|
|
|
|
template <typename X> class BuilderItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
// Property setter
|
|
|
|
|
template <typename Id, typename Arg>
|
|
|
|
|
BuilderItem(IdAndArg<Id, Arg> && idarg)
|
2024-07-18 10:11:33 +02:00
|
|
|
: apply([&idarg](X *x) { doit(x, Id{}, idarg.arg); })
|
|
|
|
|
{}
|
2024-07-03 13:23:08 +02:00
|
|
|
|
|
|
|
|
// Nested child object
|
|
|
|
|
template <typename Inner>
|
|
|
|
|
BuilderItem(Inner && p)
|
2024-07-18 10:11:33 +02:00
|
|
|
: apply([&p](X *x) { doit(x, NestId{}, std::forward<Inner>(p)); })
|
|
|
|
|
{}
|
2024-07-03 13:23:08 +02:00
|
|
|
|
2024-07-18 10:11:33 +02:00
|
|
|
const std::function<void(X *)> apply;
|
2024-07-03 13:23:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define QTC_DEFINE_BUILDER_SETTER(name, setter) \
|
2024-07-04 15:23:25 +02:00
|
|
|
class name##_TAG {}; \
|
|
|
|
|
template <typename ...Args> \
|
|
|
|
|
inline auto name(Args &&...args) { \
|
2024-07-17 14:24:01 +02:00
|
|
|
return Building::IdAndArg{name##_TAG{}, std::tuple<Args...>{std::forward<Args>(args)...}}; \
|
2024-07-04 15:23:25 +02:00
|
|
|
} \
|
|
|
|
|
template <typename L, typename ...Args> \
|
2024-07-17 14:24:01 +02:00
|
|
|
inline void doit(L *x, name##_TAG, const std::tuple<Args...> &arg) { \
|
|
|
|
|
std::apply(&L::setter, std::tuple_cat(std::make_tuple(x), arg)); \
|
2024-07-04 15:23:25 +02:00
|
|
|
}
|
2024-07-03 13:23:08 +02:00
|
|
|
|
|
|
|
|
} // Building
|