1 // Copyright 2022 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "controller/device_notify_manager.h" 16 17 #include <climits> 18 19 namespace netsim::controller { 20 Register(std::function<void (void)> callback)21unsigned int DeviceNotifyManager::Register(std::function<void(void)> callback) { 22 std::unique_lock<std::mutex> lock(mutex_); 23 if (next_available_callback_id_ == UINT_MAX) next_available_callback_id_ = 0; 24 ++next_available_callback_id_; 25 registered_callbacks_[next_available_callback_id_] = std::move(callback); 26 return next_available_callback_id_; 27 } 28 Unregister(unsigned int callback_id)29void DeviceNotifyManager::Unregister(unsigned int callback_id) { 30 std::unique_lock<std::mutex> lock(mutex_); 31 registered_callbacks_.erase(callback_id); 32 } 33 Notify()34void DeviceNotifyManager::Notify() { 35 std::unique_lock<std::mutex> lock(mutex_); 36 for (auto &[callback_id, callback] : registered_callbacks_) callback(); 37 } 38 39 unsigned int DeviceNotifyManager::next_available_callback_id_ = 0; 40 41 } // namespace netsim::controller 42