/* * Copyright (C) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef BLUETOOTH_OBSERVER_LIST_H #define BLUETOOTH_OBSERVER_LIST_H #include #include #include #include #include "bluetooth_log.h" #include "bluetooth_types.h" namespace OHOS { namespace Bluetooth { template class BluetoothObserverList final { public: BluetoothObserverList() = default; ~BluetoothObserverList(); bool Register(std::shared_ptr &observer); bool Deregister(std::shared_ptr &observer); void ForEach(const std::function)> &observer); private: std::mutex lock_{}; std::list> observers_{}; BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothObserverList); }; template BluetoothObserverList::~BluetoothObserverList() { std::lock_guard lock(lock_); observers_.clear(); } template bool BluetoothObserverList::Register(std::shared_ptr &observer) { std::lock_guard lock(lock_); for (auto it = observers_.begin(); it != observers_.end(); ++it) { if (*it == observer) { return true; } } observers_.push_back(observer); return true; } template bool BluetoothObserverList::Deregister(std::shared_ptr &observer) { std::lock_guard lock(lock_); for (auto it = observers_.begin(); it != observers_.end(); ++it) { if (*it == observer) { observers_.erase(it); return true; } } return false; } template void BluetoothObserverList::ForEach(const std::function)> &observer) { std::lock_guard lock(lock_); for (const auto &it : observers_) { if (it != nullptr) { observer(it); } } } } // namespace Bluetooth } // namespace OHOS #endif // BLUETOOTH_OBSERVER_LIST_H