TrueReality  v0.1.1912
RefStr.h
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 * Class Inspired by the Delta3D Engine
22 * http://delta3dengine.org/
23 *
24 * @author Maxim Serebrennik
25 * Based on Delta3D dtUtil::RefStr
26 */
27 #pragma once
28 #include "Export.h"
29 
30 #include <trUtil/Hash.h>
31 
32 #include <string>
33 
39 namespace trUtil
40 {
51  {
52  public:
53 
61  static size_t GetSharedStringCount();
62 
70  RefStr(const std::string& value = "");
71 
79  RefStr(const char* value);
80 
88  RefStr(const RefStr& toCopy);
89 
95  ~RefStr();
96 
104  operator const std::string&() const { return *mString; }
105 
115  trUtil::RefStr& operator=(const std::string& value);
116 
126  trUtil::RefStr& operator=(const trUtil::RefStr& value);
127 
128  RefStr operator+(const std::string& string) const;
129  RefStr operator+(const RefStr& RefStr) const;
130  RefStr operator+(const char* str) const;
131  const std::string* operator->() const { return mString; }
132  std::string::value_type operator[](int index) const { return (*mString)[index]; }
133 
141  const char* c_str() const { return mString->c_str(); }
142 
143  bool operator<(const trUtil::RefStr& toCompare) const
144  {
145  return this->Get() < toCompare.Get();
146  }
147 
148  bool operator==(const trUtil::RefStr& toCompare) const
149  {
150  return this->Get() == toCompare.Get();
151  }
152 
153  bool operator!=(const trUtil::RefStr& toCompare) const
154  {
155  return !(*this == toCompare);
156  }
157 
158  bool operator==(const std::string& toCompare) const
159  {
160  return this->Get() == toCompare;
161  }
162 
163  bool operator==(const char* toCompare) const
164  {
165  return this->Get() == toCompare;
166  }
167 
168  bool operator!=(const char* toCompare) const
169  {
170  return this->Get() != toCompare;
171  }
172 
173  bool operator!=(const std::string& toCompare) const
174  {
175  return !(*this == toCompare);
176  }
177 
178  const std::string& Get() const { return *mString; }
179 
180  private:
181  const std::string* mString;
182 
183  void Intern(const std::string& value);
184  };
185 
187  inline bool operator==(const std::string& s1, const RefStr& s2)
188  {
189  return s2 == s1;
190  }
191 
193  inline std::string operator+(const std::string& s1, const RefStr& s2)
194  {
195  return s1 + s2.Get();
196  }
197 
199  inline bool operator!=(const std::string& s1, const RefStr& s2)
200  {
201  return s2 != s1;
202  }
203 
205  TR_UTIL_EXPORT std::ostream& operator<<(std::ostream& stream, const RefStr& rs);
206 
208  template<> struct hash<const trUtil::RefStr>
209  {
210  size_t operator()(const trUtil::RefStr& string) const
211  {
212  return trUtil::__hash_string(string.c_str());
213  }
214  };
215 
217  template<> struct hash<trUtil::RefStr>
218  {
219  size_t operator()(const trUtil::RefStr& string) const
220  {
221  return trUtil::__hash_string(string.c_str());
222  }
223  };
224 }
size_t __hash_string(const char *__s)
Hash string.
Definition: Hash.h:71
std::string operator+(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:193
bool operator==(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:187
bool operator!=(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:199
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
bool operator==(const trUtil::RefStr &toCompare) const
Definition: RefStr.h:148
bool operator!=(const char *toCompare) const
Definition: RefStr.h:168
size_t operator()(const trUtil::RefStr &string) const
Definition: RefStr.h:210
bool operator!=(const trUtil::RefStr &toCompare) const
Definition: RefStr.h:153
const std::string * mString
Definition: RefStr.h:181
This Hash class and its implementations are taken from the libstdc++ hash_fun.h.
Definition: Hash.h:45
const std::string * operator->() const
Definition: RefStr.h:131
bool operator<(const trUtil::RefStr &toCompare) const
Definition: RefStr.h:143
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
size_t operator()(const trUtil::RefStr &string) const
Definition: RefStr.h:219
bool operator!=(const std::string &toCompare) const
Definition: RefStr.h:173
A class that represents date time utility.
Namespace that holds various utility classes for the engine.
Definition: SmrtPtr.h:208
bool operator==(const char *toCompare) const
Definition: RefStr.h:163
const char * c_str() const
Gets the string.
Definition: RefStr.h:141
bool operator==(const std::string &toCompare) const
Definition: RefStr.h:158
std::string::value_type operator[](int index) const
Definition: RefStr.h:132