Click here to Skip to main content
15,893,622 members
Articles / Mobile Apps

Tiny Template Library: variant

Rate me:
Please Sign up or sign in to vote.
4.88/5 (14 votes)
23 Feb 20043 min read 84.2K   697   28  
An article on how to implement and use variant<>. Variant is useful for creating heterogeneous containers and much more.
//  signal.hpp
//
//  Copyright (c) 2003 Eugene Gladyshev
//
//  Permission to copy, use, modify, sell and distribute this software
//  is granted provided this copyright notice appears in all copies.
//  This software is provided "as is" without express or implied
//  warranty, and with no claim as to its suitability for any purpose.
//

#ifndef __ttl_signal__hpp
#define __ttl_signal__hpp

#include <list>
#include "ttl/func/function.hpp"

namespace ttl
{
namespace sig
{
struct exception : ttl::exception
{
	exception() : ttl::exception("signal error") {}
};

/////////////////////////////////////////////////////////////
namespace impl
{
	struct connection_holder_base
	{
		typedef ttl::mem::memtraits::allocator<connection_holder_base> allocator;
		bool active_;
		int ref_;
		connection_holder_base() : active_(false), ref_(0) {}
		virtual ~connection_holder_base() {}
		virtual void disconnect() = 0;
		inline bool is_active() const { return active_; }
	};
}; //impl

/////////////////////////////////////////////////////////////////////
	struct connection
	{
		typedef impl::connection_holder_base holder;
		connection() : c_(0) {}
		connection( holder* c ) : c_(c) { ++c_->ref_; }
		connection( const connection& c ) : c_(c.c_) { ++c_->ref_; }
		~connection() { release(); }
		inline void disconnect()
		{
			if(c_) c_->disconnect();
		}
		inline bool is_active() const { return c_ && c_->is_active(); }
		connection& operator=( const connection& c )
		{
			if( &c == this ) return *this;
			release();
			c_ = c.c_;
			++c_->ref_;
			return *this;
		}
		
	private:
		holder* c_;
		
		void release()
		{
			if( c_ && (!--c_->ref_) ) 
			{
				ttl::mem::destroy<holder::allocator>(c_);
				c_ = 0;
			}
		}
	};
	
namespace impl
{
	template< typename F >
	struct signal_base
	{
		struct connection_holder;
		typedef std::list<connection_holder*> list;

		struct connection_holder : impl::connection_holder_base
		{
			list& l_;
			typename list::iterator it_;
			F f_;
			
			connection_holder( list& l, const F& f ) : l_(l), f_(f) {}
			connection connect()
			{
				if( is_active() ) throw ttl::sig::exception();
				it_ = l_.insert( l_.end(), this );
				active_ = true;
				++ref_;
				return connection(this);
			}
			
			virtual void disconnect()
			{
				if( !is_active() ) return;
				l_.erase(it_);
				active_ = false;
				--ref_;
			}
		};
		typedef typename ttl::mem::memtraits::allocator<connection_holder> allocator;
		
		signal_base() {}
		virtual ~signal_base() { clear(); }
		
		connection connect( const F& f )
		{
			connection_holder t( con_, f );
			connection_holder *c = ttl::mem::create<allocator>( t );
			if( !c ) throw ttl::sig::exception();
			return c->connect();
		}
	protected:
		list con_;
		void clear()
		{
			typename list::const_iterator it;
			for( it = con_.begin(); it != con_.end(); it = con_.begin() )
			{
				connection_holder *c = *it;
				c->disconnect();
				if( !c->ref_ )
				{
					ttl::mem::destroy<allocator>(c);
				}
			}
		}
	 private:
		//protect from copy
		signal_base( const signal_base& );
		const signal_base& operator=( const signal_base& );
	};
};  //impl


	template < typename R, TTL_TPARAMS_DEF(TTL_MAX_FUNCTION_PARAMS, empty_type) >
	struct signal : impl::signal_base< func::function<R,TTL_ARGS(TTL_MAX_FUNCTION_PARAMS) > >
	{
		typedef func::function< R,TTL_ARGS(TTL_MAX_FUNCTION_PARAMS) > slot_type;
		typedef impl::signal_base<slot_type> base_t;
		typedef typename base_t::list list;
		
		void operator()()
		{
			typename list::const_iterator it;
			for( it = con_.begin(); it != con_.end(); ++it )
			{
				(*it)->f_();
			}
		}
	};

#define TTL_SIG_BUILD_SIGNAL(n) \
		typedef impl::signal_base<slot_type> base_t; \
		typedef typename base_t::list list; \
		void operator()(TTL_FUNC_PARAMS(n,p)) \
		{ \
			typename list::const_iterator it; \
			for( it = con_.begin(); it != con_.end(); ++it ) \
			{ \
				(*it)->f_(TTL_ENUM_ITEMS(n,p)); \
			} \
		}
	
