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
9SyncSwitch::Handlers& SyncSwitch::Handlers::SetIfTrue(
10 const std::function<void()>& handler) {
11 true_handler = std::move(handler);
12 return *this;
13}
14
15SyncSwitch::Handlers& SyncSwitch::Handlers::SetIfFalse(
16 const std::function<void()>& handler) {
17 false_handler = std::move(handler);
18 return *this;
19}
20
21SyncSwitch::SyncSwitch() : SyncSwitch(false) {}
22
23SyncSwitch::SyncSwitch(bool value) : value_(value) {}
24
25void SyncSwitch::Execute(const SyncSwitch::Handlers& handlers) {
26 std::scoped_lock guard(mutex_);
27 if (value_) {
28 handlers.true_handler();
29 } else {
30 handlers.false_handler();
31 }
32}
33
34void SyncSwitch::SetSwitch(bool value) {
35 std::scoped_lock guard(mutex_);
36 value_ = value;
37}
38
39} // namespace fml
40