点击(此处)折叠或打开
-
/*
-
* anyout.cpp
-
*
-
* Created on: 2013-7-15
-
* Author: root
-
*/
-
-
#include <iostream>
-
#include <vector>
-
#include <string>
-
#include <ostream>
-
-
#include "boost/any.hpp"
-
-
#include <list>
-
-
struct streamer
-
{
-
virtual void print(std::ostream& o, boost::any& a)=0;
-
virtual streamer* clone()=0;
-
virtual ~streamer(){}
-
};
-
-
template <typename T> struct streamer_imp : public streamer
-
{
-
virtual void print(std::ostream& o, boost::any& a){
-
o << *boost::any_cast<T>(&a);
-
}
-
virtual streamer* clone() {
-
return new streamer_imp<T>();
-
}
-
};
-
class any_out
-
{
-
streamer* streamer_;
-
boost::any o_;
-
-
public:
-
any_out() : streamer_(0) { }
-
-
template <typename T> any_out(const T& value)
-
: streamer_(new streamer_imp<T>),o_(value)
-
{
-
-
}
-
-
any_out(const any_out& a)
-
: streamer_(a.streamer_ ? a.streamer_->clone():0),o_(a.o_)
-
{
-
-
}
-
-
template<typename T> any_out& operator=(const T& r) {
-
any_out(r).swap(*this);
-
return *this;
-
}
-
-
any_out& operator=(const any_out& r) {
-
any_out(r).swap(*this);
-
return *this;
-
}
-
-
~any_out() { delete streamer_; }
-
-
any_out& swap(any_out& r)
-
{
-
std::swap(streamer_, r.streamer_);
-
std::swap(o_,r.o_);
-
return *this;
-
}
-
-
friend std::ostream& operator<<(std::ostream& o, any_out& a) {
-
if(a.streamer_) {
-
a.streamer_->print(o,a.o_);
-
}
-
return o;
-
}
-
-
const std::type_info& getType()
-
{
-
return o_.type();
-
}
-
};
-
bool is_int(const boost::any& a)
-
{
-
return (typeid(int) == a.type());
-
}
-
//判定类型模板
-
template<typename T>
-
bool contains(const boost::any& a)
-
{
-
return (typeid(T) == a.type());
-
}
-
-
//判定类型模板any_out
-
template<typename T>
-
bool contains_anyOut( any_out& a)
-
{
-
return (typeid(T) == a.getType() );
-
}
-
-
//统计空元素数量
-
class any_counter
-
{
-
int count_;
-
public:
-
any_counter() : count_(0) {}
-
-
int operator() (const boost::any& a){
-
return a.empty() ? count_ : ++count_;
-
}
-
-
int count() const{ return count_; }
-
};
-
-
template <typename OutIt,typename Type>
-
class extrator
-
{
-
OutIt it_;
-
public:
-
extrator(OutIt it) : it_(it) { }
-
-
void operator() (boost::any& a)
-
{
-
Type* t(boost::any_cast<Type>(&a));
-
if(t){
-
*it_++ = *t;
-
}
-
}
-
-
};
-
-
template <typename Type, typename OutIt>
-
extrator<OutIt,Type> make_extrator(OutIt it){
-
return extrator<OutIt,Type>(it);
-
}
-
-
-
/*int main()
-
{
-
std::vector<any_out> vec;
-
-
any_out a(std::string("I do have operator<<"));
-
-
vec.push_back(a);
-
vec.push_back(112);
-
vec.push_back(65.535);
-
-
//
-
std::cout << vec[0] << "\n";
-
std::cout << vec[1] << "\n";
-
std::cout << vec[2] << "\n";
-
a = std::string("This is great!");
-
std::cout << a << "\n";
-
-
if( contains_anyOut<double>(vec[2]) )
-
{
-
std::cout << "have a double "<< vec[2] << " \n";
-
}
-
vec.clear();
-
-
std::vector<boost::any> vecany;
-
// vec.push_back(boost::any());
-
for(int i = 0; i < 10; ++i)
-
{
-
vecany.push_back(i);
-
}
-
// vec.push_back(boost::any());
-
-
// int ui = std::for_each(vec.begin(),vec.end(),any_counter.count());
-
// std::cout << "There are " << ui << "non empty any's in vec \n\n";
-
-
// std::list<int> lst;
-
// std::for_each(vec.begin(),vec.end(),make_extrator<int>(std::back_inserter(lst)) );
-
-
int testType = 2;
-
if( is_int(testType) )
-
{
-
std::cout << "int have a int " << " \n";
-
}
-
if( contains<int>(vecany[2]) )
-
{
-
std::cout << "have a int \n";
-
}
-
-
// std::cout << "found lst size " << lst.size() << "ints in vec \n";
-
}
- */