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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
// -*- c++ -*-
//------------------------------------------------------------------------
// $Id: autoptr_test.cpp,v 1.4 2005/03/19 17:19:24 vlg Exp $
//------------------------------------------------------------------------
// src/test/autoptr.C
//------------------------------------------------------------------------------
// Copyright (c) 1999,2001,2005 by Vladislav Grinchenko
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version
// 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------
//
// Description:
//
// This module tests and illustrates auto_ptr usage defined in
// assa/AutoPtr.h file
//
// Date: March 15, 1999
//------------------------------------------------------------------------
#include <iostream>
#include <assert.h>
#include "assa/AutoPtr.h"
using namespace std;
using namespace ASSA;
/** -------------------------------------------------------------------------
* Parent
** -------------------------------------------------------------------------
*/
class VParent
{
private:
static int m_cnt;
public:
VParent() {
cout << "-- VParent::VParent --\n";
m_cnt++;
}
~VParent() {
cout << "-- VParent::~VParent --\n";
m_cnt--;
}
VParent (const VParent& /* m_ */) {
cout << "-- VParent::VParent(const VParent&) --\n";
m_cnt++;
}
VParent& operator= (const VParent& /* m_ */) {
cout << "-- VParent::operator= --\n";
m_cnt++;
return *this;
}
static int getCount() { return m_cnt; }
static void resetCount() { m_cnt = 0; }
void printMsg() { cout << "I'm a VParent class!\n"; }
};
int VParent::m_cnt = 0;
/** -------------------------------------------------------------------------
* Child
** -------------------------------------------------------------------------
*/
class VChild : public VParent {};
void leak_mem ()
{
cout << "-- leak_mem --\n";
VParent* a = new VParent();
a->printMsg();
}
void no_leak_mem ()
{
cout << "-- no_leak_mem --\n";
AutoPtr<VParent> a (new VParent);
a->printMsg();
}
/** -------------------------------------------------------------------------
* Function
** -------------------------------------------------------------------------
*/
AutoPtr<VParent> foo ()
{
AutoPtr<VParent> vptr (new VParent);
return vptr;
}
/** --------------------------------------------------------------------------
* Main
** --------------------------------------------------------------------------
*/
int main()
{
cout << "\n*** Testing memory allocation/deallocation ***\n";
cout << "\nCalling leak_mem()\n";
leak_mem();
cout << "copies on heap still allocated: " << VParent::getCount();
assert (VParent::getCount() == 1);
cout << " ... ok\n";
VParent::resetCount ();
cout << "\nCalling no_leak_mem()\n";
no_leak_mem();
cout << "copies on heap still allocated: " << VParent::getCount();
assert (VParent::getCount() == 0);
cout << " ... ok\n";
// How about rebinding later in the game?
{
AutoPtr<VParent> fp;
bool cond = true;
if (cond) {
AutoPtr<VParent> temp (new VParent);
fp = temp;
}
}
//---------------------------------------------------------------
// A function can behave as a source of data. When an AutoPtr
// is returned, ownership of the returned value gets transferred
// to the calling function.
cout << "\n*** Testing ownership transfer by function ***\n\n";
VParent::resetCount ();
{
AutoPtr<VParent> vp;
for (int i = 0; i < 3; i++) {
vp = foo ();
}
}
//---------------------------------------------------------------
cout << "\n*** Testing ownership transfer ***\n\n";
{
AutoPtr<VParent> p1 (new VParent());
AutoPtr<VParent> p2;
cout << "\nUninitialized AutoPtr is a '0' pointer ...";
assert (p2.get() == 0);
cout << " ok\n";
cout << "Ownership transfer by assignment ... ";
p2 = p1;
assert (p2.get() != 0);
cout << "ok\n";
cout << "Ownership transfer by copy constructor ... ";
AutoPtr<VParent> p3(p2);
assert (p3.get() != 0);
cout << "ok\n";
}
cout << "\n*** Testing inheritance handling ***\n\n";
{
AutoPtr<VParent> pp (new VChild);
AutoPtr<VChild> pc (new VChild);
pp = pc;
AutoPtr<VParent> pp2 (pc);
}
cout << "ok\n";
// --------------------------------------------------------------
cout << "\n*** Testing AutoPtrArray handling ***\n\n";
{
AutoPtrArray<VParent> vp1 (new VParent[6]);
}
cout << "ok\n";
cout << "\nTest done!\n\n";
}
|