charstrmap.h 1.5 KB

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