| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2009 The Android Open Source Project | 
|  | 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 | // | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_COMMON_ACTION_PIPE_H_ | 
|  | 18 | #define UPDATE_ENGINE_COMMON_ACTION_PIPE_H_ | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 19 |  | 
|  | 20 | #include <stdio.h> | 
| Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 21 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 22 | #include <map> | 
| Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 23 | #include <memory> | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 24 | #include <string> | 
| Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 25 |  | 
| Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 26 | #include <base/logging.h> | 
| Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 27 | #include <base/macros.h> | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 28 |  | 
|  | 29 | // The structure of these classes (Action, ActionPipe, ActionProcessor, etc.) | 
|  | 30 | // is based on the KSAction* classes from the Google Update Engine code at | 
|  | 31 | // http://code.google.com/p/update-engine/ . The author of this file sends | 
|  | 32 | // a big thanks to that team for their high quality design, implementation, | 
|  | 33 | // and documentation. | 
|  | 34 |  | 
|  | 35 | // This class serves as a temporary holding area for an object passed out | 
|  | 36 | // from one Action and into another Action. It's templated so that it may | 
|  | 37 | // contain any type of object that an Action outputs/inputs. Actions | 
|  | 38 | // cannot be bonded (i.e., connected with a pipe) if their output/input | 
|  | 39 | // object types differ (a compiler error will result). | 
|  | 40 | // | 
|  | 41 | // An ActionPipe is generally created with the Bond() method and owned by | 
|  | 42 | // the two Action objects. a shared_ptr is used so that when the last Action | 
|  | 43 | // pointing to an ActionPipe dies, the ActionPipe dies, too. | 
|  | 44 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 45 | namespace chromeos_update_engine { | 
|  | 46 |  | 
|  | 47 | // Used by Actions an InputObjectType or OutputObjectType to specify that | 
|  | 48 | // for that type, no object is taken/given. | 
|  | 49 | class NoneType {}; | 
|  | 50 |  | 
|  | 51 | template<typename T> | 
|  | 52 | class Action; | 
|  | 53 |  | 
|  | 54 | template<typename ObjectType> | 
|  | 55 | class ActionPipe { | 
|  | 56 | public: | 
| Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 57 | virtual ~ActionPipe() {} | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 58 |  | 
|  | 59 | // This should be called by an Action on its input pipe. | 
|  | 60 | // Returns a reference to the stored object. | 
|  | 61 | const ObjectType& contents() const { return contents_; } | 
|  | 62 |  | 
|  | 63 | // This should be called by an Action on its output pipe. | 
|  | 64 | // Stores a copy of the passed object in this pipe. | 
|  | 65 | void set_contents(const ObjectType& contents) { contents_ = contents; } | 
|  | 66 |  | 
|  | 67 | // Bonds two Actions together with a new ActionPipe. The ActionPipe is | 
|  | 68 | // jointly owned by the two Actions and will be automatically destroyed | 
|  | 69 | // when the last Action is destroyed. | 
|  | 70 | template<typename FromAction, typename ToAction> | 
|  | 71 | static void Bond(FromAction* from, ToAction* to) { | 
| Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 72 | std::shared_ptr<ActionPipe<ObjectType>> pipe(new ActionPipe<ObjectType>); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 73 | from->set_out_pipe(pipe); | 
|  | 74 |  | 
|  | 75 | to->set_in_pipe(pipe);  // If you get an error on this line, then | 
|  | 76 | // it most likely means that the From object's OutputObjectType is | 
|  | 77 | // different from the To object's InputObjectType. | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | private: | 
|  | 81 | ObjectType contents_; | 
|  | 82 |  | 
|  | 83 | // The ctor is private. This is because this class should construct itself | 
|  | 84 | // via the static Bond() method. | 
|  | 85 | ActionPipe() {} | 
|  | 86 | DISALLOW_COPY_AND_ASSIGN(ActionPipe); | 
|  | 87 | }; | 
|  | 88 |  | 
|  | 89 | // Utility function | 
|  | 90 | template<typename FromAction, typename ToAction> | 
|  | 91 | void BondActions(FromAction* from, ToAction* to) { | 
| Alex Vakulenko | a3cf75a | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 92 | static_assert( | 
|  | 93 | std::is_same<typename FromAction::OutputObjectType, | 
|  | 94 | typename ToAction::InputObjectType>::value, | 
|  | 95 | "FromAction::OutputObjectType doesn't match ToAction::InputObjectType"); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 96 | ActionPipe<typename FromAction::OutputObjectType>::Bond(from, to); | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | }  // namespace chromeos_update_engine | 
|  | 100 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 101 | #endif  // UPDATE_ENGINE_COMMON_ACTION_PIPE_H_ |