[svn] commit: r510 - in /branches/jinmei-dnsrdata/src/lib/dns/cpp: message.cc message.h

BIND 10 source code commits bind10-changes at lists.isc.org
Tue Jan 26 07:11:25 UTC 2010


Author: jinmei
Date: Tue Jan 26 07:11:25 2010
New Revision: 510

Log:
unified the essentially same code logic using template

Modified:
    branches/jinmei-dnsrdata/src/lib/dns/cpp/message.cc
    branches/jinmei-dnsrdata/src/lib/dns/cpp/message.h

Modified: branches/jinmei-dnsrdata/src/lib/dns/cpp/message.cc
==============================================================================
--- branches/jinmei-dnsrdata/src/lib/dns/cpp/message.cc (original)
+++ branches/jinmei-dnsrdata/src/lib/dns/cpp/message.cc Tue Jan 26 07:11:25 2010
@@ -530,169 +530,129 @@
 }
 
 ///
+/// Template version of Section Iterator
+///
+template <typename T>
+struct SectionIteratorImpl {
+    SectionIteratorImpl(const typename vector<T>::const_iterator& it) :
+        it_(it) {}
+    typename vector<T>::const_iterator it_;
+};
+
+template <typename T>
+SectionIterator<T>::SectionIterator(const SectionIteratorImpl<T>& impl)
+{
+    impl_ = new SectionIteratorImpl<T>(impl.it_);
+}
+
+template <typename T>
+SectionIterator<T>::~SectionIterator()
+{
+    delete impl_;
+}
+
+template <typename T>
+SectionIterator<T>::SectionIterator(const SectionIterator<T>& source) :
+    impl_(new SectionIteratorImpl<T>(source.impl_->it_))
+{}
+
+template <typename T>
+void
+SectionIterator<T>::operator=(const SectionIterator<T>& source)
+{
+    if (impl_ == source.impl_) {
+        return;
+    }
+    SectionIteratorImpl<T>* newimpl =
+        new SectionIteratorImpl<T>(source.impl_->it_);
+    delete impl_;
+    impl_ = newimpl;
+}
+
+template <typename T>
+SectionIterator<T>&
+SectionIterator<T>::operator++()
+{
+    ++(impl_->it_);
+    return (*this);
+}
+
+template <typename T>
+SectionIterator<T>
+SectionIterator<T>::operator++(int)
+{
+    SectionIterator<T> tmp(*this);
+    ++(*this);
+    return (tmp);
+}
+
+template <typename T>
+const T&
+SectionIterator<T>::operator*() const
+{
+    return (*(impl_->it_));
+}
+
+template <typename T>
+const T*
+SectionIterator<T>::operator->() const
+{
+    return (impl_->it_.operator->());
+}
+
+template <typename T>
+bool
+SectionIterator<T>::operator!=(const SectionIterator<T>& other) const
+{
+    return (impl_->it_ != other.impl_->it_);
+}
+
+///
 /// Question iterator
 ///
-struct QuestionIteratorImpl {
-    QuestionIteratorImpl(const vector<QuestionPtr>::const_iterator& it) :
-        it_(it) {}
-    vector<QuestionPtr>::const_iterator it_;
-};
+template class SectionIterator<QuestionPtr>;
+typedef SectionIteratorImpl<QuestionPtr> QuestionIteratorImpl;
 
 const QuestionIterator
 Message::beginQuestion() const
 {
-    return QuestionIterator(QuestionIteratorImpl(impl_->questions_.begin()));
+    return (QuestionIterator(QuestionIteratorImpl(impl_->questions_.begin())));
 }
 
 const QuestionIterator
 Message::endQuestion() const
 {
-    return QuestionIterator(QuestionIteratorImpl(impl_->questions_.end()));
-}
-
-QuestionIterator::QuestionIterator(const QuestionIteratorImpl& impl)
-{
-    impl_ = new QuestionIteratorImpl(impl.it_);
-}
-
-QuestionIterator::~QuestionIterator()
-{
-    delete impl_;
-}
-
-QuestionIterator::QuestionIterator(const QuestionIterator& source) :
-    impl_(new QuestionIteratorImpl(source.impl_->it_))
-{}
-
-void
-QuestionIterator::operator=(const QuestionIterator& source)
-{
-    if (impl_ == source.impl_) {
-        return;
-    }
-    QuestionIteratorImpl* newimpl = new QuestionIteratorImpl(source.impl_->it_);
-    delete impl_;
-    impl_ = newimpl;
-}
-
-QuestionIterator&
-QuestionIterator::operator++()
-{
-    ++(impl_->it_);
-    return (*this);
-}
-
-QuestionIterator
-QuestionIterator::operator++(int)
-{
-    QuestionIterator tmp(*this);
-    ++(*this);
-    return (tmp);
-}
-
-const QuestionPtr&
-QuestionIterator::operator*() const
-{
-    return (*(impl_->it_));
-}
-
-const QuestionPtr*
-QuestionIterator::operator->() const
-{
-    return (impl_->it_.operator->());
-}
-
-bool
-QuestionIterator::operator!=(const QuestionIterator& other) const
-{
-    return (impl_->it_ != other.impl_->it_);
+    return (QuestionIterator(QuestionIteratorImpl(impl_->questions_.end())));
 }
 
 ///
 /// RRsets iterators
 ///
