| 1 | // Copyright (C) 2024 Jarek Kobus |
| 2 | // Copyright (C) 2024 The Qt Company Ltd. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | |
| 5 | #include "conditional.h" |
| 6 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | namespace Tasking { |
| 10 | |
| 11 | static Group conditionRecipe(const Storage<bool> &bodyExecutedStorage, const ConditionData &condition) |
| 12 | { |
| 13 | const auto onSetup = [bodyExecutedStorage] { |
| 14 | return *bodyExecutedStorage ? SetupResult::StopWithSuccess : SetupResult::Continue; |
| 15 | }; |
| 16 | |
| 17 | const auto onBodyDone = [bodyExecutedStorage] { *bodyExecutedStorage = true; }; |
| 18 | |
| 19 | const Group bodyTask { condition.m_body, onGroupDone(handler: onBodyDone) }; |
| 20 | |
| 21 | return { |
| 22 | onGroupSetup(handler: onSetup), |
| 23 | condition.m_condition ? Group{ !*condition.m_condition || bodyTask } : bodyTask |
| 24 | }; |
| 25 | } |
| 26 | |
| 27 | static ExecutableItem conditionsRecipe(const QList<ConditionData> &conditions) |
| 28 | { |
| 29 | Storage<bool> bodyExecutedStorage; |
| 30 | |
| 31 | GroupItems recipes; |
| 32 | for (const ConditionData &condition : conditions) |
| 33 | recipes << conditionRecipe(bodyExecutedStorage, condition); |
| 34 | |
| 35 | return Group { bodyExecutedStorage, recipes }; |
| 36 | } |
| 37 | |
| 38 | ThenItem::operator ExecutableItem() const |
| 39 | { |
| 40 | return conditionsRecipe(conditions: m_conditions); |
| 41 | } |
| 42 | |
| 43 | ThenItem::ThenItem(const If &ifItem, const Then &thenItem) |
| 44 | : m_conditions{{.m_condition: ifItem.m_condition, .m_body: thenItem.m_body}} |
| 45 | {} |
| 46 | |
| 47 | ThenItem::ThenItem(const ElseIfItem &elseIfItem, const Then &thenItem) |
| 48 | : m_conditions(elseIfItem.m_conditions) |
| 49 | { |
| 50 | m_conditions.append(t: {.m_condition: elseIfItem.m_nextCondition, .m_body: thenItem.m_body}); |
| 51 | } |
| 52 | |
| 53 | ElseItem::operator ExecutableItem() const |
| 54 | { |
| 55 | return conditionsRecipe(conditions: m_conditions); |
| 56 | } |
| 57 | |
| 58 | ElseItem::ElseItem(const ThenItem &thenItem, const Else &elseItem) |
| 59 | : m_conditions(thenItem.m_conditions) |
| 60 | { |
| 61 | m_conditions.append(t: {.m_condition: {}, .m_body: elseItem.m_body}); |
| 62 | } |
| 63 | |
| 64 | ElseIfItem::ElseIfItem(const ThenItem &thenItem, const ElseIf &elseIfItem) |
| 65 | : m_conditions(thenItem.m_conditions) |
| 66 | , m_nextCondition(elseIfItem.m_condition) |
| 67 | {} |
| 68 | |
| 69 | ThenItem operator>>(const If &ifItem, const Then &thenItem) |
| 70 | { |
| 71 | return {ifItem, thenItem}; |
| 72 | } |
| 73 | |
| 74 | ThenItem operator>>(const ElseIfItem &elseIfItem, const Then &thenItem) |
| 75 | { |
| 76 | return {elseIfItem, thenItem}; |
| 77 | } |
| 78 | |
| 79 | ElseIfItem operator>>(const ThenItem &thenItem, const ElseIf &elseIfItem) |
| 80 | { |
| 81 | return {thenItem, elseIfItem}; |
| 82 | } |
| 83 | |
| 84 | ElseItem operator>>(const ThenItem &thenItem, const Else &elseItem) |
| 85 | { |
| 86 | return {thenItem, elseItem}; |
| 87 | } |
| 88 | |
| 89 | } // namespace Tasking |
| 90 | |
| 91 | QT_END_NAMESPACE |
| 92 | |