Click here to Skip to main content
12,952,273 members (56,735 online)
Click here to Skip to main content


51 bookmarked
Posted 12 Jan 2002

Tree data class for C++

, 12 Jan 2002
Simple class to represent tree data structures in C++
#ifndef _DATA_KEEPER_H
#define _DATA_KEEPER_H

namespace _Datakeeper {

template <class Type> class Ref;

template <class Type> class Data {
	typedef unsigned long ULONG;
	Data(Type *p=NULL) { if(!p) p=new Type; pData=p; dwRefs=1;};
	ULONG AddRef() { dwRefs++; return dwRefs;};
 	ULONG Release() { 
		dwRefs--; if(dwRefs==0) {delete this;return 0;} return dwRefs;};

// accessors
	Type *operator->() { return pData;};
	operator Type&() { return *pData;};
	Type &get_Data() { return *pData;};
	~Data() { if(pData) delete pData;};
	// op new
	Type *pData;
	ULONG dwRefs;
	friend class Ref<Type>;

template <class Type> class Ref {
	typedef Data<Type> DataT;
	typedef unsigned long ULONG;
	typedef unsigned long KEY;
	// ctors
	Ref(const Type &t) { pData=new DataT(new Type(t));};
	Ref(Type *p) { if(p) pData=new DataT(p); else pData=NULL;};
	Ref() { pData=new DataT();};
	//Ref(Data<Type> *p) : pData(p) { if(p) p->AddRef();};
	Ref(const Ref<Type> &rhs) { pData=(rhs.pData); if(pData) pData->AddRef();};
	virtual ~Ref() {if(pData) pData->Release();};

	//operators =
	Ref &operator=(const Ref<Type> &rhs) 
	{ Release(); pData=rhs.pData; if(pData) pData->AddRef(); return *this;};
	Ref &operator=(const Type &t) 
	{ Release(); pData=new DataT(new Type(t)); return *this;};
	Ref &operator=(Type *p) 
	{ Release(); if(p) pData=new DataT(p); else pData=NULL; return *this;};
	// operator ==
	bool operator==(const Ref<Type> &rhs) 
	{ return pData==rhs.pData;};
	Ref Clone() { if(!pData) return Ref(); return Ref(pData->get_Data());};
	void Release() { if(pData) pData->Release(); pData=NULL;};
	bool IsNull() { return pData==NULL;};

// accessors	
	Type *operator->() {return pData->operator->();};
	Type &get_Data() { return pData->get_Data();};
	operator Type&() { return pData->get_Data();};
	KEY Key() { return ((KEY)pData)+1;};
	ULONG GetRefCount() { if(!pData) return 0; return pData->dwRefs;};
	Ref(KEY key) { try{pData=(DataT*)(key-1);if(pData) pData->AddRef();}catch(...){pData=NULL;}};
	Data<Type> *pData;
	friend class Ref<Type>;
// niama new	

}// namespace _Datakeeper

using _Datakeeper::Ref;


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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alexander Kovachev
Web Developer
United States United States
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 13 Jan 2002
Article Copyright 2002 by Alexander Kovachev
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid