Click here to Skip to main content
13,899,977 members
Click here to Skip to main content


30 bookmarked
Posted 21 May 2005
Licenced CPOL

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/compounds.hpp"

/// stdx::smart testing
//	run in debug mode an read messagess on the output window

using namespace GE_;

struct A:
	public stdx::hierarchy_node<A, stdx::comp_smart_traits>
	int a;
		static int n(0); a = ++n; 
		STRACE(trc, 1, ("Created A #%d at %p\n", a, this));
	virtual ~A() 
	{	STRACE(trc, 1, ("Deleted A #%d at %p\n", a, this)); }

struct B: public A
	B() { a += 100; }

struct C:
	public stdx::refcountable
	int c;
		static int n(0); c = ++n;
		STRACE(t,1,("Creating C #%d at %p\n",c,this));
	virtual ~C()
	{	STRACE(t,1,("Deleting C #%d at %p\n",c,this)); }

int main()
	STRACE(trc, 0, ("c0 main\n"));
	//testing herirchy of nodes with smart pointers
		A::ptr pA;

		pA->set_last(new A);
		pA->get_last()->set_last(new B);
		pA->set_last(new A);
		pA->get_last()->set_last(new B);
		pA->get_last()->set_first(new B);
		pA->set_last(new A);

		for(A::deepiterator i(pA); !!i; i++)
		{	SRETRACE(trc, ("Viewing A #%d at %p\n", i->a, &*i)); }

	//testing hierarchy valnodes
		typedef stdx::hierarchy_valnode<int> hier_int_t;

		hier_int_t::ptr pRoot(new hier_int_t(0));
		pRoot->set_last(new hier_int_t(1));
		pRoot->set_last(new hier_int_t(2));
		pRoot->get_last()->set_last(new hier_int_t(3));
		pRoot->set_last(new hier_int_t(4));

		for(hier_int_t::deepiterator i(pRoot); !!i; i++)
			int v = *i;
			SRETRACE(trc, ("value = %d\n", v));

	//testing herarchy refnodes
		typedef stdx::ptr<C>::strong C_p;
		typedef stdx::hierarchy_refnode<C_p> C_hr;
		typedef C_hr::ptr C_pp;

		C_pp pRoot(new C_hr(new C));
		for(int n=8; n>0; n--)
            pRoot->set_last(new C_hr(new C));

		for(C_hr::deepiterator i(pRoot); !!i; i++)
			C& c = *i;
			SRETRACE(trc, ("value = %d\n", c.c));


	return 0;

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.


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


About the Author

Emilio Garavaglia
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.

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190306.1 | Last Updated 27 May 2005
Article Copyright 2005 by Emilio Garavaglia
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid