juce_StringPool.h 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_STRINGPOOL_H_INCLUDED
  22. #define JUCE_STRINGPOOL_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. A StringPool holds a set of shared strings, which reduces storage overheads and improves
  26. comparison speed when dealing with many duplicate strings.
  27. When you add a string to a pool using getPooledString, it'll return a character
  28. array containing the same string. This array is owned by the pool, and the same array
  29. is returned every time a matching string is asked for. This means that it's trivial to
  30. compare two pooled strings for equality, as you can simply compare their pointers. It
  31. also cuts down on storage if you're using many copies of the same string.
  32. */
  33. class JUCE_API StringPool
  34. {
  35. public:
  36. //==============================================================================
  37. /** Creates an empty pool. */
  38. StringPool() noexcept;
  39. /** Destructor */
  40. ~StringPool();
  41. //==============================================================================
  42. /** Returns a pointer to a shared copy of the string that is passed in.
  43. The pool will always return the same String object when asked for a string that matches it.
  44. */
  45. String getPooledString (const String& original);
  46. /** Returns a pointer to a copy of the string that is passed in.
  47. The pool will always return the same String object when asked for a string that matches it.
  48. */
  49. String getPooledString (const char* original);
  50. /** Returns a pointer to a shared copy of the string that is passed in.
  51. The pool will always return the same String object when asked for a string that matches it.
  52. */
  53. String getPooledString (StringRef original);
  54. /** Returns a pointer to a copy of the string that is passed in.
  55. The pool will always return the same String object when asked for a string that matches it.
  56. */
  57. String getPooledString (String::CharPointerType start, String::CharPointerType end);
  58. //==============================================================================
  59. /** Scans the pool, and removes any strings that are unreferenced.
  60. You don't generally need to call this - it'll be called automatically when the pool grows
  61. large enough to warrant it.
  62. */
  63. void garbageCollect();
  64. /** Returns a shared global pool which is used for things like Identifiers, XML parsing. */
  65. static StringPool& getGlobalPool() noexcept;
  66. private:
  67. Array<String> strings;
  68. CriticalSection lock;
  69. uint32 lastGarbageCollectionTime;
  70. void garbageCollectIfNeeded();
  71. };
  72. #endif // JUCE_STRINGPOOL_H_INCLUDED