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