- Returns a LocalPointer that can be converted to std::unique_ptr.
return skeleton::generate(fMacros, status);
}
+template<typename Derived>
+LocalPointer<Derived> NumberFormatterSettings<Derived>::clone() const & {
+ return LocalPointer<Derived>(new Derived(*this));
+}
+
+template<typename Derived>
+LocalPointer<Derived> NumberFormatterSettings<Derived>::clone() && {
+ return LocalPointer<Derived>(new Derived(std::move(*this)));
+}
+
// Declare all classes that implement NumberFormatterSettings
// See https://stackoverflow.com/a/495056/1407170
template
return move;
}
+template<typename Derived>
+LocalPointer<Derived> NumberRangeFormatterSettings<Derived>::clone() const & {
+ return LocalPointer<Derived>(new Derived(*this));
+}
+
+template<typename Derived>
+LocalPointer<Derived> NumberRangeFormatterSettings<Derived>::clone() && {
+ return LocalPointer<Derived>(new Derived(std::move(*this)));
+}
+
// Declare all classes that implement NumberRangeFormatterSettings
// See https://stackoverflow.com/a/495056/1407170
template
* .format(1234)
* .toString(); // €1.2K in en-US
*
- * // Create a formatter in a singleton for use later:
+ * // Create a formatter in a singleton by value for use later:
* static const LocalizedNumberFormatter formatter = NumberFormatter::withLocale(...)
* .unit(NoUnit::percent())
* .precision(Precision::fixedFraction(3));
* formatter.format(5.9831).toString(); // 5.983% in en-US
*
- * // Create a "template" in a singleton but without setting a locale until the call site:
- * static const UnlocalizedNumberFormatter template = NumberFormatter::with()
+ * // Create a "template" in a singleton unique_ptr but without setting a locale until the call site:
+ * std::unique_ptr<UnlocalizedNumberFormatter> template = NumberFormatter::with()
* .sign(UNumberSignDisplay::UNUM_SIGN_ALWAYS)
* .adoptUnit(MeasureUnit::createMeter(status))
- * .unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME);
- * template.locale(...).format(1234).toString(); // +1,234 meters in en-US
+ * .unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME)
+ * .clone();
+ * template->locale(...).format(1234).toString(); // +1,234 meters in en-US
* </pre>
*
* <p>
*/
UnicodeString toSkeleton(UErrorCode& status) const;
+ /**
+ * Returns the current (Un)LocalizedNumberFormatter as a LocalPointer
+ * wrapping a heap-allocated copy of the current object.
+ *
+ * This is equivalent to new-ing the move constructor with a value object
+ * as the argument.
+ *
+ * @return A wrapped (Un)LocalizedNumberFormatter pointer, or a wrapped
+ * nullptr on failure.
+ * @draft ICU 64
+ */
+ LocalPointer<Derived> clone() const &;
+
+ /**
+ * Overload of clone for use on an rvalue reference.
+ *
+ * @return A wrapped (Un)LocalizedNumberFormatter pointer, or a wrapped
+ * nullptr on failure.
+ * @draft ICU 64
+ */
+ LocalPointer<Derived> clone() &&;
+
/**
* Sets the UErrorCode if an error occurred in the fluent chain.
* Preserves older error codes in the outErrorCode.
*/
Derived identityFallback(UNumberRangeIdentityFallback identityFallback) &&;
+ /**
+ * Returns the current (Un)LocalizedNumberRangeFormatter as a LocalPointer
+ * wrapping a heap-allocated copy of the current object.
+ *
+ * This is equivalent to new-ing the move constructor with a value object
+ * as the argument.
+ *
+ * @return A wrapped (Un)LocalizedNumberRangeFormatter pointer, or a wrapped
+ * nullptr on failure.
+ * @draft ICU 64
+ */
+ LocalPointer<Derived> clone() const &;
+
+ /**
+ * Overload of clone for use on an rvalue reference.
+ *
+ * @return A wrapped (Un)LocalizedNumberRangeFormatter pointer, or a wrapped
+ * nullptr on failure.
+ * @draft ICU 64
+ */
+ LocalPointer<Derived> clone() &&;
+
/**
* Sets the UErrorCode if an error occurred in the fluent chain.
* Preserves older error codes in the outErrorCode.
void validRanges();
void copyMove();
void localPointerCAPI();
+ void toObject();
void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
void testPlurals();
void testFieldPositions();
void testCopyMove();
+ void toObject();
void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
#include "charstr.h"
#include <cstdarg>
#include <cmath>
+#include <memory>
#include "unicode/unum.h"
#include "unicode/numberformatter.h"
#include "number_asformat.h"
TESTCASE_AUTO(validRanges);
TESTCASE_AUTO(copyMove);
TESTCASE_AUTO(localPointerCAPI);
+ TESTCASE_AUTO(toObject);
TESTCASE_AUTO_END;
}
// No need to do any cleanup since we are using LocalPointer.
}
+void NumberFormatterApiTest::toObject() {
+ IcuTestErrorCode status(*this, "toObject");
+
+ // const lvalue version
+ {
+ LocalizedNumberFormatter lnf = NumberFormatter::withLocale("en")
+ .precision(Precision::fixedFraction(2));
+ LocalPointer<LocalizedNumberFormatter> lnf2(lnf.clone());
+ assertFalse("should create successfully, const lvalue", lnf2.isNull());
+ assertEquals("object API test, const lvalue", u"1,000.00",
+ lnf2->formatDouble(1000, status).toString(status));
+ }
+
+ // rvalue reference version
+ {
+ LocalPointer<LocalizedNumberFormatter> lnf(
+ NumberFormatter::withLocale("en")
+ .precision(Precision::fixedFraction(2))
+ .clone());
+ assertFalse("should create successfully, rvalue reference", lnf.isNull());
+ assertEquals("object API test, rvalue reference", u"1,000.00",
+ lnf->formatDouble(1000, status).toString(status));
+ }
+
+ // to std::unique_ptr via constructor
+ {
+ std::unique_ptr<LocalizedNumberFormatter> lnf(
+ NumberFormatter::withLocale("en")
+ .precision(Precision::fixedFraction(2))
+ .clone());
+ assertTrue("should create successfully, unique_ptr", static_cast<bool>(lnf));
+ assertEquals("object API test, unique_ptr", u"1,000.00",
+ lnf->formatDouble(1000, status).toString(status));
+ }
+
+ // to std::unique_ptr via assignment
+ {
+ std::unique_ptr<LocalizedNumberFormatter> lnf =
+ NumberFormatter::withLocale("en")
+ .precision(Precision::fixedFraction(2))
+ .clone();
+ assertTrue("should create successfully, unique_ptr B", static_cast<bool>(lnf));
+ assertEquals("object API test, unique_ptr B", u"1,000.00",
+ lnf->formatDouble(1000, status).toString(status));
+ }
+
+ // to LocalPointer via assignment
+ {
+ LocalPointer<UnlocalizedNumberFormatter> f =
+ NumberFormatter::with().clone();
+ }
+
+ // make sure no memory leaks
+ {
+ NumberFormatter::with().clone();
+ }
+}
+
void NumberFormatterApiTest::assertFormatDescending(const char16_t* umessage, const char16_t* uskeleton,
const UnlocalizedNumberFormatter& f, Locale locale,
TESTCASE_AUTO(testPlurals);
TESTCASE_AUTO(testFieldPositions);
TESTCASE_AUTO(testCopyMove);
+ TESTCASE_AUTO(toObject);
TESTCASE_AUTO_END;
}
assertEquals("FormattedNumberRange move assignment", u"3,00–6,00 $US", result.toString(status));
}
+void NumberRangeFormatterTest::toObject() {
+ IcuTestErrorCode status(*this, "toObject");
+
+ // const lvalue version
+ {
+ LocalizedNumberRangeFormatter lnf = NumberRangeFormatter::withLocale("en");
+ LocalPointer<LocalizedNumberRangeFormatter> lnf2(lnf.clone());
+ assertFalse("should create successfully, const lvalue", lnf2.isNull());
+ assertEquals("object API test, const lvalue", u"5–7",
+ lnf2->formatFormattableRange(5, 7, status).toString(status));
+ }
+
+ // rvalue reference version
+ {
+ LocalPointer<LocalizedNumberRangeFormatter> lnf(
+ NumberRangeFormatter::withLocale("en").clone());
+ assertFalse("should create successfully, rvalue reference", lnf.isNull());
+ assertEquals("object API test, rvalue reference", u"5–7",
+ lnf->formatFormattableRange(5, 7, status).toString(status));
+ }
+
+ // to std::unique_ptr via assignment
+ {
+ std::unique_ptr<LocalizedNumberRangeFormatter> lnf =
+ NumberRangeFormatter::withLocale("en").clone();
+ assertTrue("should create successfully, unique_ptr B", static_cast<bool>(lnf));
+ assertEquals("object API test, unique_ptr B", u"5–7",
+ lnf->formatFormattableRange(5, 7, status).toString(status));
+ }
+
+ // make sure no memory leaks
+ {
+ NumberRangeFormatter::with().clone();
+ }
+}
+
void NumberRangeFormatterTest::assertFormatRange(
const char16_t* message,
const UnlocalizedNumberRangeFormatter& f,