Audio plugin host https://kx.studio/carla
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.

StringPool.h 4.0KB

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