File: transition.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (98 lines) | stat: -rw-r--r-- 3,481 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_BOREALIS_INFRA_TRANSITION_H_
#define CHROME_BROWSER_ASH_BOREALIS_INFRA_TRANSITION_H_

#include <memory>

#include "base/functional/bind.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/types/expected.h"

// TODO(b/172501195): Make these available outside namespace borealis.
namespace borealis {

// A transition object is used to represent a transformation of a start state
// |S| into a terminating state |T|. The transition can fail, in which case the
// result is an error state |E|.
//
// The transition centers around a state object of type |S|, which will become
// an instance of |T| if the transition succeeds, and an error |E| if it does
// not.
//
// The transition itself is in one of three modes:
//  - A "pending" transition has been created and is waiting for control of the
//    |S| instance to begin the transition.
//  - A "working" transition has control of the |S| object, and is transforming
//    it into |T|. There is no guarantee at this point about the existence of an
//    |S| or |T| instance.
//  - A "final" transition has completed, and either succeeded (in which case
//    a |T| instance exists) or failed (in which case |T| does not exist and an
//    |E| error has been generated.
//
// Once the transition is "final" no further work is possible and the transition
// can be deleted.
template <typename S, typename T, typename E>
class Transition {
 public:
  using InitialState = S;

  using FinalState = T;

  using ErrorState = E;

  using Result = base::expected<std::unique_ptr<T>, E>;

  using OnCompleteSignature = void(Result);

  using OnCompleteCallback = base::OnceCallback<OnCompleteSignature>;

  Transition() = default;
  virtual ~Transition() = default;

  // Begin the transition, marking this transition as "working" and giving
  // ownership of the |S| object to the transition.
  void Begin(std::unique_ptr<S> start_instance, OnCompleteCallback callback) {
    callback_ = std::move(callback);
    Start(std::move(start_instance));
  }

 protected:
  // Override this method to define the work that this transition performs. As
  // soon as this function is entered, the transition is "working", until you
  // call Succeed() or Fail(), which should be the last things you call.
  virtual void Start(std::unique_ptr<S> start_instance) = 0;

  // Called when the transition has completed successfully. This should be the
  // last thing you do.
  void Succeed(std::unique_ptr<T> terminating_instance) {
    if (!callback_) {
      return;
    }
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback_),
                                  Result(std::move(terminating_instance))));
  }

  // Called when the transition has completed unsuccessfully. This should only
  // be called at the very end of the failing transition (including cleanup if
  // needed).
  void Fail(E error) {
    if (!callback_) {
      return;
    }
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback_),
                                  base::unexpected(std::move(error))));
  }

 private:
  OnCompleteCallback callback_;
};

}  // namespace borealis

#endif  // CHROME_BROWSER_ASH_BOREALIS_INFRA_TRANSITION_H_