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