1 /* 2 ******************************************************************************* 3 * Copyright (C) 2015, International Business Machines 4 * Corporation and others. All Rights Reserved. 5 ******************************************************************************* 6 * resource.h 7 * 8 * created on: 2015nov04 9 * created by: Markus W. Scherer 10 */ 11 12 #ifndef __URESOURCE_H__ 13 #define __URESOURCE_H__ 14 15 /** 16 * \file 17 * \brief ICU resource bundle key and value types. 18 */ 19 20 // Note: Ported from ICU4J class UResource and its nested classes, 21 // but the C++ classes are separate, not nested. 22 23 // We use the Resource prefix for C++ classes, as usual. 24 // The UResource prefix would be used for C types. 25 26 #include "unicode/utypes.h" 27 #include "unicode/unistr.h" 28 #include "unicode/ures.h" 29 30 U_NAMESPACE_BEGIN 31 32 class ResourceTableSink; 33 34 // Note: In C++, we use const char * pointers for keys, 35 // rather than an abstraction like Java UResource.Key. 36 37 /** 38 * Represents a resource bundle item's value. 39 * Avoids object creations as much as possible. 40 * Mutable, not thread-safe. 41 */ 42 class U_COMMON_API ResourceValue : public UObject { 43 public: 44 virtual ~ResourceValue(); 45 46 /** 47 * @return ICU resource type, for example, URES_STRING 48 */ 49 virtual UResType getType() const = 0; 50 51 /** 52 * Sets U_RESOURCE_TYPE_MISMATCH if this is not a string resource. 53 * 54 * @see ures_getString() 55 */ 56 virtual const UChar *getString(int32_t &length, UErrorCode &errorCode) const = 0; 57 58 inline UnicodeString getUnicodeString(UErrorCode &errorCode) const { 59 int32_t len = 0; 60 const UChar *r = getString(len, errorCode); 61 return UnicodeString(TRUE, r, len); 62 } 63 64 /** 65 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an alias resource. 66 */ 67 virtual const UChar *getAliasString(int32_t &length, UErrorCode &errorCode) const = 0; 68 69 inline UnicodeString getAliasUnicodeString(UErrorCode &errorCode) const { 70 int32_t len = 0; 71 const UChar *r = getAliasString(len, errorCode); 72 return UnicodeString(TRUE, r, len); 73 } 74 75 /** 76 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an integer resource. 77 * 78 * @see ures_getInt() 79 */ 80 virtual int32_t getInt(UErrorCode &errorCode) const = 0; 81 82 /** 83 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an integer resource. 84 * 85 * @see ures_getUInt() 86 */ 87 virtual uint32_t getUInt(UErrorCode &errorCode) const = 0; 88 89 /** 90 * Sets U_RESOURCE_TYPE_MISMATCH if this is not an intvector resource. 91 * 92 * @see ures_getIntVector() 93 */ 94 virtual const int32_t *getIntVector(int32_t &length, UErrorCode &errorCode) const = 0; 95 96 /** 97 * Sets U_RESOURCE_TYPE_MISMATCH if this is not a binary-blob resource. 98 * 99 * @see ures_getBinary() 100 */ 101 virtual const uint8_t *getBinary(int32_t &length, UErrorCode &errorCode) const = 0; 102 103 protected: 104 ResourceValue() {} 105 106 private: 107 ResourceValue(const ResourceValue &); // no copy constructor 108 ResourceValue &operator=(const ResourceValue &); // no assignment operator 109 }; 110 111 /** 112 * Sink for ICU resource array contents. 113 * The base class does nothing. 114 * 115 * Nested arrays and tables are stored as nested sinks, 116 * never put() as ResourceValue items. 117 */ 118 class U_COMMON_API ResourceArraySink : public UObject { 119 public: 120 ResourceArraySink() {} 121 virtual ~ResourceArraySink(); 122 123 /** 124 * Adds a value from a resource array. 125 * 126 * @param index of the resource array item 127 * @param value resource value 128 */ 129 virtual void put(int32_t index, const ResourceValue &value, UErrorCode &errorCode); 130 131 /** 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. 136 * 137 * This sink (not the caller) owns the nested sink. 138 * 139 * @param index of the resource array item 140 * @param size number of array items 141 * @return nested-array sink, or NULL 142 */ 143 virtual ResourceArraySink *getOrCreateArraySink( 144 int32_t index, int32_t size, UErrorCode &errorCode); 145 146 /** 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. 151 * 152 * This sink (not the caller) owns the nested sink. 153 * 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 157 */ 158 virtual ResourceTableSink *getOrCreateTableSink( 159 int32_t index, int32_t initialSize, UErrorCode &errorCode); 160 161 /** 162 * "Leaves" the array. 163 * Indicates that all of the resources and sub-resources of the current array 164 * have been enumerated. 165 */ 166 virtual void leave(UErrorCode &errorCode); 167 168 private: 169 ResourceArraySink(const ResourceArraySink &); // no copy constructor 170 ResourceArraySink &operator=(const ResourceArraySink &); // no assignment operator 171 }; 172 173 /** 174 * Sink for ICU resource table contents. 175 * The base class does nothing. 176 * 177 * Nested arrays and tables are stored as nested sinks, 178 * never put() as ResourceValue items. 179 */ 180 class U_COMMON_API ResourceTableSink : public UObject { 181 public: 182 ResourceTableSink() {} 183 virtual ~ResourceTableSink(); 184 185 /** 186 * Adds a key-value pair from a resource table. 187 * 188 * @param key resource key string 189 * @param value resource value 190 */ 191 virtual void put(const char *key, const ResourceValue &value, UErrorCode &errorCode); 192 193 /** 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. 197 * 198 * The default implementation does nothing. 199 * 200 * @param key to be removed 201 */ 202 virtual void putNoFallback(const char *key, UErrorCode &errorCode); 203 204 /** 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. 209 * 210 * This sink (not the caller) owns the nested sink. 211 * 212 * @param key resource key string 213 * @param size number of array items 214 * @return nested-array sink, or NULL 215 */ 216 virtual ResourceArraySink *getOrCreateArraySink( 217 const char *key, int32_t size, UErrorCode &errorCode); 218 219 /** 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. 224 * 225 * This sink (not the caller) owns the nested sink. 226 * 227 * @param key resource key string 228 * @param initialSize size hint for creating the sink if necessary 229 * @return nested-table sink, or NULL 230 */ 231 virtual ResourceTableSink *getOrCreateTableSink( 232 const char *key, int32_t initialSize, UErrorCode &errorCode); 233 234 /** 235 * "Leaves" the table. 236 * Indicates that all of the resources and sub-resources of the current table 237 * have been enumerated. 238 */ 239 virtual void leave(UErrorCode &errorCode); 240 241 private: 242 ResourceTableSink(const ResourceTableSink &); // no copy constructor 243 ResourceTableSink &operator=(const ResourceTableSink &); // no assignment operator 244 }; 245 246 U_NAMESPACE_END 247 248 #endif 249