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     /**
57      * Executes the action specified in the constructor.
58      *
59      * Returns the opposite of the return value from the action.  For example,
60      * if the action returned true, then false will be returned.
61      *
62      * Throws an exception if an error occurs and the action cannot be
63      * successfully executed.
64      *
65      * @param environment action execution environment
66      * @return negated return value from action executed
67      */
68     virtual bool execute(ActionEnvironment& environment) override
69     {
70         return !(action->execute(environment));
71     }
72 
73     /**
74      * Returns the action to execute.
75      *
76      * @return action
77      */
78     const std::unique_ptr<Action>& getAction() const
79     {
80         return action;
81     }
82 
83     /**
84      * Returns a string description of this action.
85      *
86      * @return description of action
87      */
88     virtual std::string toString() const override
89     {
90         return "not: { ... }";
91     }
92 
93   private:
94     /**
95      * Action to execute.
96      */
97     std::unique_ptr<Action> action;
98 };
99 
100 } // namespace phosphor::power::regulators
101