section.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef BL_SECTION_HPP
  2. #define BL_SECTION_HPP
  3. /*!
  4. *\file section.hpp
  5. */
  6. // Config Class
  7. // Author: Charles Gruenwald III
  8. #include "fixed_types.h"
  9. #include "key.hpp"
  10. #include <vector>
  11. #include <map>
  12. namespace config
  13. {
  14. //
  15. class Section;
  16. typedef std::map < String, Section* > SectionList;
  17. typedef std::map < String, Key* > KeyList; // default value
  18. typedef std::map < String, std::vector<Key* > > KeyArrayList; // overriding values
  19. /*! \brief Section: A configuration section entry
  20. * This class is used to hold a given section from a configuration.
  21. * It contains both a list of subsections as well as a list of keys.
  22. * The root of a configuration tree is of this class type.
  23. */
  24. class Section
  25. {
  26. public:
  27. friend class Config;
  28. Section(const String & name, bool case_sensitive);
  29. Section(Section const & parent, const String & name, bool case_sensitive);
  30. ~Section();
  31. //! Determine if this section is the root of the given configuration tree
  32. bool isRoot() const { return m_isroot; }
  33. /*! \brief returns true if the given name is a key within this section
  34. * \param name The name of the key
  35. * \param case_sensitive Whether or not the lookup should care about case
  36. * \return True if name is a key within this section
  37. */
  38. bool hasKey(const String &name, UInt64 index) const;
  39. /*! \brief returns true if the given name is a subsection within this section
  40. * \param name The name of the subsection
  41. * \param case_sensitive Whether or not the lookup should care about case
  42. * \return True if name is a subsection within this section
  43. */
  44. bool hasSection(const String &name) const;
  45. //! SectionList() returns the list of subsections
  46. const SectionList & getSubsections() const { return m_subSections; }
  47. //! Returns the list of keys
  48. const KeyList & getKeys() const { return m_keys; }
  49. //! Returns the list of keys
  50. const KeyArrayList & getArrayKeys() const { return m_array_keys; }
  51. //! Returns the name of this section
  52. String getName() const { return m_name; }
  53. //! getKey() returns a key with the given name
  54. const Key & getKey(const String & name, uint64_t index = UINT64_MAX);
  55. /*! addSubSection() Add a subsection with the given name
  56. * \param name The name of the new subsection
  57. * \return A reference to the newly created subsection
  58. */
  59. Section & addSubsection(const String & name);
  60. /*! addKey() Add a key with the given name
  61. * \param name The name of the new key
  62. * \param value The value of the new key (as a string)
  63. * \return A reference to the newly created key
  64. */
  65. const Key & addKey(const String & name, const String & value, UInt64 index = UINT64_MAX) { return addKeyInternal(name, value, index); }
  66. const Key & addKey(const String & name, const SInt64 & value, UInt64 index = UINT64_MAX) { return addKeyInternal(name, value, index); }
  67. const Key & addKey(const String & name, const double & value, UInt64 index = UINT64_MAX) { return addKeyInternal(name, value, index); }
  68. /*! getSection() Returns a reference to the section with the given name
  69. * \param name The name of the section we are trying to obtain
  70. * \return A reference to the named section, creating it if it doesn't exist
  71. */
  72. const Section & getSection(const String & name);
  73. //! getFullPath() Returns the path from the root to this section
  74. const String getFullPath() const;
  75. /*! getParent() Returns a reference to the parent section.
  76. * Note: The isRoot() method can be used to determine if a section is the root of the tree
  77. */
  78. const Section & getParent() const { return m_parent; }
  79. private:
  80. template <class V>
  81. const Key & addKeyInternal(const String & name, const V & value, UInt64 index = UINT64_MAX);
  82. /*! clear() Clears out any sub-sections, used during a (re)load */
  83. void clear() { m_subSections.clear(); }
  84. Section & getSection_unsafe(const String & name);
  85. String m_name;
  86. //! A list of bl::config::Section subsections.
  87. SectionList m_subSections;
  88. //! A list of bl::config::Key keys in this current section.
  89. KeyList m_keys;
  90. KeyArrayList m_array_keys;
  91. //! A bl::config::Section parent
  92. Section const & m_parent;
  93. //! Whether or not this section is the root section of the config
  94. bool m_isroot;
  95. //! Whether or not key lookups are case-sensitive.
  96. bool m_case_sensitive;
  97. };
  98. }//end of namespace bl
  99. #endif //SECTION_HPP