blob: bde2f585bea0f8725d1cfa040d364309d4275d54 [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
8#include <map>
9
10#include "base/basictypes.h"
11#include "base/containers/hash_tables.h"
[email protected]4ffa7892013-09-27 16:56:0612#include "ui/gfx/gfx_export.h"
[email protected]946da2d2013-09-12 23:53:0613
14namespace ui {
15
16// This is used to generate a series of sequential ID numbers in a way that a
17// new ID is always the lowest possible ID in the sequence.
[email protected]4ffa7892013-09-27 16:56:0618class GFX_EXPORT SequentialIDGenerator {
[email protected]946da2d2013-09-12 23:53:0619 public:
20 // Creates a new generator with the specified lower bound for the IDs.
21 explicit SequentialIDGenerator(uint32 min_id);
22 ~SequentialIDGenerator();
23
24 // Generates a unique ID to represent |number|. The generated ID is the
25 // smallest available ID greater than or equal to the |min_id| specified
26 // during creation of the generator.
27 uint32 GetGeneratedID(uint32 number);
28
29 // Checks to see if the generator currently has a unique ID generated for
30 // |number|.
31 bool HasGeneratedIDFor(uint32 number) const;
32
33 // Removes the generated ID |id| from the internal mapping. Since the ID is
34 // no longer mapped to any number, subsequent calls to |GetGeneratedID()| can
35 // use this ID.
36 void ReleaseGeneratedID(uint32 id);
37
38 // Removes the ID previously generated for |number| by calling
39 // |GetGeneratedID()|.
40 void ReleaseNumber(uint32 number);
41
[email protected]2c6a1be82014-07-30 07:04:1942 void ResetForTest();
43
[email protected]946da2d2013-09-12 23:53:0644 private:
45 typedef base::hash_map<uint32, uint32> IDMap;
46
[email protected]d74eabc2013-09-17 02:26:4447 uint32 GetNextAvailableID();
48
49 void UpdateNextAvailableIDAfterRelease(uint32 id);
[email protected]946da2d2013-09-12 23:53:0650
51 IDMap number_to_id_;
52 IDMap id_to_number_;
53
[email protected]2c6a1be82014-07-30 07:04:1954 const uint32 min_id_;
[email protected]946da2d2013-09-12 23:53:0655 uint32 min_available_id_;
56
57 DISALLOW_COPY_AND_ASSIGN(SequentialIDGenerator);
58};
59
60} // namespace ui
61
[email protected]a6147a22013-09-26 06:55:0962#endif // UI_GFX_SEQUENTIAL_ID_GENERATOR_H_