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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
/**
* \file main.cpp
* \brief Sample program presenting the use of the claw::memory namespace.
* \author Julien Jorge
*/
#include <claw/smart_ptr.hpp>
#include <string>
#include <sstream>
#include <iostream>
#include <list>
/*----------------------------------------------------------------------------*/
/**
* \brief Simple class, containing a string message. To illustrate the use of
* claw::memory.
*/
class message
{
public:
message( const std::string& msg )
: m_msg(msg)
{ }
~message()
{
std::cout << "deleting " << m_msg << std::endl;
}
const std::string& get_message() const { return m_msg; }
private:
std::string m_msg;
}; // class message
/*----------------------------------------------------------------------------*/
/**
* \brief Chained structure to show the problems occuring with the use of
* references count.
*/
struct item
{
claw::memory::smart_ptr<item> next;
}; // struct item
/*----------------------------------------------------------------------------*/
/**
* \brief Print a \a message stored in a smart_ptr. The parameter is passed by
* copy.
*/
void print_copy( claw::memory::smart_ptr<message> p )
{
std::cout << "print_copy" << std::endl;
std::cout << " (->) " << p->get_message() << std::endl;
std::cout << " (*) " << (*p).get_message() << std::endl;
} // print_copy()
/*----------------------------------------------------------------------------*/
/**
* \brief Print a \a message stored in a smart_ptr. The parameter is passed by
* reference.
*/
void print_ref( claw::memory::smart_ptr<message>& p )
{
std::cout << "print_ref" << std::endl;
std::cout << " (->) " << p->get_message() << std::endl;
std::cout << " (*) " << (*p).get_message() << std::endl;
} // print_ref()
/*----------------------------------------------------------------------------*/
/**
* \brief Print a \a message stored in a smart_ptr. The parameter is passed by
* reference (to test the constant methods of smart_ptr).
*/
void print_const( const claw::memory::smart_ptr<message>& p )
{
std::cout << "print_const" << std::endl;
std::cout << " (->) " << p->get_message() << std::endl;
std::cout << " (*) " << (*p).get_message() << std::endl;
} // print_const()
/*----------------------------------------------------------------------------*/
/**
* \brief Create some smart_ptr, call print_*() methods and do some operations.
*/
void basic_test()
{
std::cout << "----------------- basic_test" << std::endl;
claw::memory::smart_ptr<message> p = new message( "basic_1" );
claw::memory::smart_ptr<message> q = new message( "basic_2" );
claw::memory::smart_ptr<message> p2(p);
claw::memory::smart_ptr<message> q2;
claw::memory::smart_ptr<message> r( NULL );
print_copy(p);
print_ref(p);
print_const(p);
std::cout << "instruction: p = p = p" << std::endl;
p = p = p;
std::cout << "instruction: q2 = q" << std::endl;
q2 = q;
} // basic_test()
/*----------------------------------------------------------------------------*/
/**
* \brief Use a smart_ptr with a class from the STL.
*/
void stl_test()
{
std::cout << "----------------- stl_test" << std::endl;
std::list< claw::memory::smart_ptr<message> > list;
std::list< claw::memory::smart_ptr<message> >::const_iterator it;
for ( unsigned int i=0; i!=10; ++i )
{
std::ostringstream oss;
oss << i;
list.push_back( new message(oss.str()) );
}
for ( it=list.begin(); it!=list.end(); ++it )
std::cout << (*it)->get_message();
std::cout << std::endl;
} // stl_test()
/*----------------------------------------------------------------------------*/
/**
* \brief Example of the problems occuring with the use of references count.
*/
void loop_test()
{
std::cout << "----------------- loop_test" << std::endl;
claw::memory::smart_ptr<item> i1 = new item;
claw::memory::smart_ptr<item> i2 = new item;
i2->next = i1;
i1->next = i2;
} // test_boucle()
/*----------------------------------------------------------------------------*/
/**
* \brief Main program. Call test methods.
*/
int main()
{
basic_test();
stl_test();
loop_test();
return 0;
} // main()
|