Chinaunix首页 | 论坛 | 博客
  • 博客访问: 855316
  • 博文数量: 254
  • 博客积分: 5350
  • 博客等级: 大校
  • 技术积分: 2045
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-27 13:27
文章分类

全部博文(254)

文章存档

2015年(1)

2014年(9)

2013年(17)

2012年(30)

2011年(150)

2010年(17)

2009年(28)

2008年(2)

分类: LINUX

2013-07-24 11:44:22


点击(此处)折叠或打开

  1. /*
  2.  * anyout.cpp
  3.  *
  4.  * Created on: 2013-7-15
  5.  * Author: root
  6.  */

  7. #include <iostream>
  8. #include <vector>
  9. #include <string>
  10. #include <ostream>

  11. #include "boost/any.hpp"

  12. #include <list>

  13. struct streamer
  14. {
  15.     virtual void print(std::ostream& o, boost::any& a)=0;
  16.     virtual streamer* clone()=0;
  17.     virtual ~streamer(){}
  18. };

  19. template <typename T> struct streamer_imp : public streamer
  20. {
  21.     virtual void print(std::ostream& o, boost::any& a){
  22.         o << *boost::any_cast<T>(&a);
  23.     }
  24.     virtual streamer* clone() {
  25.         return new streamer_imp<T>();
  26.     }
  27. };
  28. class any_out
  29. {
  30. streamer* streamer_;
  31. boost::any o_;

  32. public:
  33. any_out() : streamer_(0) { }

  34. template <typename T> any_out(const T& value)
  35.         : streamer_(new streamer_imp<T>),o_(value)
  36.  {

  37.  }

  38.  any_out(const any_out& a)
  39.  : streamer_(a.streamer_ ? a.streamer_->clone():0),o_(a.o_)
  40.  {

  41.  }

  42.  template<typename T> any_out& operator=(const T& r) {
  43.     any_out(r).swap(*this);
  44.     return *this;
  45.  }

  46.    any_out& operator=(const any_out& r) {
  47.     any_out(r).swap(*this);
  48.     return *this;
  49.  }

  50.  ~any_out() { delete streamer_; }

  51.  any_out& swap(any_out& r)
  52.  {
  53.     std::swap(streamer_, r.streamer_);
  54.     std::swap(o_,r.o_);
  55.     return *this;
  56.  }

  57.  friend std::ostream& operator<<(std::ostream& o, any_out& a) {
  58.     if(a.streamer_) {
  59.         a.streamer_->print(o,a.o_);
  60.     }
  61.     return o;
  62.  }

  63.  const std::type_info& getType()
  64.  {
  65.      return o_.type();
  66.  }
  67. };
  68. bool is_int(const boost::any& a)
  69. {
  70.      return (typeid(int) == a.type());
  71. }
  72. //判定类型模板
  73.  template<typename T>
  74.  bool contains(const boost::any& a)
  75.  {
  76.      return (typeid(T) == a.type());
  77.  }

  78.  //判定类型模板any_out
  79.   template<typename T>
  80.   bool contains_anyOut( any_out& a)
  81.   {
  82.       return (typeid(T) == a.getType() );
  83.   }

  84. //统计空元素数量
  85. class any_counter
  86. {
  87. int count_;
  88. public:
  89.     any_counter() : count_(0) {}

  90. int operator() (const boost::any& a){
  91.     return a.empty() ? count_ : ++count_;
  92. }

  93. int count() const{ return count_; }
  94. };

  95. template <typename OutIt,typename Type>
  96. class extrator
  97. {
  98. OutIt it_;
  99. public:
  100.  extrator(OutIt it) : it_(it) { }

  101.  void operator() (boost::any& a)
  102.  {
  103.     Type* t(boost::any_cast<Type>(&a));
  104.     if(t){
  105.         *it_++ = *t;
  106.     }
  107.  }

  108. };

  109. template <typename Type, typename OutIt>
  110.     extrator<OutIt,Type> make_extrator(OutIt it){
  111.         return extrator<OutIt,Type>(it);
  112.     }


  113. /*int main()
  114. {
  115.     std::vector<any_out> vec;

  116.     any_out a(std::string("I do have operator<<"));

  117.     vec.push_back(a);
  118.     vec.push_back(112);
  119.     vec.push_back(65.535);

  120.     //
  121.     std::cout << vec[0] << "\n";
  122.     std::cout << vec[1] << "\n";
  123.     std::cout << vec[2] << "\n";
  124.     a = std::string("This is great!");
  125.     std::cout << a << "\n";

  126.     if( contains_anyOut<double>(vec[2]) )
  127.     {
  128.         std::cout << "have a double "<< vec[2] << " \n";
  129.     }
  130.     vec.clear();

  131.     std::vector<boost::any> vecany;
  132. //    vec.push_back(boost::any());
  133.     for(int i = 0; i < 10; ++i)
  134.     {
  135.         vecany.push_back(i);
  136.     }
  137. //    vec.push_back(boost::any());

  138. //    int ui = std::for_each(vec.begin(),vec.end(),any_counter.count());
  139. //    std::cout << "There are " << ui << "non empty any's in vec \n\n";

  140. //    std::list<int> lst;
  141. //    std::for_each(vec.begin(),vec.end(),make_extrator<int>(std::back_inserter(lst)) );

  142.     int testType = 2;
  143.     if( is_int(testType) )
  144.     {
  145.         std::cout << "int have a int " << " \n";
  146.     }
  147.     if( contains<int>(vecany[2]) )
  148.     {
  149.         std::cout << "have a int \n";
  150.     }

  151. //    std::cout << "found lst size " << lst.size() << "ints in vec \n";
  152. }
  153. */

阅读(1010) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~