alaCarte Maps
Renderer for OpenStreetMap tiles
cached_string.cpp
Go to the documentation of this file.
1 
24 #include <boost/unordered_set.hpp>
25 #include <boost/thread/mutex.hpp>
26 #include <boost/thread/shared_mutex.hpp>
27 #include <boost/thread/locks.hpp>
28 
29 #include "utils/cached_string.hpp"
30 
37 {
38  return (hash == other.hash) || (value == other.value);
39 }
40 
46  : public std::unary_function<StringStorageElement, std::size_t>
47 {
48  std::size_t operator()(const StringStorageElement& sse) const
49  {
50  return sse.hash;
51  }
52 };
53 
59  : public std::binary_function<string, StringStorageElement, bool>
60 {
61  bool operator()(const string& str, const StringStorageElement& sse) const
62  {
63  return sse.value == str;
64  }
65 };
66 
74  : value(str)
75  , hash(boost::hash<string>()(str))
76 {
77 }
78 
86  : value(other.value)
87  , hash(other.hash)
88 {
89 }
90 
97  : public boost::noncopyable
98 {
99 public:
106  static StringStorage& Inst()
107  {
108  static StringStorage inst;
109  return inst;
110  }
111 
116  void shutdown()
117  {
118  emptyString = nullptr;
119  storage.clear();
120  }
121 
127  {
128  return emptyString;
129  }
130 
137  const StringStorageElement* resolveString(const string& str)
138  {
139  boost::upgrade_lock<boost::shared_mutex> guard(accessMutex);
140  // Search for the string using a direct string search
141  auto it = storage.find(str, stringStorageElementHasher, stringComperator);
142 
143  // check if string was already in storage
144  if(it == storage.end())
145  {
146  boost::upgrade_to_unique_lock<boost::shared_mutex> unique(guard);
147  it = storage.insert(StringStorageElement(str)).first;
148  }
149 
150  return &(*it);
151  }
152 private:
158  {
159  emptyString = resolveString("");
160  }
161 
162 private:
167 
171  boost::unordered_set<StringStorageElement, StringStorageElement::Hasher> storage;
173  boost::shared_mutex accessMutex;
174 };
175 
176 
177 
183 {
184  clear();
185 }
186 
192 {
194 }
195 
201 {
202  assign(str);
203 }
204 
210 {
211  assign(other);
212 }
213 
214 
220 {
221 }
222 
227 bool CachedString::equals( const string& other ) const
228 {
229  return str() == other;
230 }
231 
236 bool CachedString::equals( const CachedString& other ) const
237 {
238  return this->internalString == other.internalString;
239 }
240 
241 /*
242 CachedString& CachedString::operator =(const char* str)
243 {
244  return *this = CachedString(str);
245 }*/
246 
252 {
253  assign(str);
254  return *this;
255 }
256 
262 {
263  assign(other);
264  return *this;
265 }
266 
267 
272 void CachedString::assign( const string& str )
273 {
275 }
276 
281 void CachedString::assign( const CachedString& other )
282 {
283  this->internalString = other.internalString;
284 }
285 
286 
292 {
294 }
295 
296 
301 const char* CachedString::c_str() const
302 {
303  assert(internalString);
304  return internalString->value.c_str();
305 }
306 
311 const string& CachedString::str() const
312 {
313  assert(internalString);
314  return internalString->value;
315 }
316 
317 std::size_t CachedString::hash() const
318 {
319  assert(internalString);
320  return internalString->hash;
321 }
322 
328 {
330 }
331 
332 
338  : cached(true)
339 {
340  clear();
341 }
342 
343 /*
344 MaybeCachedString::MaybeCachedString( const char* str )
345 {
346 
347 }*/
348 
354  : cached(true)
355 {
356  assign(str);
357 }
358 
364  : cached(true)
365 {
366  assign(other);
367 }
368 
373  : cached(true)
374 {
375  assign(other);
376 }
377 
383 {
384  reset();
385 }
386 
387 /*
388 MaybeCachedString& MaybeCachedString::operator=( const char* str )
389 {
390 
391 }*/
392 
398 {
399  assign(str);
400  return *this;
401 }
402 
408 {
409  assign(other);
410  return *this;
411 }
412 
418 {
419  assign(other);
420  return *this;
421 }
422 
423 
428 void MaybeCachedString::assign( const string& str )
429 {
430  reset();
431  cached = false;
433 }
434 
440 {
441  reset();
442  cached = other.cached;
443  if (cached) {
445  } else {
447  }
448 }
449 
455 {
456  cached = true;
458 }
459 
464 bool MaybeCachedString::equals( const string& other ) const
465 {
466  return str() == other;
467 }
468 
473 bool MaybeCachedString::equals( const CachedString& other ) const
474 {
475  if(cached)
476  {
477  return internalString == other.internalString;
478  } else {
479  return equals(other.str());
480  }
481 }
482 
488 {
489  if(cached && other.cached)
490  {
491  return internalString == other.internalString;
492  } else {
493  return equals(other.str());
494  }
495 }
496 
501 const char* MaybeCachedString::c_str() const
502 {
503  assert(internalString);
504  return internalString->value.c_str();
505 }
506 
511 const string& MaybeCachedString::str() const
512 {
513  assert(internalString);
514  return internalString->value;
515 }
516 
521 std::size_t MaybeCachedString::hash() const
522 {
523  assert(internalString);
524  return internalString->hash;
525 }
526 
532 {
533  reset();
534  cached = true;
536 }
537 
543 {
544  if(!cached)
545  delete internalString;
546 }
547 
553 {
554  return cached;
555 }
static StringStorage & Inst()
Returns the Storage.
MaybeCachedString & operator=(const string &str)
Assigns a non cached std string.
Hasher for the StringStorageElement.
bool operator==(const StringStorageElement &other) const
This file is part of alaCarte.
void assign(const string &str)
Assigns a non cached std string.
const StringStorageElement * getEmptyString() const
Returns the storage element for an empty string.
Comparator for comparing STringStorageELements with strings.
~MaybeCachedString()
Destroys this object.
void reset()
deletes rge internal string if it is not cached
const string & str() const
Returns the internal string.
std::size_t operator()(const StringStorageElement &sse) const
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.
MaybeCachedString()
Returns the internal c string.
void shutdown()
Frees all memory allocated by this storage.
StringStorageElement::StringComparator stringComperator
The comparator for strings and storage elements.
bool equals(const string &other) const
Compares this string with a given std string.
std::size_t hash() const
const StringStorageElement * internalString
const StringStorageElement * emptyString
Pointer to the storage element holding the empty string.
std::string string
Definition: settings.hpp:110
const char * c_str() const
Returns the internal c string.
bool isCached() const
Returns weather this string is cached.
StringStorage()
Creates the StringStorage.
const StringStorageElement * resolveString(const string &str)
Returns the storage element for a given string.
boost::unordered_set< StringStorageElement, StringStorageElement::Hasher > storage
internal storage as an hash map
const StringStorageElement * internalString
boost::shared_mutex accessMutex
The mutex used to lock this class.
void clear()
Sets this string to the empty string.
std::size_t hash() const
Returns the internal hash.
bool operator()(const string &str, const StringStorageElement &sse) const
Represents a string which is cached into an internal cache.
const char * c_str() const
Returns the internal c string.
void clear()
Sets this string to be the empty string.
StringStorageElement::Hasher stringStorageElementHasher
The hasher used for StringStorageELements.
StringStorageElement(const string &str)
Creates a new StringStorageELement.
bool equals(const string &other) const
Compares this cached string with an non cached string.
CachedString::StringStorageElement StringStorageElement
Storage class for cached strings.
CachedString & operator=(const string &str)
Assigns a std string.