From 05a6fbf20c826389f3ed12da5587550a904b9aa8 Mon Sep 17 00:00:00 2001 From: Qin Wang Date: Fri, 6 Jan 2023 12:54:25 -0800 Subject: [PATCH] Migrate observer_list.h from location/nearby to third_party/nearby PiperOrigin-RevId: 500240685 --- Package.swift | 1 + internal/base/BUILD | 20 ++++++++++++ internal/base/observer_list.h | 58 +++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 internal/base/BUILD create mode 100644 internal/base/observer_list.h diff --git a/Package.swift b/Package.swift index 321d0af2..86b592b1 100644 --- a/Package.swift +++ b/Package.swift @@ -417,6 +417,7 @@ let package = Package( "internal/platform/BUILD", "internal/analytics/BUILD", "internal/network/BUILD", + "internal/base/BUILD", // tests "connections/listeners_test.cc", "connections/strategy_test.cc", diff --git a/internal/base/BUILD b/internal/base/BUILD new file mode 100644 index 00000000..94a668c2 --- /dev/null +++ b/internal/base/BUILD @@ -0,0 +1,20 @@ +licenses(["notice"]) + +cc_library( + name = "base", + srcs = [ + ], + hdrs = [ + "observer_list.h", + ], + copts = [ + "-Ithird_party", + ], + visibility = [ + "//internal:__pkg__", + "//location/nearby/cpp/fastpair:__subpackages__", + ], + deps = [ + "@com_google_absl//absl/container:flat_hash_set", + ], +) diff --git a/internal/base/observer_list.h b/internal/base/observer_list.h new file mode 100644 index 00000000..c3f45616 --- /dev/null +++ b/internal/base/observer_list.h @@ -0,0 +1,58 @@ +// Copyright 2021 Google LLC +// +// 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 +// +// https://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 THIRD_PARTY_NEARBY_INTERNAL_BASE_OBSERVER_LIST_H_ +#define THIRD_PARTY_NEARBY_INTERNAL_BASE_OBSERVER_LIST_H_ + +#include +#include + +#include "absl/container/flat_hash_set.h" + +namespace nearby { + +template +class ObserverList { + public: + using iterator = typename absl::flat_hash_set::iterator; + using const_iterator = + typename absl::flat_hash_set::const_iterator; + + void AddObserver(ObserverType* observer) { observers_.emplace(observer); } + + void RemoveObserver(ObserverType* observer) { observers_.erase(observer); } + + bool HasObserver(ObserverType* observer) { + return observers_.contains(observer); + } + + void Clear() { observers_.clear(); } + + bool empty() const { return observers_.empty(); } + + int size() const { return observers_.size(); } + + // Supports iterators + iterator begin() { return observers_.begin(); } + iterator end() { return observers_.end(); } + const_iterator begin() const { return observers_.begin(); } + const_iterator end() const { return observers_.end(); } + + private: + absl::flat_hash_set observers_; +}; + +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_INTERNAL_BASE_OBSERVER_LIST_H_