1/**
2 * @file cached_value.h
3 * Hold a cached value.
4 *
5 * @remark Copyright 2005 OProfile authors
6 * @remark Read the file COPYING
7 *
8 * @author John Levon
9 */
10
11#ifndef CACHED_VALUE_H
12#define CACHED_VALUE_H
13
14#include "op_exception.h"
15
16/**
17 * Hold a single value, returning a cached value if there is one.
18 */
19template <class T>
20class cached_value
21{
22public:
23	cached_value() : set(false) {}
24
25	typedef T value_type;
26
27	/// return the cached value
28	value_type const get() const {
29		if (!set)
30			throw op_fatal_error("cached value not set");
31		return value;
32	}
33
34	/// return true if a value is cached
35	bool cached() const { return set; }
36
37	/// set the contained value
38	value_type const reset(value_type const & val) {
39		value = val;
40		set = true;
41		return value;
42	}
43
44private:
45	/// the cached value
46	value_type value;
47	/// is the value valid?
48	bool set;
49};
50
51
52#endif /* !CACHED_VALUE_H */
53