Browse Source

[trac510] define a struct as a value_type of CounterDictionary

Yoshitaka Aharen 13 years ago
parent
commit
3582ccf1eb

+ 21 - 21
src/lib/statistics/counter_dict.cc

@@ -29,9 +29,9 @@ class CounterDictionaryImpl : boost::noncopyable {
     public:
         CounterDictionaryImpl(const size_t items);
         ~CounterDictionaryImpl();
-        void addElement(const std::string& element);
-        void deleteElement(const std::string& elemet);
-        Counter& getElement(const std::string& element);
+        void addElement(const std::string& name);
+        void deleteElement(const std::string& name);
+        Counter& getElement(const std::string& name);
     public:
         CounterDictionaryConstIteratorImpl begin() const;
         CounterDictionaryConstIteratorImpl end() const;
@@ -49,35 +49,35 @@ CounterDictionaryImpl::CounterDictionaryImpl(const size_t items) :
 CounterDictionaryImpl::~CounterDictionaryImpl() {}
 
 void
-CounterDictionaryImpl::addElement(const std::string& element) {
+CounterDictionaryImpl::addElement(const std::string& name) {
     // throw if the element already exists
-    if (dictionary_.count(element) != 0) {
+    if (dictionary_.count(name) != 0) {
         isc_throw(isc::InvalidParameter,
-                  "Element " << element << " already exists");
+                  "Element " << name << " already exists");
     }
     // Create a new Counter and add to the map
     dictionary_.insert(
-        DictionaryMap::value_type(element, CounterPtr(new Counter(items_))));
+        DictionaryMap::value_type(name, CounterPtr(new Counter(items_))));
 }
 
 void
-CounterDictionaryImpl::deleteElement(const std::string& element) {
-    size_t result = dictionary_.erase(element);
+CounterDictionaryImpl::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 " << element << " does not exist");
+        isc_throw(isc::OutOfRange, "Element " << name << " does not exist");
     }
 }
 
 Counter&
-CounterDictionaryImpl::getElement(const std::string& element) {
+CounterDictionaryImpl::getElement(const std::string& name) {
     try {
-        return (*(dictionary_.at(element)));
+        return (*(dictionary_.at(name)));
     } catch (const std::out_of_range &e) {
         // If an element with specified name does not exist, throw
         // isc::OutOfRange.
-        isc_throw(isc::OutOfRange, "Element " << element << " does not exist");
+        isc_throw(isc::OutOfRange, "Element " << name << " does not exist");
     }
 }
 
@@ -92,23 +92,23 @@ CounterDictionary::CounterDictionary(const size_t items) :
 CounterDictionary::~CounterDictionary() {}
 
 void
-CounterDictionary::addElement(const std::string& element) {
-    impl_->addElement(element);
+CounterDictionary::addElement(const std::string& name) {
+    impl_->addElement(name);
 }
 
 void
-CounterDictionary::deleteElement(const std::string& element) {
-    impl_->deleteElement(element);
+CounterDictionary::deleteElement(const std::string& name) {
+    impl_->deleteElement(name);
 }
 
 Counter&
-CounterDictionary::getElement(const std::string& element) const {
-    return (impl_->getElement(element));
+CounterDictionary::getElement(const std::string& name) const {
+    return (impl_->getElement(name));
 }
 
 Counter&
-CounterDictionary::operator[](const std::string& element) const {
-    return (impl_->getElement(element));
+CounterDictionary::operator[](const std::string& name) const {
+    return (impl_->getElement(name));
 }
 
 // Implementation detail class for CounterDictionary::ConstIterator

+ 20 - 13
src/lib/statistics/counter_dict.h

@@ -28,7 +28,7 @@ class CounterDictionary : boost::noncopyable {
         /// This constructor is mostly exception free. But it may still throw
         /// a standard exception if memory allocation fails inside the method.
         ///
-        /// Note: \a items must be greater than 0; otherwise thist constructor
+        /// Note: \a items must be greater than 0; otherwise this constructor
         /// causes assertion failure.
         /// 
         /// \param items A number of counter items to hold (greater than 0)
@@ -41,31 +41,38 @@ class CounterDictionary : boost::noncopyable {
 
         /// \brief Add an element
         ///
-        /// \throw isc::InvalidParameter \a element alerady exists.
+        /// \throw isc::InvalidParameter \a element already exists.
         ///
-        /// \param element A name of the element to append
-        void addElement(const std::string& element);
+        /// \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 element A name of the element to delete
-        void deleteElement(const std::string& element);
+        /// \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 element A name of the element to get the counters
-        Counter& getElement(const std::string &element) const;
+        /// \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 &element) const;
+        Counter& operator[](const std::string &name) const;
 
-        /// Typedef for the pair which represents an element of
+        /// \brief A helper structure to represent an element of
         /// CounterDictionary. This type is used for the iterator.
-        typedef std::pair<const std::string&, const Counter&> ValueType;
+        struct ValueType {
+            public:
+            const std::string& name;
+            const Counter& element;
+            ValueType(const std::string& name_, const Counter& element_) :
+                name(name_), element(element_)
+            {}
+        };
 
         /// \brief \c ConstIterator is a constant iterator that provides an
         /// interface for accessing elements stored in CounterDictionary.
@@ -84,13 +91,13 @@ class CounterDictionary : boost::noncopyable {
             private:
                 boost::scoped_ptr<CounterDictionaryConstIteratorImpl> impl_;
             public:
-                /// The constrcutor.
+                /// The constructor.
                 ///
                 /// This constructor is mostly exception free. But it may still
                 /// throw a standard exception if memory allocation fails
                 /// inside the method.
                 ConstIterator();
-                /// The destrcutor.
+                /// The destructor.
                 /// 
                 /// This method never throws an exception.
                 ~ConstIterator();

+ 6 - 6
src/lib/statistics/tests/counter_dict_unittest.cc

@@ -144,17 +144,17 @@ TEST_F(CounterDictionaryTest, iteratorTest) {
     BOOST_FOREACH(CounterDictionary::ValueType i,
                   static_cast<const CounterDictionary &>(counters))
     {
-        if (i.first == "test" && element_test_visited == false) {
+        if (i.name == "test" && element_test_visited == false) {
             element_test_visited = true;
             // Check if the counters have expected value
-            EXPECT_EQ(i.second.get(ITEM1), 1);
-            EXPECT_EQ(i.second.get(ITEM2), 0);
-        } else if (i.first == "sub.test" &&
+            EXPECT_EQ(i.element.get(ITEM1), 1);
+            EXPECT_EQ(i.element.get(ITEM2), 0);
+        } else if (i.name == "sub.test" &&
                    element_sub_test_visited == false) {
             element_sub_test_visited = true;
             // Check if the counters have expected value
-            EXPECT_EQ(i.second.get(ITEM1), 0);
-            EXPECT_EQ(i.second.get(ITEM2), 2);
+            EXPECT_EQ(i.element.get(ITEM1), 0);
+            EXPECT_EQ(i.element.get(ITEM2), 2);
         } else {
             // Test fails when reaches here: the element is not expected or
             //  the element appeared twice