Imported Upstream version 58.1
[platform/upstream/icu.git] / source / common / resource.h
index 042e298..43c3309 100644 (file)
@@ -1,6 +1,8 @@
+// Copyright (C) 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
 *******************************************************************************
-* Copyright (C) 2015, International Business Machines
+* Copyright (C) 2015-2016, International Business Machines
 * Corporation and others.  All Rights Reserved.
 *******************************************************************************
 * resource.h
 #include "unicode/unistr.h"
 #include "unicode/ures.h"
 
+struct ResourceData;
+
 U_NAMESPACE_BEGIN
 
-class ResourceTableSink;
+class ResourceValue;
 
 // Note: In C++, we use const char * pointers for keys,
 // rather than an abstraction like Java UResource.Key.
 
 /**
+ * Interface for iterating over a resource bundle array resource.
+ */
+class U_COMMON_API ResourceArray {
+public:
+    /** Constructs an empty array object. */
+    ResourceArray() : items16(NULL), items32(NULL), length(0) {}
+
+    /** Only for implementation use. @internal */
+    ResourceArray(const uint16_t *i16, const uint32_t *i32, int32_t len) :
+            items16(i16), items32(i32), length(len) {}
+
+    /**
+     * @return The number of items in the array resource.
+     */
+    int32_t getSize() const { return length; }
+    /**
+     * @param i Array item index.
+     * @param value Output-only, receives the value of the i'th item.
+     * @return TRUE if i is non-negative and less than getSize().
+     */
+    UBool getValue(int32_t i, ResourceValue &value) const;
+
+    /** Only for implementation use. @internal */
+    uint32_t internalGetResource(const ResourceData *pResData, int32_t i) const;
+
+private:
+    const uint16_t *items16;
+    const uint32_t *items32;
+    int32_t length;
+};
+
+/**
+ * Interface for iterating over a resource bundle table resource.
+ */
+class U_COMMON_API ResourceTable {
+public:
+    /** Constructs an empty table object. */
+    ResourceTable() : keys16(NULL), keys32(NULL), items16(NULL), items32(NULL), length(0) {}
+
+    /** Only for implementation use. @internal */
+    ResourceTable(const uint16_t *k16, const int32_t *k32,
+                  const uint16_t *i16, const uint32_t *i32, int32_t len) :
+            keys16(k16), keys32(k32), items16(i16), items32(i32), length(len) {}
+
+    /**
+     * @return The number of items in the array resource.
+     */
+    int32_t getSize() const { return length; }
+    /**
+     * @param i Array item index.
+     * @param key Output-only, receives the key of the i'th item.
+     * @param value Output-only, receives the value of the i'th item.
+     * @return TRUE if i is non-negative and less than getSize().
+     */
+    UBool getKeyAndValue(int32_t i, const char *&key, ResourceValue &value) const;
+
+private:
+    const uint16_t *keys16;
+    const int32_t *keys32;
+    const uint16_t *items16;
+    const uint32_t *items32;
+    int32_t length;
+};
+
+/**
  * Represents a resource bundle item's value.
  * Avoids object creations as much as possible.
  * Mutable, not thread-safe.
@@ -100,147 +169,109 @@ public:
      */
     virtual const uint8_t *getBinary(int32_t &length, UErrorCode &errorCode) const = 0;
 
-protected:
-    ResourceValue() {}
-
-private:
-    ResourceValue(const ResourceValue &);  // no copy constructor
-    ResourceValue &operator=(const ResourceValue &);  // no assignment operator
-};
+    /**
+     * Sets U_RESOURCE_TYPE_MISMATCH if this is not an array resource
+     */
+    virtual ResourceArray getArray(UErrorCode &errorCode) const = 0;
 
