欢迎访问Ningto's博客

Menu
  • 首页
  • 归档
  • 关于
  • 书签
  • 必应壁纸
  • IT聚合
  • 工具
    • 我的工具列表
    • 我的网盘
    • 必应每日壁纸API
    • Html转Markdown
    • 仙尘光标
Menu

boost类型转换异常捕获

最后更新 2021-06-11 10:51:05   阅读量 1058

使用boost做类型转换出错会抛出异常,这里针对这种情况做了简单封装,内部捕获异常,支持默认值、浮点数转换。

#pragma once
#include <string>
#include <iomanip>
#include <boost/lexical_cast.hpp>
#include "logger.h"

template <typename Target, typename Source>
inline Target typecast(const Source &arg)
{
    try {
        return boost::lexical_cast<Target>(arg);
    } catch (boost::bad_lexical_cast &e) {
        LOGGER_ERROR("bad_lexical_cast error:" << e.what() << ", source:" << arg);
    } catch (...) {
        LOGGER_ERROR("bad_lexical_cast unkown error, source:" << arg);
    }
    return Target();
}

template <typename Target, typename Source>
inline Target typecast(const Source &arg, const Target &defaultValue)
{
    try {
        return boost::lexical_cast<Target>(arg);
    } catch (boost::bad_lexical_cast &e) {
        LOGGER_ERROR("bad_lexical_cast error:" << e.what() << ", source:" << arg);
    } catch (...) {
        LOGGER_ERROR("bad_lexical_cast unkown error, source:" << arg);
    }
    return defaultValue;
}

template <typename Target, typename Source>
inline bool try_typecast(const Source &arg, Target &target)
{
    try {
        target = boost::lexical_cast<Target>(arg);
        return true;
    } catch (boost::bad_lexical_cast &e) {
        LOGGER_ERROR("bad_lexical_cast error:" << e.what() << ", source:" << arg);
    } catch (...) {
        LOGGER_ERROR("bad_lexical_cast unkown error, source:" << arg);
    }
    return false;
}

template <typename Target, typename Source>
inline Target numcast(Source arg)
{
    try {
        return boost::numeric_cast<Target>(arg);
    } catch (boost::bad_numeric_cast &e) {
        LOGGER_ERROR("bad_numeric_cast error:" << e.what() << ", source:" << arg);
    } catch (...) {
        LOGGER_ERROR("bad_numeric_cast unkown error, source:" << arg);
    }
    return Target();
}

// 浮点数转换为字符串,精确到指定位数
template<typename Source>
inline std::string typecast_precision(const Source &arg, int precision)
{
    std::ostringstream oss;
    oss << std::fixed << std::setprecision(precision) << arg;
    return oss.str();
}

// 浮点数转换为字符串,精确到指定位数,去掉尾部0
template<typename Source>
inline std::string typecast_precision_trimzero(const Source &arg, int precision)
{
    std::string str = typecast_precision(arg, precision);
    std::size_t pos = str.find('.');
    if (pos == std::string::npos) {
        return str;
    }

    std::size_t pos2 = str.find_last_not_of('0');
    return str.erase(pos2 + ((pos == pos2) ? 0 : 1), std::string::npos);
}

(转载本站文章请注明作者和出处:泞途 - ningto.com)

下一篇 – datetime封装常用功能
上一篇 – 多个Qt程序共享公共库

  1. Boost

toningto@outlook.com

标签云

Life Android Javascript Python Windows Product Go Tools Java MQ Qt Node.js Tips Shell Design MongoDB Others Mac Mobile React C/C++ Web Boost Linux IOS Bug Database

推广链接

【腾讯云】云产品限时秒杀,爆款1核2G云服务器,首年99元

多谢支持,用了好几年,服务很稳定支持多设备!

其他

文章RSS

Copyright © 2016 Welcome To Ningto Blog | 鄂ICP备17003086号-2