Adapted this code from here:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <list> | |
#include <boost/any.hpp> | |
#include <iostream> | |
using boost::any_cast; | |
typedef std::list<boost::any> many; | |
void append_int(many & values, int value) | |
{ | |
boost::any to_append = value; | |
values.push_back(to_append); | |
} | |
void append_string(many & values, const std::string & value) | |
{ | |
values.push_back(value); | |
} | |
void append_char_ptr(many & values, const char * value) | |
{ | |
values.push_back(value); | |
} | |
void append_any(many & values, const boost::any & value) | |
{ | |
values.push_back(value); | |
} | |
void append_nothing(many & values) | |
{ | |
values.push_back(boost::any()); | |
} | |
bool is_empty(const boost::any & operand) | |
{ | |
return operand.empty(); | |
} | |
bool is_int(const boost::any & operand) | |
{ | |
return operand.type() == typeid(int); | |
} | |
bool is_char_ptr(const boost::any & operand) | |
{ | |
try | |
{ | |
any_cast<const char *>(operand); | |
return true; | |
} | |
catch(const boost::bad_any_cast &) | |
{ | |
return false; | |
} | |
} | |
bool is_string(const boost::any & operand) | |
{ | |
return any_cast<std::string>(&operand); | |
} | |
void count_all(many & values, std::ostream & out) | |
{ | |
out << "#empty == " | |
<< std::count_if(values.begin(), values.end(), is_empty) << std::endl; | |
out << "#int == " | |
<< std::count_if(values.begin(), values.end(), is_int) << std::endl; | |
out << "#const char * == " | |
<< std::count_if(values.begin(), values.end(), is_char_ptr) << std::endl; | |
out << "#string == " | |
<< std::count_if(values.begin(), values.end(), is_string) << std::endl; | |
} | |
void print_any(boost::any const &operand) | |
{ | |
if (is_empty(operand)) | |
{ | |
std::cout << "(empty)"; | |
} | |
else if (is_int(operand)) | |
{ | |
std::cout << boost::any_cast<int>(operand); | |
} | |
else if (is_char_ptr(operand)) | |
{ | |
std::cout << boost::any_cast<char const*>(operand); | |
} | |
else if (is_string(operand)) | |
{ | |
std::cout << boost::any_cast<std::string>(operand); | |
} | |
} | |
int main() | |
{ | |
boost::any object = "there"; | |
many random_objects; | |
append_int(random_objects, 1); | |
append_char_ptr(random_objects, "hello"); | |
append_nothing(random_objects); | |
append_any(random_objects, object); | |
append_string(random_objects, "world"); | |
append_int(random_objects, 6); | |
count_all(random_objects, std::cout); | |
for (auto const i: random_objects) | |
{ | |
print_any(i); | |
std::cout << " "; | |
} | |
std::cout << std::endl; | |
return 0; | |
} |
Here's the sample output:
nonbonding@castor-ub:~/dnonbonding/tmp/any$ g++ -std=c++11 -I/home/nonbonding/dnonbonding/bin/boost_1_60_0 c.cpp
nonbonding@castor-ub:~/dnonbonding/tmp/any$ ./a.out
#empty == 1
#int == 2
#const char * == 2
#string == 1
1 hello (empty) there world 6
nonbonding@castor-ub:~/dnonbonding/tmp/any$
No comments:
Post a Comment