1 /* 2 * Copyright 2022 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 <functional> 20 #include <mutex> 21 #include <unordered_map> 22 23 namespace netsim::controller { 24 25 class DeviceNotifyManager { 26 public: 27 DeviceNotifyManager(const DeviceNotifyManager &) = delete; 28 DeviceNotifyManager &operator=(const DeviceNotifyManager &) = delete; 29 DeviceNotifyManager(DeviceNotifyManager &&) = delete; 30 DeviceNotifyManager &operator=(DeviceNotifyManager &&) = delete; 31 Get()32 static DeviceNotifyManager &Get() { 33 static DeviceNotifyManager *kInstance = new DeviceNotifyManager(); 34 return *kInstance; 35 } 36 37 // Register a callback from an observer. 38 unsigned int Register(std::function<void(void)> callback); 39 40 // Unregister a callback from an observer. 41 void Unregister(unsigned int callback_id); 42 43 // Notify observers for device updates. 44 void Notify(); 45 46 private: 47 DeviceNotifyManager() = 48 default; // Disallow instantiation outside of the class. 49 50 std::unordered_map<unsigned int, std::function<void(void)>> 51 registered_callbacks_; 52 std::mutex mutex_; 53 static unsigned int next_available_callback_id_; 54 }; 55 56 } // namespace netsim::controller 57