- 浏览: 261649 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
daknife:
谢谢你的这篇文章,让我大概了解了select的一部分底层原理。 ...
Linux-2.6.25 select系统调用源码分析 -
gjlzjb:
非常有用,谢谢哈。另外问下,您是否用过Pheonix Syst ...
Why Map/Reduce? -
zhangyafei_kimi:
canbo 写道请问,我怎么生成安装包,提供给其它用户安装呢? ...
下载最新的Google Chrome源码并编译 -
canbo:
请问,我怎么生成安装包,提供给其它用户安装呢?
下载最新的Google Chrome源码并编译
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();
}
//把流转换为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
发表评论
-
The Elements of Programing Style
2009-08-09 18:26 1235把代码写清楚,别耍小聪明。 想干什么,讲的简单点、直接点。 只 ... -
6个变态的C语言Hello World程序
2009-06-01 09:37 807转载自:http://cocre.com/?p=914 下面 ... -
在VS2005中使用IBM Purify的注意事项
2009-05-12 12:24 3929Rational Purify 使用及分析实例可以见这里htt ... -
boost.pool源码整理和使用说明
2007-07-22 13:49 270Source #ifndef __KIMI_BOOST_PO ... -
一个STL风格的动态二维数组
2007-07-22 18:05 1492#ifndef __KIMI_BOOST_ARRAY2#def ... -
boost.any源码整理和使用说明
2007-08-24 22:44 1994Source #include <algorithm& ... -
boost.array源码整理和使用说明
2007-08-24 22:45 1217Source #include <cstddef> ... -
boost.BOOST_STATIC_ASSERT源码整理和使用说明
2007-08-24 22:49 1307Source #include <boost/conf ... -
boost.shared_ptr源码整理和使用说明
2007-08-24 22:51 4145Source #pragma once //share ... -
编译期判断类的继承性
2007-08-24 23:00 1069介绍一个雕虫小技:编译期判断类的继承性。具体来说就是类型U是否 ... -
boost.type_traits源码整理和使用说明(1)
2007-08-28 01:35 2028Introduction The Boost type-tr ... -
泛型快速排序
2007-08-28 03:20 903Source #ifndef kimi_quicksort ... -
C++ Meta Programming 和 Boost MPL(1)
2007-08-30 23:01 1559本系列全部转载自kuibyshev.bokee.com ... -
C++ Meta Programming 和 Boost MPL(2)
2007-08-30 23:02 1485本系列全部转载自kuibyshev.bokee.com ... -
C++ Meta Programming 和 Boost MPL(3)
2007-08-30 23:06 1459本系列全部转载自kuibyshev.bokee.com ... -
C++ Meta Programming 和 Boost MPL(4)
2007-08-30 23:07 1632本系列全部转载自kuibyshev.bokee.com ... -
泛型归并排序
2007-09-18 00:23 1182#define SENTINEL_CARD (-1) # ... -
泛型插入排序
2007-09-18 00:25 1171#pragma once #include <iter ... -
boost.tuple源码整理和使用说明
2007-10-07 23:13 1536Introduction A tuple (or n-tup ... -
才发现VC中也可以检测内存泄漏
2009-03-30 14:54 1339#include <stdio.h> ...
相关推荐
boost::lexical_cast用法示例,包含数值转字串,字串转数值以及相应的异常处理代码
c++通过boost库实现字符串之间与数字之间的
制作一个简易的编译器,能够对于给出的符合文法规则程序代码能进行词法分析、语法分析、语义分析并最终生成汇编代码。
用C++写的C语言词法分析器,vc6.0开发
一个词法编译器。功能描述在help.txt中有说明。
The transformation of the formal grammar to the uncertain automata, the content of the lexical analyzer
注释说明,还不能处理预处理命令33种运算符说明:,() [] .在界符中有,这里出 , ? : sizeof 也没给出11种分界符32种关键字程序使用转换表设计,运行速度快,本人还对每个能识别的符号进行了编号。本程序用VS2010 MFC ...
写了一个词法分析器 ...经过词法分析后,源程序字符串(源程序的外部表示)被翻译成具有等长信息的单词串(源程序的内部表示),并产生两个表格:常数表和标识符表,它们分别包含了源程序中的所有常数和所有标识符。
词法编译器,包含.jar的可运行文件,还有NDF流程图
使用flex实现的C语言词法分析,在linux和window下都试过,应该不会有问题,内含编译好的exe文件,直接运行可能会报错,需要将编译器地址放入环境变量
compiler_lexical_analyzer
开发的一个用java语言实现的编译器,内含词法分析器,语法分析器,而且可以实现中间代码生成.用到了SLR算法和LR算法
扫描样本字符串,转换成NFA,继续转换,成为DFA,验证此字符串。
java语法分析程序 主要是用于编译器原理的实践
C#词法分析器 Lexical Scanner 能够实现输入的字符串按照词法切成小片段token 比方说 输入1+2+for 就会得到 1 digit + operator 2 digit + operator for reserved Word
boost基本入门使用示例,简单,一看就懂! 部分代码如下: int a = lexical_cast("123"); double b = lexical_cast("123.0123456789"); string s0 = lexical_cast(a);
这个是Pascal编的。教为简单。 包含词法分析在内。
编译原理词法分析器;语法分析器LR(1)
python写一个简易的词法分析器,比上次的代码加上了一个交互界面,此为demo,可以根据这个代码修改成完整的词法分析器。