forked from caiorss/C-Cpp-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboost-pointer-container.cpp
135 lines (103 loc) · 3.38 KB
/
boost-pointer-container.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Author: Caio Rodrigues
// Brief: Demonstration and testing of boost pointer container.
//---------------------------------------------------------------------
#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <boost/ptr_container/ptr_vector.hpp>
class Base{
public:
static auto nextID() -> int {
static int i = 0;
return ++i;
}
Base() = default;
// Destructor of base class must always be virtual
virtual ~Base() = default;
virtual auto getID() const -> int = 0;
virtual auto getType() const -> std::string = 0;
};
class DerivedA: public Base{
public:
const int m_id;
DerivedA(): m_id(Base::nextID()) { }
auto getType() const -> std::string {
return "DerivedA";
}
auto getID() const -> int {
return m_id;
}
~DerivedA(){
std::cout << " [INFO] Class DerivedA deleted. => Object ID = "
<< m_id << "\n";
}
};
class DerivedB: public Base{
const int m_id;
public:
DerivedB(): m_id(Base::nextID()) { }
auto getType() const -> std::string {
return "DerivedB";
}
auto getID() const -> int {
return m_id;
}
~DerivedB(){
std::cout << " [INFO] Class DerivedB deleted. => ObjectID = "
<< m_id << "\n";
}
};
void showType(Base const& obj)
{
std::cout << "Object ID = " << obj.getID()
<< " Class type = " << obj.getType()
<< "\n";
}
int main(){
std::cout << "\n === EXPERIMENT 0 ==============================" << "\n";
std::vector<DerivedA> xsa;
xsa.push_back(DerivedA());
xsa.push_back(DerivedA());
xsa.emplace_back();
std::cout << "Run std::for_each" << "\n";
std::for_each(xsa.begin(), xsa.end(), showType);
std::cout << "\n === EXPERIMENT 1 ==============================" << "\n";
std::vector<std::shared_ptr<Base>> xs;
xs.push_back(std::make_shared<DerivedA>());
xs.push_back(std::make_shared<DerivedB>());
xs.push_back(std::make_shared<DerivedA>());
xs.push_back(std::make_shared<DerivedB>());
std::cout << " <<INFO>> xs[0] type " << xs[0]->getType()
<< " ; id = " << xs[0]->getID() << "\n";
std::cout << " <<INFO>> xs[2] type " << xs[2]->getType()
<< " ; id = " << xs[2]->getID() << "\n";
std::for_each(xs.begin(), xs.end(),
[](auto const& pBase){
showType(*pBase);
});
std::vector<int> identifiers1;
std::transform(xs.begin(), xs.end(),
std::back_inserter(identifiers1),
[](auto pBase){ return pBase->getID(); });
std::cout << "\n === EXPERIMENT 2 ==============================" << "\n";
boost::ptr_vector<Base> ps;
ps.push_back(new DerivedA);
ps.push_back(new DerivedB());
ps.push_back(new DerivedA());
ps.push_back(new DerivedB);
std::cout << " <<INFO>> ps[0] type " << ps[0].getType() << " ; id = " << ps[0].getID() << "\n";
std::cout << " <<INFO>> ps[1] type " << ps[1].getType() << " ; id = " << ps[1].getID() << "\n";
std::cout << " <<INFO>> ps[2] type " << ps[2].getType() << " ; id = " << ps[2].getID() << "\n";
std::vector<int> identifiers2;
std::transform(xs.begin(), xs.end(),
std::back_inserter(identifiers2),
std::bind(&Base::getID, std::placeholders::_1));
std::cout << "\n ==> Show objects before deleting last item " << "\n";
std::for_each(ps.begin(), ps.end(), showType);
std::cout << "\n ==> Show objects after deleting last item " << "\n";
ps.pop_back();
std::for_each(ps.begin(), ps.end(), showType);
std::cout << " ============= END =================" << "\n";
return 0;
}