/////////////////////////
// c0.cpp
#include "stdafx.h"
#include "stdx/smart.hpp"
/// stdx::smart testing
// run in debug mode an read messagess on the output window
using namespace GE_;
void test_dumbstaticptr();
void test_dumbdynamicptr();
int main()
{
STRACE(trc, 0, ("c0 main\n"));
test_dumbstaticptr();
test_dumbdynamicptr();
return 0;
}
////////////////////////////////////////////////////////////
void test_dumbstaticptr()
{
STRACE(trc, 0, ("test_dumbstaticptr\n"));
typedef stdx::statptr<double>::strong double_p;
typedef stdx::statptr<double>::weak double_wp;
double_p pD;
pD = new double(2.718);
SRETRACE(trc, ("pointing to %p value %f\n", pD(), *pD));
double_wp pW(pD);
*pW = 3.14;
SRETRACE(trc, ("pointing to %p value %f\n", pD(), *pD));
SRETRACE(trc, ("pW is %s\n", (!!pW)? "valid": "null"));
pD = 0; //set to null, also destoy the double.
SRETRACE(trc, ("pW is %s\n", (!!pW)? "valid": "null"));
}
////////////////////////////////////////////////////////////
struct A:
public virtual stdx::refcountable
{
A() { STRACE(t,1,("Crated A at %p\n", this)); }
virtual ~A() { STRACE(t,1,("Deleted A at %p\n", this)); }
void trc_me() { STRACE(t,1,("I'm A at %p\n", this)); }
virtual void v_trc_me() { STRACE(t,1,("I'm A at %p\n", this)); }
};
struct B:
public virtual A
{
virtual ~B() {}
void trc_me() { STRACE(t,1,("I'm B at %p\n", this)); }
virtual void v_trc_me() { STRACE(t,1,("I'm B at %p\n", this)); }
};
struct C:
public virtual A
{
virtual ~C() {}
void trc_me() { STRACE(t,1,("I'm C at %p\n", this)); }
virtual void v_trc_me() { STRACE(t,1,("I'm C at %p\n", this)); }
};
struct D:
public B,
public C
{
virtual ~D() {}
void trc_me() { STRACE(t,1,("I'm D at %p\n", this)); }
virtual void v_trc_me() { STRACE(t,1,("I'm D at %p\n", this)); }
};
void test_dumbdynamicptr()
{
STRACE(trc, 1, ("test_dumbdynamicptr\n"));
typedef stdx::ptr<A>::strong A_p;
typedef stdx::ptr<B>::strong B_p;
typedef stdx::ptr<C>::strong C_p;
typedef stdx::ptr<D>::strong D_p;
D_p pD;
A_p pA;
B_p pB;
pD.New(); //new D;
pA = pD; //dynamic cast into A*
pA->trc_me(); //prints A
pA->v_trc_me(); //prints D
pB = pA; //dynamic_cast into B*
pB->trc_me(); //print B
pB->v_trc_me(); //print D
pA = new C; //dumb C* dyncasted to A*
pA->trc_me(); //prints A
pA->v_trc_me(); //prints C
pD = pA; //D becomes null
pB = pD; //pB becomes null - old D* object is destroyed!
SRETRACE(trc, ("pA is %s\n", (!!pA)? "valid": "null")); //valid
SRETRACE(trc, ("pB is %s\n", (!!pB)? "valid": "null")); //null
SRETRACE(trc, ("pD is %s\n", (!!pD)? "valid": "null")); //null
}