blob: 4c568126a72d3bc67c11945e5a396c12809dfd88 [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
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_COMMON_ACTION_PIPE_H_
18#define UPDATE_ENGINE_COMMON_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
Amin Hassanib2689592019-01-13 17:04:28 -080051template <typename T>
rspangler@google.com49fdf182009-10-10 00:57:34 +000052class Action;
53
Amin Hassanib2689592019-01-13 17:04:28 -080054template <typename ObjectType>
rspangler@google.com49fdf182009-10-10 00:57:34 +000055class 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.
Amin Hassanib2689592019-01-13 17:04:28 -080070 template <typename FromAction, typename ToAction>
rspangler@google.com49fdf182009-10-10 00:57:34 +000071 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_;
Kelvin Zhang9dd93052020-07-21 17:31:19 -040082 // Give unit test access
83 friend class DownloadActionTest;
rspangler@google.com49fdf182009-10-10 00:57:34 +000084
85 // The ctor is private. This is because this class should construct itself
86 // via the static Bond() method.
87 ActionPipe() {}
88 DISALLOW_COPY_AND_ASSIGN(ActionPipe);
89};
90
91// Utility function
Amin Hassanib2689592019-01-13 17:04:28 -080092template <typename FromAction, typename ToAction>
rspangler@google.com49fdf182009-10-10 00:57:34 +000093void BondActions(FromAction* from, ToAction* to) {
Alex Vakulenko0103c362016-01-20 07:56:15 -080094 static_assert(
95 std::is_same<typename FromAction::OutputObjectType,
96 typename ToAction::InputObjectType>::value,
97 "FromAction::OutputObjectType doesn't match ToAction::InputObjectType");
rspangler@google.com49fdf182009-10-10 00:57:34 +000098 ActionPipe<typename FromAction::OutputObjectType>::Bond(from, to);
99}
100
101} // namespace chromeos_update_engine
102
Alex Deymo39910dc2015-11-09 17:04:30 -0800103#endif // UPDATE_ENGINE_COMMON_ACTION_PIPE_H_