~ubuntu-branches/ubuntu/oneiric/libclaw/oneiric

« back to all changes in this revision

Viewing changes to examples/memory/main.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Jorge
  • Date: 2008-05-17 15:36:57 UTC
  • Revision ID: james.westby@ubuntu.com-20080517153657-0b1204j754ykoz48
Tags: upstream-1.5.2b
ImportĀ upstreamĀ versionĀ 1.5.2b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * \file main.cpp
 
3
 * \brief Sample program presenting the use of the claw::memory namespace.
 
4
 * \author Julien Jorge
 
5
 */
 
6
#include <claw/smart_ptr.hpp>
 
7
#include <string>
 
8
#include <sstream>
 
9
#include <iostream>
 
10
#include <list>
 
11
 
 
12
/*----------------------------------------------------------------------------*/
 
13
/**
 
14
 * \brief Simple class, containing a string message. To illustrate the use of
 
15
 *        claw::memory.
 
16
 */
 
17
class message
 
18
{
 
19
public:
 
20
  message( const std::string& msg )
 
21
    : m_msg(msg)
 
22
  { }
 
23
 
 
24
  ~message()
 
25
  {
 
26
    std::cout << "deleting " << m_msg << std::endl;
 
27
  }
 
28
 
 
29
  const std::string& get_message() const { return m_msg; }
 
30
 
 
31
private:
 
32
  std::string m_msg;
 
33
 
 
34
}; // class message
 
35
 
 
36
/*----------------------------------------------------------------------------*/
 
37
/**
 
38
 * \brief Chained structure to show the problems occuring with the use of
 
39
 *        references count.
 
40
 */
 
41
struct item
 
42
{
 
43
  claw::memory::smart_ptr<item> next;
 
44
}; // struct item
 
45
 
 
46
/*----------------------------------------------------------------------------*/
 
47
/**
 
48
 * \brief Print a \a message stored in a smart_ptr. The parameter is passed by
 
49
 *        copy.
 
50
 */
 
51
void print_copy( claw::memory::smart_ptr<message> p )
 
52
{
 
53
  std::cout << "print_copy" << std::endl;
 
54
  std::cout << "  (->) " << p->get_message() << std::endl;
 
55
  std::cout << "  (*) " << (*p).get_message() << std::endl;
 
56
} // print_copy()
 
57
 
 
58
/*----------------------------------------------------------------------------*/
 
59
/**
 
60
 * \brief Print a \a message stored in a smart_ptr. The parameter is passed by
 
61
 *        reference.
 
62
 */
 
63
void print_ref( claw::memory::smart_ptr<message>& p )
 
64
{
 
65
  std::cout << "print_ref" << std::endl;
 
66
  std::cout << "  (->) " << p->get_message() << std::endl;
 
67
  std::cout << "  (*) " << (*p).get_message() << std::endl;
 
68
} // print_ref()
 
69
 
 
70
/*----------------------------------------------------------------------------*/
 
71
/**
 
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).
 
74
 */
 
75
void print_const( const claw::memory::smart_ptr<message>& p )
 
76
{
 
77
  std::cout << "print_const" << std::endl;
 
78
  std::cout << "  (->) " << p->get_message() << std::endl;
 
79
  std::cout << "  (*) " << (*p).get_message() << std::endl;
 
80
} // print_const()
 
81
 
 
82
/*----------------------------------------------------------------------------*/
 
83
/**
 
84
 * \brief Create some smart_ptr, call print_*() methods and do some operations.
 
85
 */
 
86
void basic_test()
 
87
{
 
88
  std::cout << "----------------- basic_test" << std::endl;
 
89
 
 
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 );
 
95
 
 
96
  print_copy(p);
 
97
  print_ref(p);
 
98
  print_const(p);
 
99
 
 
100
  std::cout << "instruction: p = p = p" << std::endl;
 
101
  p = p = p;
 
102
 
 
103
  std::cout << "instruction: q2 = q" << std::endl;
 
104
  q2 = q;
 
105
} // basic_test()
 
106
 
 
107
/*----------------------------------------------------------------------------*/
 
108
/**
 
109
 * \brief Use a smart_ptr with a class from the STL.
 
110
 */
 
111
void stl_test()
 
112
{
 
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;
 
116
 
 
117
  for ( unsigned int i=0; i!=10; ++i )
 
118
    {
 
119
      std::ostringstream oss;
 
120
      oss << i;
 
121
 
 
122
      list.push_back( new message(oss.str()) );
 
123
    }
 
124
 
 
125
  for ( it=list.begin(); it!=list.end(); ++it )
 
126
    std::cout << (*it)->get_message();
 
127
 
 
128
  std::cout << std::endl;
 
129
} // stl_test()
 
130
 
 
131
/*----------------------------------------------------------------------------*/
 
132
/**
 
133
 * \brief Example of the problems occuring with the use of references count.
 
134
 */
 
135
void loop_test()
 
136
{
 
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;
 
140
 
 
141
  i2->next = i1;
 
142
  i1->next = i2;
 
143
} // test_boucle()
 
144
 
 
145
/*----------------------------------------------------------------------------*/
 
146
/**
 
147
 * \brief Main program. Call test methods.
 
148
 */
 
149
int main()
 
150
{
 
151
  basic_test();
 
152
  stl_test();
 
153
  loop_test();
 
154
 
 
155
  return 0;
 
156
} // main()