Ray's playground

 

Boost.Any

Anything Goes
 1 #include <iostream>
 2 #include <string>
 3 #include <utility>
 4 #include <vector>
 5 #include "boost/any.hpp"
 6 using namespace std;
 7 
 8 class A{
 9 public:
10     void some_function(){
11         cout << "A::some_function()" << endl;
12     }
13 };
14 
15 class B{
16 public:
17     void some_function(){
18         cout << "B::some_function()" << endl;
19     }
20 };
21 
22 class C{
23 public:
24     void some_function(){
25         cout << "C::some_function()" << endl;
26     }
27 };
28 
29 void print_any(boost::any& a){
30     if(A* pA = boost::any_cast<A>(&a))
31     {
32         pA->some_function();
33     }
34     else if(B* pB = boost::any_cast<B>(&a))
35     {
36         pB->some_function();
37     }
38     else if(C* pC = boost::any_cast<C>(&a))
39     {
40         pC->some_function();
41     }
42 }
43 
44 int main(){
45     cout << "Example of using any" << endl;
46     vector<boost::any> store_anything;
47 
48     store_anything.push_back(A());
49     store_anything.push_back(B());
50     store_anything.push_back(C());
51 
52     store_anything.push_back(string("This is great!"));
53     store_anything.push_back(3);
54     store_anything.push_back(make_pair(true7.92));
55 
56     void print_any(boost::any& a);
57 
58     for_each(store_anything.begin(), store_anything.end(), print_any);
59 
60     cin.get();
61 }

 

 

shared_ptr
 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <vector>
 5 #include "boost\any.hpp"
 6 #include "boost\shared_ptr.hpp"
 7 using namespace std;
 8 
 9 class A{
10 public:
11     virtual ~A(){
12         cout << "A::~A()" << endl;
13     }
14 
15     void not_virtual()
16     {
17         cout << "A::not_virtual()" << endl;
18     }
19 
20     virtual void is_virtual(){
21         cout << "A::is_virtual()" << endl;
22     }
23 };
24 
25 class B : public A{
26 public:
27 
28     void not_virtual()
29     {
30         cout << "B::not_virtual()" << endl;
31     }
32 
33     virtual void is_virtual(){
34         cout << "B::is_virtual()" << endl;
35     }
36 };
37 
38 void foo(boost::any& a)
39 {
40     try{
41         boost::shared_ptr<A> ptr = boost::any_cast<boost::shared_ptr<A> >(a);
42         cout << "This any contained a boost::shared_ptr<A>" << endl;
43 
44         ptr->is_virtual();
45         ptr->not_virtual();
46         return;
47     }
48     catch(boost::bad_any_cast& e){}
49 
50     try{
51         boost::shared_ptr<B> ptr = boost::any_cast<boost::shared_ptr<B> >(a);
52         cout << "This any contained a boost::shared_ptr<B>" << endl;
53 
54         ptr->is_virtual();
55         ptr->not_virtual();
56         return;
57     }
58     catch(boost::bad_any_cast& e){}
59 
60     cout << "The any didn't contain anything that concerns this function!" << endl;
61 }
62 
63 int main()
64 {
65     cout << "Example of any and shared_ptr" << endl;
66     boost::any a1(boost::shared_ptr<A>(new A));
67     boost::any a2(string("Just a string"));
68 
69     {
70         boost::any b1(boost::shared_ptr<A>(new B));
71         boost::any b2(boost::shared_ptr<B>(new B));
72         std::vector<boost::any> vec;
73         vec.push_back(a1);
74         vec.push_back(a2);
75         vec.push_back(b1);
76         vec.push_back(b2);
77 
78         for_each(vec.begin(),vec.end(),foo);
79         cout << "\n";
80     }
81 
82     cout << "any's b1 and b2 have been destroyed which means that the shared_ptrs' reference counts became zero\n";
83 
84     cin.get();
85 }

 

 

posted on 2010-08-01 13:54  Ray Z  阅读(868)  评论(0编辑  收藏  举报

导航