Re: Tracking Object Creation and Destruction
Ross Boylan <ross@biostat.ucsf.edu> wrote in
news:1170813856.28142.465.camel@iron.psg.net:
I would like to be able to do the following test automatically, as
part of a test suite:
initializeCounts();
exerciseTest();
checkCounts();
The counts would be things like "class A had 10 instances allocated
and 10 freed." That is, they are on a class basis. Mostly, I want to
be sure that all A's created in the course of the test are freed
afterward.
I'm no expert so there may be something missing from this that I would
be happy to see corrected but the following template classes might do
what you want. I think that any class that derives privately from
Counter<> as shown below will have its counts output.
// Counter.hpp
template<typename T> class CountImpl{
int createCount;
int destructCount;
public:
CountImpl(){
createCount=0;
destructCount=0;
};
~CountImpl(){
std::cout<<"created "
<<typeid(T).name()<<" "<<createCount<<std::endl;
std::cout<<"destroyed "
<<typeid(T).name()<<" "<<destructCount<<std::endl;
}
void create(){createCount++;};
void destroy(){destructCount++;};
};
template<typename T>class Counter
{
static CountImpl<T> myCount;
public:
protected:
Counter(){myCount.create();
};
Counter(const Counter&){
myCount.create();
};
virtual ~Counter(){myCount.destroy();
}
};
template<typename T> CountImpl<T> Counter<T>::myCount;
// End Counter.hpp
//Test.cpp
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
#include "Counter.hpp"
struct Toto:private Counter<Toto>
{
int id ;
std::string value ;
Toto( int id, std::string const& value )
: id( id )
, value( value )
{
}
} ;
struct Cmp:private Counter<Cmp>
{
bool operator()( Toto const& lhs, int rhs ) const
{
return lhs.id < rhs ;
};
bool operator()(int lhs, Toto const& rhs ) const
{
return lhs< rhs.id ;
}
} ;
struct B:public Toto,private Counter<B>{
B():Toto(1,""){};
};
int main()
{
B test;
std::vector< Toto > v ;
static char const* const
init[] =
{
"one", "two", "three", "four", "five"
} ;
for ( int i = 0 ; i < 5 ; ++ i ) {
v.push_back( Toto( i + 1, init[ i ] ) ) ;
}
std::vector< Toto >::iterator
i = std::upper_bound( v.begin(),
v.end(),
3,
Cmp() ) ;
std::cout << i->value << std::endl ;
return 0 ;
}
Please tell me what I missed.
Otis Bricker
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]