Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: media/base/simple_state_machine.h

Issue 2568663002: [WIP] - trying out some state machine ideas.
Patch Set: added SimpleStateMachine, updated example Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/base/BUILD.gn ('k') | media/base/state.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_SIMPLE_STATE_MACHINE_H_
6 #define MEDIA_BASE_SIMPLE_STATE_MACHINE_H_
7
8 #include <tuple>
9 #include <type_traits>
10
11 #include "base/memory/ref_counted.h"
12 #include "media/base/media_export.h"
13 #include "media/base/state_machine.h"
14
15 namespace media {
16
17 // Convenient template for state machines that have all of their state provided
18 // during construction. Provides storage for each of the states, registers
19 // them with StateMachine, and provides a getter for them.
20 // |StateTypes| should be the raw type for a State subclass. These must be
21 // unique right now, unfortunately, for the provided Get template to work. We
22 // could work around this with an optional ordinal parameter in the getter.
23 template <typename... StateTypes>
24 class SimpleStateMachine : public StateMachine {
25 public:
26 SimpleStateMachine(scoped_refptr<StateTypes>... states) : states_(states...) {
27 // Register all states with StateMachine.
28 RegisterStates<0>();
29 }
30
31 // Return the state of type |T|.
32 template <typename T>
33 T* Get() const {
34 return FindElement<0, T>();
35 }
36
37 private:
38 typedef std::tuple<scoped_refptr<StateTypes>...> states_t;
39 states_t states_;
40
41 // Specialization to do nothing if we're asking for a state by index
42 // that's more than we have.
43 template <size_t pos>
44 typename std::enable_if<pos >= std::tuple_size<states_t>::value>::type
45 RegisterStates() {}
46
47 // Specialization to register the |pos|-th state, and recurse to the
48 // next one.
49 template <size_t pos>
50 typename std::enable_if <
51 pos<std::tuple_size<states_t>::value>::type RegisterStates() {
52 UsingState(std::get<pos>(states_));
53 RegisterStates<pos + 1>();
54 }
55
56 // Check for type equality.
57 // TODO: there has to be a better way. rewrite.
58 template <typename T1, typename T2>
59 struct is_same : std::false_type {};
60 template <typename T>
61 struct is_same<T, T> : std::true_type {};
62
63 // When the element at |pos| is a scoped_refptr to |T|, return it as a
64 // raw pointer. If we wanted to support duplicate types, then we should
65 // also take an ordinal. If it's zero, then we return, else we recurse
66 // on |pos+1| and |ordinal-1|.
67 template <size_t pos, typename T>
68 typename std::enable_if <
69 pos<std::tuple_size<states_t>::value &&
70 is_same<typename std::tuple_element<pos, states_t>::type,
71 scoped_refptr<T>>::value,
72 T*>::type
73 FindElement() const {
74 return std::get<pos>(states_).get();
75 }
76
77 // Continue searching for an element of type |scoped_refptr<T>| if the
78 // |pos|-th element doesn't match.
79 template <size_t pos, typename T>
80 typename std::enable_if <
81 pos<std::tuple_size<states_t>::value &&
82 !is_same<typename std::tuple_element<pos, states_t>::type,
83 scoped_refptr<T>>::value,
84 T*>::type
85 FindElement() const {
86 return FindElement<pos + 1, T>();
87 }
88 };
89
90 } // namespace media
91
92 #endif // MEDIA_BASE_SIMPLE_STATE_MACHINE_H_
OLDNEW
« no previous file with comments | « media/base/BUILD.gn ('k') | media/base/state.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698