sequential_id_generator.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #ifndef UI_GFX_SEQUENTIAL_ID_GENERATOR_H_
  5. #define UI_GFX_SEQUENTIAL_ID_GENERATOR_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <unordered_map>
  9. #include "ui/gfx/gfx_export.h"
  10. namespace ui {
  11. // This is used to generate a series of sequential ID numbers in a way that a
  12. // new ID is always the lowest possible ID in the sequence.
  13. class GFX_EXPORT SequentialIDGenerator {
  14. public:
  15. // Creates a new generator with the specified lower bound for the IDs.
  16. explicit SequentialIDGenerator(uint32_t min_id);
  17. SequentialIDGenerator(const SequentialIDGenerator&) = delete;
  18. SequentialIDGenerator& operator=(const SequentialIDGenerator&) = delete;
  19. ~SequentialIDGenerator();
  20. // Generates a unique ID to represent |number|. The generated ID is the
  21. // smallest available ID greater than or equal to the |min_id| specified
  22. // during creation of the generator.
  23. uint32_t GetGeneratedID(uint32_t number);
  24. // Checks to see if the generator currently has a unique ID generated for
  25. // |number|.
  26. bool HasGeneratedIDFor(uint32_t number) const;
  27. // Removes the ID previously generated for |number| by calling
  28. // |GetGeneratedID()| - does nothing if the number is not mapped.
  29. void ReleaseNumber(uint32_t number);
  30. // Releases ID previously generated by calling |GetGeneratedID()|. Does
  31. // nothing if the ID is not mapped.
  32. void ReleaseID(uint32_t id);
  33. void ResetForTest();
  34. private:
  35. typedef std::unordered_map<uint32_t, uint32_t> IDMap;
  36. uint32_t GetNextAvailableID();
  37. void UpdateNextAvailableIDAfterRelease(uint32_t id);
  38. IDMap number_to_id_;
  39. IDMap id_to_number_;
  40. const uint32_t min_id_;
  41. uint32_t min_available_id_;
  42. };
  43. } // namespace ui
  44. #endif // UI_GFX_SEQUENTIAL_ID_GENERATOR_H_