TrueReality  v0.1.1912
RefStr.cpp
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * The Base of this class has been adopted from the Delta3D engine
6 *
7 * This library is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the Free
9 * Software Foundation; either version 3.0 of the License, or (at your option)
10 * any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * @author Maxim Serebrennik
22 * Based on Delta3D dtUtil::RefStr
23 */
24 
25 #include <trUtil/RefStr.h>
26 
27 #include <ostream>
28 
29 #if defined( _LIBCPP_VERSION ) || (defined(_MSC_VER) && _MSC_VER >= 1700) || defined(__GNUG__)
30 # include <unordered_set>
31 # define _UNORDERED_MAP
32 #elif defined(_MSC_VER)
33 # include <hash_set>
34 #else
35 # include <set>
36 #endif
37 
38 #define USE_TABLE 1
39 
40 #define THREAD_SAFETY 1
41 
42 #if THREAD_SAFETY
43 #include <OpenThreads/Mutex>
44 #endif
45 
46 
47 namespace trUtil
48 {
49  static size_t StringCount = 0;
50 #if USE_TABLE
51 
52 #if THREAD_SAFETY
53  static OpenThreads::Mutex gStringSetMutex;
54 #endif
55 
56 #ifdef _UNORDERED_MAP
57  static std::unordered_set<std::string> StringSet(3000);
58 #elif defined(__GNUG__)
59  static __gnu_cxx::hash_set<std::string> StringSet(3000);
60 #elif defined(_MSC_VER)
61  static stdext::hash_set<std::string> StringSet;
62 #else
63  static std::set<std::string> StringSet;
64 #endif
65 #endif
67  {
68  return StringCount;
69  }
70 
72  RefStr::RefStr(const std::string& value) : mString(nullptr)
73  {
74  Intern(value);
75  }
76 
78  RefStr::RefStr(const char* value) : mString(nullptr)
79  {
80  Intern(value);
81  }
82 
84  RefStr::RefStr(const RefStr& toCopy) : mString(nullptr)
85  {
86 #if USE_TABLE
87  //If we are using the table, we'll get the same pointer anyway.
88  mString = toCopy.mString;
89 #else
90  Intern(*toCopy.mString);
91 #endif
92  }
93 
96  {
97 #if !USE_TABLE
98  delete mString;
99  --StringCount;
100 #endif
101  }
102 
104  RefStr RefStr::operator+(const std::string& string) const
105  {
106  return RefStr(*mString + string);
107  }
108 
110  RefStr RefStr::operator+(const RefStr& value) const
111  {
112  return RefStr(*mString + *value.mString);
113  }
114 
116  RefStr RefStr::operator+(const char* str) const
117  {
118  return RefStr(*mString + str);
119  }
120 
122  trUtil::RefStr& RefStr::operator=(const std::string& value)
123  {
124  Intern(value);
125  return *this;
126  }
127 
130  {
131  if (this == &value)
132  return *this;
133 #if USE_TABLE
134  //If we are using the table, we'll get the same pointer anyway.
135  mString = value.mString;
136 #else
137  Intern(*value.mString);
138 #endif
139  return *this;
140  }
141 
143  void RefStr::Intern(const std::string& value)
144  {
145 #if USE_TABLE
146 
147 #if THREAD_SAFETY
148  // for thread safety, lock this section
149  gStringSetMutex.lock();
150 #endif
151 
152  //One can only insert a string once, but it will still return the iterator.
153  mString = &(*StringSet.insert(value).first);
154  StringCount = StringSet.size();
155 
156 #if THREAD_SAFETY
157  gStringSetMutex.unlock();
158 #endif
159 
160 #else
161  if (mString != nullptr)
162  {
163  delete mString;
164  --StringCount;
165  }
166  mString = new std::string(value);
167  ++StringCount;
168 #endif
169  }
170 
172  std::ostream& operator<<(std::ostream& stream, const RefStr& rs)
173  {
174  stream << rs.Get();
175  return stream;
176  }
177 
178 }
RefStr(const std::string &value="")
Constructor.
Definition: RefStr.cpp:72
static OpenThreads::Mutex gStringSetMutex
Definition: RefStr.cpp:53
static size_t StringCount
Definition: RefStr.cpp:49
A string wrapper that will make sure that all of the strings with the same value will point to the sa...
Definition: RefStr.h:50
static size_t GetSharedStringCount()
Gets shared string count.
Definition: RefStr.cpp:66
trUtil::RefStr & operator=(const std::string &value)
Assignment operator that takes a string.
Definition: RefStr.cpp:122
const std::string * mString
Definition: RefStr.h:181
TR_UTIL_EXPORT std::ostream & operator<<(std::ostream &os, const EnumerationString &e)
Helper method to print EnumerationNumeric to an output stream.
const std::string & Get() const
Definition: RefStr.h:178
void Intern(const std::string &value)
Definition: RefStr.cpp:143
RefStr operator+(const std::string &string) const
Definition: RefStr.cpp:104
static std::set< std::string > StringSet
Definition: RefStr.cpp:63
Namespace that holds various utility classes for the engine.
Definition: SmrtPtr.h:208
~RefStr()
Destructor.
Definition: RefStr.cpp:95