blob: 7e247d3c98184ea2432b7f85230169c92b8ee6ec [file] [log] [blame]
[email protected]946da2d2013-09-12 23:53:061// Copyright (c) 2013 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
[email protected]a6147a22013-09-26 06:55:095#ifndef UI_GFX_SEQUENTIAL_ID_GENERATOR_H_
6#define UI_GFX_SEQUENTIAL_ID_GENERATOR_H_
[email protected]946da2d2013-09-12 23:53:067
avic89eb8d42015-12-23 08:08:188#include <stdint.h>
9
[email protected]946da2d2013-09-12 23:53:0610#include <map>
11
[email protected]946da2d2013-09-12 23:53:0612#include "base/containers/hash_tables.h"
avic89eb8d42015-12-23 08:08:1813#include "base/macros.h"
[email protected]4ffa7892013-09-27 16:56:0614#include "ui/gfx/gfx_export.h"
[email protected]946da2d2013-09-12 23:53:0615
16namespace ui {
17
18// This is used to generate a series of sequential ID numbers in a way that a
19// new ID is always the lowest possible ID in the sequence.
[email protected]4ffa7892013-09-27 16:56:0620class GFX_EXPORT SequentialIDGenerator {
[email protected]946da2d2013-09-12 23:53:0621 public:
22 // Creates a new generator with the specified lower bound for the IDs.
avic89eb8d42015-12-23 08:08:1823 explicit SequentialIDGenerator(uint32_t min_id);
[email protected]946da2d2013-09-12 23:53:0624 ~SequentialIDGenerator();
25
26 // Generates a unique ID to represent |number|. The generated ID is the
27 // smallest available ID greater than or equal to the |min_id| specified
28 // during creation of the generator.
avic89eb8d42015-12-23 08:08:1829 uint32_t GetGeneratedID(uint32_t number);
[email protected]946da2d2013-09-12 23:53:0630
31 // Checks to see if the generator currently has a unique ID generated for
32 // |number|.
avic89eb8d42015-12-23 08:08:1833 bool HasGeneratedIDFor(uint32_t number) const;
[email protected]946da2d2013-09-12 23:53:0634
Eugene Girarda575d302017-08-28 16:58:3635 // Removes the ID previously generated for |number| if necessary.
36 void MaybeReleaseNumber(uint32_t number);
37
[email protected]946da2d2013-09-12 23:53:0638 // Removes the generated ID |id| from the internal mapping. Since the ID is
39 // no longer mapped to any number, subsequent calls to |GetGeneratedID()| can
40 // use this ID.
avic89eb8d42015-12-23 08:08:1841 void ReleaseGeneratedID(uint32_t id);
[email protected]946da2d2013-09-12 23:53:0642
43 // Removes the ID previously generated for |number| by calling
44 // |GetGeneratedID()|.
avic89eb8d42015-12-23 08:08:1845 void ReleaseNumber(uint32_t number);
[email protected]946da2d2013-09-12 23:53:0646
[email protected]2c6a1be82014-07-30 07:04:1947 void ResetForTest();
48
[email protected]946da2d2013-09-12 23:53:0649 private:
avic89eb8d42015-12-23 08:08:1850 typedef base::hash_map<uint32_t, uint32_t> IDMap;
[email protected]946da2d2013-09-12 23:53:0651
avic89eb8d42015-12-23 08:08:1852 uint32_t GetNextAvailableID();
[email protected]d74eabc2013-09-17 02:26:4453
avic89eb8d42015-12-23 08:08:1854 void UpdateNextAvailableIDAfterRelease(uint32_t id);
[email protected]946da2d2013-09-12 23:53:0655
56 IDMap number_to_id_;
57 IDMap id_to_number_;
58
avic89eb8d42015-12-23 08:08:1859 const uint32_t min_id_;
60 uint32_t min_available_id_;
[email protected]946da2d2013-09-12 23:53:0661
62 DISALLOW_COPY_AND_ASSIGN(SequentialIDGenerator);
63};
64
65} // namespace ui
66
[email protected]a6147a22013-09-26 06:55:0967#endif // UI_GFX_SEQUENTIAL_ID_GENERATOR_H_