rrset_collection_base.h 5.45 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#ifndef RRSET_COLLECTION_BASE_H
#define RRSET_COLLECTION_BASE_H 1

#include <dns/rrset.h>
#include <dns/name.h>

#include <boost/shared_ptr.hpp>

#include <iterator>

namespace isc {
namespace dns {

/// \brief Generic class to represent a set of RRsets.
///
/// This is a generic container and the stored set of RRsets does not
/// necessarily form a valid zone (e.g. there doesn't necessarily have
/// to be an SOA at the "origin"). Instead, it will be used to represent
/// a single zone for the purpose of zone loading/checking. It provides
/// a simple find() method to find an RRset for the given name and type
/// (and maybe class) and a way to iterate over all RRsets.
///
/// See \c RRsetCollection for a simple libdns++ implementation using an
/// STL container. libdatasrc will have another implementation.
class RRsetCollectionBase {
public:
    /// \brief Find a matching RRset in the collection.
    ///
    /// Returns the RRset in the collection that exactly matches the
    /// given \c name and \c rrtype.  If no matching RRset is found,
    /// \c NULL is returned.
    ///
    /// \param name The name of the RRset to search for.
    /// \param rrtype The type of the RRset to search for.
49
    /// \param rrclass The class of the RRset to search for.
50
    /// \returns A pointer to the RRset if found, \c NULL otherwise.
51
52
    virtual const isc::dns::AbstractRRset* find
        (const isc::dns::Name& name, const isc::dns::RRType& rrtype,
Mukund Sivaraman's avatar
Mukund Sivaraman committed
53
         const isc::dns::RRClass& rrclass)
54
55
        const = 0;

56
57
58
    /// \brief Destructor
    virtual ~RRsetCollectionBase() {}

59
60
protected:
    class Iter; // forward declaration
61
62

    /// \brief Wraps Iter with a reference count.
63
64
    typedef boost::shared_ptr<Iter> IterPtr;

65
66
67
68
69
70
71
72
    /// \brief A helper iterator interface for \c RRsetCollectionBase.
    ///
    /// This is a protected iterator class that is a helper interface
    /// used by the public iterator.  Derived classes of
    /// \c RRsetCollectionBase are supposed to implement this class and
    /// the \c getBeginning() and \c getEnd() methods, so that the
    /// public interator interface can be provided. This is a forward
    /// iterator only.
73
74
    class Iter {
    public:
75
76
        /// \brief Returns the \c AbstractRRset currently pointed to by
        /// the iterator.
77
        virtual const isc::dns::AbstractRRset& getValue() = 0;
78
79
80

        /// \brief Returns an \c IterPtr wrapping an Iter pointing to
        /// the next \c AbstractRRset in sequence in the collection.
81
        virtual IterPtr getNext() = 0;
82
83
84
85
86
87
88
89
90
91

        /// \brief Check if another iterator is equal to this one.
        ///
        /// Returns \c true if this iterator is equal to \c other,
        /// \c false otherwise. Note that if \c other is not the same
        /// type as \c this, or cannot be compared meaningfully, the
        /// method must return \c false.
        ///
        /// \param other The other iterator to compare against.
        /// \returns \c true if equal, \c false otherwise.
92
93
94
        virtual bool equals(Iter& other) = 0;
    };

95
96
    /// \brief Returns an \c IterPtr wrapping an Iter pointing to the
    /// beginning of the collection.
97
    virtual IterPtr getBeginning() = 0;
98
99
100

    /// \brief Returns an \c IterPtr wrapping an Iter pointing past the
    /// end of the collection.
101
102
103
    virtual IterPtr getEnd() = 0;

public:
104
105
106
107
    /// \brief A forward \c std::iterator for \c RRsetCollectionBase.
    ///
    /// It behaves like a \c std::iterator forward iterator, so please
    /// see its documentation for usage.
108
    class Iterator : std::iterator<std::forward_iterator_tag,
109
110
111
                                   const isc::dns::AbstractRRset>
    {
    public:
112
        explicit Iterator(IterPtr iter) :
113
114
115
116
117
118
119
            iter_(iter)
        {}

        reference operator*() {
            return (iter_->getValue());
        }

120
        Iterator& operator++() {
121
122
123
124
            iter_ = iter_->getNext();
            return (*this);
        }

125
126
        Iterator operator++(int) {
            Iterator tmp(iter_);
127
128
129
130
            ++*this;
            return (tmp);
        }

131
        bool operator==(const Iterator& other) const {
132
133
134
            return (iter_->equals(*other.iter_));
        }

135
        bool operator!=(const Iterator& other) const {
136
137
138
139
140
141
142
            return (!iter_->equals(*other.iter_));
        }

    private:
        IterPtr iter_;
    };

143
144
    /// \brief Returns an iterator pointing to the beginning of the
    /// collection.
145
146
    Iterator begin() {
      return Iterator(getBeginning());
147
148
    }

149
150
    /// \brief Returns an iterator pointing past the end of the
    /// collection.
151
152
    Iterator end() {
      return Iterator(getEnd());
153
154
155
156
157
158
159
160
161
162
163
    }
};

} // end of namespace dns
} // end of namespace isc

#endif  // RRSET_COLLECTION_BASE_H

// Local Variables:
// mode: c++
// End: