1 // © 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"
39 // Note: In C++, we use const char * pointers for keys,
40 // rather than an abstraction like Java UResource.Key.
43 * Interface for iterating over a resource bundle array resource.
45 class U_COMMON_API ResourceArray
{
47 /** Constructs an empty array object. */
48 ResourceArray() : items16(NULL
), items32(NULL
), length(0) {}
50 /** Only for implementation use. @internal */
51 ResourceArray(const uint16_t *i16
, const uint32_t *i32
, int32_t len
,
52 const ResourceTracer
& traceInfo
) :
53 items16(i16
), items32(i32
), length(len
),
54 fTraceInfo(traceInfo
) {}
57 * @return The number of items in the array resource.
59 int32_t getSize() const { return length
; }
61 * @param i Array item index.
62 * @param value Output-only, receives the value of the i'th item.
63 * @return TRUE if i is non-negative and less than getSize().
65 UBool
getValue(int32_t i
, ResourceValue
&value
) const;
67 /** Only for implementation use. @internal */
68 uint32_t internalGetResource(const ResourceData
*pResData
, int32_t i
) const;
71 const uint16_t *items16
;
72 const uint32_t *items32
;
74 ResourceTracer fTraceInfo
;
78 * Interface for iterating over a resource bundle table resource.
80 class U_COMMON_API ResourceTable
{
82 /** Constructs an empty table object. */
83 ResourceTable() : keys16(NULL
), keys32(NULL
), items16(NULL
), items32(NULL
), length(0) {}
85 /** Only for implementation use. @internal */
86 ResourceTable(const uint16_t *k16
, const int32_t *k32
,
87 const uint16_t *i16
, const uint32_t *i32
, int32_t len
,
88 const ResourceTracer
& traceInfo
) :
89 keys16(k16
), keys32(k32
), items16(i16
), items32(i32
), length(len
),
90 fTraceInfo(traceInfo
) {}
93 * @return The number of items in the array resource.
95 int32_t getSize() const { return length
; }
97 * @param i Table item index.
98 * @param key Output-only, receives the key of the i'th item.
99 * @param value Output-only, receives the value of the i'th item.
100 * @return TRUE if i is non-negative and less than getSize().
102 UBool
getKeyAndValue(int32_t i
, const char *&key
, ResourceValue
&value
) const;
105 * @param key Key string to find in the table.
106 * @param value Output-only, receives the value of the item with that key.
107 * @return TRUE if the table contains the key.
109 UBool
findValue(const char *key
, ResourceValue
&value
) const;
112 const uint16_t *keys16
;
113 const int32_t *keys32
;
114 const uint16_t *items16
;
115 const uint32_t *items32
;
117 ResourceTracer fTraceInfo
;
121 * Represents a resource bundle item's value.
122 * Avoids object creations as much as possible.
123 * Mutable, not thread-safe.
125 class U_COMMON_API ResourceValue
: public UObject
{
127 virtual ~ResourceValue();
130 * @return ICU resource type, for example, URES_STRING
132 virtual UResType
getType() const = 0;
135 * Sets U_RESOURCE_TYPE_MISMATCH if this is not a string resource.
137 * @see ures_getString()
139 virtual const UChar
*getString(int32_t &length
, UErrorCode
&errorCode
) const = 0;
141 inline UnicodeString
getUnicodeString(UErrorCode
&errorCode
) const {
143 const UChar
*r
= getString(len
, errorCode
);
144 return UnicodeString(TRUE
, r
, len
);
148 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an alias resource.
150 virtual const UChar
*getAliasString(int32_t &length
, UErrorCode
&errorCode
) const = 0;
152 inline UnicodeString
getAliasUnicodeString(UErrorCode
&errorCode
) const {
154 const UChar
*r
= getAliasString(len
, errorCode
);
155 return UnicodeString(TRUE
, r
, len
);
159 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an integer resource.
163 virtual int32_t getInt(UErrorCode
&errorCode
) const = 0;
166 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an integer resource.
168 * @see ures_getUInt()
170 virtual uint32_t getUInt(UErrorCode
&errorCode
) const = 0;
173 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an intvector resource.
175 * @see ures_getIntVector()
177 virtual const int32_t *getIntVector(int32_t &length
, UErrorCode
&errorCode
) const = 0;
180 * Sets U_RESOURCE_TYPE_MISMATCH if this is not a binary-blob resource.
182 * @see ures_getBinary()
184 virtual const uint8_t *getBinary(int32_t &length
, UErrorCode
&errorCode
) const = 0;
187 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an array resource
189 virtual ResourceArray
getArray(UErrorCode
&errorCode
) const = 0;
192 * Sets U_RESOURCE_TYPE_MISMATCH if this is not a table resource
194 virtual ResourceTable
getTable(UErrorCode
&errorCode
) const = 0;
197 * Is this a no-fallback/no-inheritance marker string?
198 * Such a marker is used for
199 * CLDR no-fallback data values of (three empty-set symbols)=={2205, 2205, 2205}
200 * when enumerating tables with fallback from the specific resource bundle to root.
202 * @return TRUE if this is a no-inheritance marker string
204 virtual UBool
isNoInheritanceMarker() const = 0;
207 * Sets the dest strings from the string values in this array resource.
209 * @return the number of strings in this array resource.
210 * If greater than capacity, then an overflow error is set.
212 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an array resource
213 * or if any of the array items is not a string
215 virtual int32_t getStringArray(UnicodeString
*dest
, int32_t capacity
,
216 UErrorCode
&errorCode
) const = 0;
221 * if (getType() == URES_STRING) {
222 * return new String[] { getString(); }
224 * return getStringArray();
228 * Sets U_RESOURCE_TYPE_MISMATCH if this is
229 * neither a string resource nor an array resource containing strings
231 * @see getStringArray()
233 virtual int32_t getStringArrayOrStringAsArray(UnicodeString
*dest
, int32_t capacity
,
234 UErrorCode
&errorCode
) const = 0;
239 * if (getType() == URES_STRING) {
240 * return getString();
242 * return getStringArray()[0];
246 * Sets U_RESOURCE_TYPE_MISMATCH if this is
247 * neither a string resource nor an array resource containing strings
249 * @see getStringArray()
251 virtual UnicodeString
getStringOrFirstOfArray(UErrorCode
&errorCode
) const = 0;
257 ResourceValue(const ResourceValue
&); // no copy constructor
258 ResourceValue
&operator=(const ResourceValue
&); // no assignment operator
262 * Sink for ICU resource bundle contents.
264 class U_COMMON_API ResourceSink
: public UObject
{
267 virtual ~ResourceSink();
270 * Called once for each bundle (child-parent-...-root).
271 * The value is normally an array or table resource,
272 * and implementations of this method normally iterate over the
273 * tree of resource items stored there.
275 * @param key The key string of the enumeration-start resource.
276 * Empty if the enumeration starts at the top level of the bundle.
277 * @param value Call getArray() or getTable() as appropriate.
278 * Then reuse for output values from Array and Table getters.
279 * @param noFallback true if the bundle has no parent;
280 * that is, its top-level table has the nofallback attribute,
281 * or it is the root bundle of a locale tree.
283 virtual void put(const char *key
, ResourceValue
&value
, UBool noFallback
,
284 UErrorCode
&errorCode
) = 0;
287 ResourceSink(const ResourceSink
&); // no copy constructor
288 ResourceSink
&operator=(const ResourceSink
&); // no assignment operator