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