Flutter Engine
The Flutter Engine
sync_switch.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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#include "flutter/fml/synchronization/sync_switch.h"
6
7namespace fml {
8
10 const std::function<void()>& handler) {
11 true_handler = handler;
12 return *this;
13}
14
16 const std::function<void()>& handler) {
17 false_handler = handler;
18 return *this;
19}
20
22 : mutex_(std::unique_ptr<fml::SharedMutex>(fml::SharedMutex::Create())),
23 value_(value) {}
24
25void SyncSwitch::Execute(const SyncSwitch::Handlers& handlers) const {
26 fml::SharedLock lock(*mutex_);
27 if (value_) {
28 handlers.true_handler();
29 } else {
30 handlers.false_handler();
31 }
32}
33
35 {
36 fml::UniqueLock lock(*mutex_);
37 value_ = value;
38 }
39 for (Observer* observer : observers_) {
40 observer->OnSyncSwitchUpdate(value);
41 }
42}
43
44void SyncSwitch::AddObserver(Observer* observer) const {
45 fml::UniqueLock lock(*mutex_);
46 if (std::find(observers_.begin(), observers_.end(), observer) ==
47 observers_.end()) {
48 observers_.push_back(observer);
49 }
50}
51
52void SyncSwitch::RemoveObserver(Observer* observer) const {
53 fml::UniqueLock lock(*mutex_);
54 observers_.erase(std::remove(observers_.begin(), observers_.end(), observer),
55 observers_.end());
56}
57} // namespace fml
static sk_sp< Effect > Create()
Definition: RefCntTest.cpp:117
int find(T *array, int N, T item)
Observes changes to the SyncSwitch.
Definition: sync_switch.h:25
void AddObserver(Observer *observer) const
Threadsafe.
Definition: sync_switch.cc:44
SyncSwitch(bool value=false)
Definition: sync_switch.cc:21
void RemoveObserver(Observer *observer) const
Threadsafe.
Definition: sync_switch.cc:52
void Execute(const Handlers &handlers) const
Definition: sync_switch.cc:25
void SetSwitch(bool value)
Definition: sync_switch.cc:34
uint8_t value
Dart_NativeFunction function
Definition: fuchsia.cc:51
Definition: ascii_trie.cc:9
def remove(*paths)
Definition: ref_ptr.h:256
Represents the 2 code paths available when calling |SyncSwitch::Execute|.
Definition: sync_switch.h:35
std::function< void()> false_handler
Definition: sync_switch.h:43
Handlers & SetIfFalse(const std::function< void()> &handler)
Sets the handler that will be executed if the |SyncSwitch| is false.
Definition: sync_switch.cc:15
Handlers & SetIfTrue(const std::function< void()> &handler)
Sets the handler that will be executed if the |SyncSwitch| is true.
Definition: sync_switch.cc:9
std::function< void()> true_handler
Definition: sync_switch.h:42