1 // © 2020 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 // charstrmap.h
5 // created: 2020sep01 Frank Yung-Fong Tang
6 
7 #ifndef __CHARSTRMAP_H__
8 #define __CHARSTRMAP_H__
9 
10 #include <utility>
11 #include "unicode/utypes.h"
12 #include "unicode/uobject.h"
13 #include "uhash.h"
14 
15 U_NAMESPACE_BEGIN
16 
17 /**
18  * Map of const char * keys & values.
19  * Stores pointers as is: Does not own/copy/adopt/release strings.
20  */
21 class CharStringMap final : public UMemory {
22 public:
23     /** Constructs an unusable non-map. */
CharStringMap()24     CharStringMap() : map(nullptr) {}
CharStringMap(int32_t size,UErrorCode & errorCode)25     CharStringMap(int32_t size, UErrorCode &errorCode) {
26         map = uhash_openSize(uhash_hashChars, uhash_compareChars, uhash_compareChars,
27                              size, &errorCode);
28     }
CharStringMap(CharStringMap && other)29     CharStringMap(CharStringMap &&other) U_NOEXCEPT : map(other.map) {
30         other.map = nullptr;
31     }
32     CharStringMap(const CharStringMap &other) = delete;
~CharStringMap()33     ~CharStringMap() {
34         uhash_close(map);
35     }
36 
37     CharStringMap &operator=(CharStringMap &&other) U_NOEXCEPT {
38         map = other.map;
39         other.map = nullptr;
40         return *this;
41     }
42     CharStringMap &operator=(const CharStringMap &other) = delete;
43 
get(const char * key)44     const char *get(const char *key) const { return static_cast<const char *>(uhash_get(map, key)); }
put(const char * key,const char * value,UErrorCode & errorCode)45     void put(const char *key, const char *value, UErrorCode &errorCode) {
46         uhash_put(map, const_cast<char *>(key), const_cast<char *>(value), &errorCode);
47     }
48 
49 private:
50     UHashtable *map;
51 };
52 
53 U_NAMESPACE_END
54 
55 #endif  //  __CHARSTRMAP_H__
56