stlab.adobe.com Adobe Systems Incorporated

number_formatter.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2007 Adobe Systems Incorporated
00003     Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
00004     or a copy at http://stlab.adobe.com/licenses.html)
00005 */
00006 
00007 /****************************************************************************************************/
00008 
00009 #ifndef ADOBE_NUMBER_FORMATTER_HPP
00010 #define ADOBE_NUMBER_FORMATTER_HPP
00011 
00012 /****************************************************************************************************/
00013 
00014 #include <adobe/config.hpp>
00015 
00016 #include <adobe/future/platform_number_formatter_data.hpp>
00017 
00018 #include <boost/bind.hpp>
00019 
00020 #include <adobe/future/locale.hpp>
00021 
00022 #include <string>
00023 
00024 // 1310 == VC++ 7.1
00025 #if defined(_MSC_VER) && _MSC_VER <= 1310
00026     #include <sstream>
00027 #endif
00028 
00029 /****************************************************************************************************/
00030 
00031 namespace adobe {
00032 
00033 /****************************************************************************************************/
00034 
00035 struct number_formatter_t
00036 {
00037     number_formatter_t()
00038     {
00039         // NOTE (fbrereto) : MSVC 8 doesn't allow "this" to be used
00040         //                   in a constructor initializer, so we
00041         //                   assign it here.
00042 
00043         connection_m = adobe::monitor_locale(boost::bind(&number_formatter_t::monitor_locale,
00044                                                          boost::ref(*this),
00045                                                          _1));
00046     }
00047 
00048     ~number_formatter_t()
00049     {
00050         connection_m.disconnect();
00051     }
00052 
00053     void        set_format(const std::string& format);
00054     std::string get_format() const;
00055 
00056 // 1310 == VC++ 7.1
00057 #if defined(_MSC_VER) && _MSC_VER <= 1310
00058     // These workarounds are in place because of
00059     // an internal compiler error generated by
00060     // this code in MSVC 7.1.
00061 
00062     template <typename Numeric>
00063     std::string format(const Numeric& x)
00064     {
00065         std::stringstream result;
00066         result << x;
00067         return result.str();
00068     }
00069 
00070     template <typename Numeric>
00071     Numeric parse(const std::string& str, any_regular_t = any_regular_t())
00072     {
00073         return static_cast<Numeric>(std::atof(str.c_str()));
00074     }
00075 #else
00076     template <typename Numeric>
00077     std::string format(const Numeric& x)
00078     { return format<any_regular_t>(any_regular_t(x)); }
00079 
00080     template <typename Numeric>
00081     Numeric parse(const std::string& str, any_regular_t = any_regular_t())
00082     { return parse<any_regular_t>(str, any_regular_t(Numeric())).template cast<Numeric>(); }
00083 #endif
00084 
00085 private:
00086     void monitor_locale(const dictionary_t&);
00087 
00088     number_formatter_platform_data_t data_m;
00089     boost::signals::connection       connection_m;
00090 };
00091 
00092 /****************************************************************************************************/
00093 
00094 template <>
00095 std::string number_formatter_t::format<any_regular_t>(const any_regular_t& x);
00096 
00097 template <>
00098 any_regular_t number_formatter_t::parse<any_regular_t>(const std::string& str, any_regular_t dummy);
00099 
00100 /****************************************************************************************************/
00101 
00102 bool completely_valid_number_string_given_current_locale(const std::string& value);
00103 
00104 /****************************************************************************************************/
00105 
00106 } // namespace adobe
00107 
00108 /****************************************************************************************************/
00109 
00110 // ADOBE_NUMBER_FORMATTER_HPP
00111 #endif
00112 
00113 /****************************************************************************************************/

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google