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