xref: /netbsd-src/external/bsd/elftosb/dist/common/OptionDictionary.h (revision 993229b6fea628ff8b1fa09146c80b0cfb2768eb)
1 /*
2  * File:	OptionDictionary.h
3  *
4  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5  * See included license file for license details.
6  */
7 #if !defined(_OptionDictionary_h_)
8 #define _OptionDictionary_h_
9 
10 #include "OptionContext.h"
11 #include <map>
12 
13 namespace elftosb
14 {
15 
16 /*!
17  * \brief Concrete implementation of OptionContext.
18  *
19  * This context subclass supports having a parent context. If an option is not
20  * found in the receiving instance, the request is passed to the parent.
21  * The hasOption() and getOption() methods will ask up the parent chain
22  * if the requested option does not exist in the receiving instance.
23  * But the setOption() and deleteOption() methods only operate locally,
24  * on the instance on which they were called. This allows a caller to
25  * locally override an option value without affecting any of the parent
26  * contexts.
27  */
28 class OptionDictionary : public OptionContext
29 {
30 public:
31 	//! \brief Default constructor.
OptionDictionary()32 	OptionDictionary() : m_parent(0) {}
33 
34 	//! \brief Constructor taking a parent context.
OptionDictionary(OptionContext * parent)35 	OptionDictionary(OptionContext * parent) : m_parent(parent) {}
36 
37 	//! \brief Destructor.
38 	~OptionDictionary();
39 
40 	//! \name Parents
41 	//@{
42 	//! \brief Returns the current parent context.
43 	//! \return The current parent context instance.
44 	//! \retval NULL No parent has been set.
getParent()45 	inline OptionContext * getParent() const { return m_parent; }
46 
47 	//! \brief Change the parent context.
48 	//! \param newParent The parent context object. May be NULL, in which case
49 	//!		the object will no longer have a parent context.
setParent(OptionContext * newParent)50 	inline void setParent(OptionContext * newParent) { m_parent = newParent; }
51 	//@}
52 
53 	//! \name Options
54 	//@{
55 	//! \brief Detemine whether the named option is present in the table.
56 	virtual bool hasOption(const std::string & name) const;
57 
58 	//! \brief Returns the option's value.
59 	virtual const Value * getOption(const std::string & name) const;
60 
61 	//! \brief Adds or changes an option's value.
62 	virtual void setOption(const std::string & name, Value * value);
63 
64 	//! \brief Removes an option from the table.
65 	virtual void deleteOption(const std::string & name);
66 	//@}
67 
68 	//! \name Locking
69 	//@{
70 	//! \brief Returns true if the specified option is locked from further changes.
71 	bool isOptionLocked(const std::string & name) const;
72 
73 	//! \brief Prevent further modifications of an option's value.
74 	void lockOption(const std::string & name);
75 
76 	//! \brief Allow an option to be changed.
77 	void unlockOption(const std::string & name);
78 	//@}
79 
80 	//! \name Operators
81 	//@{
82 	//! \brief Indexing operator; returns the value for the option \a name.
83 	const Value * operator [] (const std::string & name) const;
84 	//@}
85 
86 protected:
87 	OptionContext * m_parent;	//!< Our parent context.
88 
89 	/*!
90 	 * \brief Information about one option's value.
91 	 */
92 	struct OptionValue
93 	{
94 		Value * m_value;	//!< The object for this option's value.
95 		bool m_isLocked;	//!< True if this value is locked from further changes.
96 
97 		//! \brief Constructor.
OptionValueOptionValue98 		OptionValue() : m_value(0), m_isLocked(false) {}
99 	};
100 
101 	typedef std::map<std::string, OptionValue> option_map_t;	//!< Map from option name to value.
102 	option_map_t m_options;	//!< The option dictionary.
103 };
104 
105 }; // namespace elftosb
106 
107 #endif // _OptionDictionary_h_
108