PageRenderTime 28ms CodeModel.GetById 21ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llui/llfunctorregistry.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 140 lines | 81 code | 19 blank | 40 comment | 6 complexity | 1da7455d14ba0228fe43a5fb7377685b MD5 | raw file
  1/**
  2 * @file llfunctorregistry.h
  3 * @author Kent Quirk
  4 * @brief Maintains a registry of named callback functors taking a single LLSD parameter
  5 *
  6 * $LicenseInfo:firstyear=2008&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  9 * 
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation;
 13 * version 2.1 of the License only.
 14 * 
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 * 
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 * 
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 */
 27
 28#ifndef LL_LLFUNCTORREGISTRY_H
 29#define LL_LLFUNCTORREGISTRY_H
 30
 31#include <string>
 32#include <map>
 33
 34#include <boost/function.hpp>
 35
 36#include "llsd.h"
 37#include "llsingleton.h"
 38
 39/**
 40 * @class LLFunctorRegistry
 41 * @brief Maintains a collection of named functors for remote binding
 42 * (mainly for use in callbacks from notifications and other signals)
 43 * @see LLNotifications
 44 *
 45 * This class maintains a collection of named functors in a singleton.
 46 * We wanted to be able to persist notifications with their callbacks
 47 * across restarts of the viewer; we couldn't store functors that way.
 48 * Using this registry, systems that require a functor to be maintained
 49 * long term can register it at system startup, and then pass in the
 50 * functor by name. 
 51 */
 52
 53template <typename FUNCTOR_TYPE>
 54class LLFunctorRegistry : public LLSingleton<LLFunctorRegistry<FUNCTOR_TYPE> >
 55{
 56	friend class LLSingleton<LLFunctorRegistry>;
 57	LOG_CLASS(LLFunctorRegistry);
 58private:
 59	LLFunctorRegistry() : LOGFUNCTOR("LogFunctor"), DONOTHING("DoNothing")
 60	{
 61		mMap[LOGFUNCTOR] = log_functor;
 62		mMap[DONOTHING] = do_nothing;
 63	}
 64
 65public:
 66	typedef FUNCTOR_TYPE ResponseFunctor;
 67	typedef typename std::map<std::string, FUNCTOR_TYPE> FunctorMap;
 68	
 69	bool registerFunctor(const std::string& name, ResponseFunctor f)
 70	{
 71		bool retval = true;
 72		typename FunctorMap::iterator it = mMap.find(name);
 73		if (mMap.count(name) == 0)
 74		{
 75			mMap[name] = f;
 76		}
 77		else
 78		{
 79			llerrs << "attempt to store duplicate name '" << name << "' in LLFunctorRegistry. NOT ADDED." << llendl;
 80			retval = false;
 81		}
 82		
 83		return retval;
 84	}
 85
 86	bool unregisterFunctor(const std::string& name)
 87	{
 88		if (mMap.count(name) == 0)
 89		{
 90			llwarns << "trying to remove '" << name << "' from LLFunctorRegistry but it's not there." << llendl;
 91			return false;
 92		}
 93		mMap.erase(name);
 94		return true;
 95	}
 96
 97	FUNCTOR_TYPE getFunctor(const std::string& name)
 98	{
 99		typename FunctorMap::iterator it = mMap.find(name);
100		if (mMap.count(name) != 0)
101		{
102			return mMap[name];
103		}
104		else
105		{
106			lldebugs << "tried to find '" << name << "' in LLFunctorRegistry, but it wasn't there." << llendl;
107			return mMap[LOGFUNCTOR];
108		}
109	}
110
111	const std::string LOGFUNCTOR;
112	const std::string DONOTHING;
113	
114private:
115
116	static void log_functor(const LLSD& notification, const LLSD& payload)
117	{
118		lldebugs << "log_functor called with payload: " << payload << llendl;
119	}
120
121	static void do_nothing(const LLSD& notification, const LLSD& payload)
122	{
123		// what the sign sez
124	}
125
126	FunctorMap mMap;
127};
128
129template <typename FUNCTOR_TYPE>
130class LLFunctorRegistration
131{
132public:
133	LLFunctorRegistration(const std::string& name, FUNCTOR_TYPE functor) 
134	{
135		LLFunctorRegistry<FUNCTOR_TYPE>::instance().registerFunctor(name, functor);
136	}
137};
138
139#endif//LL_LLFUNCTORREGISTRY_H
140