Click here to Skip to main content
11,567,926 members (43,988 online)
Click here to Skip to main content
Articles » Database » Database » General » Downloads
Add your own
alternative version

Implementation of a B-Tree Database Class

, 29 Jun 2006 214.4K 4.3K 105
An article and source code regarding the implmentation of B-Trees in C++.
/***************************************************************************
 smartptrs.h  -  Header file that contains the definition and inline
                 implementation of smart pointers. This is not entirely
                 my code ... most of it was lifted from the MSDN. Search
                 for "refcount smart pointers" and view the "Templates and
                 Smart Pointers" article there.
                 What did I do? I added the RefCount::lessthan function and
                 the Ptr::operator < operator. I also allow the assignment
                 of const T* by casting it to non-const. This was required
                 so that the smart pointers could be stored in std::vectors,
                 lists, maps, etc. Finally, I added a few more comparison
                 operators.

 begin     : April 2004
 copyright : (C) 2004 by Phil Cairns
 email     : philcairns@hotmail.com

 This code may be used in compiled form in any way you desire (including
 commercial use). The code may be redistributed unmodified by any means
 providing it is not sold for profit without the authors written consent,
 and providing that this notice and the authors name and all copyright
 notices remains intact.

 This software is provided "as is" without express or implied warranty. Use
 it at your own risk!
 ***************************************************************************/

#if !defined(__smartptrs_h)
#define __smartptrs_h

namespace Database
{
	/*!
	Base class of smart pointer classes.
	*/
	class RefCount
	{
	private:
		int _crefs;

	public:
		RefCount() : _crefs(0) {}
		virtual ~RefCount() {}
		virtual void upcount() { ++_crefs; }
		virtual void downcount(void)
		{
			if (--_crefs == 0)
			{
				delete this;
			}
		}
		virtual bool lessthan(const RefCount* other)
		{
			return this < other;
		}
	};

	/*!
	Smart pointer template.
	*/
	template <class T> class Ptr
	{
	private:
		T* _p;

	public:
		Ptr(const Ptr<T>& ptr) : _p(ptr._p) { if (_p) _p->upcount(); }
		Ptr() : _p(0) {}
		Ptr(T* p) : _p(p) { if (_p) _p->upcount(); }
		~Ptr(void) { if (_p) _p->downcount(); }
		operator T*(void) const { return _p; }
		T& operator*(void) const { return *_p; }
		T* operator->(void) const { return _p; }
		Ptr& operator=(const Ptr<T> &p) { return operator=((T*)(Ptr<T>)p); }
		Ptr& operator=(T* p)
		{
			if (_p) _p->downcount();
			_p = p;
			if (_p) _p->upcount();
			return *this;
		}
		Ptr& operator=(const T* p)
		{
			return operator=((T*)p);
		}
		bool operator ==(const Ptr<T> &p) const { return _p == p._p; }
		bool operator ==(const T* p) const { return _p == p; }
		bool operator !=(const Ptr<T> &p) const { return _p != p._p; }
		bool operator !=(const T* p) const { return _p != p; }
		bool operator < (const Ptr<T> &p) const { return _p->lessthan(p._p); }
	};

	typedef Ptr<RefCount> RefCountPtr;
};

#endif

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 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

Share

About the Author

_oti
Australia Australia
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 29 Jun 2006
Article Copyright 2004 by _oti
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid