Click here to Skip to main content
11,631,896 members (72,641 online)
Click here to Skip to main content
Articles » Database » Database » General » Downloads
Add your own
alternative version

PostgreSQL/libpqxx Class Generator

, 18 Aug 2006 53.6K 1.3K 35
Automated generation of PostgreSQL data transfer classes.
/***************************************************************************
 SyncObject.h - Declaration of a synchroniser - similar to Java's
                object synchronisation functionality.

 begin     : August 2006
 copyright : (C) 2006 by Phil Cairns
 email     : oti169@hotmail.com
 $Id: SyncObject.h 2080 2006-08-18 01:53:19Z phil $

 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!
 ***************************************************************************/

#pragma once

namespace Pagaros
{
	class SyncObject
	{
	public:
		SyncObject(void)
		{
			_mutex = CreateMutex(0, FALSE, 0);
			_eventOne = CreateEvent(0, FALSE, FALSE, 0);
			_eventAll = CreateEvent(0, TRUE, FALSE, 0);
		}

		~SyncObject(void)
		{
			CloseHandle(_eventOne);
			CloseHandle(_eventAll);
			CloseHandle(_mutex);
		}

		bool lock(void)
		{
			//ATLTRACE("Locking ... ");
			return (WAIT_OBJECT_0 == WaitForSingleObject(_mutex, INFINITE));
		}

		bool unlock(void)
		{
			//ATLTRACE("unlocked\n");
			return (TRUE == ReleaseMutex(_mutex));
		}

		virtual bool wait(DWORD millis = INFINITE)
		{
			HANDLE	handles[2] = { _eventOne, _eventAll };
			DWORD dwRet = WaitForMultipleObjects(2, handles, FALSE, millis);
			return ((WAIT_OBJECT_0 == dwRet) || (WAIT_OBJECT_0 + 1 == dwRet));
		}

		bool notify(void)
		{
			return (TRUE == SetEvent(_eventOne));
		}

		bool notifyAll(void)
		{
			return (TRUE == SetEvent(_eventAll));
		}

		bool resetAll(void)
		{
			return (TRUE == ResetEvent(_eventAll));
		}

	protected:
		HANDLE getEventOne() const { return _eventOne; }
		HANDLE getEventAll() const { return _eventAll; }
		HANDLE getMutex() const { return _mutex; }

	private:
		HANDLE	_mutex;
		HANDLE	_eventOne;
		HANDLE	_eventAll;
	};

	class SyncLock
	{
	public:
		SyncLock(SyncObject* so) : _so(so) { _so->lock(); }
		SyncLock(SyncObject& so) : _so(&so) { _so->lock(); }
		~SyncLock() { _so->unlock(); }
		bool wait(DWORD millis = INFINITE)
		{
			bool ret = false;
			_so->unlock();
			ret = _so->wait(millis);
			_so->lock();
			return ret;
		}
		void notify()
		{
			_so->notify();
		}
		void notifyAll()
		{
			_so->notifyAll();
		}
		void resetAll()
		{
			_so->resetAll();
		}
	private:
		SyncObject* _so;
	};
};

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
Web01 | 2.8.150728.1 | Last Updated 18 Aug 2006
Article Copyright 2005 by _oti
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid