3
* \brief Sample program presenting the use of the claw::memory namespace.
6
#include <claw/smart_ptr.hpp>
12
/*----------------------------------------------------------------------------*/
14
* \brief Simple class, containing a string message. To illustrate the use of
20
message( const std::string& msg )
26
std::cout << "deleting " << m_msg << std::endl;
29
const std::string& get_message() const { return m_msg; }
36
/*----------------------------------------------------------------------------*/
38
* \brief Chained structure to show the problems occuring with the use of
43
claw::memory::smart_ptr<item> next;
46
/*----------------------------------------------------------------------------*/
48
* \brief Print a \a message stored in a smart_ptr. The parameter is passed by
51
void print_copy( claw::memory::smart_ptr<message> p )
53
std::cout << "print_copy" << std::endl;
54
std::cout << " (->) " << p->get_message() << std::endl;
55
std::cout << " (*) " << (*p).get_message() << std::endl;
58
/*----------------------------------------------------------------------------*/
60
* \brief Print a \a message stored in a smart_ptr. The parameter is passed by
63
void print_ref( claw::memory::smart_ptr<message>& p )
65
std::cout << "print_ref" << std::endl;
66
std::cout << " (->) " << p->get_message() << std::endl;
67
std::cout << " (*) " << (*p).get_message() << std::endl;
70
/*----------------------------------------------------------------------------*/
72
* \brief Print a \a message stored in a smart_ptr. The parameter is passed by
73
* reference (to test the constant methods of smart_ptr).
75
void print_const( const claw::memory::smart_ptr<message>& p )
77
std::cout << "print_const" << std::endl;
78
std::cout << " (->) " << p->get_message() << std::endl;
79
std::cout << " (*) " << (*p).get_message() << std::endl;
82
/*----------------------------------------------------------------------------*/
84
* \brief Create some smart_ptr, call print_*() methods and do some operations.
88
std::cout << "----------------- basic_test" << std::endl;
90
claw::memory::smart_ptr<message> p = new message( "basic_1" );
91
claw::memory::smart_ptr<message> q = new message( "basic_2" );
92
claw::memory::smart_ptr<message> p2(p);
93
claw::memory::smart_ptr<message> q2;
94
claw::memory::smart_ptr<message> r( NULL );
100
std::cout << "instruction: p = p = p" << std::endl;
103
std::cout << "instruction: q2 = q" << std::endl;
107
/*----------------------------------------------------------------------------*/
109
* \brief Use a smart_ptr with a class from the STL.
113
std::cout << "----------------- stl_test" << std::endl;
114
std::list< claw::memory::smart_ptr<message> > list;
115
std::list< claw::memory::smart_ptr<message> >::const_iterator it;
117
for ( unsigned int i=0; i!=10; ++i )
119
std::ostringstream oss;
122
list.push_back( new message(oss.str()) );
125
for ( it=list.begin(); it!=list.end(); ++it )
126
std::cout << (*it)->get_message();
128
std::cout << std::endl;
131
/*----------------------------------------------------------------------------*/
133
* \brief Example of the problems occuring with the use of references count.
137
std::cout << "----------------- loop_test" << std::endl;
138
claw::memory::smart_ptr<item> i1 = new item;
139
claw::memory::smart_ptr<item> i2 = new item;
145
/*----------------------------------------------------------------------------*/
147
* \brief Main program. Call test methods.