1 /* 2 * Copyright 2018 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "rtc_base/unique_id_generator.h" 12 13 #include <limits> 14 #include <vector> 15 16 #include "rtc_base/helpers.h" 17 #include "rtc_base/string_encode.h" 18 #include "rtc_base/string_to_number.h" 19 20 namespace rtc { 21 UniqueRandomIdGenerator()22UniqueRandomIdGenerator::UniqueRandomIdGenerator() : known_ids_() {} UniqueRandomIdGenerator(ArrayView<uint32_t> known_ids)23UniqueRandomIdGenerator::UniqueRandomIdGenerator(ArrayView<uint32_t> known_ids) 24 : known_ids_(known_ids.begin(), known_ids.end()) {} 25 26 UniqueRandomIdGenerator::~UniqueRandomIdGenerator() = default; 27 GenerateId()28uint32_t UniqueRandomIdGenerator::GenerateId() { 29 RTC_CHECK_LT(known_ids_.size(), std::numeric_limits<uint32_t>::max() - 1); 30 while (true) { 31 auto pair = known_ids_.insert(CreateRandomNonZeroId()); 32 if (pair.second) { 33 return *pair.first; 34 } 35 } 36 } 37 AddKnownId(uint32_t value)38bool UniqueRandomIdGenerator::AddKnownId(uint32_t value) { 39 return known_ids_.insert(value).second; 40 } 41 UniqueStringGenerator()42UniqueStringGenerator::UniqueStringGenerator() : unique_number_generator_() {} UniqueStringGenerator(ArrayView<std::string> known_ids)43UniqueStringGenerator::UniqueStringGenerator(ArrayView<std::string> known_ids) { 44 for (const std::string& str : known_ids) { 45 AddKnownId(str); 46 } 47 } 48 49 UniqueStringGenerator::~UniqueStringGenerator() = default; 50 GenerateString()51std::string UniqueStringGenerator::GenerateString() { 52 return ToString(unique_number_generator_.GenerateNumber()); 53 } 54 AddKnownId(const std::string & value)55bool UniqueStringGenerator::AddKnownId(const std::string& value) { 56 absl::optional<uint32_t> int_value = StringToNumber<uint32_t>(value); 57 // The underlying generator works for uint32_t values, so if the provided 58 // value is not a uint32_t it will never be generated anyway. 59 if (int_value.has_value()) { 60 return unique_number_generator_.AddKnownId(int_value.value()); 61 } 62 return false; 63 } 64 65 } // namespace rtc 66