zoukankan      html  css  js  c++  java
  • c++11实现一个简单的lexical_cast

      boost中有一个lexical_cast可以用统一的方式来做基本类型之间的转换,比如字符串到数字,数字到字符串,bool和字符串及数字之间的相互转换。boost::lexical_cast的用法比较简单:

    #include <boost/lexical_cast.hpp>
    #include <iostream>
    #include <string> 
    #define ERROR_LEXICAL_CAST     1 
    int main()
    {
        using boost::lexical_cast;
        int         a = 0;
        double        b = 0.0;
        std::string s = ""; 
        int            e = 0;    
        try
        { 
            // ----- 字符串 --> 数值 
            a = lexical_cast<int>("123");
            b = lexical_cast<double>("123.12");
            // ----- 数值 --> 字符串
            s = lexical_cast<std::string>("123456.7"); 
            // ----- 异常处理演示
            e = lexical_cast<int>("abc");
        }
        catch(boost::bad_lexical_cast& e)
        {
            // bad lexical cast: source type value could not be interpreted as target
            std::cout << e.what() << std::endl;
            return ERROR_LEXICAL_CAST;
        } 
        
        std::cout << a << std::endl;    // 输出:123 
        std::cout << b << std::endl;    // 输出:123.12 
        std::cout << s << std::endl;     // 输出:123456.7 
        return 0;
    }
    View Code

      c++11中缺少lexical_cast方法,但是c++11已经提供了一些基本类型转换的方法,比如to_string, atoi, atof等等,但是我们不能通过一种通用的方式来做基本类型转换,因此我希望做一个类似boost的lexical_cast做基本类型的转换,这也是我们的c++社区的一个开发计划

      由于c++11已经提供了一些便利的方法,我要做的事情就变得很简单了,就是把他们糅合在一起并提供一个统一的lexical_cast的方法即可。

      实现思路也很简单,转换主要有这几种:1.数字到字符串的转换;2.字符串到数字的转换;3.bool与字符串的相互转换;4.数字转换为bool;具体的实现代码如下:

    #include <type_traits>
    #include <string>
    #include <cstdlib>
    #include <algorithm>
    #include <stdexcept>
    #include <cctype>
    #include <cstring>
    using namespace std;
    
    namespace detail
    {
        const char* strue = "true";
        const char* sfalse = "false";
    
        template <typename To, typename From>
        struct Converter
        {
        };
    
        //to numeric
        template <typename From>
        struct Converter<int, From>
        {
            static int convert(const From& from)
            {
                return std::atoi(from);
            }
        };
    
        template <typename From>
        struct Converter<long, From>
        {
            static long convert(const From& from)
            {
                return std::atol(from);
            }
        };
    
        template <typename From>
        struct Converter<long long, From>
        {
            static long long convert(const From& from)
            {
                return std::atoll(from);
            }
        };
    
        template <typename From>
        struct Converter<double, From>
        {
            static double convert(const From& from)
            {
                return std::atof(from);
            }
        };
    
        template <typename From>
        struct Converter<float, From>
        {
            static float convert(const From& from)
            {
                return (float)std::atof(from);
            }
        };
    
        //to bool
        template <typename From>
        struct Converter<bool, From>
        {
            static typename std::enable_if<std::is_integral<From>::value, bool>::type convert(From from)
            {
                return !!from;
            }
        };
    
        bool checkbool(const char* from, const size_t len, const char* s)
        {
            for (size_t i = 0; i < len; i++)
            {
                if (from[i] != s[i])
                {
                    return false;
                }
            }
    
            return true;
        }
    
        static bool convert(const char* from)
        {
            const unsigned int len = strlen(from);
            if (len != 4 && len != 5)
                throw std::invalid_argument("argument is invalid");
    
            bool r = true;
            if (len == 4)
            {
                r = checkbool(from, len, strue);
    
                if (r)
                    return true;
            }
            else
            {
                r = checkbool(from, len, sfalse);
    
                if (r)
                    return false;
            }
    
            throw std::invalid_argument("argument is invalid");
        }
    
        template <>
        struct Converter<bool, string>
        {
            static bool convert(const string& from)
            {
                return detail::convert(from.c_str());
            }
        };
    
        template <>
        struct Converter<bool, const char*>
        {
            static bool convert(const char* from)
            {
                return detail::convert(from);
            }
        };
    
        template <>
        struct Converter<bool, char*>
        {
            static bool convert(char* from)
            {
                return detail::convert(from);
            }
        };
    
        template <unsigned N>
        struct Converter<bool, const char[N]>
        {
            static bool convert(const char(&from)[N])
            {
                return detail::convert(from);
            }
        };
    
        template <unsigned N>
        struct Converter<bool, char[N]>
        {
            static bool convert(const char(&from)[N])
            {
                return detail::convert(from);
            }
        };
    
        //to string
        template <typename From>
        struct Converter<string, From>
        {
            static string convert(const From& from)
            {
                return std::to_string(from);
            }
        };
    }
    
    template <typename To, typename From>
    typename std::enable_if<!std::is_same<To, From>::value, To>::type lexical_cast(const From& from)
    {
        return detail::Converter<To, From>::convert(from);
    }
    
    template <typename To, typename From>
    typename std::enable_if<std::is_same<To, From>::value, To>::type lexical_cast(const From& from)
    {
        return from;
    }

    前后花了一个多小时,一个基本的类型转换类就完成了,再测试一下吧,测试代码:

    void test()
    {
        cout<<lexical_cast<int>(1)<<endl;
        cout << lexical_cast<int>("1") << endl;
        cout << lexical_cast<long>("1") << endl;
        cout << lexical_cast<string>(1) << endl;
        cout << lexical_cast<bool>(1) << endl;
        cout << lexical_cast<double>("1.2") << endl;
        cout << lexical_cast<float>("1.2") << endl;
        string s = "true";
        cout << lexical_cast<bool>(s) << endl;
        char* p = "false";
        cout << lexical_cast<bool>(p) << endl;
        const char* q = "false";
        cout << lexical_cast<bool>(q) << endl;
        cout << lexical_cast<bool>("false") << endl;
        cout << lexical_cast<bool>("test") << endl;
    }
    
    int main()
    {
        try
        {
            test();
        }
        catch (const std::exception& e)
        {
            cout << e.what() << endl;
        }
    
        return 0;
    }

    测试结果:

  • 相关阅读:
    Oracle数据库(3-7)
    Oracle数据库之PL/SQL程序设计简介
    JAVA总结
    JAVA数据库编程、JAVA XML解析技术
    JAVA网络编程
    【转载】linux启动jmeter,执行./jmeter.sh报错解决方法
    【转载】JMeter学习(十九)JMeter测试MongoDB
    【转载】JMeter学习(二十)JMeter处理Cookie与Session
    【转载】JMeter学习(十八)JMeter测试Java(二)
    【转载】JMeter学习(十七)JMeter测试Java
  • 原文地址:https://www.cnblogs.com/qicosmos/p/4537051.html
Copyright © 2011-2022 走看看