Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleSharedPtr.cpp

Example for SharedPointer usage

Author
woelk
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
@example ExampleSharedPtr.cpp
@relates SharedPtr
@brief Example for SharedPointer usage
@ingroup g_examples
@author woelk
*/
#include <Base/Common/SharedPtr.hh>
#include <iostream>
#include <vector>
#include <string>
using namespace BIAS;
using namespace std;
/** \cond POORLY_NAMED_EXAMPLE_CLASSES **/
/* some base class */
class foo {
public:
virtual ~foo() {};
int i;
double d;
};
std::ostream& operator<<(std::ostream& os, const foo& arg)
{ os << arg.i<<" "<<arg.d; return os; }
/// some derive class
class uta : public foo {
public:
virtual ~uta() {};
string name;
};
std::ostream& operator<<(std::ostream& os, const uta& arg)
{ os << arg.i<<" "<<arg.d<<" "<<arg.name; return os; }
/** \endcond **/
int main(int argc, char *argv[])
{
/// initialize the shared ptr
SharedPtr<foo> p_foo = SharedPtr<foo>(new foo);
/// check the reference count
cout << "reference count of p_foo: "<<RefCount(p_foo)<<endl;
// assign values
p_foo->i = 5;
(*p_foo).d = 3.1415;
cout << "content of p_foo: "<<*p_foo<<endl;
/// copy the pointer
SharedPtr<foo> p_foo2 = p_foo;
// thest for NULL
if (p_foo2){
cout << "simple test for NULL pointer : (p_foo2) \n"
<< "content of p_foo2: "<<*p_foo2<<endl;
}
/// the reference count should be 2: p_foo and p_foo2 both point to the
/// same object
// comparison
if (p_foo == p_foo2)
cout << "p_foo and p_foo2 point ot the same object: reference count "
<<RefCount(p_foo)<<" and "<<RefCount(p_foo2)<<endl;
else
cout << "this cannot happen\n";
// assigning NULL to p_foo2 let the reference count of p_foo drop
p_foo2 = SharedPtr<foo>(NULL);
cout << "re-assigned p_foo2: reference count "<<RefCount(p_foo)<<endl;
// a pointer to a const foo
/// casting works slighty different
pc_foo = ConstCast<const foo>(p_foo);
SharedPtr<uta> p_uta(new uta);
p_uta->i=2;
p_uta->d=2.0*3.1415;
p_uta->name="diogenes";
/// dynamic casting works slighty different
p_foo2 = DynamicCast<foo>(p_uta);
if (p_foo2 == p_uta)
cout << "p_uta and p_foo2 point to the same object: ref count "
<<RefCount(p_uta)<<endl
<< " content of p_foo2: "<<*p_foo2<<endl;
else
cerr << "this will not happen\n";
SharedPtr<uta> p_uta2 = DynamicCast<uta>(p_foo2);
if (p_uta2)
cout << "*p_uta2: "<<*p_uta2<<endl;
// no call to delete necessary, the object is automatically deleted when
// the reference count drops to zero
return 0;
}