blob: 3a1219a2b97a3631358ded65b0a607bf95c24fee [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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.com49fdf182009-10-10 00:57:34 +000016
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_ACTION_PIPE_H_
18#define UPDATE_ENGINE_ACTION_PIPE_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
20#include <stdio.h>
Alex Deymobc91a272014-05-20 16:45:33 -070021
rspangler@google.com49fdf182009-10-10 00:57:34 +000022#include <map>
Alex Deymobc91a272014-05-20 16:45:33 -070023#include <memory>
rspangler@google.com49fdf182009-10-10 00:57:34 +000024#include <string>
Alex Deymobc91a272014-05-20 16:45:33 -070025
Alex Deymobc91a272014-05-20 16:45:33 -070026#include <base/logging.h>
Ben Chan05735a12014-09-03 07:48:22 -070027#include <base/macros.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000028
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.com49fdf182009-10-10 00:57:34 +000045namespace chromeos_update_engine {
46
47// Used by Actions an InputObjectType or OutputObjectType to specify that
48// for that type, no object is taken/given.
49class NoneType {};
50
51template<typename T>
52class Action;
53
54template<typename ObjectType>
55class ActionPipe {
56 public:
Darin Petkovfbb40092010-07-29 17:05:50 -070057 virtual ~ActionPipe() {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000058
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 Chanf9cb98c2014-09-21 18:31:30 -070072 std::shared_ptr<ActionPipe<ObjectType>> pipe(new ActionPipe<ObjectType>);
rspangler@google.com49fdf182009-10-10 00:57:34 +000073 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
90template<typename FromAction, typename ToAction>
91void BondActions(FromAction* from, ToAction* to) {
92 // TODO(adlr): find something like this that the compiler accepts:
93 // COMPILE_ASSERT(typeof(typename FromAction::OutputObjectType) ==
94 // typeof(typename ToAction::InputObjectType),
95 // FromAction_OutputObjectType_doesnt_match_ToAction_InputObjectType);
96 ActionPipe<typename FromAction::OutputObjectType>::Bond(from, to);
97}
98
99} // namespace chromeos_update_engine
100
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700101#endif // UPDATE_ENGINE_ACTION_PIPE_H_