	template < typename R, TTL_TPARAMS(TTL_MAX_FUNCTION_PARAMS) >
	struct signal<R (TTL_ARGS(1)), T2, T3, T4, T5, T6, T7, T8, T9, T10 > : 
		impl::signal_base< func::function<R (TTL_ARGS(1)), T2, T3, T4, T5, T6, T7, T8, T9, T10> >
	{
		typedef func::function<R (TTL_ARGS(1)), T2, T3, T4, T5, T6, T7, T8, T9, T10> slot_type;
		TTL_SIG_BUILD_SIGNAL(1)
	};
	
	template < typename R, TTL_TPARAMS(TTL_MAX_FUNCTION_PARAMS) >
	struct signal<R (TTL_ARGS(2)), T3, T4, T5, T6, T7, T8, T9, T10 > : 
		impl::signal_base< func::function<R (TTL_ARGS(2)), T3, T4, T5, T6, T7, T8, T9, T10> >
	{
		typedef func::function<R (TTL_ARGS(2)), T3, T4, T5, T6, T7, T8, T9, T10> slot_type;
		TTL_SIG_BUILD_SIGNAL(2)
	};
	
	template < typename R, TTL_TPARAMS(TTL_MAX_FUNCTION_PARAMS) >
	struct signal<R (TTL_ARGS(3)), T4, T5, T6, T7, T8, T9, T10 > : 
		impl::signal_base< func::function<R (TTL_ARGS(3)), T4, T5, T6, T7, T8, T9, T10> >
	{
		typedef func::function<R (TTL_ARGS(3)), T4, T5, T6, T7, T8, T9, T10> slot_type;
		TTL_SIG_BUILD_SIGNAL(3)
	};
	
	template < typename R, TTL_TPARAMS(TTL_MAX_FUNCTION_PARAMS) >
	struct signal<R (TTL_ARGS(4)), T5, T6, T7, T8, T9, T10 > : 
		impl::signal_base< func::function<R (TTL_ARGS(4)), T5, T6, T7, T8, T9, T10> >
	{
		typedef func::function<R (TTL_ARGS(4)), T5, T6, T7, T8, T9, T10> slot_type;
		TTL_SIG_BUILD_SIGNAL(4)
	};
	
	template < typename R, TTL_TPARAMS(TTL_MAX_FUNCTION_PARAMS) >
	struct signal<R (TTL_ARGS(5)), T6, T7, T8, T9, T10 > : 
		impl::signal_base< func::function<R (TTL_ARGS(5)), T6, T7, T8, T9, T10> >
	{
		typedef func::function<R (TTL_ARGS(5)), T6, T7, T8, T9, T10> slot_type;
		TTL_SIG_BUILD_SIGNAL(5)
	};
	
	template < typename R, TTL_TPARAMS(TTL_MAX_FUNCTION_PARAMS) >
	struct signal<R (TTL_ARGS(6)), T7, T8, T9, T10 > : 
		impl::signal_base< func::function<R (TTL_ARGS(6)), T7, T8, T9, T10> >
	{
		typedef func::function<R (TTL_ARGS(6)), T7, T8, T9, T10> slot_type;
		TTL_SIG_BUILD_SIGNAL(6)
	};
	
	template < typename R, TTL_TPARAMS(TTL_MAX_FUNCTION_PARAMS) >
	struct signal<R (TTL_ARGS(7)), T8, T9, T10 > : 
		impl::signal_base< func::function<R (TTL_ARGS(7)), T8, T9, T10> >
	{
		typedef func::function<R (TTL_ARGS(7)), T8, T9, T10> slot_type;
		TTL_SIG_BUILD_SIGNAL(7)
	};

	template < typename R, TTL_TPARAMS(TTL_MAX_FUNCTION_PARAMS) >
	struct signal<R (TTL_ARGS(8)), T9, T10 > : 
		impl::signal_base< func::function<R (TTL_ARGS(8)), T9, T10> >
	{
		typedef func::function<R (TTL_ARGS(8)), T9, T10> slot_type;
		TTL_SIG_BUILD_SIGNAL(8)
	};
	
	template < typename R, TTL_TPARAMS(TTL_MAX_FUNCTION_PARAMS) >
	struct signal<R (TTL_ARGS(9)), T10 > : 
		impl::signal_base< func::function<R (TTL_ARGS(9)), T10> >
	{
		typedef func::function<R (TTL_ARGS(9)), T10> slot_type;
		TTL_SIG_BUILD_SIGNAL(9)
	};

	template < typename R, TTL_TPARAMS(TTL_MAX_FUNCTION_PARAMS) >
	struct signal<R (TTL_ARGS(10)) > : 
		impl::signal_base< func::function<R (TTL_ARGS(10)) > >
	{
		typedef func::function< R (TTL_ARGS(10)) > slot_type;
		TTL_SIG_BUILD_SIGNAL(10)
	};
}; //sig
}; //ttl

#endif //__ttl_signal__hpp

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


Written By
kig
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions