Ticket #55: rrsetlist.diff

File rrsetlist.diff, 4.6 KB (added by jinmei, 10 years ago)
  • rrsetlist.cc

     
    3030namespace dns {
    3131
    3232void
    33 RRsetList::addRRset(const RRsetPtr rrsetptr)
     33RRsetList::addRRset(ConstRRsetPtr rrsetptr)
    3434{
    35     const RRsetPtr rrset_found = findRRset(rrsetptr->getType(),
    36                                            rrsetptr->getClass());
     35    ConstRRsetPtr rrset_found = findRRset(rrsetptr->getType(),
     36                                          rrsetptr->getClass());
    3737    if (rrset_found) {
    3838        dns_throw(DuplicateRRset, "");
    3939    }
    4040    rrsets_.push_back(rrsetptr);
    4141}
    4242
    43 const RRsetPtr
    44 RRsetList::findRRset(const RRsetPtr rrsetptr)
     43ConstRRsetPtr
     44RRsetList::findRRset(ConstRRsetPtr rrsetptr)
    4545{
    46     BOOST_FOREACH(const RRsetPtr t, rrsets_) {
     46    BOOST_FOREACH(ConstRRsetPtr t, rrsets_) {
    4747        if (rrsetptr == t) {
    4848            return rrsetptr;
    4949        }
     
    5151    return RRsetPtr();
    5252}
    5353
    54 const RRsetPtr
     54ConstRRsetPtr
    5555RRsetList::findRRset(const RRType& rrtype, const RRClass& rrclass)
    5656{
    57     BOOST_FOREACH(const RRsetPtr rrsetptr, rrsets_) {
     57    BOOST_FOREACH(ConstRRsetPtr rrsetptr, rrsets_) {
    5858        const AbstractRRset* rrset = rrsetptr.get();
    5959        if ((rrset->getClass() == rrclass) && (rrset->getType() == rrtype)) {
    6060            return rrsetptr;
  • rrsetlist.h

     
    1818#define __RRSETLIST_H 1
    1919
    2020#include <iostream>
     21#include <iterator>
    2122#include <vector>
    2223
    2324#include <boost/shared_ptr.hpp>
     
    3536        isc::Exception(file, line, what) {}
    3637};
    3738
     39template <typename T>
     40class RRsetListIterator :
     41        public std::iterator<std::input_iterator_tag, ConstRRsetPtr> {
     42public:
     43    RRsetListIterator() {}
     44    explicit RRsetListIterator(const T& it) :
     45        it_(it) {}
     46    RRsetListIterator& operator++()
     47    {
     48        ++it_;
     49        return (*this);
     50    }
     51    RRsetListIterator operator++(int)
     52    {
     53        RRsetListIterator tmp(*this);
     54        ++it_;
     55        return (tmp);
     56    }
     57    ConstRRsetPtr& operator*() const
     58    {
     59        return (*it_);
     60    }
     61    ConstRRsetPtr* operator->() const
     62    {
     63        return (it_.operator->());
     64    }
     65    bool operator==(const RRsetListIterator& other)
     66    {
     67        return (it_ == other.it_);
     68    }
     69   
     70private:
     71    T it_;
     72};
     73
    3874class RRsetList {
    3975public:
    40     void addRRset(const RRsetPtr new_rrsetptr);
    41     const RRsetPtr findRRset(const RRType& rrtype,
    42                              const RRClass& rrclass = RRClass::IN());
    43     const RRsetPtr findRRset(const RRsetPtr);
     76    void addRRset(ConstRRsetPtr new_rrsetptr);
     77    ConstRRsetPtr findRRset(const RRType& rrtype,
     78                            const RRClass& rrclass = RRClass::IN());
     79    ConstRRsetPtr findRRset(ConstRRsetPtr);
    4480
    45     const RRsetPtr operator[](RRType t) { return (this->findRRset(t)); }
     81    ConstRRsetPtr operator[](RRType t) { return (this->findRRset(t)); }
    4682
    47     typedef std::vector<RRsetPtr>::const_iterator const_iterator;
    48     const_iterator begin() const { return (rrsets_.begin()); }
    49     const_iterator end() const { return (rrsets_.end)(); }
     83    typedef RRsetListIterator<std::vector<ConstRRsetPtr>::iterator> iterator;
     84    typedef RRsetListIterator<std::vector<ConstRRsetPtr>::const_iterator>
     85    const_iterator;
    5086
    51     typedef std::vector<RRsetPtr>::iterator iterator;
    52     iterator begin() { return (rrsets_.begin()); }
    53     iterator end() { return (rrsets_.end)(); }
     87    const_iterator begin() const { return (const_iterator(rrsets_.begin())); }
     88    const_iterator end() const { return (const_iterator(rrsets_.end())); }
    5489
     90    iterator begin() { return (iterator(rrsets_.begin())); }
     91    iterator end() { return (iterator(rrsets_.end())); }
     92
    5593    size_t size() const { return (rrsets_.size()); }
    5694
    5795private:
    58     std::vector<RRsetPtr> rrsets_;
     96    std::vector<ConstRRsetPtr> rrsets_;
    5997};
    6098
    6199} // end of namespace dns
  • rrsetlist_unittest.cc

     
    104104    RRsetList list;
    105105    setupList(list);
    106106
    107     RRsetPtr p;
     107    ConstRRsetPtr p;
    108108
    109109    p = list[RRType::CNAME()];
    110110    EXPECT_TRUE(p->getType() == RRType::CNAME());
     
    147147
    148148    bool has_a, has_aaaa, has_ns, has_soa, has_cname;
    149149    int i = 0;
    150     BOOST_FOREACH(RRsetPtr rrset, list) {
     150    BOOST_FOREACH(ConstRRsetPtr rrset, list) {
    151151        if (rrset->getType() == RRType::A()) {
    152152            has_a = true;
    153153        }