1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2015-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * created on: 2015nov04
11 * created by: Markus W. Scherer
14 #ifndef __URESOURCE_H__
15 #define __URESOURCE_H__
19 * \brief ICU resource bundle key and value types.
22 // Note: Ported from ICU4J class UResource and its nested classes,
23 // but the C++ classes are separate, not nested.
25 // We use the Resource prefix for C++ classes, as usual.
26 // The UResource prefix would be used for C types.
28 #include "unicode/utypes.h"
29 #include "unicode/unistr.h"
30 #include "unicode/ures.h"
38 // Note: In C++, we use const char * pointers for keys,
39 // rather than an abstraction like Java UResource.Key.
42 * Interface for iterating over a resource bundle array resource.
44 class U_COMMON_API ResourceArray {
46 /** Constructs an empty array object. */
47 ResourceArray() : items16(NULL), items32(NULL), length(0) {}
49 /** Only for implementation use. @internal */
50 ResourceArray(const uint16_t *i16, const uint32_t *i32, int32_t len) :
51 items16(i16), items32(i32), length(len) {}
54 * @return The number of items in the array resource.
56 int32_t getSize() const { return length; }
58 * @param i Array item index.
59 * @param value Output-only, receives the value of the i'th item.
60 * @return TRUE if i is non-negative and less than getSize().
62 UBool getValue(int32_t i, ResourceValue &value) const;
64 /** Only for implementation use. @internal */
65 uint32_t internalGetResource(const ResourceData *pResData, int32_t i) const;
68 const uint16_t *items16;
69 const uint32_t *items32;
74 * Interface for iterating over a resource bundle table resource.
76 class U_COMMON_API ResourceTable {
78 /** Constructs an empty table object. */
79 ResourceTable() : keys16(NULL), keys32(NULL), items16(NULL), items32(NULL), length(0) {}
81 /** Only for implementation use. @internal */
82 ResourceTable(const uint16_t *k16, const int32_t *k32,
83 const uint16_t *i16, const uint32_t *i32, int32_t len) :
84 keys16(k16), keys32(k32), items16(i16), items32(i32), length(len) {}
87 * @return The number of items in the array resource.
89 int32_t getSize() const { return length; }
91 * @param i Array item index.
92 * @param key Output-only, receives the key of the i'th item.
93 * @param value Output-only, receives the value of the i'th item.
94 * @return TRUE if i is non-negative and less than getSize().
96 UBool getKeyAndValue(int32_t i, const char *&key, ResourceValue &value) const;
99 const uint16_t *keys16;
100 const int32_t *keys32;
101 const uint16_t *items16;
102 const uint32_t *items32;
107 * Represents a resource bundle item's value.
108 * Avoids object creations as much as possible.
109 * Mutable, not thread-safe.
111 class U_COMMON_API ResourceValue : public UObject {
113 virtual ~ResourceValue();
116 * @return ICU resource type, for example, URES_STRING
118 virtual UResType getType() const = 0;
121 * Sets U_RESOURCE_TYPE_MISMATCH if this is not a string resource.
123 * @see ures_getString()
125 virtual const UChar *getString(int32_t &length, UErrorCode &errorCode) const = 0;
127 inline UnicodeString getUnicodeString(UErrorCode &errorCode) const {
129 const UChar *r = getString(len, errorCode);
130 return UnicodeString(TRUE, r, len);
134 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an alias resource.
136 virtual const UChar *getAliasString(int32_t &length, UErrorCode &errorCode) const = 0;
138 inline UnicodeString getAliasUnicodeString(UErrorCode &errorCode) const {
140 const UChar *r = getAliasString(len, errorCode);
141 return UnicodeString(TRUE, r, len);
145 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an integer resource.
149 virtual int32_t getInt(UErrorCode &errorCode) const = 0;
152 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an integer resource.
154 * @see ures_getUInt()
156 virtual uint32_t getUInt(UErrorCode &errorCode) const = 0;
159 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an intvector resource.
161 * @see ures_getIntVector()
163 virtual const int32_t *getIntVector(int32_t &length, UErrorCode &errorCode) const = 0;
166 * Sets U_RESOURCE_TYPE_MISMATCH if this is not a binary-blob resource.
168 * @see ures_getBinary()
170 virtual const uint8_t *getBinary(int32_t &length, UErrorCode &errorCode) const = 0;
173 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an array resource
175 virtual ResourceArray getArray(UErrorCode &errorCode) const = 0;
178 * Sets U_RESOURCE_TYPE_MISMATCH if this is not a table resource
180 virtual ResourceTable getTable(UErrorCode &errorCode) const = 0;
183 * Is this a no-fallback/no-inheritance marker string?
184 * Such a marker is used for
185 * CLDR no-fallback data values of (three empty-set symbols)=={2205, 2205, 2205}
186 * when enumerating tables with fallback from the specific resource bundle to root.
188 * @return TRUE if this is a no-inheritance marker string
190 virtual UBool isNoInheritanceMarker() const = 0;
193 * Sets the dest strings from the string values in this array resource.
195 * @return the number of strings in this array resource.
196 * If greater than capacity, then an overflow error is set.
198 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an array resource
199 * or if any of the array items is not a string
201 virtual int32_t getStringArray(UnicodeString *dest, int32_t capacity,
202 UErrorCode &errorCode) const = 0;
207 * if (getType() == URES_STRING) {
208 * return new String[] { getString(); }
210 * return getStringArray();
214 * Sets U_RESOURCE_TYPE_MISMATCH if this is
215 * neither a string resource nor an array resource containing strings
217 * @see getStringArray()
219 virtual int32_t getStringArrayOrStringAsArray(UnicodeString *dest, int32_t capacity,
220 UErrorCode &errorCode) const = 0;
225 * if (getType() == URES_STRING) {
226 * return getString();
228 * return getStringArray()[0];
232 * Sets U_RESOURCE_TYPE_MISMATCH if this is
233 * neither a string resource nor an array resource containing strings
235 * @see getStringArray()
237 virtual UnicodeString getStringOrFirstOfArray(UErrorCode &errorCode) const = 0;
243 ResourceValue(const ResourceValue &); // no copy constructor
244 ResourceValue &operator=(const ResourceValue &); // no assignment operator
248 * Sink for ICU resource bundle contents.
250 class U_COMMON_API ResourceSink : public UObject {
253 virtual ~ResourceSink();
256 * Called once for each bundle (child-parent-...-root).
257 * The value is normally an array or table resource,
258 * and implementations of this method normally iterate over the
259 * tree of resource items stored there.
261 * @param key The key string of the enumeration-start resource.
262 * Empty if the enumeration starts at the top level of the bundle.
263 * @param value Call getArray() or getTable() as appropriate.
264 * Then reuse for output values from Array and Table getters.
265 * @param noFallback true if the bundle has no parent;
266 * that is, its top-level table has the nofallback attribute,
267 * or it is the root bundle of a locale tree.
269 virtual void put(const char *key, ResourceValue &value, UBool noFallback,
270 UErrorCode &errorCode) = 0;
273 ResourceSink(const ResourceSink &); // no copy constructor
274 ResourceSink &operator=(const ResourceSink &); // no assignment operator