Click here to Skip to main content
12,401,832 members (58,804 online)
Click here to Skip to main content

Stats

69.2K views
1.7K downloads
22 bookmarked
Posted

Simple service base class for Windows

, 23 Mar 2004
This class provides a simple way to implement Windows service in C++.
#ifndef INTELLIGER_SMART_REFERENCE_H
#define INTELLIGER_SMART_REFERENCE_H

template <class reft>
class sref
{
public:
	sref()
	{
		m_ref_count = 0;
		m_r = 0;
	}

	sref(reft* r)
	{
		m_ref_count = 0;
		m_r = r;
		inc_ref();
	}

	sref(sref& sr)
	{
		m_ref_count = 0;
		sr.inc_ref();
		inc_ref();
		m_r = sr.m_r;
	}

	~sref()
	{
		clean();
	}

	operator reft*()
	{
		return m_r;
	}
	
	reft& operator *()
	{
		if(!m_r)
		{
			throw reft();
		}
		return *m_r;
	}

	const sref& operator = (sref& sr)
	{
		sr.inc_ref();
		if(sr.m_r != m_r)
		{
			clean();
		}
		inc_ref();
		m_r = sr.m_r;
		return *this;
	}

	const sref& operator =(reft* r)
	{
		if(r != m_r)
		{
			clean();
		}
		inc_ref();
		m_r = r;
		return *this;
	}

	reft* operator->()
	{
		if(!m_r)
		{
			throw reft();
		}
		return m_r;
	}

	void* operator new(size_t s)
	{
		throw;
		return 0;
	}

protected:
	reft* m_r;
	short m_ref_count;
	
	void inc_ref(){ ++m_ref_count; }

	bool dec_ref(){ if(!(--m_ref_count)){return true;} return false; }

	virtual void clean()
	{
		if(dec_ref())
		{
			delete m_r;
			m_r = 0;
		}
	}

};

template <class reft>
class sref_a
{
public:
	sref_a()
	{
		m_ref_count = 0;
		m_length = 0;
		m_r = 0;
	}

	sref_a(reft* r, int length)
	{
		m_ref_count = 0;
		m_length = length;
		m_r = r;
		inc_ref();
	}

	sref_a(sref_a& sr)
	{
		m_ref_count = 0;
		sr.inc_ref();
		inc_ref();
		m_r = sr.m_r;
		m_length = sr.m_length;
	}
	~sref_a()
	{
		clean();
	}
	int length(){ return m_length; }

	reft& operator *()
	{
		if(!m_r)
		{
			throw reft();
		}
		return *m_r;
	}

	const sref_a& operator = (sref_a& sr)
	{
		sr.inc_ref();
		if(sr.m_r != m_r)
		{
			clean();
		}
		inc_ref();
		m_r = sr.m_r;
		m_length = sr.m_length;
		return *this;
	}

	operator reft*()
	{
		return m_r;
	}

	void* operator new(size_t s)
	{
		throw;
		return 0;
	}

protected:
	reft* m_r;
	short m_ref_count;
	int m_length;
	
	void inc_ref(){ ++m_ref_count; }

	bool dec_ref(){ if(!(--m_ref_count)){return true;} return false; }

	virtual void clean()
	{
		if(dec_ref())
		{
			delete[] m_r;
			m_r = 0;
		}
	}
};

#define I_NEW(type, args)\
	sref<type>(new type args)

#define I_NEWA(type, count)\
	sref_a<type>(new type[count], count)

#endif
// INTELLIGER_SMART_REFERENCE_H

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

EasyWay
Web Developer
Bosnia and Herzegovina Bosnia and Herzegovina
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 24 Mar 2004
Article Copyright 2004 by EasyWay
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid