| Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
| Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| Sreeja Kamishetty | d9f68db | 2022-08-03 19:42:47 | [diff] [blame] | 5 | #ifndef BASE_STATE_TRANSITIONS_H_ |
| 6 | #define BASE_STATE_TRANSITIONS_H_ |
| Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 7 | |
| 8 | #include <vector> |
| 9 | |
| Hans Wennborg | f30ad80 | 2020-06-20 16:50:20 | [diff] [blame] | 10 | #include "base/check_op.h" |
| Lei Zhang | de19767 | 2021-04-29 08:11:24 | [diff] [blame] | 11 | #include "base/containers/contains.h" |
| Chris Fredrickson | fde9a731f | 2025-07-08 20:13:58 | [diff] [blame] | 12 | #include "base/containers/span.h" |
| Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 13 | |
| Sreeja Kamishetty | d9f68db | 2022-08-03 19:42:47 | [diff] [blame] | 14 | namespace base { |
| Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 15 | |
| 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 Daly | 5f8a336 | 2020-06-17 11:02:42 | [diff] [blame] | 22 | // In foo.h |
| 23 | // --------- |
| Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 24 | // enum class State { |
| 25 | // kState1, |
| 26 | // kState2, |
| 27 | // kState3, |
| 28 | // }; |
| 29 | // |
| Fergal Daly | 5f8a336 | 2020-06-17 11:02:42 | [diff] [blame] | 30 | // // 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 Kamishetty | d9f68db | 2022-08-03 19:42:47 | [diff] [blame] | 39 | // #include "base/state_transitions.h" |
| Fergal Daly | 5f8a336 | 2020-06-17 11:02:42 | [diff] [blame] | 40 | // |
| Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 41 | // std::ostream& operator<<(std::ostream& o, const State& s) { |
| 42 | // return o << static_cast<int>(s); |
| 43 | // } |
| Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 44 | // |
| 45 | // void DCheckStateTransition(State old_state, State new_state) { |
| 46 | // #if DCHECK_IS_ON() |
| arthursonzogni | bbf29f6 | 2020-06-22 17:02:45 | [diff] [blame] | 47 | // static const base::NoDestructor<StateTransitions<State>> transitions( |
| 48 | // StateTransitions<State>({ |
| 49 | // {kState1, {kState2, kState3}}, |
| 50 | // {kState2, {kState3}}, |
| 51 | // {kState3, {}}, |
| 52 | // })); |
| Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 53 | // DCHECK_STATE_TRANSITION(transitions, old_state, new_state); |
| 54 | // #endif // DCHECK_IS_ON() |
| 55 | // } |
| Fergal Daly | 5f8a336 | 2020-06-17 11:02:42 | [diff] [blame] | 56 | // --------- |
| Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 57 | |
| 58 | template <typename State> |
| 59 | struct 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 Fredrickson | fde9a731f | 2025-07-08 20:13:58 | [diff] [blame] | 75 | span<const State> GetValidTransitions(const State& source) const |
| 76 | LIFETIME_BOUND { |
| Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 77 | for (const StateTransition& state_transition : state_transitions) { |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 78 | if (state_transition.source == source) { |
| Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 79 | return state_transition.destinations; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 80 | } |
| Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 81 | } |
| Chris Fredrickson | fde9a731f | 2025-07-08 20:13:58 | [diff] [blame] | 82 | return span<const State>(); |
| Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 83 | } |
| 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 Daly | 9bd2a727 | 2024-12-13 17:44:40 | [diff] [blame] | 99 | // 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 Kamishetty | d9f68db | 2022-08-03 19:42:47 | [diff] [blame] | 105 | } // namespace base |
| Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 106 | |
| Sreeja Kamishetty | d9f68db | 2022-08-03 19:42:47 | [diff] [blame] | 107 | #endif // BASE_STATE_TRANSITIONS_H_ |