blob: 5dcff7a9b91591f34c3ccd702d4db2c169221ed8 [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>
Shawn McCarney8a3db362020-02-05 16:24:16 -060022#include <string>
Shawn McCarneyd3bbfe22019-11-13 13:53:20 -060023#include <utility>
24#include <vector>
25
26namespace phosphor::power::regulators
27{
28
29/**
30 * @class OrAction
31 *
32 * Executes a sequence of actions and tests whether any of them returned true.
33 *
34 * Implements the "or" action in the JSON config file.
35 */
36class OrAction : public Action
37{
38 public:
39 // Specify which compiler-generated methods we want
40 OrAction() = delete;
41 OrAction(const OrAction&) = delete;
42 OrAction(OrAction&&) = delete;
43 OrAction& operator=(const OrAction&) = delete;
44 OrAction& operator=(OrAction&&) = delete;
45 virtual ~OrAction() = default;
46
47 /**
48 * Constructor.
49 *
50 * @param actions actions to execute
51 */
52 explicit OrAction(std::vector<std::unique_ptr<Action>> actions) :
53 actions{std::move(actions)}
54 {
55 }
56
57 /**
58 * Executes the actions specified in the constructor.
59 *
60 * Returns true if any of the actions returned true, otherwise returns
61 * false.
62 *
63 * Note: All of the actions will be executed even if an action before the
64 * end returns true. This ensures that actions with beneficial side-effects
65 * are always executed, such as a register read that clears latched fault
66 * bits.
67 *
68 * Throws an exception if an error occurs and an action cannot be
69 * successfully executed.
70 *
71 * @param environment action execution environment
72 * @return true if any actions returned true, otherwise returns false
73 */
74 virtual bool execute(ActionEnvironment& environment) override
75 {
76 bool returnValue{false};
77 for (std::unique_ptr<Action>& action : actions)
78 {
79 if (action->execute(environment) == true)
80 {
81 returnValue = true;
82 }
83 }
84 return returnValue;
85 }
86
87 /**
88 * Returns the actions to execute.
89 *
90 * @return actions to execute
91 */
92 const std::vector<std::unique_ptr<Action>>& getActions() const
93 {
94 return actions;
95 }
96
Shawn McCarney8a3db362020-02-05 16:24:16 -060097 /**
98 * Returns a string description of this action.
99 *
100 * @return description of action
101 */
102 virtual std::string toString() const override
103 {
104 return "or: [ ... ]";
105 }
106
Shawn McCarneyd3bbfe22019-11-13 13:53:20 -0600107 private:
108 /**
109 * Actions to execute.
110 */
111 std::vector<std::unique_ptr<Action>> actions{};
112};
113
114} // namespace phosphor::power::regulators