Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
34void SyncSwitch::SetSwitch(bool value) {
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()
Observes changes to the SyncSwitch.
Definition sync_switch.h:25
void AddObserver(Observer *observer) const
Threadsafe.
SyncSwitch(bool value=false)
void RemoveObserver(Observer *observer) const
Threadsafe.
void Execute(const Handlers &handlers) const
void SetSwitch(bool value)
uint8_t value
Definition ref_ptr.h:256
Represents the 2 code paths available when calling |SyncSwitchExecute|.
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.
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