|
@@ -15,67 +15,45 @@
|
|
#ifndef __COUNTER_DICT_H
|
|
#ifndef __COUNTER_DICT_H
|
|
#define __COUNTER_DICT_H 1
|
|
#define __COUNTER_DICT_H 1
|
|
|
|
|
|
-#include <string>
|
|
|
|
-#include <vector>
|
|
|
|
-#include <utility>
|
|
|
|
|
|
+#include <statistics/counter.h>
|
|
|
|
+#include <exceptions/exceptions.h>
|
|
|
|
+
|
|
#include <boost/noncopyable.hpp>
|
|
#include <boost/noncopyable.hpp>
|
|
#include <boost/scoped_ptr.hpp>
|
|
#include <boost/scoped_ptr.hpp>
|
|
|
|
+#include <boost/shared_ptr.hpp>
|
|
#include <boost/iterator/iterator_facade.hpp>
|
|
#include <boost/iterator/iterator_facade.hpp>
|
|
|
|
|
|
-#include <exceptions/exceptions.h>
|
|
|
|
-#include <statistics/counter.h>
|
|
|
|
|
|
+#include <cassert>
|
|
|
|
+#include <stdexcept>
|
|
|
|
+#include <string>
|
|
|
|
+#include <vector>
|
|
|
|
+#include <map>
|
|
|
|
+#include <iterator>
|
|
|
|
+#include <utility>
|
|
|
|
+
|
|
|
|
+namespace {
|
|
|
|
+typedef boost::shared_ptr<isc::statistics::Counter> CounterPtr;
|
|
|
|
+typedef std::map<std::string, CounterPtr> DictionaryMap;
|
|
|
|
+}
|
|
|
|
|
|
namespace isc {
|
|
namespace isc {
|
|
namespace statistics {
|
|
namespace statistics {
|
|
|
|
|
|
-class CounterDictionaryImpl;
|
|
|
|
-class CounterDictionaryConstIteratorImpl;
|
|
|
|
-
|
|
|
|
class CounterDictionary : boost::noncopyable {
|
|
class CounterDictionary : boost::noncopyable {
|
|
private:
|
|
private:
|
|
- boost::scoped_ptr<CounterDictionaryImpl> impl_;
|
|
|
|
|
|
+ DictionaryMap dictionary_;
|
|
|
|
+ std::vector<std::string> elements_;
|
|
|
|
+ const size_t items_;
|
|
// Default constructor is forbidden; number of counter items must be
|
|
// Default constructor is forbidden; number of counter items must be
|
|
// specified at the construction of this class.
|
|
// specified at the construction of this class.
|
|
CounterDictionary();
|
|
CounterDictionary();
|
|
public:
|
|
public:
|
|
- /// The constructor.
|
|
|
|
- /// This constructor is mostly exception free. But it may still throw
|
|
|
|
- /// a standard exception if memory allocation fails inside the method.
|
|
|
|
- ///
|
|
|
|
- /// \param items A number of counter items to hold (greater than 0)
|
|
|
|
- ///
|
|
|
|
- /// \throw isc::InvalidParameter \a items is 0
|
|
|
|
- CounterDictionary(const size_t items);
|
|
|
|
-
|
|
|
|
- /// The destructor.
|
|
|
|
- ///
|
|
|
|
- /// This method never throws an exception.
|
|
|
|
- ~CounterDictionary();
|
|
|
|
-
|
|
|
|
- /// \brief Add an element
|
|
|
|
- ///
|
|
|
|
- /// \throw isc::InvalidParameter \a element already exists.
|
|
|
|
- ///
|
|
|
|
- /// \param name A name of the element to append
|
|
|
|
- void addElement(const std::string& name);
|
|
|
|
-
|
|
|
|
- /// \brief Delete
|
|
|
|
- ///
|
|
|
|
- /// \throw isc::OutOfRange \a element does not exist.
|
|
|
|
- ///
|
|
|
|
- /// \param name A name of the element to delete
|
|
|
|
- void deleteElement(const std::string& name);
|
|
|
|
-
|
|
|
|
- /// \brief Lookup
|
|
|
|
- ///
|
|
|
|
- /// \throw isc::OutOfRange \a element does not exist.
|
|
|
|
- ///
|
|
|
|
- /// \param name A name of the element to get the counters
|
|
|
|
- Counter& getElement(const std::string &name) const;
|
|
|
|
-
|
|
|
|
- /// Same as getElement()
|
|
|
|
- Counter& operator[](const std::string &name) const;
|
|
|
|
-
|
|
|
|
|
|
+ explicit inline CounterDictionary(const size_t items);
|
|
|
|
+ inline ~CounterDictionary();
|
|
|
|
+ inline void addElement(const std::string& name);
|
|
|
|
+ inline void deleteElement(const std::string& name);
|
|
|
|
+ inline Counter& getElement(const std::string& name);
|
|
|
|
+ inline Counter& operator[](const std::string& name);
|
|
/// \brief \c ConstIterator is a constant iterator that provides an
|
|
/// \brief \c ConstIterator is a constant iterator that provides an
|
|
/// interface for enumerating name of zones stored in CounterDictionary.
|
|
/// interface for enumerating name of zones stored in CounterDictionary.
|
|
///
|
|
///
|
|
@@ -90,68 +68,134 @@ public:
|
|
const std::string,
|
|
const std::string,
|
|
boost::forward_traversal_tag>
|
|
boost::forward_traversal_tag>
|
|
{
|
|
{
|
|
- private:
|
|
|
|
- boost::scoped_ptr<CounterDictionaryConstIteratorImpl> impl_;
|
|
|
|
public:
|
|
public:
|
|
/// The constructor.
|
|
/// The constructor.
|
|
///
|
|
///
|
|
/// This constructor is mostly exception free. But it may still
|
|
/// This constructor is mostly exception free. But it may still
|
|
/// throw a standard exception if memory allocation fails
|
|
/// throw a standard exception if memory allocation fails
|
|
/// inside the method.
|
|
/// inside the method.
|
|
- ConstIterator();
|
|
|
|
|
|
+ inline ConstIterator() {}
|
|
/// The destructor.
|
|
/// The destructor.
|
|
///
|
|
///
|
|
/// This method never throws an exception.
|
|
/// This method never throws an exception.
|
|
- ~ConstIterator();
|
|
|
|
|
|
+ inline ~ConstIterator() {}
|
|
/// The assignment operator.
|
|
/// The assignment operator.
|
|
///
|
|
///
|
|
/// This method is mostly exception free. But it may still
|
|
/// This method is mostly exception free. But it may still
|
|
/// throw a standard exception if memory allocation fails
|
|
/// throw a standard exception if memory allocation fails
|
|
/// inside the method.
|
|
/// inside the method.
|
|
- ConstIterator& operator=(const ConstIterator &source);
|
|
|
|
|
|
+ inline ConstIterator& operator=(const ConstIterator& source) {
|
|
|
|
+ iterator_ = source.iterator_;
|
|
|
|
+ return (*this);
|
|
|
|
+ }
|
|
|
|
+
|
|
/// The copy constructor.
|
|
/// The copy constructor.
|
|
///
|
|
///
|
|
/// This constructor is mostly exception free. But it may still
|
|
/// This constructor is mostly exception free. But it may still
|
|
/// throw a standard exception if memory allocation fails
|
|
/// throw a standard exception if memory allocation fails
|
|
/// inside the method.
|
|
/// inside the method.
|
|
- ConstIterator(const ConstIterator& source);
|
|
|
|
- /// The constructor from implementation detail.
|
|
|
|
- ///
|
|
|
|
- /// This method is used to create an instance of ConstIterator
|
|
|
|
- /// by CounterDict::begin() and CounterDict::end().
|
|
|
|
- ///
|
|
|
|
- /// This constructor is mostly exception free. But it may still
|
|
|
|
- /// throw a standard exception if memory allocation fails
|
|
|
|
- /// inside the method.
|
|
|
|
- ConstIterator(
|
|
|
|
- const CounterDictionaryConstIteratorImpl& source);
|
|
|
|
|
|
+ inline ConstIterator(const ConstIterator& source) :
|
|
|
|
+ iterator_(source.iterator_)
|
|
|
|
+ {}
|
|
|
|
+ //
|
|
|
|
+ // Constructor from implementation detail DictionaryMap::const_iterator
|
|
|
|
+ inline ConstIterator(
|
|
|
|
+ DictionaryMap::const_iterator iterator) :
|
|
|
|
+ iterator_(iterator)
|
|
|
|
+ {}
|
|
|
|
+
|
|
private:
|
|
private:
|
|
/// \brief An internal method to increment this iterator.
|
|
/// \brief An internal method to increment this iterator.
|
|
- void increment();
|
|
|
|
|
|
+ inline void increment() {
|
|
|
|
+ ++iterator_;
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
/// \brief An internal method to check equality.
|
|
/// \brief An internal method to check equality.
|
|
- bool equal(const ConstIterator& other) const;
|
|
|
|
|
|
+ inline bool equal(const ConstIterator& other) const {
|
|
|
|
+ return (iterator_ == other.iterator_);
|
|
|
|
+ }
|
|
|
|
+
|
|
/// \brief An internal method to dereference this iterator.
|
|
/// \brief An internal method to dereference this iterator.
|
|
- const value_type& dereference() const;
|
|
|
|
|
|
+ inline const value_type& dereference() const {
|
|
|
|
+ return (iterator_->first);
|
|
|
|
+ }
|
|
|
|
+
|
|
private:
|
|
private:
|
|
friend class boost::iterator_core_access;
|
|
friend class boost::iterator_core_access;
|
|
|
|
+ DictionaryMap::const_iterator iterator_;
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+ inline ConstIterator begin() const;
|
|
|
|
+ inline ConstIterator end() const;
|
|
|
|
+
|
|
typedef ConstIterator const_iterator;
|
|
typedef ConstIterator const_iterator;
|
|
|
|
+};
|
|
|
|
|
|
- /// \brief Return an iterator corresponding to the beginning of the
|
|
|
|
- /// elements stored in CounterDictionary.
|
|
|
|
- ///
|
|
|
|
- /// This method is mostly exception free. But it may still throw a
|
|
|
|
- /// standard exception if memory allocation fails inside the method.
|
|
|
|
- const_iterator begin() const;
|
|
|
|
|
|
|
|
- /// \brief Return an iterator corresponding to the end of the elements
|
|
|
|
- /// stored in CounterDictionary.
|
|
|
|
- ///
|
|
|
|
- /// This method is mostly exception free. But it may still throw a
|
|
|
|
- /// standard exception if memory allocation fails inside the method.
|
|
|
|
- const_iterator end() const;
|
|
|
|
-};
|
|
|
|
|
|
+inline CounterDictionary::ConstIterator
|
|
|
|
+CounterDictionary::begin() const {
|
|
|
|
+ return (CounterDictionary::ConstIterator(dictionary_.begin()));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+inline CounterDictionary::ConstIterator
|
|
|
|
+CounterDictionary::end() const {
|
|
|
|
+ return (CounterDictionary::ConstIterator(dictionary_.end()));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Constructor with number of items
|
|
|
|
+inline CounterDictionary::CounterDictionary(const size_t items) :
|
|
|
|
+ items_(items)
|
|
|
|
+{
|
|
|
|
+ // The number of items must not be 0
|
|
|
|
+ if (items == 0) {
|
|
|
|
+ isc_throw(isc::InvalidParameter, "Items must not be 0");
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Destructor
|
|
|
|
+inline CounterDictionary::~CounterDictionary() {}
|
|
|
|
+
|
|
|
|
+inline void
|
|
|
|
+CounterDictionary::addElement(const std::string& name) {
|
|
|
|
+ // throw if the element already exists
|
|
|
|
+ if (dictionary_.count(name) != 0) {
|
|
|
|
+ isc_throw(isc::InvalidParameter,
|
|
|
|
+ "Element " << name << " already exists");
|
|
|
|
+ }
|
|
|
|
+ assert(items_ != 0);
|
|
|
|
+ // Create a new Counter and add to the map
|
|
|
|
+ dictionary_.insert(
|
|
|
|
+ DictionaryMap::value_type(name, CounterPtr(new Counter(items_))));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+inline void
|
|
|
|
+CounterDictionary::deleteElement(const std::string& name) {
|
|
|
|
+ size_t result = dictionary_.erase(name);
|
|
|
|
+ if (result != 1) {
|
|
|
|
+ // If an element with specified name does not exist, throw
|
|
|
|
+ // isc::OutOfRange.
|
|
|
|
+ isc_throw(isc::OutOfRange, "Element " << name << " does not exist");
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+inline Counter&
|
|
|
|
+CounterDictionary::getElement(const std::string& name) {
|
|
|
|
+ DictionaryMap::const_iterator i = dictionary_.find(name);
|
|
|
|
+ if (i != dictionary_.end()) {
|
|
|
|
+ // the key was found. return the element.
|
|
|
|
+ return (*(i->second));
|
|
|
|
+ } else {
|
|
|
|
+ // If an element with specified name does not exist, throw
|
|
|
|
+ // isc::OutOfRange.
|
|
|
|
+ isc_throw(isc::OutOfRange, "Element " << name << " does not exist");
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+inline Counter&
|
|
|
|
+CounterDictionary::operator[](const std::string& name) {
|
|
|
|
+ return (getElement(name));
|
|
|
|
+}
|
|
|
|
|
|
} // namespace statistics
|
|
} // namespace statistics
|
|
} // namespace isc
|
|
} // namespace isc
|