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