1 // 2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh) 3 // 4 // Distributed under the Boost Software License, Version 1.0. (See 5 // accompanying file LICENSE_1_0.txt or copy at 6 // http://www.boost.org/LICENSE_1_0.txt) 7 // 8 #ifndef BOOST_LOCALE_FORMATTER_HPP_INCLUDED 9 #define BOOST_LOCALE_FORMATTER_HPP_INCLUDED 10 11 #include <string> 12 #include <memory> 13 #include <boost/cstdint.hpp> 14 #include <boost/locale/config.hpp> 15 #include <unicode/locid.h> 16 17 namespace boost { 18 namespace locale { 19 namespace impl_icu { 20 21 /// 22 /// \brief Special base polymorphic class that is used as a character type independent base for all formatter classes 23 /// 24 25 class base_formatter { 26 public: ~base_formatter()27 virtual ~base_formatter() 28 { 29 } 30 }; 31 32 /// 33 /// \brief A class that is used for formatting numbers, currency and dates/times 34 /// 35 template<typename CharType> 36 class formatter : public base_formatter { 37 public: 38 typedef CharType char_type; 39 typedef std::basic_string<CharType> string_type; 40 41 /// 42 /// Format the value and return the number of Unicode code points 43 /// 44 virtual string_type format(double value,size_t &code_points) const = 0; 45 /// 46 /// Format the value and return the number of Unicode code points 47 /// 48 virtual string_type format(int64_t value,size_t &code_points) const = 0; 49 /// 50 /// Format the value and return the number of Unicode code points 51 /// 52 virtual string_type format(int32_t value,size_t &code_points) const = 0; 53 54 /// 55 /// Parse the string and return the number of used characters. If it returns 0 56 /// then parsing failed. 57 /// 58 virtual size_t parse(string_type const &str,double &value) const = 0; 59 /// 60 /// Parse the string and return the number of used characters. If it returns 0 61 /// then parsing failed. 62 /// 63 virtual size_t parse(string_type const &str,int64_t &value) const = 0; 64 /// 65 /// Parse the string and return the number of used characters. If it returns 0 66 /// then parsing failed. 67 /// 68 virtual size_t parse(string_type const &str,int32_t &value) const = 0; 69 70 /// 71 /// Get formatter for the current state of ios_base -- flags and locale, 72 /// NULL may be returned if an invalid combination of flags is provided or this type 73 /// of formatting is not supported by locale. See: create 74 /// 75 /// Note: formatter is cached. If \a ios is not changed (no flags or locale changed) 76 /// the formatter would remain the same. Otherwise it would be rebuild and cached 77 /// for future use. It is useful for saving time for generation 78 /// of multiple values with same locale. 79 /// 80 /// For example, this code: 81 /// 82 /// \code 83 /// std::cout << as::spellout; 84 /// for(int i=1;i<=10;i++) 85 /// std::cout << i <<std::endl; 86 /// \endcode 87 /// 88 /// Would create a new spelling formatter only once. 89 /// 90 static formatter *create(std::ios_base &ios,icu::Locale const &l,std::string const &enc); 91 ~formatter()92 virtual ~formatter() 93 { 94 } 95 }; // class formatter 96 97 /// 98 /// Specialization for real implementation 99 /// 100 template<> 101 formatter<char> *formatter<char>::create(std::ios_base &ios,icu::Locale const &l,std::string const &enc); 102 103 /// 104 /// Specialization for real implementation 105 /// 106 template<> 107 formatter<wchar_t> *formatter<wchar_t>::create(std::ios_base &ios,icu::Locale const &l,std::string const &e); 108 109 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T 110 /// 111 /// Specialization for real implementation 112 /// 113 template<> 114 formatter<char16_t> *formatter<char16_t>::create(std::ios_base &ios,icu::Locale const &l,std::string const &e); 115 #endif 116 117 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T 118 /// 119 /// Specialization for real implementation 120 /// 121 template<> 122 formatter<char32_t> *formatter<char32_t>::create(std::ios_base &ios,icu::Locale const &l,std::string const &e); 123 #endif 124 125 } // namespace impl_icu 126 } // namespace locale 127 } // namespace boost 128 129 130 131 #endif 132 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 133