| 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, |
| 12 | const ConditionData &condition) |
| 13 | { |
| 14 | Storage<bool> skipContinuationStorage; |
| 15 | |
| 16 | const auto onSetup = [bodyExecutedStorage] { |
| 17 | return *bodyExecutedStorage ? SetupResult::StopWithSuccess : SetupResult::Continue; |
| 18 | }; |
| 19 | |
| 20 | const auto onConditionDone = [skipContinuationStorage](DoneWith result) { |
| 21 | *skipContinuationStorage = result != DoneWith::Success; |
| 22 | return DoneResult::Success; |
| 23 | }; |
| 24 | |
| 25 | const auto onContinuationSetup = [skipContinuationStorage, bodyExecutedStorage] { |
| 26 | *bodyExecutedStorage = !*skipContinuationStorage; |
| 27 | return *skipContinuationStorage ? SetupResult::StopWithSuccess : SetupResult::Continue; |
| 28 | }; |
| 29 | |
| 30 | return { |
| 31 | skipContinuationStorage, |
| 32 | onGroupSetup(handler: onSetup), |
| 33 | condition.m_condition ? Group { |
| 34 | *condition.m_condition, |
| 35 | onGroupDone(handler: onConditionDone) |
| 36 | } : nullItem, |
| 37 | Group { |
| 38 | onGroupSetup(handler: onContinuationSetup), |
| 39 | condition.m_body |
| 40 | } |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | static ExecutableItem conditionsRecipe(const QList<ConditionData> &conditions) |
| 45 | { |
| 46 | Storage<bool> bodyExecutedStorage; |
| 47 | |
| 48 | QList<GroupItem> recipes; |
| 49 | for (const ConditionData &condition : conditions) |
| 50 | recipes << conditionRecipe(bodyExecutedStorage, condition); |
| 51 | |
| 52 | return Group { bodyExecutedStorage, recipes }; |
| 53 | } |
| 54 | |
| 55 | ThenItem::operator ExecutableItem() const |
| 56 | { |
| 57 | return conditionsRecipe(conditions: m_conditions); |
| 58 | } |
| 59 | |
| 60 | ThenItem::ThenItem(const If &ifItem, const Then &thenItem) |
| 61 | : m_conditions{{.m_condition: ifItem.m_condition, .m_body: thenItem.m_body}} |
| 62 | {} |
| 63 | |
| 64 | ThenItem::ThenItem(const ElseIfItem &elseIfItem, const Then &thenItem) |
| 65 | : m_conditions(elseIfItem.m_conditions) |
| 66 | { |
| 67 | m_conditions.append(t: {.m_condition: elseIfItem.m_nextCondition, .m_body: thenItem.m_body}); |
| 68 | } |
| 69 | |
| 70 | ElseItem::operator ExecutableItem() const |
| 71 | { |
| 72 | return conditionsRecipe(conditions: m_conditions); |
| 73 | } |
| 74 | |
| 75 | ElseItem::ElseItem(const ThenItem &thenItem, const Else &elseItem) |
| 76 | : m_conditions(thenItem.m_conditions) |
| 77 | { |
| 78 | m_conditions.append(t: {.m_condition: {}, .m_body: elseItem.m_body}); |
| 79 | } |
| 80 | |
| 81 | ElseIfItem::ElseIfItem(const ThenItem &thenItem, const ElseIf &elseIfItem) |
| 82 | : m_conditions(thenItem.m_conditions) |
| 83 | , m_nextCondition(elseIfItem.m_condition) |
| 84 | {} |
| 85 | |
| 86 | ThenItem operator>>(const If &ifItem, const Then &thenItem) |
| 87 | { |
| 88 | return {ifItem, thenItem}; |
| 89 | } |
| 90 | |
| 91 | ThenItem operator>>(const ElseIfItem &elseIfItem, const Then &thenItem) |
| 92 | { |
| 93 | return {elseIfItem, thenItem}; |
| 94 | } |
| 95 | |
| 96 | ElseIfItem operator>>(const ThenItem &thenItem, const ElseIf &elseIfItem) |
| 97 | { |
| 98 | return {thenItem, elseIfItem}; |
| 99 | } |
| 100 | |
| 101 | ElseItem operator>>(const ThenItem &thenItem, const Else &elseItem) |
| 102 | { |
| 103 | return {thenItem, elseItem}; |
| 104 | } |
| 105 | |
| 106 | } // namespace Tasking |
| 107 | |
| 108 | QT_END_NAMESPACE |
| 109 | |