ICU-2135 add TransliteratorPointer; use to avoid memory leaks

X-SVN-Rev: 10646
This commit is contained in:
Alan Liu 2002-12-12 19:54:39 +00:00
parent fb67a77d12
commit 8f688eaa68

View File

@ -68,6 +68,32 @@ TransliteratorRoundTripTest::runIndexedTest(int32_t index, UBool exec,
} }
} }
//--------------------------------------------------------------------
// TransliteratorPointer
//--------------------------------------------------------------------
/**
* A transliterator pointer wrapper that deletes the contained
* pointer automatically when the wrapper goes out of scope.
* Sometimes called a "janitor" or "smart pointer".
*/
class TransliteratorPointer {
Transliterator* t;
// disallowed:
TransliteratorPointer(const TransliteratorPointer& rhs);
TransliteratorPointer& operator=(const TransliteratorPointer& rhs);
public:
TransliteratorPointer(Transliterator* adopted) {
t = adopted;
}
~TransliteratorPointer() {
delete t;
}
inline Transliterator* operator->() { return t; }
inline operator const Transliterator*() const { return t; }
inline operator Transliterator*() { return t; }
};
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// Legal // Legal
//-------------------------------------------------------------------- //--------------------------------------------------------------------
@ -520,9 +546,9 @@ void RTTest::test2(UBool quickRt, int32_t density) {
UnicodeString cs, targ, reverse; UnicodeString cs, targ, reverse;
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
UParseError parseError ; UParseError parseError ;
Transliterator* sourceToTarget = TransliteratorPointer sourceToTarget(
Transliterator::createInstance(transliteratorID, UTRANS_FORWARD, parseError, Transliterator::createInstance(transliteratorID, UTRANS_FORWARD, parseError,
status); status));
if (sourceToTarget == NULL) { if (sourceToTarget == NULL) {
parent->errln("FAIL: createInstance(" + transliteratorID + parent->errln("FAIL: createInstance(" + transliteratorID +
") returned NULL. Error: " + u_errorName(status) ") returned NULL. Error: " + u_errorName(status)
@ -531,13 +557,12 @@ void RTTest::test2(UBool quickRt, int32_t density) {
return; return;
} }
Transliterator* targetToSource = sourceToTarget->createInverse(status); TransliteratorPointer targetToSource(sourceToTarget->createInverse(status));
if (targetToSource == NULL) { if (targetToSource == NULL) {
parent->errln("FAIL: " + transliteratorID + parent->errln("FAIL: " + transliteratorID +
".createInverse() returned NULL. Error:" + u_errorName(status) ".createInverse() returned NULL. Error:" + u_errorName(status)
+ "\n\tpreContext : " + prettify(parseError.preContext) + "\n\tpreContext : " + prettify(parseError.preContext)
+ "\n\tpostContext : " + prettify(parseError.postContext)); + "\n\tpostContext : " + prettify(parseError.postContext));
delete sourceToTarget;
return; return;
} }
@ -562,20 +587,20 @@ void RTTest::test2(UBool quickRt, int32_t density) {
UParseError parseError; UParseError parseError;
rules = sourceToTarget->toRules(rules, TRUE); rules = sourceToTarget->toRules(rules, TRUE);
// parent->logln((UnicodeString)"toRules => " + rules); // parent->logln((UnicodeString)"toRules => " + rules);
Transliterator *sourceToTarget2 = Transliterator::createFromRules( TransliteratorPointer sourceToTarget2(Transliterator::createFromRules(
"s2t2", rules, "s2t2", rules,
UTRANS_FORWARD, UTRANS_FORWARD,
parseError, status); parseError, status));
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
parent->errln("FAIL: createFromRules %s\n", u_errorName(status)); parent->errln("FAIL: createFromRules %s\n", u_errorName(status));
return; return;
} }
rules = targetToSource->toRules(rules, FALSE); rules = targetToSource->toRules(rules, FALSE);
Transliterator *targetToSource2 = Transliterator::createFromRules( TransliteratorPointer targetToSource2(Transliterator::createFromRules(
"t2s2", rules, "t2s2", rules,
UTRANS_FORWARD, UTRANS_FORWARD,
parseError, status); parseError, status));
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
parent->errln("FAIL: createFromRules %s\n", u_errorName(status)); parent->errln("FAIL: createFromRules %s\n", u_errorName(status));
return; return;
@ -610,8 +635,6 @@ void RTTest::test2(UBool quickRt, int32_t density) {
logToRulesFails("Target-Source, toRules", cs, targ, targ2); logToRulesFails("Target-Source, toRules", cs, targ, targ2);
} }
} }
delete sourceToTarget2;
delete targetToSource2;
} }
parent->logln("Checking that all source characters convert to target - Singles"); parent->logln("Checking that all source characters convert to target - Singles");
@ -836,8 +859,6 @@ void RTTest::test2(UBool quickRt, int32_t density) {
} }
} }
parent->logln(""); parent->logln("");
delete sourceToTarget;
delete targetToSource;
} }
void RTTest::logWrongScript(const UnicodeString& label, void RTTest::logWrongScript(const UnicodeString& label,
@ -1103,9 +1124,9 @@ static const char latinForIndic[] = "[['.0-9A-Za-z~\\u00C0-\\u00C5\\u00C7-\\u00C
void TransliteratorRoundTripTest::TestDevanagariLatin() { void TransliteratorRoundTripTest::TestDevanagariLatin() {
{ {
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
Transliterator* t1 = Transliterator::createInstance("[\\u0000-\\u00FE \\u0982\\u0983 [:Bengali:][:nonspacing mark:]];NFD;Bengali-InterIndic;InterIndic-Gujarati;NFC;( [ \\u0000-\\u00FE [:Gujarati:][[:nonspacing mark:]])",UTRANS_FORWARD, status); TransliteratorPointer t1(Transliterator::createInstance("[\\u0000-\\u00FE \\u0982\\u0983 [:Bengali:][:nonspacing mark:]];NFD;Bengali-InterIndic;InterIndic-Gujarati;NFC;( [ \\u0000-\\u00FE [:Gujarati:][[:nonspacing mark:]])",UTRANS_FORWARD, status));
if(t1){ if(t1){
t1->createInverse(status); TransliteratorPointer t2(t1->createInverse(status));
if(U_FAILURE(status)){ if(U_FAILURE(status)){
errln("FAIL: could not create the Inverse:-( \n"); errln("FAIL: could not create the Inverse:-( \n");
} }