resource.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2015-2016, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *******************************************************************************
  8. * resource.h
  9. *
  10. * created on: 2015nov04
  11. * created by: Markus W. Scherer
  12. */
  13. #ifndef __URESOURCE_H__
  14. #define __URESOURCE_H__
  15. /**
  16. * \file
  17. * \brief ICU resource bundle key and value types.
  18. */
  19. // Note: Ported from ICU4J class UResource and its nested classes,
  20. // but the C++ classes are separate, not nested.
  21. // We use the Resource prefix for C++ classes, as usual.
  22. // The UResource prefix would be used for C types.
  23. #include "unicode/utypes.h"
  24. #include "unicode/unistr.h"
  25. #include "unicode/ures.h"
  26. #include "restrace.h"
  27. struct ResourceData;
  28. U_NAMESPACE_BEGIN
  29. class ResourceValue;
  30. // Note: In C++, we use const char * pointers for keys,
  31. // rather than an abstraction like Java UResource.Key.
  32. /**
  33. * Interface for iterating over a resource bundle array resource.
  34. */
  35. class U_COMMON_API ResourceArray {
  36. public:
  37. /** Constructs an empty array object. */
  38. ResourceArray() : items16(nullptr), items32(nullptr), length(0) {}
  39. /** Only for implementation use. @internal */
  40. ResourceArray(const uint16_t *i16, const uint32_t *i32, int32_t len,
  41. const ResourceTracer& traceInfo) :
  42. items16(i16), items32(i32), length(len),
  43. fTraceInfo(traceInfo) {}
  44. /**
  45. * @return The number of items in the array resource.
  46. */
  47. int32_t getSize() const { return length; }
  48. /**
  49. * @param i Array item index.
  50. * @param value Output-only, receives the value of the i'th item.
  51. * @return true if i is non-negative and less than getSize().
  52. */
  53. UBool getValue(int32_t i, ResourceValue &value) const;
  54. /** Only for implementation use. @internal */
  55. uint32_t internalGetResource(const ResourceData *pResData, int32_t i) const;
  56. private:
  57. const uint16_t *items16;
  58. const uint32_t *items32;
  59. int32_t length;
  60. ResourceTracer fTraceInfo;
  61. };
  62. /**
  63. * Interface for iterating over a resource bundle table resource.
  64. */
  65. class U_COMMON_API ResourceTable {
  66. public:
  67. /** Constructs an empty table object. */
  68. ResourceTable() : keys16(nullptr), keys32(nullptr), items16(nullptr), items32(nullptr), length(0) {}
  69. /** Only for implementation use. @internal */
  70. ResourceTable(const uint16_t *k16, const int32_t *k32,
  71. const uint16_t *i16, const uint32_t *i32, int32_t len,
  72. const ResourceTracer& traceInfo) :
  73. keys16(k16), keys32(k32), items16(i16), items32(i32), length(len),
  74. fTraceInfo(traceInfo) {}
  75. /**
  76. * @return The number of items in the array resource.
  77. */
  78. int32_t getSize() const { return length; }
  79. /**
  80. * @param i Table item index.
  81. * @param key Output-only, receives the key of the i'th item.
  82. * @param value Output-only, receives the value of the i'th item.
  83. * @return true if i is non-negative and less than getSize().
  84. */
  85. UBool getKeyAndValue(int32_t i, const char *&key, ResourceValue &value) const;
  86. /**
  87. * @param key Key string to find in the table.
  88. * @param value Output-only, receives the value of the item with that key.
  89. * @return true if the table contains the key.
  90. */
  91. UBool findValue(const char *key, ResourceValue &value) const;
  92. private:
  93. const uint16_t *keys16;
  94. const int32_t *keys32;
  95. const uint16_t *items16;
  96. const uint32_t *items32;
  97. int32_t length;
  98. ResourceTracer fTraceInfo;
  99. };
  100. /**
  101. * Represents a resource bundle item's value.
  102. * Avoids object creations as much as possible.
  103. * Mutable, not thread-safe.
  104. */
  105. class U_COMMON_API ResourceValue : public UObject {
  106. public:
  107. virtual ~ResourceValue();
  108. /**
  109. * @return ICU resource type, for example, URES_STRING
  110. */
  111. virtual UResType getType() const = 0;
  112. /**
  113. * Sets U_RESOURCE_TYPE_MISMATCH if this is not a string resource.
  114. *
  115. * @see ures_getString()
  116. */
  117. virtual const char16_t *getString(int32_t &length, UErrorCode &errorCode) const = 0;
  118. inline UnicodeString getUnicodeString(UErrorCode &errorCode) const {
  119. int32_t len = 0;
  120. const char16_t *r = getString(len, errorCode);
  121. return UnicodeString(true, r, len);
  122. }
  123. /**
  124. * Sets U_RESOURCE_TYPE_MISMATCH if this is not an alias resource.
  125. */
  126. virtual const char16_t *getAliasString(int32_t &length, UErrorCode &errorCode) const = 0;
  127. inline UnicodeString getAliasUnicodeString(UErrorCode &errorCode) const {
  128. int32_t len = 0;
  129. const char16_t *r = getAliasString(len, errorCode);
  130. return UnicodeString(true, r, len);
  131. }
  132. /**
  133. * Sets U_RESOURCE_TYPE_MISMATCH if this is not an integer resource.
  134. *
  135. * @see ures_getInt()
  136. */
  137. virtual int32_t getInt(UErrorCode &errorCode) const = 0;
  138. /**
  139. * Sets U_RESOURCE_TYPE_MISMATCH if this is not an integer resource.
  140. *
  141. * @see ures_getUInt()
  142. */
  143. virtual uint32_t getUInt(UErrorCode &errorCode) const = 0;
  144. /**
  145. * Sets U_RESOURCE_TYPE_MISMATCH if this is not an intvector resource.
  146. *
  147. * @see ures_getIntVector()
  148. */
  149. virtual const int32_t *getIntVector(int32_t &length, UErrorCode &errorCode) const = 0;
  150. /**
  151. * Sets U_RESOURCE_TYPE_MISMATCH if this is not a binary-blob resource.
  152. *
  153. * @see ures_getBinary()
  154. */
  155. virtual const uint8_t *getBinary(int32_t &length, UErrorCode &errorCode) const = 0;
  156. /**
  157. * Sets U_RESOURCE_TYPE_MISMATCH if this is not an array resource
  158. */
  159. virtual ResourceArray getArray(UErrorCode &errorCode) const = 0;
  160. /**
  161. * Sets U_RESOURCE_TYPE_MISMATCH if this is not a table resource
  162. */
  163. virtual ResourceTable getTable(UErrorCode &errorCode) const = 0;
  164. /**
  165. * Is this a no-fallback/no-inheritance marker string?
  166. * Such a marker is used for
  167. * CLDR no-fallback data values of (three empty-set symbols)=={2205, 2205, 2205}
  168. * when enumerating tables with fallback from the specific resource bundle to root.
  169. *
  170. * @return true if this is a no-inheritance marker string
  171. */
  172. virtual UBool isNoInheritanceMarker() const = 0;
  173. /**
  174. * Sets the dest strings from the string values in this array resource.
  175. *
  176. * @return the number of strings in this array resource.
  177. * If greater than capacity, then an overflow error is set.
  178. *
  179. * Sets U_RESOURCE_TYPE_MISMATCH if this is not an array resource
  180. * or if any of the array items is not a string
  181. */
  182. virtual int32_t getStringArray(UnicodeString *dest, int32_t capacity,
  183. UErrorCode &errorCode) const = 0;
  184. /**
  185. * Same as
  186. * <pre>
  187. * if (getType() == URES_STRING) {
  188. * return new String[] { getString(); }
  189. * } else {
  190. * return getStringArray();
  191. * }
  192. * </pre>
  193. *
  194. * Sets U_RESOURCE_TYPE_MISMATCH if this is
  195. * neither a string resource nor an array resource containing strings
  196. * @see getString()
  197. * @see getStringArray()
  198. */
  199. virtual int32_t getStringArrayOrStringAsArray(UnicodeString *dest, int32_t capacity,
  200. UErrorCode &errorCode) const = 0;
  201. /**
  202. * Same as
  203. * <pre>
  204. * if (getType() == URES_STRING) {
  205. * return getString();
  206. * } else {
  207. * return getStringArray()[0];
  208. * }
  209. * </pre>
  210. *
  211. * Sets U_RESOURCE_TYPE_MISMATCH if this is
  212. * neither a string resource nor an array resource containing strings
  213. * @see getString()
  214. * @see getStringArray()
  215. */
  216. virtual UnicodeString getStringOrFirstOfArray(UErrorCode &errorCode) const = 0;
  217. protected:
  218. ResourceValue() {}
  219. private:
  220. ResourceValue(const ResourceValue &); // no copy constructor
  221. ResourceValue &operator=(const ResourceValue &); // no assignment operator
  222. };
  223. /**
  224. * Sink for ICU resource bundle contents.
  225. */
  226. class U_COMMON_API ResourceSink : public UObject {
  227. public:
  228. ResourceSink() {}
  229. virtual ~ResourceSink();
  230. /**
  231. * Called once for each bundle (child-parent-...-root).
  232. * The value is normally an array or table resource,
  233. * and implementations of this method normally iterate over the
  234. * tree of resource items stored there.
  235. *
  236. * @param key The key string of the enumeration-start resource.
  237. * Empty if the enumeration starts at the top level of the bundle.
  238. * @param value Call getArray() or getTable() as appropriate. Then reuse for
  239. * output values from Array and Table getters. Note: ResourceTable and
  240. * ResourceArray instances must outlive the ResourceValue instance for
  241. * ResourceTracer to be happy.
  242. * @param noFallback true if the bundle has no parent;
  243. * that is, its top-level table has the nofallback attribute,
  244. * or it is the root bundle of a locale tree.
  245. */
  246. virtual void put(const char *key, ResourceValue &value, UBool noFallback,
  247. UErrorCode &errorCode) = 0;
  248. private:
  249. ResourceSink(const ResourceSink &) = delete; // no copy constructor
  250. ResourceSink &operator=(const ResourceSink &) = delete; // no assignment operator
  251. };
  252. U_NAMESPACE_END
  253. #endif