Coverage Report

Created: 2025-06-13 06:34

/src/icu/icu4c/source/common/charstrmap.h
Line
Count
Source (jump to first uncovered line)
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. */
24
0
    CharStringMap() : map(nullptr) {}
25
5
    CharStringMap(int32_t size, UErrorCode &errorCode) {
26
5
        map = uhash_openSize(uhash_hashChars, uhash_compareChars, uhash_compareChars,
27
5
                             size, &errorCode);
28
5
    }
29
10
    CharStringMap(CharStringMap &&other) noexcept : map(other.map) {
30
10
        other.map = nullptr;
31
10
    }
32
    CharStringMap(const CharStringMap &other) = delete;
33
10
    ~CharStringMap() {
34
10
        uhash_close(map);
35
10
    }
36
37
0
    CharStringMap &operator=(CharStringMap &&other) noexcept {
38
0
        map = other.map;
39
0
        other.map = nullptr;
40
0
        return *this;
41
0
    }
42
    CharStringMap &operator=(const CharStringMap &other) = delete;
43
44
1.88k
    const char *get(const char *key) const { return static_cast<const char *>(uhash_get(map, key)); }
45
1.29k
    void put(const char *key, const char *value, UErrorCode &errorCode) {
46
1.29k
        uhash_put(map, const_cast<char *>(key), const_cast<char *>(value), &errorCode);
47
1.29k
    }
48
49
private:
50
    UHashtable *map;
51
};
52
53
U_NAMESPACE_END
54
55
#endif  //  __CHARSTRMAP_H__