xref: /openbmc/phosphor-power/phosphor-regulators/src/actions/not_action.hpp (revision 0c9a33d66f74a76c1842990f41da678f79fa0c05)
16aac4148SShawn McCarney /**
26aac4148SShawn McCarney  * Copyright © 2019 IBM Corporation
36aac4148SShawn McCarney  *
46aac4148SShawn McCarney  * Licensed under the Apache License, Version 2.0 (the "License");
56aac4148SShawn McCarney  * you may not use this file except in compliance with the License.
66aac4148SShawn McCarney  * You may obtain a copy of the License at
76aac4148SShawn McCarney  *
86aac4148SShawn McCarney  *     http://www.apache.org/licenses/LICENSE-2.0
96aac4148SShawn McCarney  *
106aac4148SShawn McCarney  * Unless required by applicable law or agreed to in writing, software
116aac4148SShawn McCarney  * distributed under the License is distributed on an "AS IS" BASIS,
126aac4148SShawn McCarney  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136aac4148SShawn McCarney  * See the License for the specific language governing permissions and
146aac4148SShawn McCarney  * limitations under the License.
156aac4148SShawn McCarney  */
166aac4148SShawn McCarney #pragma once
176aac4148SShawn McCarney 
186aac4148SShawn McCarney #include "action.hpp"
196aac4148SShawn McCarney #include "action_environment.hpp"
206aac4148SShawn McCarney 
216aac4148SShawn McCarney #include <memory>
228a3db36aSShawn McCarney #include <string>
236aac4148SShawn McCarney #include <utility>
246aac4148SShawn McCarney 
256aac4148SShawn McCarney namespace phosphor::power::regulators
266aac4148SShawn McCarney {
276aac4148SShawn McCarney 
286aac4148SShawn McCarney /**
296aac4148SShawn McCarney  * @class NotAction
306aac4148SShawn McCarney  *
316aac4148SShawn McCarney  * Executes an action and negates its return value.
326aac4148SShawn McCarney  *
336aac4148SShawn McCarney  * Implements the "not" action in the JSON config file.
346aac4148SShawn McCarney  */
356aac4148SShawn McCarney class NotAction : public Action
366aac4148SShawn McCarney {
376aac4148SShawn McCarney   public:
386aac4148SShawn McCarney     // Specify which compiler-generated methods we want
396aac4148SShawn McCarney     NotAction() = delete;
406aac4148SShawn McCarney     NotAction(const NotAction&) = delete;
416aac4148SShawn McCarney     NotAction(NotAction&&) = delete;
426aac4148SShawn McCarney     NotAction& operator=(const NotAction&) = delete;
436aac4148SShawn McCarney     NotAction& operator=(NotAction&&) = delete;
446aac4148SShawn McCarney     virtual ~NotAction() = default;
456aac4148SShawn McCarney 
466aac4148SShawn McCarney     /**
476aac4148SShawn McCarney      * Constructor.
486aac4148SShawn McCarney      *
496aac4148SShawn McCarney      * @param action action to execute
506aac4148SShawn McCarney      */
NotAction(std::unique_ptr<Action> action)516aac4148SShawn McCarney     explicit NotAction(std::unique_ptr<Action> action) :
526aac4148SShawn McCarney         action{std::move(action)}
53*0c9a33d6SAdriana Kobylak     {}
546aac4148SShawn McCarney 
556aac4148SShawn McCarney     /**
566aac4148SShawn McCarney      * Executes the action specified in the constructor.
576aac4148SShawn McCarney      *
586aac4148SShawn McCarney      * Returns the opposite of the return value from the action.  For example,
596aac4148SShawn McCarney      * if the action returned true, then false will be returned.
606aac4148SShawn McCarney      *
616aac4148SShawn McCarney      * Throws an exception if an error occurs and the action cannot be
626aac4148SShawn McCarney      * successfully executed.
636aac4148SShawn McCarney      *
646aac4148SShawn McCarney      * @param environment action execution environment
656aac4148SShawn McCarney      * @return negated return value from action executed
666aac4148SShawn McCarney      */
execute(ActionEnvironment & environment)676aac4148SShawn McCarney     virtual bool execute(ActionEnvironment& environment) override
686aac4148SShawn McCarney     {
696aac4148SShawn McCarney         return !(action->execute(environment));
706aac4148SShawn McCarney     }
716aac4148SShawn McCarney 
726aac4148SShawn McCarney     /**
736aac4148SShawn McCarney      * Returns the action to execute.
746aac4148SShawn McCarney      *
756aac4148SShawn McCarney      * @return action
766aac4148SShawn McCarney      */
getAction() const776aac4148SShawn McCarney     const std::unique_ptr<Action>& getAction() const
786aac4148SShawn McCarney     {
796aac4148SShawn McCarney         return action;
806aac4148SShawn McCarney     }
816aac4148SShawn McCarney 
828a3db36aSShawn McCarney     /**
838a3db36aSShawn McCarney      * Returns a string description of this action.
848a3db36aSShawn McCarney      *
858a3db36aSShawn McCarney      * @return description of action
868a3db36aSShawn McCarney      */
toString() const878a3db36aSShawn McCarney     virtual std::string toString() const override
888a3db36aSShawn McCarney     {
898a3db36aSShawn McCarney         return "not: { ... }";
908a3db36aSShawn McCarney     }
918a3db36aSShawn McCarney 
926aac4148SShawn McCarney   private:
936aac4148SShawn McCarney     /**
946aac4148SShawn McCarney      * Action to execute.
956aac4148SShawn McCarney      */
966aac4148SShawn McCarney     std::unique_ptr<Action> action;
976aac4148SShawn McCarney };
986aac4148SShawn McCarney 
996aac4148SShawn McCarney } // namespace phosphor::power::regulators
100