Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 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 | |
Amit Hilbuch | dbb49df | 2019-01-23 22:54:24 | [diff] [blame] | 11 | #include "rtc_base/unique_id_generator.h" |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 12 | |
| 13 | #include <limits> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "rtc_base/helpers.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 17 | #include "rtc_base/string_encode.h" |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 18 | #include "rtc_base/string_to_number.h" |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 19 | |
Amit Hilbuch | dbb49df | 2019-01-23 22:54:24 | [diff] [blame] | 20 | namespace rtc { |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 21 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 22 | UniqueRandomIdGenerator::UniqueRandomIdGenerator() : known_ids_() {} |
Amit Hilbuch | dbb49df | 2019-01-23 22:54:24 | [diff] [blame] | 23 | UniqueRandomIdGenerator::UniqueRandomIdGenerator(ArrayView<uint32_t> known_ids) |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 24 | : known_ids_(known_ids.begin(), known_ids.end()) {} |
| 25 | |
| 26 | UniqueRandomIdGenerator::~UniqueRandomIdGenerator() = default; |
| 27 | |
| 28 | uint32_t UniqueRandomIdGenerator::GenerateId() { |
Elad Alon | efc9a14 | 2019-02-08 22:35:59 | [diff] [blame] | 29 | RTC_CHECK_LT(known_ids_.size(), std::numeric_limits<uint32_t>::max() - 1); |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 30 | while (true) { |
Amit Hilbuch | dbb49df | 2019-01-23 22:54:24 | [diff] [blame] | 31 | auto pair = known_ids_.insert(CreateRandomNonZeroId()); |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 32 | if (pair.second) { |
| 33 | return *pair.first; |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
Elad Alon | efc9a14 | 2019-02-08 22:35:59 | [diff] [blame] | 38 | bool UniqueRandomIdGenerator::AddKnownId(uint32_t value) { |
| 39 | return known_ids_.insert(value).second; |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 40 | } |
| 41 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 42 | UniqueStringGenerator::UniqueStringGenerator() : unique_number_generator_() {} |
Amit Hilbuch | dbb49df | 2019-01-23 22:54:24 | [diff] [blame] | 43 | UniqueStringGenerator::UniqueStringGenerator(ArrayView<std::string> known_ids) { |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 44 | for (const std::string& str : known_ids) { |
| 45 | AddKnownId(str); |
| 46 | } |
| 47 | } |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 48 | |
| 49 | UniqueStringGenerator::~UniqueStringGenerator() = default; |
| 50 | |
| 51 | std::string UniqueStringGenerator::GenerateString() { |
Amit Hilbuch | dbb49df | 2019-01-23 22:54:24 | [diff] [blame] | 52 | return ToString(unique_number_generator_.GenerateNumber()); |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 53 | } |
| 54 | |
Elad Alon | efc9a14 | 2019-02-08 22:35:59 | [diff] [blame] | 55 | bool UniqueStringGenerator::AddKnownId(const std::string& value) { |
Amit Hilbuch | dbb49df | 2019-01-23 22:54:24 | [diff] [blame] | 56 | absl::optional<uint32_t> int_value = StringToNumber<uint32_t>(value); |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 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()) { |
Elad Alon | efc9a14 | 2019-02-08 22:35:59 | [diff] [blame] | 60 | return unique_number_generator_.AddKnownId(int_value.value()); |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 61 | } |
Elad Alon | efc9a14 | 2019-02-08 22:35:59 | [diff] [blame] | 62 | return false; |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 63 | } |
| 64 | |
Amit Hilbuch | dbb49df | 2019-01-23 22:54:24 | [diff] [blame] | 65 | } // namespace rtc |