PageRenderTime 21ms CodeModel.GetById 16ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/indra/llcommon/lleventemitter.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 102 lines | 47 code | 18 blank | 37 comment | 6 complexity | 8103e4954ca20c0c670b90a81a38f531 MD5 | raw file
  1/** 
  2 * @file lleventemitter.h
  3 * @brief General event emitter class
  4 *
  5 * $LicenseInfo:firstyear=2005&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27// header guard
 28#ifndef LL_EVENTEMITTER_H
 29#define LL_EVENTEMITTER_H
 30
 31// standard headers
 32#include <algorithm>
 33#include <typeinfo>
 34#include <iostream>
 35#include <string>
 36#include <list>
 37
 38#include "stdtypes.h"
 39
 40///////////////////////////////////////////////////////////////////////////////
 41//	templatized emitter class
 42template < class T >
 43class eventEmitter
 44{
 45	public:
 46		typedef typename T::EventType EventType;
 47		typedef std::list< T* > ObserverContainer;
 48		typedef void ( T::*observerMethod )( const EventType& );
 49
 50	protected:
 51		ObserverContainer observers;
 52
 53	public:
 54		eventEmitter () { };
 55
 56		~eventEmitter () { };
 57
 58		///////////////////////////////////////////////////////////////////////////////
 59		//
 60		BOOL addObserver ( T* observerIn )
 61		{
 62			if ( ! observerIn )
 63				return FALSE;
 64
 65			// check if observer already exists
 66			if ( std::find ( observers.begin (), observers.end (), observerIn ) != observers.end () )
 67				return FALSE;
 68
 69			// save it
 70			observers.push_back ( observerIn );
 71
 72			return true;
 73		};
 74
 75		///////////////////////////////////////////////////////////////////////////////
 76		//
 77		BOOL remObserver ( T* observerIn )
 78		{
 79			if ( ! observerIn )
 80				return FALSE;
 81
 82			observers.remove ( observerIn );
 83
 84			return TRUE;
 85		};
 86
 87		///////////////////////////////////////////////////////////////////////////////
 88		//
 89		void update ( observerMethod method, const EventType& msgIn )
 90		{
 91			typename std::list< T* >::iterator iter = observers.begin ();
 92
 93			while ( iter != observers.end () )
 94			{
 95				( ( *iter )->*method ) ( msgIn );
 96
 97				++iter;
 98			};
 99		};
100};
101
102#endif // lleventemitter_h