Return-Path: X-Original-To: apmail-cordova-commits-archive@www.apache.org Delivered-To: apmail-cordova-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 950B211741 for ; Wed, 30 Jul 2014 19:06:21 +0000 (UTC) Received: (qmail 1119 invoked by uid 500); 30 Jul 2014 19:06:21 -0000 Delivered-To: apmail-cordova-commits-archive@cordova.apache.org Received: (qmail 1045 invoked by uid 500); 30 Jul 2014 19:06:21 -0000 Mailing-List: contact commits-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cordova.apache.org Delivered-To: mailing list commits@cordova.apache.org Received: (qmail 911 invoked by uid 99); 30 Jul 2014 19:06:21 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 30 Jul 2014 19:06:21 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 0C7D599FD42; Wed, 30 Jul 2014 19:06:21 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: bhiggins@apache.org To: commits@cordova.apache.org Date: Wed, 30 Jul 2014 19:06:50 -0000 Message-Id: <52cf4f6926f64d5db0d97da078d00369@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [33/38] git commit: BB10: Use auto_ptr for clean & safe memory management. BB10: Use auto_ptr for clean & safe memory management. Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/commit/88568d5c Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/tree/88568d5c Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/diff/88568d5c Branch: refs/heads/lyon-g11n Commit: 88568d5c4bd98c033f44aedc3115d2ec76bd51ec Parents: eb9bbeb Author: Lianghui Chen Authored: Wed Jul 30 10:54:28 2014 -0400 Committer: Lianghui Chen Committed: Wed Jul 30 10:54:28 2014 -0400 ---------------------------------------------------------------------- .../native/src/globalization_ndk.cpp | 43 ++++++++------------ 1 file changed, 16 insertions(+), 27 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/blob/88568d5c/src/blackberry10/native/src/globalization_ndk.cpp ---------------------------------------------------------------------- diff --git a/src/blackberry10/native/src/globalization_ndk.cpp b/src/blackberry10/native/src/globalization_ndk.cpp index c0c0d2d..ab13d77 100644 --- a/src/blackberry10/native/src/globalization_ndk.cpp +++ b/src/blackberry10/native/src/globalization_ndk.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -105,11 +106,12 @@ std::string resultDateInJson(const UDate& date) status); return errorInJson(UNKNOWN_ERROR, "Failed to create Calendar instance!"); } + std::auto_ptr deleter(cal); + cal->setTime(date, status); if (status != U_ZERO_ERROR && status != U_ERROR_WARNING_START) { slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::resultInJson: failed to setTime: %d", status); - delete cal; return errorInJson(UNKNOWN_ERROR, "Failed to set Calendar time!"); } @@ -122,8 +124,6 @@ std::string resultDateInJson(const UDate& date) result["second"] = cal->get(UCAL_SECOND, status); result["millisecond"] = cal->get(UCAL_MILLISECOND, status); - delete cal; - Json::Value root; root["result"] = result; @@ -369,10 +369,10 @@ std::string GlobalizationNDK::dateToString(const std::string& args) slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::dateToString: unable to create DateFormat!"); return errorInJson(UNKNOWN_ERROR, "Unable to create DateFormat instance!"); } + std::auto_ptr deleter(df); UnicodeString result; df->format(date.asDouble(), result); - delete df; std::string utf8; result.toUTF8String(utf8); @@ -421,11 +421,11 @@ std::string GlobalizationNDK::stringToDate(const std::string& args) slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToDate: unable to create DateFormat instance!"); return errorInJson(UNKNOWN_ERROR, "Unable to create DateFormat instance!"); } + std::auto_ptr deleter(df); UnicodeString uDate = UnicodeString::fromUTF8(dateValue); UErrorCode status = U_ZERO_ERROR; UDate date = df->parse(uDate, status); - delete df; // Note: not sure why U_ERROR_WARNING_START is returned when parse succeeded. if (status != U_ZERO_ERROR && status != U_ERROR_WARNING_START) { @@ -467,9 +467,9 @@ std::string GlobalizationNDK::getDatePattern(const std::string& args) slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDatePattern: unable to create DateFormat instance!"); return errorInJson(UNKNOWN_ERROR, "Unable to create DateFormat instance!"); } + std::auto_ptr deleter(df); if (df->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) { - delete df; slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDatePattern: DateFormat instance not SimpleDateFormat!"); return errorInJson(UNKNOWN_ERROR, "DateFormat instance not SimpleDateFormat!"); } @@ -491,8 +491,6 @@ std::string GlobalizationNDK::getDatePattern(const std::string& args) int utc_offset = tz.getRawOffset() / 1000; // UTC_OFFSET in seconds. int dst_offset = tz.getDSTSavings() / 1000; // DST_OFFSET in seconds; - delete sdf; - return resultInJson(ptUtf8, tzUtf8, utc_offset, dst_offset); } @@ -640,9 +638,9 @@ std::string GlobalizationNDK::getDateNames(const std::string& args) slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: unable to create DateFormat instance!"); return errorInJson(UNKNOWN_ERROR, "Unable to create DateFormat instance!"); } + std::auto_ptr deleter(df); if (df->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) { - delete df; slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: DateFormat instance not SimpleDateFormat!"); return errorInJson(UNKNOWN_ERROR, "DateFormat instance not SimpleDateFormat!"); } @@ -652,16 +650,14 @@ std::string GlobalizationNDK::getDateNames(const std::string& args) Calendar* cal = Calendar::createInstance(status); if (!cal) { - delete sdf; slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: unable to create Calendar instance: %x.", status); return errorInJson(UNKNOWN_ERROR, "Unable to create Calendar instance!"); } + std::auto_ptr caldeleter(cal); UCalendarDaysOfWeek ud = cal->getFirstDayOfWeek(status); if (status != U_ZERO_ERROR && status != U_ERROR_WARNING_START) { - delete cal; - delete sdf; slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: failed to getFirstDayOfWeek: %d!", status); return errorInJson(PARSING_ERROR, "Failed to getFirstDayOfWeek!"); @@ -691,9 +687,6 @@ std::string GlobalizationNDK::getDateNames(const std::string& args) utf8Names.push_back(utf8); } - delete cal; - delete sdf; - if (!utf8Names.size()) { slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: unable to get symbols: item: %d, type: %d.", item, type); @@ -877,12 +870,12 @@ std::string GlobalizationNDK::numberToString(const std::string& args) status, type); return errorInJson(UNKNOWN_ERROR, "Failed to create NumberFormat instance!"); } + std::auto_ptr deleter(nf); UnicodeString result; nf->format(nv.asDouble(), result); std::string utf8; result.toUTF8String(utf8); - delete nf; return resultInJson(utf8); } @@ -950,12 +943,12 @@ std::string GlobalizationNDK::stringToNumber(const std::string& args) status, type); return errorInJson(UNKNOWN_ERROR, "Failed to create NumberFormat instance!"); } + std::auto_ptr deleter(nf); UnicodeString uStr = UnicodeString::fromUTF8(str); Formattable value; nf->parse(uStr, value, status); - delete nf; if (status != U_ZERO_ERROR && status != U_ERROR_WARNING_START) { slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToNumber: failed to parse string: %s", @@ -1018,6 +1011,7 @@ std::string GlobalizationNDK::getNumberPattern(const std::string& args) status, type); return errorInJson(UNKNOWN_ERROR, "Failed to create NumberFormat instance!"); } + std::auto_ptr deleter(nf); if (nf->getDynamicClassID() != DecimalFormat::getStaticClassID()) { slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getNumberPattern: DecimalFormat expected: %p != %p", @@ -1028,7 +1022,6 @@ std::string GlobalizationNDK::getNumberPattern(const std::string& args) DecimalFormat* df = (DecimalFormat*) nf; const DecimalFormatSymbols* dfs = df->getDecimalFormatSymbols(); if (!dfs) { - delete nf; slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getNumberPattern: unable to get DecimalFormatSymbols!"); return errorInJson(UNKNOWN_ERROR, "Failed to get DecimalFormatSymbols instance!"); } @@ -1072,8 +1065,6 @@ std::string GlobalizationNDK::getNumberPattern(const std::string& args) ucs.toUTF8String(symbol); ucs.remove(); - delete nf; - return resultInJson(pattern, symbol, fraction, rounding, positive, negative, decimal, grouping); } @@ -1124,28 +1115,28 @@ std::string GlobalizationNDK::getCurrencyPattern(const std::string& args) i); continue; } + std::auto_ptr ndeleter(nf); + const UChar* currency = nf->getCurrency(); if (!currency) { slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getCurrencyPattern: locale %d: failed to getCurrency!", i); - delete nf; continue; } if (!ucc.compare(currency, -1)) { - df = (DecimalFormat*) nf; + df = (DecimalFormat*) ndeleter.release(); break; } - - delete nf; } if (!df) return errorInJson(UNKNOWN_ERROR, "Currency not supported!"); + std::auto_ptr deleter(df); + const DecimalFormatSymbols* dfs = df->getDecimalFormatSymbols(); if (!dfs) { - delete df; slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getCurrencyPattern: unable to get DecimalFormatSymbols!"); return errorInJson(UNKNOWN_ERROR, "Failed to get DecimalFormatSymbols!"); } @@ -1170,8 +1161,6 @@ std::string GlobalizationNDK::getCurrencyPattern(const std::string& args) ucs.toUTF8String(grouping); ucs.remove(); - delete df; - return resultInJson(pattern, cc, fraction, rounding, decimal, grouping); }