Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 我学boost(1)-测试boost库的any
【标  题】:我学boost(1)-测试boost库的any
【关键字】:boost,boost,any
【来  源】:http://www.cublog.cn/u/9337/showart.php?id=178698

我学boost(1)-测试boost库的any

Your Ad Here
测试boost库的any类.装上了,首先就用个any来试试看了.
用any没有用出些感慨,不过顺便用了一下lexical_cast ,感觉那功能不是一般的强大,我喜欢.比itoa好很多了

//////////////////////////////////////////////
/// @defgroup anytest 测试boost any
/// @author  abao++
/// @version 1.0
/// @date    2006-9-30 8:42:09
/// @{
//////////////////////////////////////////////

#include <boost/any.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include<string>
#include<vector>
using namespace std;
using namespace boost;
#define is(a) _v.type()==typeid(a)
//一个变体类
/*!
 设计一个class,利用any类使其容纳int,long整数,chat* string字符串,
 float double浮点数,并提供输出
*/
class myvar
{
public:
 any _v;
 
 myvar(){}
 template<typename T> myvar(T v){ _v=v;}
 template<typename T> myvar& operator=(T v){ _v=v;return *this;}
 friend ostream &operator <<(ostream &o,myvar v);
 bool operator!=(myvar& _r){ return _v.content==_r._v.content;}
 bool is_char() { return is(char);}
 bool is_int() { return is(int);}
 bool is_long() { return is(long);}
 bool is_double() { return is(double);}
 bool is_float() { return is(float);}
 bool is_char_ptr()
 {
  try
  {
   any_cast<const char *>(_v);
   return true;
  }
  catch(const boost::bad_any_cast &)
  {
   return false;
  }
 }
 bool is_string()
 {
  return bool(any_cast<string>(&_v)); 
 }
 
 string tostring()
 {
  string a;
  a=lexical_cast<string>(*this);
  return a;
 }
};
ostream &operator <<(ostream &o,myvar v)
{
 if(v._v.type()==typeid(char))
  o<<any_cast<char>(v._v);
 else if(v._v.type()==typeid(int))
  o<<any_cast<int>(v._v);
 else if(v._v.type()==typeid(long))
  o<<any_cast<long>(v._v);
 else if(v._v.type()==typeid(double))
  o<<any_cast<double>(v._v);
 else if(v._v.type()==typeid(float))
  o<<any_cast<float>(v._v);
 else if(v._v.type()==typeid(const char*))
  o<<any_cast<const char*>(v._v);
 else if(v._v.type()==typeid(string))
  o<<any_cast<string>(v._v);
 else if(v._v.type()==typeid(myvar))
 {
  if(v!=any_cast<myvar>(v._v))
   o<<any_cast<myvar>(v._v);
  else
   o<<"(a value reference self)";
 }
 else
  o<<"(undefine type)";
 return o;
}
template<typename ValueType>
bool myvar_castout(myvar & v)
{
 try
 {
  cout<<lexical_cast<ValueType>(v)<<"\t";
  return true;
 }
 catch(const boost::bad_lexical_cast &)
 {
  cout<<"bad"<<"\t";
  return false;
 }
}
int main(int argc, char* argv[])
{
 vector<myvar> t;
 t.push_back('0');
 t.push_back(1);
 t.push_back(2L);
 t.push_back(3.0);
 t.push_back("4");
 t.push_back(string("5"));
 t.push_back(myvar("6"));
 myvar temp("7");
 t.push_back(temp);
 for(vector<myvar>::iterator it=t.begin();it<t.end();it++)  
 {
  cout<<*it<<"\t";
  
  myvar_castout<int>(*it);
  myvar_castout<char>(*it);
  myvar_castout<long>(*it);
  myvar_castout<float>(*it);
  myvar_castout<double>(*it);
  myvar_castout<char*>(*it);
  myvar_castout<string>(*it);
  cout<<endl;
 }
 cin.get();
 return 0;
}

/// @}  文件注释完毕
关于空指针的讨论:【上一篇】
智能指针二(代码):【下一篇】
【相关文章】
  • 我学boost(2) array
  • Boost 库2005下编译安装手记
  • boost::singleton : 简单,高效,线程安全的singleton模式实现
  • jiqingdianying
  • boost timer类介绍
  • boost lambda简介
  • boost pool 内存池
  • boost condition
  • VC利用boost库解析正则表达式
  • boost的安装以及它的regex的配置
  • 【随机文章】
  • 基于Linux的视频点播系统的实现
  • 禁止master扩展存储过程xp_cmdshell命令
  • SP2中文正式版安装设置图文指南
  • Scott Mitchell的ASP.NET2.0数据教程中文版索引
  • lockfile
  • 色友必看 如何移除数码照片的紫边
  • 韦伯论魅力型的统治
  • csdn的blog为什么多出来一个%20
  • Programming in Lua (第一章---起步)
  • 千兆以太网基础教程
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.