The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
3.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /**
  20. A StringPool holds a set of shared strings, which reduces storage overheads and improves
  21. comparison speed when dealing with many duplicate strings.
  22. When you add a string to a pool using getPooledString, it'll return a character
  23. array containing the same string. This array is owned by the pool, and the same array
  24. is returned every time a matching string is asked for. This means that it's trivial to
  25. compare two pooled strings for equality, as you can simply compare their pointers. It
  26. also cuts down on storage if you're using many copies of the same string.
  27. */
  28. class JUCE_API StringPool
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates an empty pool. */
  33. StringPool() noexcept;
  34. /** Destructor */
  35. ~StringPool();
  36. //==============================================================================
  37. /** Returns a pointer to a shared copy of the string that is passed in.
  38. The pool will always return the same String object when asked for a string that matches it.
  39. */
  40. String getPooledString (const String& original);
  41. /** Returns a pointer to a copy of the string that is passed in.
  42. The pool will always return the same String object when asked for a string that matches it.
  43. */
  44. String getPooledString (const char* original);
  45. /** Returns a pointer to a shared copy of the string that is passed in.
  46. The pool will always return the same String object when asked for a string that matches it.
  47. */
  48. String getPooledString (StringRef original);
  49. /** Returns a pointer to a copy of the string that is passed in.
  50. The pool will always return the same String object when asked for a string that matches it.
  51. */
  52. String getPooledString (String::CharPointerType start, String::CharPointerType end);
  53. //==============================================================================
  54. /** Scans the pool, and removes any strings that are unreferenced.
  55. You don't generally need to call this - it'll be called automatically when the pool grows
  56. large enough to warrant it.
  57. */
  58. void garbageCollect();
  59. /** Returns a shared global pool which is used for things like Identifiers, XML parsing. */
  60. static StringPool& getGlobalPool() noexcept;
  61. private:
  62. Array<String> strings;
  63. CriticalSection lock;
  64. uint32 lastGarbageCollectionTime;
  65. void garbageCollectIfNeeded();
  66. JUCE_DECLARE_NON_COPYABLE (StringPool)
  67. };