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 //
16 
17 #ifndef UPDATE_ENGINE_COMMON_ACTION_PIPE_H_
18 #define UPDATE_ENGINE_COMMON_ACTION_PIPE_H_
19 
20 #include <stdio.h>
21 
22 #include <map>
23 #include <memory>
24 #include <string>
25 
26 #include <base/logging.h>
27 #include <base/macros.h>
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 
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:
~ActionPipe()57   virtual ~ActionPipe() {}
58 
59   // This should be called by an Action on its input pipe.
60   // Returns a reference to the stored object.
contents()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.
set_contents(const ObjectType & contents)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>
Bond(FromAction * from,ToAction * to)71   static void Bond(FromAction* from, ToAction* to) {
72     std::shared_ptr<ActionPipe<ObjectType>> pipe(new ActionPipe<ObjectType>);
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.
ActionPipe()85   ActionPipe() {}
86   DISALLOW_COPY_AND_ASSIGN(ActionPipe);
87 };
88 
89 // Utility function
90 template<typename FromAction, typename ToAction>
BondActions(FromAction * from,ToAction * to)91 void BondActions(FromAction* from, ToAction* to) {
92   static_assert(
93       std::is_same<typename FromAction::OutputObjectType,
94                    typename ToAction::InputObjectType>::value,
95       "FromAction::OutputObjectType doesn't match ToAction::InputObjectType");
96   ActionPipe<typename FromAction::OutputObjectType>::Bond(from, to);
97 }
98 
99 }  // namespace chromeos_update_engine
100 
101 #endif  // UPDATE_ENGINE_COMMON_ACTION_PIPE_H_
102