• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-167  評論-8  文章-0  trackbacks-0

            Source

            #include <cstddef>

            #include <string>

            #include <typeinfo>

            //#include <boost/config.hpp>

            #include <boost/limits.hpp>

            #include <boost/throw_exception.hpp>

            #include <boost/type_traits/is_pointer.hpp>

            #include <sstream>

             

             

             

            //

            namespace kimi_boost

            {

                // exception used to indicate runtime lexical_cast failure

                class bad_lexical_cast : public std::bad_cast

                {

                public:

                    bad_lexical_cast() :

                    source(&typeid(void)), target(&typeid(void))

                    {

                    }

                    bad_lexical_cast(

                       const std::type_info &source_type,

                        const std::type_info &target_type) :

                        source(&source_type), target(&target_type)

                    {

                    }

                    const std::type_info &source_type() const

                    {

                        return *source;

                    }

                    const std::type_info &target_type() const

                    {

                        return *target;

                    }

                    virtual const char *what() const throw()

                    {

                        return "bad lexical cast: "

                               "source type value could not be interpreted as target";

                    }

                    virtual ~bad_lexical_cast() throw()

                    {

                    }

                private:

                    const std::type_info *source;

                    const std::type_info *target;

                };

             

             

               

                namespace detail // stream wrapper for handling lexical conversions

                {

                    template<typename Target, typename Source>

                    class lexical_stream

                    {

                    private:

                        typedef char char_type;

                                 std::basic_stringstream<char_type> stream;

             

                    public:

                        lexical_stream()

                        {

                            stream.unsetf(std::ios::skipws);

                            if(std::numeric_limits<Target>::is_specialized)

                                stream.precision(std::numeric_limits<Target>::digits10 + 1);

                            else if(std::numeric_limits<Source>::is_specialized)

                                stream.precision(std::numeric_limits<Source>::digits10 + 1);

                        }

             

                        ~lexical_stream()

                        {

                        }

             

                                 //Source類型輸入到流中

                        bool operator<<(const Source &input)

                        {

                            return !(stream << input).fail();

                        }

             

                                 //把流轉(zhuǎn)換為Target類型輸出

                        template<typename InputStreamable>

                        bool operator>>(InputStreamable &output)

                        {

                                        return !boost::is_pointer<InputStreamable>::value &&

                                   stream >> output &&

                                   stream.get() ==

                                       std::char_traits<char_type>::eof();

                        }

             

                                 //string特化

                                 template<>

                        bool operator>>(std::string &output)

                        {

                            output = stream.str();

                            return true;

                        }

                    };//class lexical_stream

                }//namespace detail

             

             

                namespace detail

                {

                    template<class T>

                    struct array_to_pointer_decay

                    {

                        typedef T type;

                    };

             

                    template<class T, std::size_t N>

                    struct array_to_pointer_decay<T[N]>

                    {

                        typedef const T * type;

                    };

                }

             

                template<typename Target, typename Source>

                Target lexical_cast(const Source &arg)

                {

                    typedef typename detail::array_to_pointer_decay<Source>::type NewSource;

             

                    detail::lexical_stream<Target, NewSource> interpreter;

                    Target result;

             

                    if(!(interpreter << arg && interpreter >> result))

                                 boost::throw_exception(bad_lexical_cast(typeid(NewSource), typeid(Target)));

                    return result;

                }

            }

            Test code

            void kimi_lexical_cast_test()

            {

                   try

                   {

                          int i=kimi_boost::lexical_cast<int>("4365");

                          float f=kimi_boost::lexical_cast<float>("234.546");

                          double d=kimi_boost::lexical_cast<double>("24534.546345");

                          std::string s=kimi_boost::lexical_cast<std::string>(24534.546345);

                   }

                   catch(kimi_boost::bad_lexical_cast& e)

                   {

                          cout<<e.what()<<endl;

                   }

             

                   try{

                          int i2=kimi_boost::lexical_cast<int>("0.335");

                   }

                   catch(kimi_boost::bad_lexical_cast& e)

                   {

                          cout<<"source type: "<<e.source_type().name()<<endl;

                          cout<<"target type: "<<e.target_type().name()<<endl;

                          cout<<e.what()<<endl;

                   }

            }

            Output

            source type: char const *

            target type: int

            bad lexical cast: source type value could not be interpreted as target

            posted on 2010-05-27 10:37 老馬驛站 閱讀(1263) 評論(0)  編輯 收藏 引用 所屬分類: Boost
            久久成人精品视频| 伊人久久成人成综合网222| 久久免费的精品国产V∧| 国产精品久久久亚洲| 亚洲午夜久久久精品影院| 国产成人精品久久一区二区三区av | 久久久国产亚洲精品| 久久久精品久久久久影院| 国内精品久久久久影院优| 国产综合免费精品久久久| 久久久噜噜噜久久中文字幕色伊伊| 久久国产精品99精品国产| 精品久久久久中文字幕一区| 国产香蕉久久精品综合网| 久久精品国产亚洲AV麻豆网站| 久久精品亚洲精品国产欧美| 亚洲国产另类久久久精品黑人| 久久精品国产精品青草| 国产一区二区久久久| 国产ww久久久久久久久久| 久久午夜综合久久| 国产成人久久激情91| A级毛片无码久久精品免费| 亚洲一区二区三区日本久久九| 一97日本道伊人久久综合影院| 久久久这里只有精品加勒比| 久久精品国产亚洲av日韩| 欧美日韩精品久久久久| 久久嫩草影院免费看夜色| 亚洲一区中文字幕久久| 精品熟女少妇a∨免费久久| 精品久久久久久国产| 中文成人无码精品久久久不卡| 久久久久国产精品嫩草影院| 久久精品视频网| 一本大道加勒比久久综合| 996久久国产精品线观看| 久久精品人人做人人爽97| 国产亚洲综合久久系列| 久久久久久亚洲精品成人| 久久久久国产精品熟女影院|