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.

91 lines
3.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. A StringPool holds a set of shared strings, which reduces storage overheads and improves
  22. comparison speed when dealing with many duplicate strings.
  23. When you add a string to a pool using getPooledString, it'll return a character
  24. array containing the same string. This array is owned by the pool, and the same array
  25. is returned every time a matching string is asked for. This means that it's trivial to
  26. compare two pooled strings for equality, as you can simply compare their pointers. It
  27. also cuts down on storage if you're using many copies of the same string.
  28. @tags{Core}
  29. */
  30. class JUCE_API StringPool
  31. {
  32. public:
  33. //==============================================================================
  34. /** Creates an empty pool. */
  35. StringPool() noexcept;
  36. /** Destructor */
  37. ~StringPool();
  38. //==============================================================================
  39. /** Returns a pointer to a shared copy of the string that is passed in.
  40. The pool will always return the same String object when asked for a string that matches it.
  41. */
  42. String getPooledString (const String& original);
  43. /** Returns a pointer to a copy of the string that is passed in.
  44. The pool will always return the same String object when asked for a string that matches it.
  45. */
  46. String getPooledString (const char* original);
  47. /** Returns a pointer to a shared copy of the string that is passed in.
  48. The pool will always return the same String object when asked for a string that matches it.
  49. */
  50. String getPooledString (StringRef original);
  51. /** Returns a pointer to a copy of the string that is passed in.
  52. The pool will always return the same String object when asked for a string that matches it.
  53. */
  54. String getPooledString (String::CharPointerType start, String::CharPointerType end);
  55. //==============================================================================
  56. /** Scans the pool, and removes any strings that are unreferenced.
  57. You don't generally need to call this - it'll be called automatically when the pool grows
  58. large enough to warrant it.
  59. */
  60. void garbageCollect();
  61. /** Returns a shared global pool which is used for things like Identifiers, XML parsing. */
  62. static StringPool& getGlobalPool() noexcept;
  63. private:
  64. Array<String> strings;
  65. CriticalSection lock;
  66. uint32 lastGarbageCollectionTime;
  67. void garbageCollectIfNeeded();
  68. JUCE_DECLARE_NON_COPYABLE (StringPool)
  69. };
  70. } // namespace juce