1 /* 2 * Copyright 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <com_android_input_flags.h> 20 #include <functional> 21 22 namespace android { 23 24 /** 25 * Provide a local override for a flag value. The value is restored when the object of this class 26 * goes out of scope. 27 * This class is not intended to be used directly, because its usage is cumbersome. 28 * Instead, a wrapper macro SCOPED_FLAG_OVERRIDE is provided. 29 */ 30 class ScopedFlagOverride { 31 public: ScopedFlagOverride(std::function<bool ()> read,std::function<void (bool)> write,bool value)32 ScopedFlagOverride(std::function<bool()> read, std::function<void(bool)> write, bool value) 33 : mInitialValue(read()), mWriteValue(write) { 34 mWriteValue(value); 35 } ~ScopedFlagOverride()36 ~ScopedFlagOverride() { mWriteValue(mInitialValue); } 37 38 private: 39 const bool mInitialValue; 40 std::function<void(bool)> mWriteValue; 41 }; 42 43 typedef bool (*ReadFlagValueFunction)(); 44 typedef void (*WriteFlagValueFunction)(bool); 45 46 /** 47 * Use this macro to locally override a flag value. 48 * Example usage: 49 * SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); 50 * Note: this works by creating a local variable in your current scope. Don't call this twice for 51 * the same flag, because the variable names will clash! 52 */ 53 #define SCOPED_FLAG_OVERRIDE(NAME, VALUE) \ 54 ReadFlagValueFunction read##NAME = com::android::input::flags::NAME; \ 55 WriteFlagValueFunction write##NAME = com::android::input::flags::NAME; \ 56 ScopedFlagOverride override##NAME(read##NAME, write##NAME, (VALUE)) 57 58 } // namespace android 59