blob: a59c210161fe096d7c47a043c5499a01b71743f5 [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
adlr@google.comc98a7ed2009-12-04 18:54:03 +00005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_PIPE_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_PIPE_H__
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
8#include <stdio.h>
9#include <iostream>
10#include <map>
11#include <string>
12#include <tr1/memory>
rspangler@google.com49fdf182009-10-10 00:57:34 +000013#include "base/basictypes.h"
Chris Masone790e62e2010-08-12 10:41:18 -070014#include "base/logging.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000015
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
rspangler@google.com49fdf182009-10-10 00:57:34 +000032namespace chromeos_update_engine {
33
34// Used by Actions an InputObjectType or OutputObjectType to specify that
35// for that type, no object is taken/given.
36class NoneType {};
37
38template<typename T>
39class Action;
40
41template<typename ObjectType>
42class ActionPipe {
43 public:
Darin Petkovfbb40092010-07-29 17:05:50 -070044 virtual ~ActionPipe() {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000045
46 // This should be called by an Action on its input pipe.
47 // Returns a reference to the stored object.
48 const ObjectType& contents() const { return contents_; }
49
50 // This should be called by an Action on its output pipe.
51 // Stores a copy of the passed object in this pipe.
52 void set_contents(const ObjectType& contents) { contents_ = contents; }
53
54 // Bonds two Actions together with a new ActionPipe. The ActionPipe is
55 // jointly owned by the two Actions and will be automatically destroyed
56 // when the last Action is destroyed.
57 template<typename FromAction, typename ToAction>
58 static void Bond(FromAction* from, ToAction* to) {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080059 std::tr1::shared_ptr<ActionPipe<ObjectType> > pipe(
60 new ActionPipe<ObjectType>);
rspangler@google.com49fdf182009-10-10 00:57:34 +000061 from->set_out_pipe(pipe);
62
63 to->set_in_pipe(pipe); // If you get an error on this line, then
64 // it most likely means that the From object's OutputObjectType is
65 // different from the To object's InputObjectType.
66 }
67
68 private:
69 ObjectType contents_;
70
71 // The ctor is private. This is because this class should construct itself
72 // via the static Bond() method.
73 ActionPipe() {}
74 DISALLOW_COPY_AND_ASSIGN(ActionPipe);
75};
76
77// Utility function
78template<typename FromAction, typename ToAction>
79void BondActions(FromAction* from, ToAction* to) {
80 // TODO(adlr): find something like this that the compiler accepts:
81 // COMPILE_ASSERT(typeof(typename FromAction::OutputObjectType) ==
82 // typeof(typename ToAction::InputObjectType),
83 // FromAction_OutputObjectType_doesnt_match_ToAction_InputObjectType);
84 ActionPipe<typename FromAction::OutputObjectType>::Bond(from, to);
85}
86
87} // namespace chromeos_update_engine
88
adlr@google.comc98a7ed2009-12-04 18:54:03 +000089#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PIPE_H__