blob: 824a763b4a3ebee27e70bd8e89d4568a49851234 [file] [log] [blame]
Shawn McCarney6aac4142019-11-12 19:47:13 -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
24namespace phosphor::power::regulators
25{
26
27/**
28 * @class NotAction
29 *
30 * Executes an action and negates its return value.
31 *
32 * Implements the "not" action in the JSON config file.
33 */
34class NotAction : public Action
35{
36 public:
37 // Specify which compiler-generated methods we want
38 NotAction() = delete;
39 NotAction(const NotAction&) = delete;
40 NotAction(NotAction&&) = delete;
41 NotAction& operator=(const NotAction&) = delete;
42 NotAction& operator=(NotAction&&) = delete;
43 virtual ~NotAction() = default;
44
45 /**
46 * Constructor.
47 *
48 * @param action action to execute
49 */
50 explicit NotAction(std::unique_ptr<Action> action) :
51 action{std::move(action)}
52 {
53 }
54
55 /**
56 * Executes the action specified in the constructor.
57 *
58 * Returns the opposite of the return value from the action. For example,
59 * if the action returned true, then false will be returned.
60 *
61 * Throws an exception if an error occurs and the action cannot be
62 * successfully executed.
63 *
64 * @param environment action execution environment
65 * @return negated return value from action executed
66 */
67 virtual bool execute(ActionEnvironment& environment) override
68 {
69 return !(action->execute(environment));
70 }
71
72 /**
73 * Returns the action to execute.
74 *
75 * @return action
76 */
77 const std::unique_ptr<Action>& getAction() const
78 {
79 return action;
80 }
81
82 private:
83 /**
84 * Action to execute.
85 */
86 std::unique_ptr<Action> action;
87};
88
89} // namespace phosphor::power::regulators