Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

A policy based reference counting implementation for compound objects

, 26 May 2005
Reference counting smart pointers and handles of various flavours.
/////////////////////////
//	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


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Emilio Garavaglia
Architect
Italy Italy
Born and living in Milan (Italy), I'm an engineer in electronics actually working in the ICT department of an important oil/gas & energy company as responsible for planning and engineering of ICT infrastructures.
Interested in programming since the '70s, today I still define architectures for the ICT, deploying dedicated specific client application for engineering purposes, working with C++, MFC, STL, and recently also C# and D.

| Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 27 May 2005
Article Copyright 2005 by Emilio Garavaglia
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid