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
6
7#include <algorithm>
8#include <mutex>
9
10namespace fml {
11
13 const std::function<void()>& handler) {
15 return *this;
16}
17
19 const std::function<void()>& handler) {
20 false_handler = handler;
21 return *this;
22}
23
24SyncSwitch::SyncSwitch(bool value) : value_(value) {}
25
26void SyncSwitch::Execute(const SyncSwitch::Handlers& handlers) const {
27 std::shared_lock lock(mutex_);
28 if (value_) {
29 handlers.true_handler();
30 } else {
31 handlers.false_handler();
32 }
33}
34
35void SyncSwitch::SetSwitch(bool value) {
36 {
37 std::unique_lock lock(mutex_);
38 value_ = value;
39 }
40 for (Observer* observer : observers_) {
41 observer->OnSyncSwitchUpdate(value);
42 }
43}
44
45void SyncSwitch::AddObserver(Observer* observer) const {
46 std::unique_lock lock(mutex_);
47 if (std::find(observers_.begin(), observers_.end(), observer) ==
48 observers_.end()) {
49 observers_.push_back(observer);
50 }
51}
52
53void SyncSwitch::RemoveObserver(Observer* observer) const {
54 std::unique_lock lock(mutex_);
55 observers_.erase(std::remove(observers_.begin(), observers_.end(), observer),
56 observers_.end());
57}
58} // namespace fml
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)
int32_t value
const gchar FlBinaryMessengerMessageHandler handler
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.
std::function< void()> true_handler
Definition sync_switch.h:42