blob: 7adcc6fd433589d436ebb04700d8da1c737f92ba [file] [log] [blame]
Shawn McCarneyd3bbfe22019-11-13 13:53:20 -06001/**
2 * Copyright © 2019 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
18#include "action.hpp"
19#include "action_environment.hpp"
20
21#include <memory>
22#include <utility>
23#include <vector>
24
25namespace phosphor::power::regulators
26{
27
28/**
29 * @class OrAction
30 *
31 * Executes a sequence of actions and tests whether any of them returned true.
32 *
33 * Implements the "or" action in the JSON config file.
34 */
35class OrAction : public Action
36{
37 public:
38 // Specify which compiler-generated methods we want
39 OrAction() = delete;
40 OrAction(const OrAction&) = delete;
41 OrAction(OrAction&&) = delete;
42 OrAction& operator=(const OrAction&) = delete;
43 OrAction& operator=(OrAction&&) = delete;
44 virtual ~OrAction() = default;
45
46 /**
47 * Constructor.
48 *
49 * @param actions actions to execute
50 */
51 explicit OrAction(std::vector<std::unique_ptr<Action>> actions) :
52 actions{std::move(actions)}
53 {
54 }
55
56 /**
57 * Executes the actions specified in the constructor.
58 *
59 * Returns true if any of the actions returned true, otherwise returns
60 * false.
61 *
62 * Note: All of the actions will be executed even if an action before the
63 * end returns true. This ensures that actions with beneficial side-effects
64 * are always executed, such as a register read that clears latched fault
65 * bits.
66 *
67 * Throws an exception if an error occurs and an action cannot be
68 * successfully executed.
69 *
70 * @param environment action execution environment
71 * @return true if any actions returned true, otherwise returns false
72 */
73 virtual bool execute(ActionEnvironment& environment) override
74 {
75 bool returnValue{false};
76 for (std::unique_ptr<Action>& action : actions)
77 {
78 if (action->execute(environment) == true)
79 {
80 returnValue = true;
81 }
82 }
83 return returnValue;
84 }
85
86 /**
87 * Returns the actions to execute.
88 *
89 * @return actions to execute
90 */
91 const std::vector<std::unique_ptr<Action>>& getActions() const
92 {
93 return actions;
94 }
95
96 private:
97 /**
98 * Actions to execute.
99 */
100 std::vector<std::unique_ptr<Action>> actions{};
101};
102
103} // namespace phosphor::power::regulators