PageRenderTime 50ms CodeModel.GetById 41ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llui/lllazyvalue.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 82 lines | 45 code | 10 blank | 27 comment | 2 complexity | fbb4d466ce594660eabdc6bfb1a6c3a0 MD5 | raw file
 1/** 
 2 * @file lllazyvalue.h
 3 * @brief generic functor/value abstraction for lazy evaluation of a value
 4 * parsing construction parameters from xml and LLSD
 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_LAZY_VALUE_H
29#define LL_LAZY_VALUE_H
30
31#include <boost/function.hpp>
32
33// Holds on to a value of type T *or* calls a functor to generate a value of type T
34template<typename T>
35class LLLazyValue
36{
37public:
38	typedef typename boost::add_reference<typename boost::add_const<T>::type>::type	T_const_ref;
39	typedef typename boost::function<T_const_ref (void)>							function_type;
40
41public:
42	LLLazyValue(const function_type& value) 
43	:	mValueGetter(value)
44	{} 
45	LLLazyValue(T_const_ref value)
46	:	mValue(value)
47	{}
48	LLLazyValue()
49	:	mValue()
50	{}
51
52	void set(const LLLazyValue& val)
53	{
54		mValueGetter = val.mValueGetter;
55	}
56
57	void set(T_const_ref val)
58	{
59		mValue = val;
60		mValueGetter = NULL;
61	}
62
63	T_const_ref get() const
64	{
65		if (!mValueGetter.empty())
66		{
67			return mValueGetter();
68		}
69		return mValue;
70	}
71
72	bool isUsingFunction() const
73	{
74		return mValueGetter != NULL;
75	}
76
77private:
78	function_type	mValueGetter;
79	T				mValue;
80};
81
82#endif // LL_LAZY_VALUE_H