[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 1 | // 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] | a6147a2 | 2013-09-26 06:55:09 | [diff] [blame] | 5 | #ifndef UI_GFX_SEQUENTIAL_ID_GENERATOR_H_ |
6 | #define UI_GFX_SEQUENTIAL_ID_GENERATOR_H_ | ||||
[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 7 | |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 8 | #include <stdint.h> |
9 | |||||
[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 10 | #include <map> |
11 | |||||
[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 12 | #include "base/containers/hash_tables.h" |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | 4ffa789 | 2013-09-27 16:56:06 | [diff] [blame] | 14 | #include "ui/gfx/gfx_export.h" |
[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 15 | |
16 | namespace 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] | 4ffa789 | 2013-09-27 16:56:06 | [diff] [blame] | 20 | class GFX_EXPORT SequentialIDGenerator { |
[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 21 | public: |
22 | // Creates a new generator with the specified lower bound for the IDs. | ||||
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 23 | explicit SequentialIDGenerator(uint32_t min_id); |
[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 24 | ~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. | ||||
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 29 | uint32_t GetGeneratedID(uint32_t number); |
[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 30 | |
31 | // Checks to see if the generator currently has a unique ID generated for | ||||
32 | // |number|. | ||||
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 33 | bool HasGeneratedIDFor(uint32_t number) const; |
[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 34 | |
Eugene Girard | a575d30 | 2017-08-28 16:58:36 | [diff] [blame] | 35 | // Removes the ID previously generated for |number| if necessary. |
36 | void MaybeReleaseNumber(uint32_t number); | ||||
37 | |||||
[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 38 | // 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. | ||||
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 41 | void ReleaseGeneratedID(uint32_t id); |
[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 42 | |
43 | // Removes the ID previously generated for |number| by calling | ||||
44 | // |GetGeneratedID()|. | ||||
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 45 | void ReleaseNumber(uint32_t number); |
[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 46 | |
[email protected] | 2c6a1be8 | 2014-07-30 07:04:19 | [diff] [blame] | 47 | void ResetForTest(); |
48 | |||||
[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 49 | private: |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 50 | typedef base::hash_map<uint32_t, uint32_t> IDMap; |
[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 51 | |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 52 | uint32_t GetNextAvailableID(); |
[email protected] | d74eabc | 2013-09-17 02:26:44 | [diff] [blame] | 53 | |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 54 | void UpdateNextAvailableIDAfterRelease(uint32_t id); |
[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 55 | |
56 | IDMap number_to_id_; | ||||
57 | IDMap id_to_number_; | ||||
58 | |||||
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 59 | const uint32_t min_id_; |
60 | uint32_t min_available_id_; | ||||
[email protected] | 946da2d | 2013-09-12 23:53:06 | [diff] [blame] | 61 | |
62 | DISALLOW_COPY_AND_ASSIGN(SequentialIDGenerator); | ||||
63 | }; | ||||
64 | |||||
65 | } // namespace ui | ||||
66 | |||||
[email protected] | a6147a2 | 2013-09-26 06:55:09 | [diff] [blame] | 67 | #endif // UI_GFX_SEQUENTIAL_ID_GENERATOR_H_ |