OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_SERVICES_MEDIA_FACTORY_WATCHED_H_ | 5 #ifndef MOJO_SERVICES_MEDIA_FACTORY_WATCHED_H_ |
6 #define MOJO_SERVICES_MEDIA_FACTORY_WATCHED_H_ | 6 #define MOJO_SERVICES_MEDIA_FACTORY_WATCHED_H_ |
7 | 7 |
8 #include "services/media/factory_service/rule_set.h" | 8 #include "services/media/factory_service/rule_set.h" |
9 | 9 |
10 namespace mojo { | 10 namespace mojo { |
11 namespace media { | 11 namespace media { |
12 | 12 |
13 // Wraps an object, providing Events that occur when certain criteria are met. | 13 // Wraps an object, providing Events that occur when certain criteria are met. |
14 template<typename T> class Watched : public RuleSet { | 14 template <typename T> |
| 15 class Watched : public RuleSet { |
15 public: | 16 public: |
16 Watched() {} | 17 Watched() {} |
17 explicit Watched(T t) : t_(t) {} | 18 explicit Watched(T t) : t_(t) {} |
18 | 19 |
19 // Returns a Event that will occur when the value of this Watched changes | 20 // Returns a Event that will occur when the value of this Watched changes |
20 // to comparand (immediately if they're already equal). | 21 // to comparand (immediately if they're already equal). |
21 Event Becomes(const T& comparand) { | 22 Event Becomes(const T& comparand) { |
22 return AddRule([this, comparand]() -> bool { | 23 return AddRule([this, comparand]() -> bool { return t_ == comparand; }); |
23 return t_ == comparand; | |
24 }); | |
25 } | 24 } |
26 | 25 |
27 // Returns a Event that will occur when the value of this Watched changes | 26 // Returns a Event that will occur when the value of this Watched changes |
28 // to something other than comparand (immediately if they're already not | 27 // to something other than comparand (immediately if they're already not |
29 // equal). | 28 // equal). |
30 Event BecomesOtherThan(const T& comparand) { | 29 Event BecomesOtherThan(const T& comparand) { |
31 return AddRule([this, comparand]() -> bool { | 30 return AddRule([this, comparand]() -> bool { return t_ != comparand; }); |
32 return t_ != comparand; | |
33 }); | |
34 } | 31 } |
35 | 32 |
36 void SetWithConsequences(const T& t) { | 33 void SetWithConsequences(const T& t) { |
37 t_ = t; | 34 t_ = t; |
38 CheckRules(); | 35 CheckRules(); |
39 } | 36 } |
40 | 37 |
41 operator T() { | 38 operator T() { return t_; } |
42 return t_; | |
43 } | |
44 | 39 |
45 operator const T&() const { | 40 operator const T&() const { return t_; } |
46 return t_; | |
47 } | |
48 | 41 |
49 private: | 42 private: |
50 T t_; | 43 T t_; |
51 }; | 44 }; |
52 | 45 |
53 } // namespace media | 46 } // namespace media |
54 } // namespace mojo | 47 } // namespace mojo |
55 | 48 |
56 #endif // MOJO_SERVICES_MEDIA_FACTORY_WATCHED_H_ | 49 #endif // MOJO_SERVICES_MEDIA_FACTORY_WATCHED_H_ |
OLD | NEW |