2 *******************************************************************************
3 * Copyright (C) 2015, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
8 * created on: 2015nov04
9 * created by: Markus W. Scherer
12 #ifndef __URESOURCE_H__
13 #define __URESOURCE_H__
17 * \brief ICU resource bundle key and value types.
20 // Note: Ported from ICU4J class UResource and its nested classes,
21 // but the C++ classes are separate, not nested.
23 // We use the Resource prefix for C++ classes, as usual.
24 // The UResource prefix would be used for C types.
26 #include "unicode/utypes.h"
27 #include "unicode/unistr.h"
28 #include "unicode/ures.h"
32 class ResourceTableSink
;
34 // Note: In C++, we use const char * pointers for keys,
35 // rather than an abstraction like Java UResource.Key.
38 * Represents a resource bundle item's value.
39 * Avoids object creations as much as possible.
40 * Mutable, not thread-safe.
42 class U_COMMON_API ResourceValue
: public UObject
{
44 virtual ~ResourceValue();
47 * @return ICU resource type, for example, URES_STRING
49 virtual UResType
getType() const = 0;
52 * Sets U_RESOURCE_TYPE_MISMATCH if this is not a string resource.
54 * @see ures_getString()
56 virtual const UChar
*getString(int32_t &length
, UErrorCode
&errorCode
) const = 0;
58 inline UnicodeString
getUnicodeString(UErrorCode
&errorCode
) const {
60 const UChar
*r
= getString(len
, errorCode
);
61 return UnicodeString(TRUE
, r
, len
);
65 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an alias resource.
67 virtual const UChar
*getAliasString(int32_t &length
, UErrorCode
&errorCode
) const = 0;
69 inline UnicodeString
getAliasUnicodeString(UErrorCode
&errorCode
) const {
71 const UChar
*r
= getAliasString(len
, errorCode
);
72 return UnicodeString(TRUE
, r
, len
);
76 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an integer resource.
80 virtual int32_t getInt(UErrorCode
&errorCode
) const = 0;
83 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an integer resource.
87 virtual uint32_t getUInt(UErrorCode
&errorCode
) const = 0;
90 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an intvector resource.
92 * @see ures_getIntVector()
94 virtual const int32_t *getIntVector(int32_t &length
, UErrorCode
&errorCode
) const = 0;
97 * Sets U_RESOURCE_TYPE_MISMATCH if this is not a binary-blob resource.
99 * @see ures_getBinary()
101 virtual const uint8_t *getBinary(int32_t &length
, UErrorCode
&errorCode
) const = 0;
107 ResourceValue(const ResourceValue
&); // no copy constructor
108 ResourceValue
&operator=(const ResourceValue
&); // no assignment operator
112 * Sink for ICU resource array contents.
113 * The base class does nothing.
115 * Nested arrays and tables are stored as nested sinks,
116 * never put() as ResourceValue items.
118 class U_COMMON_API ResourceArraySink
: public UObject
{
120 ResourceArraySink() {}
121 virtual ~ResourceArraySink();
124 * Adds a value from a resource array.
126 * @param index of the resource array item
127 * @param value resource value
129 virtual void put(int32_t index
, const ResourceValue
&value
, UErrorCode
&errorCode
);
132 * Returns a nested resource array at the array index as another sink.
133 * Creates the sink if none exists for the key.
134 * Returns NULL if nested arrays are not supported.
135 * The default implementation always returns NULL.
137 * This sink (not the caller) owns the nested sink.
139 * @param index of the resource array item
140 * @param size number of array items
141 * @return nested-array sink, or NULL
143 virtual ResourceArraySink
*getOrCreateArraySink(
144 int32_t index
, int32_t size
, UErrorCode
&errorCode
);
147 * Returns a nested resource table at the array index as another sink.
148 * Creates the sink if none exists for the key.
149 * Returns NULL if nested tables are not supported.
150 * The default implementation always returns NULL.
152 * This sink (not the caller) owns the nested sink.
154 * @param index of the resource array item
155 * @param initialSize size hint for creating the sink if necessary
156 * @return nested-table sink, or NULL
158 virtual ResourceTableSink
*getOrCreateTableSink(
159 int32_t index
, int32_t initialSize
, UErrorCode
&errorCode
);
162 * "Leaves" the array.
163 * Indicates that all of the resources and sub-resources of the current array
164 * have been enumerated.
166 virtual void leave(UErrorCode
&errorCode
);
169 ResourceArraySink(const ResourceArraySink
&); // no copy constructor
170 ResourceArraySink
&operator=(const ResourceArraySink
&); // no assignment operator
174 * Sink for ICU resource table contents.
175 * The base class does nothing.
177 * Nested arrays and tables are stored as nested sinks,
178 * never put() as ResourceValue items.
180 class U_COMMON_API ResourceTableSink
: public UObject
{
182 ResourceTableSink() {}
183 virtual ~ResourceTableSink();
186 * Adds a key-value pair from a resource table.
188 * @param key resource key string
189 * @param value resource value
191 virtual void put(const char *key
, const ResourceValue
&value
, UErrorCode
&errorCode
);
194 * Adds a no-fallback/no-inheritance marker for this key.
195 * Used for CLDR no-fallback data values of (three empty-set symbols)=={2205, 2205, 2205}
196 * when enumerating tables with fallback from the specific resource bundle to root.
198 * The default implementation does nothing.
200 * @param key to be removed
202 virtual void putNoFallback(const char *key
, UErrorCode
&errorCode
);
205 * Returns a nested resource array for the key as another sink.
206 * Creates the sink if none exists for the key.
207 * Returns NULL if nested arrays are not supported.
208 * The default implementation always returns NULL.
210 * This sink (not the caller) owns the nested sink.
212 * @param key resource key string
213 * @param size number of array items
214 * @return nested-array sink, or NULL
216 virtual ResourceArraySink
*getOrCreateArraySink(
217 const char *key
, int32_t size
, UErrorCode
&errorCode
);
220 * Returns a nested resource table for the key as another sink.
221 * Creates the sink if none exists for the key.
222 * Returns NULL if nested tables are not supported.
223 * The default implementation always returns NULL.
225 * This sink (not the caller) owns the nested sink.
227 * @param key resource key string
228 * @param initialSize size hint for creating the sink if necessary
229 * @return nested-table sink, or NULL
231 virtual ResourceTableSink
*getOrCreateTableSink(
232 const char *key
, int32_t initialSize
, UErrorCode
&errorCode
);
235 * "Leaves" the table.
236 * Indicates that all of the resources and sub-resources of the current table
237 * have been enumerated.
239 virtual void leave(UErrorCode
&errorCode
);
242 ResourceTableSink(const ResourceTableSink
&); // no copy constructor
243 ResourceTableSink
&operator=(const ResourceTableSink
&); // no assignment operator