Make UniqueNumberGenerator::AddKnownId() return a value
Make AddKnownId() return a value to indicate whether the ID was
known before, or has only been made known now.
This allows users of the class to RTC_DCHECK that no collisions
existed in their seed set, for instance.
This change is done for the following classes:
1. UniqueNumberGenerator
2. UniqueRandomIdGenerator
3. UniqueStringGenerator
Bug: None
Change-Id: I627d2821cb76aa333075e36575088d76dbeb3665
Reviewed-on: https://webrtc-review.googlesource.com/c/121780
Commit-Queue: Elad Alon <eladalon@webrtc.org>
Reviewed-by: Amit Hilbuch <amithi@webrtc.org>
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26621}
diff --git a/rtc_base/unique_id_generator.h b/rtc_base/unique_id_generator.h
index 849daa9..836dc70 100644
--- a/rtc_base/unique_id_generator.h
+++ b/rtc_base/unique_id_generator.h
@@ -43,7 +43,8 @@
TIntegral operator()() { return GenerateNumber(); }
// Adds an id that this generator should no longer generate.
- void AddKnownId(TIntegral value);
+ // Return value indicates whether the ID was hitherto unknown.
+ bool AddKnownId(TIntegral value);
private:
static_assert(std::is_integral<TIntegral>::value, "Must be integral type.");
@@ -71,7 +72,8 @@
uint32_t operator()() { return GenerateId(); }
// Adds an id that this generator should no longer generate.
- void AddKnownId(uint32_t value);
+ // Return value indicates whether the ID was hitherto unknown.
+ bool AddKnownId(uint32_t value);
private:
std::set<uint32_t> known_ids_;
@@ -93,7 +95,8 @@
std::string operator()() { return GenerateString(); }
// Adds an id that this generator should no longer generate.
- void AddKnownId(const std::string& value);
+ // Return value indicates whether the ID was hitherto unknown.
+ bool AddKnownId(const std::string& value);
private:
// This implementation will be simple and will generate "0", "1", ...
@@ -123,8 +126,8 @@
}
template <typename TIntegral>
-void UniqueNumberGenerator<TIntegral>::AddKnownId(TIntegral value) {
- known_ids_.insert(value);
+bool UniqueNumberGenerator<TIntegral>::AddKnownId(TIntegral value) {
+ return known_ids_.insert(value).second;
}
} // namespace rtc