2006-03-20 21:14:16 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Test program for The Loki Library
|
|
|
|
// Copyright (c) 2006 Richard Sposato
|
2011-09-07 22:51:17 +00:00
|
|
|
// Permission to use, copy, modify, distribute and sell this software for any
|
|
|
|
// purpose is hereby granted without fee, provided that the above copyright
|
|
|
|
// notice appear in all copies and that both that copyright notice and this
|
2006-03-20 21:14:16 +00:00
|
|
|
// permission notice appear in supporting documentation.
|
2011-09-07 22:51:17 +00:00
|
|
|
// The authors make no representations about the
|
|
|
|
// suitability of this software for any purpose. It is provided "as is"
|
2006-03-20 21:14:16 +00:00
|
|
|
// without express or implied warranty.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2006-10-17 20:36:13 +00:00
|
|
|
// $Id$
|
2006-03-20 21:14:16 +00:00
|
|
|
|
2006-04-05 22:56:58 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2006-03-20 21:14:16 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class BaseClass
|
|
|
|
{
|
|
|
|
public:
|
2011-09-07 22:51:17 +00:00
|
|
|
BaseClass( void ) : m_refCount( 1 )
|
2006-03-20 21:14:16 +00:00
|
|
|
{
|
|
|
|
s_constructions++;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~BaseClass( void )
|
|
|
|
{
|
|
|
|
s_destructions++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// These 2 functions are so we can pretend we have a COM object.
|
2011-09-07 22:51:17 +00:00
|
|
|
void AddRef( void ) { ++m_refCount; }
|
|
|
|
void Release( void )
|
|
|
|
{
|
|
|
|
assert( 0 < m_refCount );
|
|
|
|
--m_refCount;
|
|
|
|
if ( 0 == m_refCount )
|
|
|
|
delete this;
|
|
|
|
}
|
2006-03-20 21:14:16 +00:00
|
|
|
|
|
|
|
// This function is used only for the DeepCopy policy.
|
|
|
|
virtual BaseClass * Clone( void ) const
|
|
|
|
{
|
|
|
|
return new BaseClass();
|
|
|
|
}
|
|
|
|
|
2006-04-05 22:56:58 +00:00
|
|
|
void DoThat( void ) const {}
|
|
|
|
|
2006-03-20 21:14:16 +00:00
|
|
|
static inline bool AllDestroyed( void )
|
|
|
|
{
|
|
|
|
return ( s_constructions == s_destructions );
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool ExtraConstructions( void )
|
|
|
|
{
|
|
|
|
return ( s_constructions > s_destructions );
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool ExtraDestructions( void )
|
|
|
|
{
|
|
|
|
return ( s_constructions < s_destructions );
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int GetCtorCount( void )
|
|
|
|
{
|
|
|
|
return s_constructions;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int GetDtorCount( void )
|
|
|
|
{
|
|
|
|
return s_destructions;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Not implemented.
|
|
|
|
BaseClass( const BaseClass & );
|
|
|
|
/// Not implemented.
|
|
|
|
BaseClass & operator = ( const BaseClass & );
|
|
|
|
|
|
|
|
static unsigned int s_constructions;
|
|
|
|
static unsigned int s_destructions;
|
2011-09-07 22:51:17 +00:00
|
|
|
|
|
|
|
unsigned int m_refCount;
|
2006-03-20 21:14:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class PublicSubClass : public BaseClass
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// This function is used only for the DeepCopy policy.
|
|
|
|
virtual BaseClass * Clone( void ) const
|
|
|
|
{
|
2006-04-05 22:56:58 +00:00
|
|
|
return new PublicSubClass;
|
2006-03-20 21:14:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class PrivateSubClass : private BaseClass
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// This function is used only for the DeepCopy policy.
|
|
|
|
virtual BaseClass * Clone( void ) const
|
|
|
|
{
|
2006-04-05 22:56:58 +00:00
|
|
|
return new PrivateSubClass;
|
2006-03-20 21:14:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-04-05 22:56:58 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/** @class MimicCOM Acts like a COM object by having an intrusive ref count.
|
|
|
|
*/
|
|
|
|
class MimicCOM
|
|
|
|
{
|
|
|
|
public:
|
2006-05-18 05:05:21 +00:00
|
|
|
|
|
|
|
static MimicCOM * QueryInterface( void )
|
2006-04-05 22:56:58 +00:00
|
|
|
{
|
2006-05-18 05:05:21 +00:00
|
|
|
MimicCOM * p = new MimicCOM;
|
|
|
|
p->AddRef();
|
|
|
|
return p;
|
2006-04-05 22:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MimicCOM( void )
|
|
|
|
{
|
|
|
|
s_destructions++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddRef( void )
|
|
|
|
{
|
|
|
|
m_count++;
|
|
|
|
m_AddRefCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Release( void )
|
|
|
|
{
|
|
|
|
m_ReleaseCount++;
|
|
|
|
assert( 0 < m_count );
|
|
|
|
m_count--;
|
|
|
|
if ( 0 == m_count )
|
|
|
|
{
|
|
|
|
/** @note I consider "delete this;" to be very unsafe! I'm only
|
|
|
|
using it here for the purpose of testing.
|
|
|
|
*/
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoThat( void ) {}
|
|
|
|
|
|
|
|
static inline bool AllDestroyed( void )
|
|
|
|
{
|
|
|
|
return ( s_constructions == s_destructions );
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool ExtraConstructions( void )
|
|
|
|
{
|
|
|
|
return ( s_constructions > s_destructions );
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool ExtraDestructions( void )
|
|
|
|
{
|
|
|
|
return ( s_constructions < s_destructions );
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int GetCtorCount( void )
|
|
|
|
{
|
|
|
|
return s_constructions;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int GetDtorCount( void )
|
|
|
|
{
|
|
|
|
return s_destructions;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Not implemented.
|
|
|
|
MimicCOM( const MimicCOM & );
|
|
|
|
/// Not implemented.
|
|
|
|
MimicCOM & operator = ( const MimicCOM & );
|
|
|
|
|
2006-05-18 05:05:21 +00:00
|
|
|
MimicCOM( void )
|
|
|
|
: m_count( 0 )
|
|
|
|
, m_AddRefCount( 0 )
|
|
|
|
, m_ReleaseCount( 0 )
|
|
|
|
{
|
|
|
|
s_constructions++;
|
|
|
|
}
|
|
|
|
|
2006-04-05 22:56:58 +00:00
|
|
|
static unsigned int s_constructions;
|
|
|
|
static unsigned int s_destructions;
|
|
|
|
|
|
|
|
unsigned int m_count;
|
|
|
|
unsigned int m_AddRefCount;
|
|
|
|
unsigned int m_ReleaseCount;
|
|
|
|
};
|
|
|
|
|
2006-03-20 21:14:16 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|