1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 #include "unicode/utypes.h"
5 
6 #if !UCONFIG_NO_FORMATTING
7 #ifndef __NUMBER_ASFORMAT_H__
8 #define __NUMBER_ASFORMAT_H__
9 
10 #include "unicode/numberformatter.h"
11 #include "number_types.h"
12 #include "number_decimalquantity.h"
13 #include "number_scientific.h"
14 #include "number_patternstring.h"
15 #include "number_modifiers.h"
16 #include "number_multiplier.h"
17 #include "number_roundingutils.h"
18 #include "decNumber.h"
19 #include "charstr.h"
20 
21 U_NAMESPACE_BEGIN namespace number {
22 namespace impl {
23 
24 /**
25  * A wrapper around LocalizedNumberFormatter implementing the Format interface, enabling improved
26  * compatibility with other APIs.
27  *
28  * @draft ICU 62
29  * @see NumberFormatter
30  */
31 class U_I18N_API LocalizedNumberFormatterAsFormat : public Format {
32   public:
33     LocalizedNumberFormatterAsFormat(const LocalizedNumberFormatter& formatter, const Locale& locale);
34 
35     /**
36      * Destructor.
37      */
38     ~LocalizedNumberFormatterAsFormat() U_OVERRIDE;
39 
40     /**
41      * Equals operator.
42      */
43     UBool operator==(const Format& other) const U_OVERRIDE;
44 
45     /**
46      * Creates a copy of this object.
47      */
48     LocalizedNumberFormatterAsFormat* clone() const U_OVERRIDE;
49 
50     /**
51      * Formats a Number using the wrapped LocalizedNumberFormatter. The provided formattable must be a
52      * number type.
53      */
54     UnicodeString& format(const Formattable& obj, UnicodeString& appendTo, FieldPosition& pos,
55                           UErrorCode& status) const U_OVERRIDE;
56 
57     /**
58      * Formats a Number using the wrapped LocalizedNumberFormatter. The provided formattable must be a
59      * number type.
60      */
61     UnicodeString& format(const Formattable& obj, UnicodeString& appendTo, FieldPositionIterator* posIter,
62                           UErrorCode& status) const U_OVERRIDE;
63 
64     /**
65      * Not supported: sets an error index and returns.
66      */
67     void parseObject(const UnicodeString& source, Formattable& result,
68                      ParsePosition& parse_pos) const U_OVERRIDE;
69 
70     /**
71      * Gets the LocalizedNumberFormatter that this wrapper class uses to format numbers.
72      *
73      * For maximum efficiency, this function returns by const reference. You must copy the return value
74      * into a local variable if you want to use it beyond the lifetime of the current object:
75      *
76      * <pre>
77      * LocalizedNumberFormatter localFormatter = fmt->getNumberFormatter();
78      * </pre>
79      *
80      * You can however use the return value directly when chaining:
81      *
82      * <pre>
83      * FormattedNumber result = fmt->getNumberFormatter().formatDouble(514.23, status);
84      * </pre>
85      *
86      * @return The unwrapped LocalizedNumberFormatter.
87      */
88     const LocalizedNumberFormatter& getNumberFormatter() const;
89 
90     UClassID getDynamicClassID() const U_OVERRIDE;
91     static UClassID U_EXPORT2 getStaticClassID();
92 
93   private:
94     LocalizedNumberFormatter fFormatter;
95 
96     // Even though the locale is inside the LocalizedNumberFormatter, we have to keep it here, too, because
97     // LocalizedNumberFormatter doesn't have a getLocale() method, and ICU-TC didn't want to add one.
98     Locale fLocale;
99 };
100 
101 } // namespace impl
102 } // namespace number
103 U_NAMESPACE_END
104 
105 #endif // __NUMBER_ASFORMAT_H__
106 
107 #endif /* #if !UCONFIG_NO_FORMATTING */
108