Flutter Engine
The Flutter Engine
ax_unique_id.cc
Go to the documentation of this file.
1// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ax_unique_id.h"
6
7#include <memory>
8#include <unordered_set>
9
11#include "base/logging.h"
12
13namespace ui {
14
15namespace {
16
17std::unordered_set<int32_t> g_assigned_ids;
18
19} // namespace
20
22
23AXUniqueId::AXUniqueId(const int32_t max_id) : id_(GetNextAXUniqueId(max_id)) {}
24
26 g_assigned_ids.erase(id_);
27}
28
29bool AXUniqueId::operator==(const AXUniqueId& other) const {
30 return Get() == other.Get();
31}
32
33bool AXUniqueId::operator!=(const AXUniqueId& other) const {
34 return !(*this == other);
35}
36
37bool AXUniqueId::IsAssigned(const int32_t id) const {
38 return base::Contains(g_assigned_ids, id);
39}
40
41int32_t AXUniqueId::GetNextAXUniqueId(const int32_t max_id) {
42 static int32_t current_id = 0;
43 static bool has_wrapped = false;
44
45 const int32_t prev_id = current_id;
46 do {
47 if (current_id >= max_id) {
48 current_id = 1;
49 has_wrapped = true;
50 } else {
51 ++current_id;
52 }
53 if (current_id == prev_id) {
54 BASE_LOG() << "There are over 2 billion available IDs, so the newly "
55 "created ID cannot be equal to the most recently created "
56 "ID.";
57 }
58 // If it |has_wrapped| then we need to continue until we find the first
59 // unassigned ID.
60 } while (has_wrapped && IsAssigned(current_id));
61
62 g_assigned_ids.insert(current_id);
63 return current_id;
64}
65
66} // namespace ui
bool operator!=(const AXUniqueId &other) const
Definition: ax_unique_id.cc:33
int32_t Get() const
Definition: ax_unique_id.h:30
bool operator==(const AXUniqueId &other) const
Definition: ax_unique_id.cc:29
virtual ~AXUniqueId()
Definition: ax_unique_id.cc:25
bool Contains(const Container &container, const Value &value)
#define BASE_LOG()
Definition: logging.h:54