summaryrefslogtreecommitdiffstats
path: root/common/OptionDictionary.h
blob: 275bd01edc2f593d97f2b697f52dab296884efb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * File:	OptionDictionary.h
 *
 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
 * See included license file for license details.
 */
#if !defined(_OptionDictionary_h_)
#define _OptionDictionary_h_

#include "OptionContext.h"
#include <map>

namespace elftosb
{

/*!
 * \brief Concrete implementation of OptionContext.
 *
 * This context subclass supports having a parent context. If an option is not
 * found in the receiving instance, the request is passed to the parent.
 * The hasOption() and getOption() methods will ask up the parent chain
 * if the requested option does not exist in the receiving instance.
 * But the setOption() and deleteOption() methods only operate locally,
 * on the instance on which they were called. This allows a caller to
 * locally override an option value without affecting any of the parent
 * contexts.
 */
class OptionDictionary : public OptionContext
{
public:
	//! \brief Default constructor.
	OptionDictionary() : m_parent(0) {}
	
	//! \brief Constructor taking a parent context.
	OptionDictionary(OptionContext * parent) : m_parent(parent) {}
	
	//! \brief Destructor.
	~OptionDictionary();
	
	//! \name Parents
	//@{
	//! \brief Returns the current parent context.
	//! \return The current parent context instance.
	//! \retval NULL No parent has been set.
	inline OptionContext * getParent() const { return m_parent; }
	
	//! \brief Change the parent context.
	//! \param newParent The parent context object. May be NULL, in which case
	//!		the object will no longer have a parent context.
	inline void setParent(OptionContext * newParent) { m_parent = newParent; }
	//@}
	
	//! \name Options
	//@{
	//! \brief Detemine whether the named option is present in the table.
	virtual bool hasOption(const std::string & name) const;
	
	//! \brief Returns the option's value.
	virtual const Value * getOption(const std::string & name) const;
	
	//! \brief Adds or changes an option's value.
	virtual void setOption(const std::string & name, Value * value);
	
	//! \brief Removes an option from the table.
	virtual void deleteOption(const std::string & name);
	//@}

	//! \name Locking
	//@{
	//! \brief Returns true if the specified option is locked from further changes.
	bool isOptionLocked(const std::string & name) const;

	//! \brief Prevent further modifications of an option's value.
	void lockOption(const std::string & name);

	//! \brief Allow an option to be changed.
	void unlockOption(const std::string & name);
	//@}
	
	//! \name Operators
	//@{
	//! \brief Indexing operator; returns the value for the option \a name.
	const Value * operator [] (const std::string & name) const;
	//@}
	
protected:
	OptionContext * m_parent;	//!< Our parent context.

	/*!
	 * \brief Information about one option's value.
	 */
	struct OptionValue
	{
		Value * m_value;	//!< The object for this option's value.
		bool m_isLocked;	//!< True if this value is locked from further changes.

		//! \brief Constructor.
		OptionValue() : m_value(0), m_isLocked(false) {}
	};
	
	typedef std::map<std::string, OptionValue> option_map_t;	//!< Map from option name to value.
	option_map_t m_options;	//!< The option dictionary.
};

}; // namespace elftosb

#endif // _OptionDictionary_h_