-/**
- * Sink for ICU resource array contents.
- * The base class does nothing.
- *
- * Nested arrays and tables are stored as nested sinks,
- * never put() as ResourceValue items.
- */
-class U_COMMON_API ResourceArraySink : public UObject {
-public:
-    ResourceArraySink() {}
-    virtual ~ResourceArraySink();
+    /**
+     * Sets U_RESOURCE_TYPE_MISMATCH if this is not a table resource
+     */
+    virtual ResourceTable getTable(UErrorCode &errorCode) const = 0;
 
     /**
-     * Adds a value from a resource array.
+     * Is this a no-fallback/no-inheritance marker string?
+     * Such a marker is used for
+     * CLDR no-fallback data values of (three empty-set symbols)=={2205, 2205, 2205}
+     * when enumerating tables with fallback from the specific resource bundle to root.
      *
-     * @param index of the resource array item
-     * @param value resource value
+     * @return TRUE if this is a no-inheritance marker string
      */
-    virtual void put(int32_t index, const ResourceValue &value, UErrorCode &errorCode);
+    virtual UBool isNoInheritanceMarker() const = 0;
 
     /**
-     * Returns a nested resource array at the array index as another sink.
-     * Creates the sink if none exists for the key.
-     * Returns NULL if nested arrays are not supported.
-     * The default implementation always returns NULL.
+     * Sets the dest strings from the string values in this array resource.
      *
-     * This sink (not the caller) owns the nested sink.
+     * @return the number of strings in this array resource.
+     *     If greater than capacity, then an overflow error is set.
      *
-     * @param index of the resource array item
-     * @param size number of array items
-     * @return nested-array sink, or NULL
+     * Sets U_RESOURCE_TYPE_MISMATCH if this is not an array resource
+     *     or if any of the array items is not a string
      */
-    virtual ResourceArraySink *getOrCreateArraySink(
-            int32_t index, int32_t size, UErrorCode &errorCode);
+    virtual int32_t getStringArray(UnicodeString *dest, int32_t capacity,
+                                   UErrorCode &errorCode) const = 0;
 
     /**
-     * Returns a nested resource table at the array index as another sink.
-     * Creates the sink if none exists for the key.
-     * Returns NULL if nested tables are not supported.
-     * The default implementation always returns NULL.
-     *
-     * This sink (not the caller) owns the nested sink.
+     * Same as
+     * <pre>
+     * if (getType() == URES_STRING) {
+     *     return new String[] { getString(); }
+     * } else {
+     *     return getStringArray();
+     * }
+     * </pre>
      *
-     * @param index of the resource array item
-     * @param initialSize size hint for creating the sink if necessary
-     * @return nested-table sink, or NULL
+     * Sets U_RESOURCE_TYPE_MISMATCH if this is
+     *     neither a string resource nor an array resource containing strings
+     * @see getString()
+     * @see getStringArray()
      */
-    virtual ResourceTableSink *getOrCreateTableSink(
-            int32_t index, int32_t initialSize, UErrorCode &errorCode);
+    virtual int32_t getStringArrayOrStringAsArray(UnicodeString *dest, int32_t capacity,
+                                                  UErrorCode &errorCode) const = 0;
 
     /**
-     * "Leaves" the array.
-     * Indicates that all of the resources and sub-resources of the current array
-     * have been enumerated.
+     * Same as
+     * <pre>
+     * if (getType() == URES_STRING) {
+     *     return getString();
+     * } else {
+     *     return getStringArray()[0];
+     * }
+     * </pre>
+     *
+     * Sets U_RESOURCE_TYPE_MISMATCH if this is
+     *     neither a string resource nor an array resource containing strings
+     * @see getString()
+     * @see getStringArray()
      */
-    virtual void leave(UErrorCode &errorCode);
+    virtual UnicodeString getStringOrFirstOfArray(UErrorCode &errorCode) const = 0;
+
+protected:
+    ResourceValue() {}
 
 private:
-    ResourceArraySink(const ResourceArraySink &);  // no copy constructor
-    ResourceArraySink &operator=(const ResourceArraySink &);  // no assignment operator
+    ResourceValue(const ResourceValue &);  // no copy constructor
+    ResourceValue &operator=(const ResourceValue &);  // no assignment operator
 };
 
 /**
- * Sink for ICU resource table contents.
- * The base class does nothing.
- *
- * Nested arrays and tables are stored as nested sinks,
- * never put() as ResourceValue items.
+ * Sink for ICU resource bundle contents.
  */
-class U_COMMON_API ResourceTableSink : public UObject {
+class U_COMMON_API ResourceSink : public UObject {
 public:
-    ResourceTableSink() {}
-    virtual ~ResourceTableSink();
-
-    /**
-     * Adds a key-value pair from a resource table.
-     *
-     * @param key resource key string
-     * @param value resource value
-     */
-    virtual void put(const char *key, const ResourceValue &value, UErrorCode &errorCode);
+    ResourceSink() {}
+    virtual ~ResourceSink();
 
     /**
-     * Adds a no-fallback/no-inheritance marker for this key.
-     * Used for CLDR no-fallback data values of (three empty-set symbols)=={2205, 2205, 2205}
-     * when enumerating tables with fallback from the specific resource bundle to root.
-     *
-     * The default implementation does nothing.
-     *
-     * @param key to be removed
-     */
-    virtual void putNoFallback(const char *key, UErrorCode &errorCode);
-
-    /**
-     * Returns a nested resource array for the key as another sink.
-     * Creates the sink if none exists for the key.
-     * Returns NULL if nested arrays are not supported.
-     * The default implementation always returns NULL.
-     *
-     * This sink (not the caller) owns the nested sink.
+     * Called once for each bundle (child-parent-...-root).
+     * The value is normally an array or table resource,
+     * and implementations of this method normally iterate over the
+     * tree of resource items stored there.
      *
-     * @param key resource key string
-     * @param size number of array items
-     * @return nested-array sink, or NULL
-     */
-    virtual ResourceArraySink *getOrCreateArraySink(
-            const char *key, int32_t size, UErrorCode &errorCode);
-
-    /**
-     * Returns a nested resource table for the key as another sink.
-     * Creates the sink if none exists for the key.
-     * Returns NULL if nested tables are not supported.
-     * The default implementation always returns NULL.
-     *
-     * This sink (not the caller) owns the nested sink.
-     *
-     * @param key resource key string
-     * @param initialSize size hint for creating the sink if necessary
-     * @return nested-table sink, or NULL
-     */
-    virtual ResourceTableSink *getOrCreateTableSink(
-            const char *key, int32_t initialSize, UErrorCode &errorCode);
-
-    /**
-     * "Leaves" the table.
-     * Indicates that all of the resources and sub-resources of the current table
-     * have been enumerated.
+     * @param key The key string of the enumeration-start resource.
+     *     Empty if the enumeration starts at the top level of the bundle.
+     * @param value Call getArray() or getTable() as appropriate.
+     *     Then reuse for output values from Array and Table getters.
+     * @param noFallback true if the bundle has no parent;
+     *     that is, its top-level table has the nofallback attribute,
+     *     or it is the root bundle of a locale tree.
      */
-    virtual void leave(UErrorCode &errorCode);
+    virtual void put(const char *key, ResourceValue &value, UBool noFallback,
+                     UErrorCode &errorCode) = 0;
 
 private:
-    ResourceTableSink(const ResourceTableSink &);  // no copy constructor
-    ResourceTableSink &operator=(const ResourceTableSink &);  // no assignment operator
+    ResourceSink(const ResourceSink &);  // no copy constructor
+    ResourceSink &operator=(const ResourceSink &);  // no assignment operator
 };
 
 U_NAMESPACE_END