KOSMIndoorMap

stringpool.h
1 /*
2  SPDX-FileCopyrightText: 2020-2021 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #ifndef KOSM_STRINGPOOL_H
8 #define KOSM_STRINGPOOL_H
9 
10 #include <kosm_export.h>
11 
12 #include <cstring>
13 #include <vector>
14 
15 namespace OSM {
16 
17 enum class StringMemory { Persistent, Transient };
18 
19 /** @internal */
20 class KOSM_EXPORT StringKeyRegistryBase
21 {
22 protected:
23  explicit StringKeyRegistryBase();
27 
28  const char* makeKeyInternal(const char *name, std::size_t len, StringMemory memOpt);
29  const char* keyInternal(const char *name) const;
30 
31  std::vector<char*> m_pool;
32  std::vector<const char*> m_registry;
33 };
34 
35 /** Registry of unique string keys.
36  * @tparam T Sub-classes of StringKey, to have a compile-time check against comparing keys from different pools.
37  */
38 template <typename T>
40 {
41 public:
42  explicit StringKeyRegistry() = default;
43  StringKeyRegistry(const StringKeyRegistry&) = delete;
45  ~StringKeyRegistry() = default;
46  StringKeyRegistry& operator=(const StringKeyRegistry&) = delete;
47  StringKeyRegistry& operator=(StringKeyRegistry&&) = default;
48 
49  /** Add a new string to the registry if needed, or returns an existing one if already present. */
50  inline T makeKey(const char *name, StringMemory memOpt)
51  {
52  return makeKey(name, std::strlen(name), memOpt);
53  }
54  inline T makeKey(const char *name, std::size_t len, StringMemory memOpt)
55  {
56  T key;
57  key.key = makeKeyInternal(name, len, memOpt);
58  return key;
59  }
60 
61  /** Looks up an existing key, if that doesn't exist an null key is returned. */
62  inline T key(const char *name) const
63  {
64  T key;
65  key.key = keyInternal(name);
66  return key;
67  }
68 };
69 
70 /** Base class for unique string keys. */
71 class StringKey
72 {
73 public:
74  constexpr inline StringKey() = default;
75  constexpr inline const char* name() const { return key; }
76  constexpr inline bool isNull() const { return !key; }
77 
78  // yes, pointer compare is enough here
79  inline constexpr bool operator<(StringKey other) const { return key < other.key; }
80  inline constexpr bool operator==(StringKey other) const { return key == other.key; }
81  inline constexpr bool operator!=(StringKey other) const { return key != other.key; }
82 
83 protected:
84  explicit constexpr inline StringKey(const char *keyData) : key(keyData) {}
85 
86 private:
87  template <typename T> friend class StringKeyRegistry;
88  const char* key = nullptr;
89 };
90 
91 }
92 
93 #endif // KOSM_STRINGPOOL_H
Registry of unique string keys.
Definition: stringpool.h:39
Base class for unique string keys.
Definition: stringpool.h:71
T key(const char *name) const
Looks up an existing key, if that doesn&#39;t exist an null key is returned.
Definition: stringpool.h:62
T makeKey(const char *name, StringMemory memOpt)
Add a new string to the registry if needed, or returns an existing one if already present...
Definition: stringpool.h:50
Low-level types and functions to work with raw OSM data as efficiently as possible.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Sep 27 2021 23:05:52 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.