KOSMIndoorMap

stringpool.h
1/*
2 SPDX-FileCopyrightText: 2020-2021 Volker Krause <vkrause@kde.org>
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
15namespace OSM {
16
17enum class StringMemory { Persistent, Transient };
18
19/** @internal */
20class KOSM_EXPORT StringKeyRegistryBase
21{
22protected:
23 explicit StringKeyRegistryBase();
25 StringKeyRegistryBase& operator=(StringKeyRegistryBase&&) noexcept;
27
28 [[nodiscard]] const char* makeKeyInternal(const char *name, std::size_t len, StringMemory memOpt);
29 [[nodiscard]] 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 */
38template <typename T>
40{
41public:
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. */
72{
73public:
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
83protected:
84 explicit constexpr inline StringKey(const char *keyData) : key(keyData) {}
85
86private:
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:40
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
T key(const char *name) const
Looks up an existing key, if that doesn't exist an null key is returned.
Definition stringpool.h:62
Base class for unique string keys.
Definition stringpool.h:72
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-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:03 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.