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  * @see NumberFormatter
29  */
30 class U_I18N_API LocalizedNumberFormatterAsFormat : public Format {
31   public:
32     LocalizedNumberFormatterAsFormat(const LocalizedNumberFormatter& formatter, const Locale& locale);
33 
34     /**
35      * Destructor.
36      */
37     ~LocalizedNumberFormatterAsFormat() U_OVERRIDE;
38 
39     /**
40      * Equals operator.
41      */
42     UBool operator==(const Format& other) const U_OVERRIDE;
43 
44     /**
45      * Creates a copy of this object.
46      */
47     LocalizedNumberFormatterAsFormat* clone() const U_OVERRIDE;
48 
49     /**
50      * Formats a Number using the wrapped LocalizedNumberFormatter. The provided formattable must be a
51      * number type.
52      */
53     UnicodeString& format(const Formattable& obj, UnicodeString& appendTo, FieldPosition& pos,
54                           UErrorCode& status) const U_OVERRIDE;
55 
56     /**
57      * Formats a Number using the wrapped LocalizedNumberFormatter. The provided formattable must be a
58      * number type.
59      */
60     UnicodeString& format(const Formattable& obj, UnicodeString& appendTo, FieldPositionIterator* posIter,
61                           UErrorCode& status) const U_OVERRIDE;
62 
63     /**
64      * Not supported: sets an error index and returns.
65      */
66     void parseObject(const UnicodeString& source, Formattable& result,
67                      ParsePosition& parse_pos) const U_OVERRIDE;
68 
69     /**
70      * Gets the LocalizedNumberFormatter that this wrapper class uses to format numbers.
71      *
72      * For maximum efficiency, this function returns by const reference. You must copy the return value
73      * into a local variable if you want to use it beyond the lifetime of the current object:
74      *
75      * <pre>
76      * LocalizedNumberFormatter localFormatter = fmt->getNumberFormatter();
77      * </pre>
78      *
79      * You can however use the return value directly when chaining:
80      *
81      * <pre>
82      * FormattedNumber result = fmt->getNumberFormatter().formatDouble(514.23, status);
83      * </pre>
84      *
85      * @return The unwrapped LocalizedNumberFormatter.
86      */
87     const LocalizedNumberFormatter& getNumberFormatter() const;
88 
89     UClassID getDynamicClassID() const U_OVERRIDE;
90     static UClassID U_EXPORT2 getStaticClassID();
91 
92   private:
93     LocalizedNumberFormatter fFormatter;
94 
95     // Even though the locale is inside the LocalizedNumberFormatter, we have to keep it here, too, because
96     // LocalizedNumberFormatter doesn't have a getLocale() method, and ICU-TC didn't want to add one.
97     Locale fLocale;
98 };
99 
100 } // namespace impl
101 } // namespace number
102 U_NAMESPACE_END
103 
104 #endif // __NUMBER_ASFORMAT_H__
105 
106 #endif /* #if !UCONFIG_NO_FORMATTING */
107