-struct SectionIteratorImpl {
-    SectionIteratorImpl(const vector<RRsetPtr>::const_iterator& it) :
-        it_(it) {}
-    vector<RRsetPtr>::const_iterator it_;
-};
-
-const SectionIterator
+template class SectionIterator<RRsetPtr>;
+typedef SectionIteratorImpl<RRsetPtr> RRsetIteratorImpl;
+
+const SectionIterator<RRsetPtr>
 Message::beginSection(const Section& section) const
 {
     if (section == Section::QUESTION()) {
         dns_throw(InvalidMessageSection, "");
     }
 
-    return SectionIterator(SectionIteratorImpl(impl_->rrsets_[sectionCodeToId(section)].begin()));
-}
-
-const SectionIterator
+    return (RRsetIterator(
+                RRsetIteratorImpl(
+                    impl_->rrsets_[sectionCodeToId(section)].begin())));
+}
+
+const SectionIterator<RRsetPtr>
 Message::endSection(const Section& section) const
 {
     if (section == Section::QUESTION()) {
         dns_throw(InvalidMessageSection, "");
     }
 
-    return SectionIterator(SectionIteratorImpl(impl_->rrsets_[sectionCodeToId(section)].end()));
-}
-
-SectionIterator::SectionIterator(const SectionIteratorImpl& impl)
-{
-    impl_ = new SectionIteratorImpl(impl.it_);
-}
-
-SectionIterator::~SectionIterator()
-{
-    delete impl_;
-}
-
-SectionIterator::SectionIterator(const SectionIterator& source) :
-    impl_(new SectionIteratorImpl(source.impl_->it_))
-{}
-
-void
-SectionIterator::operator=(const SectionIterator& source)
-{
-    if (impl_ == source.impl_) {
-        return;
-    }
-    SectionIteratorImpl* newimpl = new SectionIteratorImpl(source.impl_->it_);
-    delete impl_;
-    impl_ = newimpl;
-}
-
-SectionIterator&
-SectionIterator::operator++()
-{
-    ++(impl_->it_);
-    return (*this);
-}
-
-SectionIterator
-SectionIterator::operator++(int)
-{
-    SectionIterator tmp(*this);
-    ++(*this);
-    return (tmp);
-}
-
-const RRsetPtr&
-SectionIterator::operator*() const
-{
-    return (*(impl_->it_));
-}
-
-const RRsetPtr*
-SectionIterator::operator->() const
-{
-    return (impl_->it_.operator->());
-}
-
-bool
-SectionIterator::operator!=(const SectionIterator& other) const
-{
-    return (impl_->it_ != other.impl_->it_);
+    return (RRsetIterator(
+                RRsetIteratorImpl(
+                    impl_->rrsets_[sectionCodeToId(section)].end())));
 }
 
 ostream&

Modified: branches/jinmei-dnsrdata/src/lib/dns/cpp/message.h
==============================================================================
--- branches/jinmei-dnsrdata/src/lib/dns/cpp/message.h (original)
+++ branches/jinmei-dnsrdata/src/lib/dns/cpp/message.h Tue Jan 26 07:11:25 2010
@@ -53,8 +53,8 @@
 class Message;
 struct MessageImpl;
 
-class QuestionIteratorImpl;
-class SectionIteratorImpl;
+template <typename T>
+struct SectionIteratorImpl;
 
 class MessageFlag {
 public:
@@ -439,39 +439,25 @@
     return (s);
 }
 
-class QuestionIterator : public std::iterator<std::input_iterator_tag, RRsetPtr>
-{
-public:
-    QuestionIterator() : impl_(NULL) {}
-    QuestionIterator(const QuestionIteratorImpl& impl);
-    ~QuestionIterator();
-    QuestionIterator(const QuestionIterator& source);
-    void operator=(const QuestionIterator& source);
-    QuestionIterator& operator++();
-    QuestionIterator operator++(int);
-    const QuestionPtr& operator*() const;
-    const QuestionPtr* operator->() const;
-    bool operator!=(const QuestionIterator& other) const;
-private:
-    QuestionIteratorImpl* impl_;
-};
-
-class SectionIterator : public std::iterator<std::input_iterator_tag, RRsetPtr>
-{
-public:
-    SectionIterator() : impl_(NULL) {}
-    SectionIterator(const SectionIteratorImpl& impl);
-    ~SectionIterator();
-    SectionIterator(const SectionIterator& source);
-    void operator=(const SectionIterator& source);
-    SectionIterator& operator++();
-    SectionIterator operator++(int);
-    const RRsetPtr& operator*() const;
-    const RRsetPtr* operator->() const;
-    bool operator!=(const SectionIterator& other) const;
-private:
-    SectionIteratorImpl* impl_;
-};
+template <typename T>
+class SectionIterator : public std::iterator<std::input_iterator_tag, T> {
+public:
+    SectionIterator<T>() : impl_(NULL) {}
+    SectionIterator<T>(const SectionIteratorImpl<T>& impl);
+    ~SectionIterator<T>();
+    SectionIterator<T>(const SectionIterator<T>& source);
+    void operator=(const SectionIterator<T>& source);
+    SectionIterator<T>& operator++();
+    SectionIterator<T> operator++(int);
+    const T& operator*() const;
+    const T* operator->() const;
+    bool operator!=(const SectionIterator<T>& other) const;
+private:
+    SectionIteratorImpl<T>* impl_;
+};
+
+typedef SectionIterator<QuestionPtr> QuestionIterator;
+typedef SectionIterator<RRsetPtr> RRsetIterator;
 
 class Message {
 public:
@@ -499,8 +485,8 @@
     //     of RR in the message
     const QuestionIterator beginQuestion() const;
     const QuestionIterator endQuestion() const;
-    const SectionIterator beginSection(const Section& section) const;
-    const SectionIterator endSection(const Section& section) const;
+    const RRsetIterator beginSection(const Section& section) const;
+    const RRsetIterator endSection(const Section& section) const;
 
     void addQuestion(QuestionPtr question);
     void addQuestion(const Question& question);




More information about the bind10-changes mailing list