Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleSharedPtr.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 /**
26  @example ExampleSharedPtr.cpp
27  @relates SharedPtr
28  @brief Example for SharedPointer usage
29  @ingroup g_examples
30  @author woelk
31 */
32 
33 #include <Base/Common/SharedPtr.hh>
34 
35 #include <iostream>
36 #include <vector>
37 #include <string>
38 
39 using namespace BIAS;
40 using namespace std;
41 
42 /** \cond POORLY_NAMED_EXAMPLE_CLASSES **/
43 /* some base class */
44 class foo {
45 public:
46  virtual ~foo() {};
47  int i;
48  double d;
49 };
50 
51 std::ostream& operator<<(std::ostream& os, const foo& arg)
52 { os << arg.i<<" "<<arg.d; return os; }
53 
54 /// some derive class
55 class uta : public foo {
56 public:
57  virtual ~uta() {};
58  string name;
59 };
60 
61 std::ostream& operator<<(std::ostream& os, const uta& arg)
62 { os << arg.i<<" "<<arg.d<<" "<<arg.name; return os; }
63 
64 /** \endcond **/
65 
66 int main(int argc, char *argv[])
67 {
68  /// initialize the shared ptr
69  SharedPtr<foo> p_foo = SharedPtr<foo>(new foo);
70 
71  /// check the reference count
72  cout << "reference count of p_foo: "<<RefCount(p_foo)<<endl;
73 
74  // assign values
75  p_foo->i = 5;
76  (*p_foo).d = 3.1415;
77 
78  cout << "content of p_foo: "<<*p_foo<<endl;
79 
80  /// copy the pointer
81  SharedPtr<foo> p_foo2 = p_foo;
82  // thest for NULL
83  if (p_foo2){
84  cout << "simple test for NULL pointer : (p_foo2) \n"
85  << "content of p_foo2: "<<*p_foo2<<endl;
86  }
87 
88  /// the reference count should be 2: p_foo and p_foo2 both point to the
89  /// same object
90  // comparison
91  if (p_foo == p_foo2)
92  cout << "p_foo and p_foo2 point ot the same object: reference count "
93  <<RefCount(p_foo)<<" and "<<RefCount(p_foo2)<<endl;
94  else
95  cout << "this cannot happen\n";
96 
97  // assigning NULL to p_foo2 let the reference count of p_foo drop
98  p_foo2 = SharedPtr<foo>(NULL);
99  cout << "re-assigned p_foo2: reference count "<<RefCount(p_foo)<<endl;
100 
101  // a pointer to a const foo
102  SharedPtr<const foo> pc_foo;
103 
104  /// casting works slighty different
105  pc_foo = ConstCast<const foo>(p_foo);
106 
107  SharedPtr<uta> p_uta(new uta);
108  p_uta->i=2;
109  p_uta->d=2.0*3.1415;
110  p_uta->name="diogenes";
111 
112  /// dynamic casting works slighty different
113  p_foo2 = DynamicCast<foo>(p_uta);
114  if (p_foo2 == p_uta)
115  cout << "p_uta and p_foo2 point to the same object: ref count "
116  <<RefCount(p_uta)<<endl
117  << " content of p_foo2: "<<*p_foo2<<endl;
118  else
119  cerr << "this will not happen\n";
120 
121  SharedPtr<uta> p_uta2 = DynamicCast<uta>(p_foo2);
122  if (p_uta2)
123  cout << "*p_uta2: "<<*p_uta2<<endl;
124 
125  // no call to delete necessary, the object is automatically deleted when
126  // the reference count drops to zero
127  return 0;
128 }
pointer with reference count and automatic deletion
Definition: SharedPtr.hh:50
unsigned RefCount(const SharedPtr< T > &t)
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260