counter_dict.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #ifndef __COUNTER_DICT_H
  2. #define __COUNTER_DICT_H 1
  3. #include <string>
  4. #include <vector>
  5. #include <utility>
  6. #include <boost/noncopyable.hpp>
  7. #include <boost/scoped_ptr.hpp>
  8. #include <boost/iterator/iterator_facade.hpp>
  9. #include <exceptions/exceptions.h>
  10. #include <statistics/counter.h>
  11. namespace isc {
  12. namespace statistics {
  13. class CounterDictionaryImpl;
  14. class CounterDictionaryConstIteratorImpl;
  15. class CounterDictionary : boost::noncopyable {
  16. private:
  17. boost::scoped_ptr<CounterDictionaryImpl> impl_;
  18. // Default constructor is forbidden; number of counter items must be
  19. // specified at the construction of this class.
  20. CounterDictionary();
  21. public:
  22. /// The constructor.
  23. /// This constructor is mostly exception free. But it may still throw
  24. /// a standard exception if memory allocation fails inside the method.
  25. ///
  26. /// Note: \a items must be greater than 0; otherwise this constructor
  27. /// causes assertion failure.
  28. ///
  29. /// \param items A number of counter items to hold (greater than 0)
  30. CounterDictionary(const size_t items);
  31. /// The destructor.
  32. ///
  33. /// This method never throws an exception.
  34. ~CounterDictionary();
  35. /// \brief Add an element
  36. ///
  37. /// \throw isc::InvalidParameter \a element already exists.
  38. ///
  39. /// \param name A name of the element to append
  40. void addElement(const std::string& name);
  41. /// \brief Delete
  42. ///
  43. /// \throw isc::OutOfRange \a element does not exist.
  44. ///
  45. /// \param name A name of the element to delete
  46. void deleteElement(const std::string& name);
  47. /// \brief Lookup
  48. ///
  49. /// \throw isc::OutOfRange \a element does not exist.
  50. ///
  51. /// \param name A name of the element to get the counters
  52. Counter& getElement(const std::string &name) const;
  53. /// Same as getElement()
  54. Counter& operator[](const std::string &name) const;
  55. /// \brief A helper structure to represent an element of
  56. /// CounterDictionary. This type is used for the iterator.
  57. struct ValueType {
  58. public:
  59. const std::string& name;
  60. const Counter& element;
  61. ValueType(const std::string& name_, const Counter& element_) :
  62. name(name_), element(element_)
  63. {}
  64. };
  65. /// \brief \c ConstIterator is a constant iterator that provides an
  66. /// interface for accessing elements stored in CounterDictionary.
  67. ///
  68. /// This class is derived from boost::iterator_facade and uses pImpl
  69. /// idiom not to expose implementation detail of
  70. /// CounterDictionary::iterator.
  71. ///
  72. /// It is intended to walk through the elements when sending the
  73. /// counters to statistics module.
  74. class ConstIterator :
  75. public boost::iterator_facade<ConstIterator,
  76. const ValueType,
  77. boost::forward_traversal_tag>
  78. {
  79. private:
  80. boost::scoped_ptr<CounterDictionaryConstIteratorImpl> impl_;
  81. public:
  82. /// The constructor.
  83. ///
  84. /// This constructor is mostly exception free. But it may still
  85. /// throw a standard exception if memory allocation fails
  86. /// inside the method.
  87. ConstIterator();
  88. /// The destructor.
  89. ///
  90. /// This method never throws an exception.
  91. ~ConstIterator();
  92. /// The assignment operator.
  93. ///
  94. /// This method is mostly exception free. But it may still
  95. /// throw a standard exception if memory allocation fails
  96. /// inside the method.
  97. ConstIterator& operator=(const ConstIterator &source);
  98. /// The copy constructor.
  99. ///
  100. /// This constructor is mostly exception free. But it may still
  101. /// throw a standard exception if memory allocation fails
  102. /// inside the method.
  103. ConstIterator(const ConstIterator& source);
  104. /// The constructor from implementation detail.
  105. ///
  106. /// This method is used to create an instance of ConstIterator
  107. /// by CounterDict::begin() and CounterDict::end().
  108. ///
  109. /// This constructor is mostly exception free. But it may still
  110. /// throw a standard exception if memory allocation fails
  111. /// inside the method.
  112. ConstIterator(
  113. const CounterDictionaryConstIteratorImpl& source);
  114. private:
  115. /// \brief An internal method to increment this iterator.
  116. void increment();
  117. /// \brief An internal method to check equality.
  118. bool equal(const ConstIterator& other) const;
  119. /// \brief An internal method to dereference this iterator.
  120. const value_type dereference() const;
  121. private:
  122. friend class boost::iterator_core_access;
  123. };
  124. typedef ConstIterator const_iterator;
  125. /// \brief Return an iterator corresponding to the beginning of the
  126. /// elements stored in CounterDictionary.
  127. ///
  128. /// This method is mostly exception free. But it may still throw a
  129. /// standard exception if memory allocation fails inside the method.
  130. const_iterator begin() const;
  131. /// \brief Return an iterator corresponding to the end of the elements
  132. /// stored in CounterDictionary.
  133. ///
  134. /// This method is mostly exception free. But it may still throw a
  135. /// standard exception if memory allocation fails inside the method.
  136. const_iterator end() const;
  137. };
  138. } // namespace statistics
  139. } // namespace isc
  140. #endif