alaCarte Maps
Renderer for OpenStreetMap tiles
cached_string.hpp
Go to the documentation of this file.
1 
21 #pragma once
22 #ifndef _CACHED_STRING_HPP
23 #define _CACHED_STRING_HPP
24 
25 #include <boost/noncopyable.hpp>
26 #include <sstream>
27 
28 #include "settings.hpp"
29 
30 
31 
32 class MaybeCachedString;
33 
41 {
43  friend class MaybeCachedString;
44 private:
46  {
47  StringStorageElement(const string& str);
49 
50  const string value;
51  const std::size_t hash;
52 
53  bool operator == (const StringStorageElement& other) const;
54  struct Hasher;
55  struct StringComparator;
56  };
57  class StringStorage;
58 
59 public:
60  CachedString();
61  explicit CachedString(const char* str);
62  explicit CachedString(const string& str);
63  CachedString(const CachedString& other);
64  ~CachedString();
65 
66 
67  CachedString& operator =(const string& str);
68  CachedString& operator =(const CachedString& other);
69 
70  bool equals(const string& other) const;
71  bool equals(const CachedString& other) const;
72 
73 
74  void assign(const string& str);
75  void assign(const CachedString& other);
76 
77  void clear();
78 
79  const char* c_str() const;
80  const string& str() const;
81 
82  std::size_t hash() const;
83 
84  inline bool operator <(const CachedString& other) const
85  {
86  return internalString < other.internalString;
87  }
88 
89  static void Shutdown();
90 
91 private:
92  template<typename Archive>
93  void save(Archive & ar, const unsigned int version) const
94  {
95  // Save the the hole string, sorry
96  ar << str();
97  }
98  template<class Archive>
99  void load(Archive & ar, const unsigned int version)
100  {
101  string str;
102  ar >> str;
103  *this = CachedString(str);
104  }
105 
106  BOOST_SERIALIZATION_SPLIT_MEMBER()
107 private:
109 };
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
127 {
128  friend class CachedString;
129 private:
132 public:
134  explicit MaybeCachedString(const string& str);
135  MaybeCachedString(const CachedString& other);
136  MaybeCachedString(const MaybeCachedString& other);
138 
139 
140  MaybeCachedString& operator =(const string& str);
143 
144  bool equals(const string& other) const;
145  bool equals(const CachedString& other) const;
146  bool equals(const MaybeCachedString& other) const;
147 
148  void assign(const string& str);
149  void assign(const MaybeCachedString& other);
150  void assign(const CachedString& other);
151 
152  const char* c_str() const;
153  const string& str() const;
154 
155  void clear();
156  std::size_t hash() const;
157  bool isCached() const;
158 
159  inline bool operator <(const MaybeCachedString& other) const
160  {
161  if(cached && other.cached)
162  {
163  return internalString < other.internalString;
164  } else {
165  return str() < other.str();
166  }
167  }
168 
169 private:
170  void reset();
171 
172  bool cached;
173  const StringStorageElement* internalString;
174 };
175 
176 
177 
178 // ############################# equal operators #################################
179 
180 inline bool operator ==(const CachedString& first, const CachedString& second) { return second.equals(first); }
181 inline bool operator ==(const CachedString& first, const string& second) { return first.equals(second); }
182 inline bool operator ==(const CachedString& first, const MaybeCachedString& second) { return second.equals(first); }
183 
184 inline bool operator ==(const string& first, const CachedString& second) { return second.equals(first); }
185 inline bool operator ==(const string& first, const MaybeCachedString& second) { return second.equals(first); }
186 
187 inline bool operator ==(const MaybeCachedString& first, const string& second) { return first.equals(second); }
188 inline bool operator ==(const MaybeCachedString& first, const CachedString& second) { return first.equals(second); }
189 inline bool operator ==(const MaybeCachedString& first, const MaybeCachedString& second) { return first.equals(second); }
190 
191 
192 
193 // ############################# unequal operators #################################
194 
195 inline bool operator !=(const CachedString& first, const CachedString& second) { return !second.equals(first); }
196 inline bool operator !=(const CachedString& first, const string& second) { return !first.equals(second); }
197 inline bool operator !=(const CachedString& first, const MaybeCachedString& second) { return !second.equals(first); }
198 
199 inline bool operator !=(const string& first, const CachedString& second) { return !second.equals(first); }
200 inline bool operator !=(const string& first, const MaybeCachedString& second) { return !second.equals(first); }
201 
202 inline bool operator !=(const MaybeCachedString& first, const string& second) { return !first.equals(second); }
203 inline bool operator !=(const MaybeCachedString& first, const CachedString& second) { return !first.equals(second); }
204 inline bool operator !=(const MaybeCachedString& first, const MaybeCachedString& second) { return !first.equals(second); }
205 
206 
207 
208 // ############################# stream operators #################################
209 
210 template<typename Stream>
211 Stream& operator >> (Stream& stream, CachedString& str)
212 {
213  str = CachedString(static_cast<std::stringstream const&>(std::stringstream() << stream.rdbuf()).str());
214 
215  return stream;
216 }
217 
218 template<typename Stream>
219 Stream& operator << (Stream& stream, const CachedString& str)
220 {
221  stream << str.str();
222 
223  return stream;
224 }
225 
226 
227 // ############################# hash calculation operators #################################
228 
229 inline std::size_t hash_value(const CachedString& cs)
230 {
231  return cs.hash();
232 }
233 
234 inline std::size_t hash_value(const MaybeCachedString& mcs)
235 {
236  return mcs.hash();
237 }
238 
239 
240 // ############################# Comparator for CachedString's #################################
241 
243  : public std::binary_function<MaybeCachedString, CachedString, bool>
244 {
245  bool operator()(const MaybeCachedString& str, const CachedString& cs) const
246  {
247  return cs == str;
248  }
249 };
250 
251 #endif
bool operator!=(const CachedString &first, const CachedString &second)
Hasher for the StringStorageElement.
bool operator==(const StringStorageElement &other) const
This file is part of alaCarte.
Comparator for comparing STringStorageELements with strings.
void load(Archive &ar, const unsigned int version)
const string & str() const
Returns the internal string.
static void Shutdown()
Frees all internal memory used by the cache mechanism.
void assign(const string &str)
Assigns a std string.
~CachedString()
Tidies up the cached string.
Represents a string which could be cached into an internal cache.
CachedString()
Cretaes a new Cached string.
const string & str() const
Returns the internal string.
bool equals(const string &other) const
Compares this string with a given std string.
std::size_t hash() const
const StringStorageElement * internalString
void save(Archive &ar, const unsigned int version) const
const char * c_str() const
Returns the internal c string.
friend class MaybeCachedString
Stream & operator<<(Stream &stream, const CachedString &str)
const StringStorageElement * internalString
void clear()
Sets this string to the empty string.
CachedString::StringStorage StringStorage
std::size_t hash() const
Returns the internal hash.
bool operator<(const CachedString &other) const
std::size_t hash_value(const CachedString &cs)
Stream & operator>>(Stream &stream, CachedString &str)
Represents a string which is cached into an internal cache.
StringStorageElement(const string &str)
Creates a new StringStorageELement.
bool equals(const string &other) const
Compares this cached string with an non cached string.
bool operator()(const MaybeCachedString &str, const CachedString &cs) const
CachedString::StringStorageElement StringStorageElement
friend class boost::serialization::access
Storage class for cached strings.
CachedString & operator=(const string &str)
Assigns a std string.