Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: c17f0fa7991bdc62080aed47b7b63bb2c5071c3c [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2020 The Chromium Authors
Fergal Daly6abbb2b2020-06-06 11:06:462// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Sreeja Kamishettyd9f68db2022-08-03 19:42:475#ifndef BASE_STATE_TRANSITIONS_H_
6#define BASE_STATE_TRANSITIONS_H_
Fergal Daly6abbb2b2020-06-06 11:06:467
8#include <vector>
9
Hans Wennborgf30ad802020-06-20 16:50:2010#include "base/check_op.h"
Lei Zhangde197672021-04-29 08:11:2411#include "base/containers/contains.h"
Chris Fredricksonfde9a731f2025-07-08 20:13:5812#include "base/containers/span.h"
Fergal Daly6abbb2b2020-06-06 11:06:4613
Sreeja Kamishettyd9f68db2022-08-03 19:42:4714namespace base {
Fergal Daly6abbb2b2020-06-06 11:06:4615
16// This class represents a set of state transitions where each state is a value
17// that supports copy, << and == (e.g. an enum element). It's intended to be
18// used in DCHECK-enabled builds to check that only valid transitions occur. Its
19// implementation favours convenience and simplicity over performance. To use it
20// follow this example:
21
Fergal Daly5f8a3362020-06-17 11:02:4222// In foo.h
23// ---------
Fergal Daly6abbb2b2020-06-06 11:06:4624// enum class State {
25// kState1,
26// kState2,
27// kState3,
28// };
29//
Fergal Daly5f8a3362020-06-17 11:02:4230// // This may require exporting the symbol (e.g. CONTENT_EXPORT) if it will be
31// // used by any other components: one common way this can happen is if the
32// // enum is logged in tests (e.g. via gtest's EXPECT_* macros).
33// std::ostream& operator<<(std::ostream& o, const State& s);
34// ---------
35//
36// In foo.cc
37// ---------
38// #include "base/no_destructor.h"
Sreeja Kamishettyd9f68db2022-08-03 19:42:4739// #include "base/state_transitions.h"
Fergal Daly5f8a3362020-06-17 11:02:4240//
Fergal Daly6abbb2b2020-06-06 11:06:4641// std::ostream& operator<<(std::ostream& o, const State& s) {
42// return o << static_cast<int>(s);
43// }
Fergal Daly6abbb2b2020-06-06 11:06:4644//
45// void DCheckStateTransition(State old_state, State new_state) {
46// #if DCHECK_IS_ON()
arthursonzognibbf29f62020-06-22 17:02:4547// static const base::NoDestructor<StateTransitions<State>> transitions(
48// StateTransitions<State>({
49// {kState1, {kState2, kState3}},
50// {kState2, {kState3}},
51// {kState3, {}},
52// }));
Fergal Daly6abbb2b2020-06-06 11:06:4653// DCHECK_STATE_TRANSITION(transitions, old_state, new_state);
54// #endif // DCHECK_IS_ON()
55// }
Fergal Daly5f8a3362020-06-17 11:02:4256// ---------
Fergal Daly6abbb2b2020-06-06 11:06:4657
58template <typename State>
59struct StateTransitions {
60 public:
61 // Represents a state and all of the states that are valid transitions from
62 // it.
63 struct StateTransition {
64 StateTransition(State source, std::vector<State> destinations)
65 : source(std::move(source)), destinations(std::move(destinations)) {}
66
67 const State source;
68 const std::vector<State> destinations;
69 };
70
71 explicit StateTransitions(std::vector<StateTransition> state_transitions)
72 : state_transitions(std::move(state_transitions)) {}
73
74 // Returns a list of states that are valid to transition to from |source|.
Chris Fredricksonfde9a731f2025-07-08 20:13:5875 span<const State> GetValidTransitions(const State& source) const
76 LIFETIME_BOUND {
Fergal Daly6abbb2b2020-06-06 11:06:4677 for (const StateTransition& state_transition : state_transitions) {
Peter Kasting134ef9af2024-12-28 02:30:0978 if (state_transition.source == source) {
Fergal Daly6abbb2b2020-06-06 11:06:4679 return state_transition.destinations;
Peter Kasting134ef9af2024-12-28 02:30:0980 }
Fergal Daly6abbb2b2020-06-06 11:06:4681 }
Chris Fredricksonfde9a731f2025-07-08 20:13:5882 return span<const State>();
Fergal Daly6abbb2b2020-06-06 11:06:4683 }
84
85 // Tests whether transitioning from |source| to |destination| is valid.
86 bool IsTransitionValid(const State& source, const State& destination) const {
87 return base::Contains(GetValidTransitions(source), destination);
88 }
89
90 const std::vector<StateTransition> state_transitions;
91};
92
93// DCHECK if transitioning from |old_state| to |new_state| is not valid
94// according to |transitions|.
95#define DCHECK_STATE_TRANSITION(transitions, old_state, new_state) \
96 DCHECK((transitions)->IsTransitionValid((old_state), (new_state))) \
97 << "Invalid transition: " << old_state << " -> " << new_state
98
Fergal Daly9bd2a7272024-12-13 17:44:4099// CHECK if transitioning from |old_state| to |new_state| is not valid
100// according to |transitions|.
101#define CHECK_STATE_TRANSITION(transitions, old_state, new_state) \
102 CHECK((transitions)->IsTransitionValid((old_state), (new_state))) \
103 << "Invalid transition: " << old_state << " -> " << new_state
104
Sreeja Kamishettyd9f68db2022-08-03 19:42:47105} // namespace base
Fergal Daly6abbb2b2020-06-06 11:06:46106
Sreeja Kamishettyd9f68db2022-08-03 19:42:47107#endif // BASE_STATE_TRANSITIONS_H_