1 /**
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 <string>
23 #include <utility>
24 
25 namespace phosphor::power::regulators
26 {
27 
28 /**
29  * @class NotAction
30  *
31  * Executes an action and negates its return value.
32  *
33  * Implements the "not" action in the JSON config file.
34  */
35 class NotAction : public Action
36 {
37   public:
38     // Specify which compiler-generated methods we want
39     NotAction() = delete;
40     NotAction(const NotAction&) = delete;
41     NotAction(NotAction&&) = delete;
42     NotAction& operator=(const NotAction&) = delete;
43     NotAction& operator=(NotAction&&) = delete;
44     virtual ~NotAction() = default;
45 
46     /**
47      * Constructor.
48      *
49      * @param action action to execute
50      */
51     explicit NotAction(std::unique_ptr<Action> action) :
52         action{std::move(action)}
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     /**
83      * Returns a string description of this action.
84      *
85      * @return description of action
86      */
87     virtual std::string toString() const override
88     {
89         return "not: { ... }";
90     }
91 
92   private:
93     /**
94      * Action to execute.
95      */
96     std::unique_ptr<Action> action;
97 };
98 
99 } // namespace phosphor::power::regulators
100