OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium 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 |
| 5 #ifndef MEDIA_BASE_STATE_MACHINE_H_ |
| 6 #define MEDIA_BASE_STATE_MACHINE_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "media/base/media_export.h" |
| 11 #include "media/base/state.h" |
| 12 |
| 13 namespace media { |
| 14 |
| 15 // Stateless implementation of a state machine. The subclasses must handle |
| 16 // being given the State objects, since they'll probably want concrete |
| 17 // subclasses anyway. Subclasses should call UsingState to record a ref to the |
| 18 // state, and automatically get updates for it. |
| 19 class MEDIA_EXPORT StateMachine : public State::Client { |
| 20 public: |
| 21 ~StateMachine() override; |
| 22 |
| 23 // Subclasses will find out about State objects (or concrete subclasses of |
| 24 // state) via methods on the StateMachine subclass / constructor / or |
| 25 // whatever they like. |
| 26 |
| 27 // Thing that owns / runs the state machine (e.g., StateMachineDriver). |
| 28 class Owner { |
| 29 public: |
| 30 // Request that our owner run us, but not re-entrantly. It is unclear |
| 31 // if it may run before returning from this or not, assuming that |us| |
| 32 // is not running currently. It must not run immediately if this is |
| 33 // called recursively from |us->Run()|. |
| 34 // Probably this is never allowed to run before returning. |
| 35 virtual void RequestRun(StateMachine* us) = 0; |
| 36 }; |
| 37 |
| 38 // Set the owner that we'll use. |
| 39 void SetOwner(Owner* owner); |
| 40 |
| 41 // Run the state machine once. |
| 42 virtual void Run() const = 0; |
| 43 |
| 44 // State::Client |
| 45 // If any state notifies us that it has changed. We'll request that our |
| 46 // owner runs us via owner_->RequestRun(this). |
| 47 void OnStateChanged() override; |
| 48 |
| 49 protected: |
| 50 StateMachine(); |
| 51 |
| 52 // Adds |state| to the set that we're using. This will cause us to register |
| 53 // as a client with them, and probably keep a ref (which the caller likely |
| 54 // also has too, to the subclass type). By registering state, we will |
| 55 // automatically request a run when it changes. Alternatively, if it makes |
| 56 // things cleaner, the state could keep a list of (machine, driver) pairs |
| 57 // that it notifies, but that seems unlikely to improve it. |
| 58 void UsingState(scoped_refptr<State> state); |
| 59 |
| 60 private: |
| 61 Owner* owner_; |
| 62 std::vector<scoped_refptr<State>> states_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(StateMachine); |
| 65 }; |
| 66 |
| 67 } // namespace media |
| 68 |
| 69 #endif // MEDIA_BASE_STATE_MACHINE_H_ |
OLD | NEW |