/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


#ifndef	__NFUTURE__
#define	__NFUTURE__


		#include "thread/atomic_value.h"
		#include "memory/nshared_ptr.h"


namespace GS {
	namespace ASync {

/*
	@short	Future.
	@author	Emmanuel Julien (ejulien@nworks.fr)
*/
template <class T> class Future
{
		T			value;
		Threading::Atomic32	set;

public:

		bool		IsSet() const
		{	return set.Get() == 1;	}
		void		Set(const T &_value)
		{
			value = _value;
			set.Set(1);
		}

		void		Wait()
		{	while (!IsSet());	}
	
		T			&Get()
		{
			Wait();
			return value;
		}
};

//
template <> class Future <void>
{
		Threading::Atomic32	set;

public:

		bool		IsSet() const
		{	return set.Get() == 1;	}
		void		Set()
		{	set.Set(1);	}

		void		Wait()
		{	while (!IsSet());	}
};

	} // ASync
} // GS


#endif	// __NFUTURE__