Go to Overview over all GrAL packages.
Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

test/reference-count/test-refcount.C

Go to the documentation of this file.
00001 
00002 // $LICENSE
00003 
00004 
00005 #include "Utility/reference-count.h"
00006 
00007 #include <iostream.h>
00008 
00009 #include "Utility/pre-post-conditions.h"
00010 
00011 class testA {
00012 public:
00013   virtual testA* clone() const = 0;
00014   virtual ~testA() {}
00015   virtual void do_mut() = 0;
00016   virtual void do_const() const = 0;
00017 };
00018 
00019 class testD : public testA {
00020   int i;
00021 public:
00022   testD(int ii = 0) : i(ii) {}
00023   testD(testD const& rhs) : i(rhs.i) 
00024   { cerr << "D::D(D const&) called, i = " << i << "\n";}
00025   virtual ~testD() { cerr << "D::~D() called, i = " << i << "\n";}
00026   virtual  testD*  clone() const {
00027     cerr << "D::clone() called , i = " << i << "\n";
00028     return new testD(*this);
00029   }
00030   virtual void do_mut() {++i;} 
00031   virtual void do_const() const {}
00032 
00033 };
00034 
00035 template<>
00036 struct copy_traits<testA> : public copy_traits_base<testA> {
00037   static testA* clone(testA const& a) { return a.clone();}
00038 };
00039 
00040 int main() {
00041 
00042   for(int k = 1; k <= 10; ++k)
00043   {
00044     copy_on_write_ptr<int> p = new int(1);
00045     int i = *p;
00046     REQUIRE_ALWAYS((i==1), "Different value of i = " << i << "\n",1);
00047     copy_on_write_ptr<int> q = p;
00048     copy_on_write_ptr<int> r = p;
00049     
00050     REQUIRE_ALWAYS( (*p == *q), "" , 1);
00051     cout << "*p = " << *p << "  *q = " << *q << "  *r = " << *r << endl;
00052     *q = 2;
00053     cout << "*p = " << *p << "  *q = " << *q << "  *r = " << *r << endl;
00054     *p = 3;
00055     cout << "*p = " << *p << "  *q = " << *q << "  *r = " << *r << endl;
00056     r = p;
00057     cout << "*p = " << *p << "  *q = " << *q << "  *r = " << *r << endl;
00058     ++(*r);
00059     cout << "*p = " << *p << "  *q = " << *q << "  *r = " << *r << endl;
00060 
00061 
00062     // test abstract class as template parameter
00063     copy_on_write_ptr<testA> pA1(new testD(1));
00064     copy_on_write_ptr<testA> pA2 = pA1;
00065     copy_on_write_ptr<testA> pA3 = pA1;
00066 
00067     cerr << "-> pA1->do_mut() \n"; 
00068     pA1->do_mut();
00069     cerr << "-> pA3 = pA1 \n"; 
00070     pA3 = pA1;
00071     cerr << "-> pA2->do_const()\n"; 
00072     pA2->do_const(); // selects T* operator->, not T const* operator-> !
00073     cerr << "-> pA3 = pA2 \n"; 
00074     pA3 = pA2;
00075     cerr << "-> pA2->do_const()\n";    
00076     pA2->do_const(); // selects T* operator->, not T const* operator-> !
00077   }
00078 
00079   return 0;
00080 }

Copyright (c) Guntram Berti 1997-2002. See the GrAL Homepage for up-to-date information.

Generated at Tue Feb 26 15:57:02 2002 for Utilities by doxygen 1.2.11-20011104 written by Dimitri van Heesch, © 1997-2000