]> granicus.if.org Git - icu/commitdiff
ICU-9460 deprecate Collator::safeClone() and default-implement it in the base class
authorMarkus Scherer <markus.icu@gmail.com>
Wed, 8 Aug 2012 21:03:52 +0000 (21:03 +0000)
committerMarkus Scherer <markus.icu@gmail.com>
Wed, 8 Aug 2012 21:03:52 +0000 (21:03 +0000)
X-SVN-Rev: 32127

icu4c/source/i18n/coll.cpp
icu4c/source/i18n/tblcoll.cpp
icu4c/source/i18n/unicode/coll.h
icu4c/source/i18n/unicode/tblcoll.h

index 9ba9a6dbd68beb9d829f5277b28e8160f529f4a4..9e9ca0239d0a269641120420a7f0e68512b793e1 100644 (file)
@@ -425,6 +425,11 @@ Collator::createInstance(const Locale &loc,
 }
 #endif
 
+Collator *
+Collator::safeClone() const {
+    return clone();
+}
+
 // implement deprecated, previously abstract method
 Collator::EComparisonResult Collator::compare(const UnicodeString& source, 
                                     const UnicodeString& target) const
index 8e23b956d7afdbb98ece3dda1730ff162ed95555..96ff6875f2a5e6caf8e86d96abe1eb987416824a 100644 (file)
@@ -206,32 +206,33 @@ UBool RuleBasedCollator::operator==(const Collator& that) const
 // aliasing, not write-through
 RuleBasedCollator& RuleBasedCollator::operator=(const RuleBasedCollator& that)
 {
-    if (this != &that)
-    {
-        if (dataIsOwned)
-        {
-            ucol_close(ucollator);
-        }
+    if (this == &that) { return *this; }
 
-        urulestring.truncate(0); // empty the rule string
-        dataIsOwned = TRUE;
-        isWriteThroughAlias = FALSE;
+    UErrorCode intStatus = U_ZERO_ERROR;
+    int32_t buffersize = U_COL_SAFECLONE_BUFFERSIZE;
+    UCollator *ucol = ucol_safeClone(that.ucollator, NULL, &buffersize, &intStatus);
+    if (U_FAILURE(intStatus)) { return *this; }
 
-        UErrorCode intStatus = U_ZERO_ERROR;
-        int32_t buffersize = U_COL_SAFECLONE_BUFFERSIZE;
-        ucollator = ucol_safeClone(that.ucollator, NULL, &buffersize,
-                                        &intStatus);
-        if (U_SUCCESS(intStatus)) {
-            setRuleStringFromCollator();
-        }
+    if (dataIsOwned) {
+        ucol_close(ucollator);
     }
+    ucollator = ucol;
+    dataIsOwned = TRUE;
+    isWriteThroughAlias = FALSE;
+    setRuleStringFromCollator();
     return *this;
 }
 
 // aliasing, not write-through
 Collator* RuleBasedCollator::clone() const
 {
-    return new RuleBasedCollator(*this);
+    RuleBasedCollator* coll = new RuleBasedCollator(*this);
+    // There is a small chance that the internal ucol_safeClone() call fails.
+    if (coll != NULL && coll->ucollator == NULL) {
+        delete coll;
+        return NULL;
+    }
+    return coll;
 }
 
 
@@ -519,29 +520,6 @@ uint32_t RuleBasedCollator::getVariableTop(UErrorCode &status) const {
   return ucol_getVariableTop(ucollator, &status);
 }
 
-Collator* RuleBasedCollator::safeClone(void) const
-{
-    UErrorCode intStatus = U_ZERO_ERROR;
-    int32_t buffersize = U_COL_SAFECLONE_BUFFERSIZE;
-    UCollator *ucol = ucol_safeClone(ucollator, NULL, &buffersize,
-                                    &intStatus);
-    if (U_FAILURE(intStatus)) {
-        return NULL;
-    }
-
-    RuleBasedCollator *result = new RuleBasedCollator();
-    // Null pointer check
-    if (result != NULL) {
-        result->ucollator = ucol;
-        result->dataIsOwned = TRUE;
-        result->isWriteThroughAlias = FALSE;
-        result->setRuleStringFromCollator();
-    }
-
-    return result;
-}
-
-
 int32_t RuleBasedCollator::getSortKey(const UnicodeString& source,
                                          uint8_t *result, int32_t resultLength)
                                          const
index b1d802809cd03cf2ee15bcb5573de4bd30ea1a8e..5a5a4d4085f767ff84519ee4a19f9d06a8f3dc58 100644 (file)
@@ -270,8 +270,8 @@ public:
     virtual UBool operator!=(const Collator& other) const;
 
     /**
-     * Makes a shallow copy of the current object.
-     * @return a copy of this object
+     * Makes a copy of this object.
+     * @return a copy of this object, owned by the caller
      * @stable ICU 2.0
      */
     virtual Collator* clone(void) const = 0;
@@ -933,13 +933,14 @@ public:
      */
     virtual UnicodeSet *getTailoredSet(UErrorCode &status) const;
 
-
     /**
-     * Thread safe cloning operation
-     * @return pointer to the new clone, user should remove it.
-     * @stable ICU 2.2
+     * Same as clone().
+     * The base class implementation simply calls clone().
+     * @return a copy of this object, owned by the caller
+     * @see clone()
+     * @deprecated ICU 50 no need to have two methods for cloning
      */
-    virtual Collator* safeClone(void) const = 0;
+    virtual Collator* safeClone(void) const;
 
     /**
      * Get the sort key as an array of bytes from an UnicodeString.
index f08ee599ecf3ea185bb01ff19dda583b3c74b6d5..9d62fa6136b5fa97693be60f8f67fd381d99609d 100644 (file)
@@ -224,9 +224,8 @@ public:
     virtual UBool operator==(const Collator& other) const;
 
     /**
-     * Makes a deep copy of the object.
-     * The caller owns the returned object.
-     * @return the cloned object.
+     * Makes a copy of this object.
+     * @return a copy of this object, owned by the caller
      * @stable ICU 2.0
      */
     virtual Collator* clone(void) const;
@@ -536,13 +535,6 @@ public:
      */
     virtual UnicodeSet *getTailoredSet(UErrorCode &status) const;
 
-    /**
-     * Thread safe cloning operation.
-     * @return pointer to the new clone, user should remove it.
-     * @stable ICU 2.2
-     */
-    virtual Collator* safeClone(void) const;
-
     /**
      * Get the sort key as an array of bytes from an UnicodeString.
      * @param source string to be processed.