Branch data Line data Source code
1 : : #include <cassert>
2 : : #include <stdexcept>
3 : : #include <iterator>
4 : : #include <map>
5 : : #include <boost/noncopyable.hpp>
6 : : #include <boost/shared_ptr.hpp>
7 : :
8 : : #include <statistics/counter_dict.h>
9 : :
10 : : namespace {
11 : : typedef boost::shared_ptr<isc::statistics::Counter> CounterPtr;
12 : : typedef std::map<std::string, CounterPtr> DictionaryMap;
13 : : }
14 : :
15 : : namespace isc {
16 : : namespace statistics {
17 : :
18 : : // Implementation detail class for CounterDictionary::ConstIterator
19 : : class CounterDictionaryConstIteratorImpl;
20 : :
21 : : class CounterDictionaryImpl : boost::noncopyable {
22 : : private:
23 : : DictionaryMap dictionary_;
24 : : std::vector<std::string> elements_;
25 : : const size_t items_;
26 : : // Default constructor is forbidden; number of counter items must be
27 : : // specified at the construction of this class.
28 : : CounterDictionaryImpl();
29 : : public:
30 : : CounterDictionaryImpl(const size_t items);
31 : : ~CounterDictionaryImpl();
32 : : void addElement(const std::string& name);
33 : : void deleteElement(const std::string& name);
34 : : Counter& getElement(const std::string& name);
35 : : public:
36 : : CounterDictionaryConstIteratorImpl begin() const;
37 : : CounterDictionaryConstIteratorImpl end() const;
38 : : };
39 : :
40 : : // Constructor with number of items
41 : 116 : CounterDictionaryImpl::CounterDictionaryImpl(const size_t items) :
42 : 1 : items_(items)
43 : : {
44 : : // The number of items must not be 0
45 [ + + ]: 116 : if (items == 0) {
46 [ + - ][ + - ]: 2 : isc_throw(isc::InvalidParameter, "Items must not be 0");
47 : : }
48 : 115 : }
49 : :
50 : : // Destructor
51 : 230 : CounterDictionaryImpl::~CounterDictionaryImpl() {}
52 : :
53 : : void
54 : 124 : CounterDictionaryImpl::addElement(const std::string& name) {
55 : : // throw if the element already exists
56 [ + + ]: 124 : if (dictionary_.count(name) != 0) {
57 [ + - ][ + - ]: 2 : isc_throw(isc::InvalidParameter,
[ + - ]
58 : : "Element " << name << " already exists");
59 : : }
60 [ - + ]: 123 : assert(items_ != 0);
61 : : // Create a new Counter and add to the map
62 : : dictionary_.insert(
63 [ + - ][ + - ]: 246 : DictionaryMap::value_type(name, CounterPtr(new Counter(items_))));
64 : 123 : }
65 : :
66 : : void
67 : 2 : CounterDictionaryImpl::deleteElement(const std::string& name) {
68 : 4 : size_t result = dictionary_.erase(name);
69 [ + + ]: 2 : if (result != 1) {
70 : : // If an element with specified name does not exist, throw
71 : : // isc::OutOfRange.
72 [ + - ][ + - ]: 2 : isc_throw(isc::OutOfRange, "Element " << name << " does not exist");
[ + - ]
73 : : }
74 : 1 : }
75 : :
76 : : Counter&
77 : 33 : CounterDictionaryImpl::getElement(const std::string& name) {
78 : 66 : DictionaryMap::const_iterator i = dictionary_.find(name);
79 [ + + ]: 33 : if (i != dictionary_.end()) {
80 : : // the key was found. return the element.
81 : 32 : return (*(i->second));
82 : : } else {
83 : : // If an element with specified name does not exist, throw
84 : : // isc::OutOfRange.
85 [ + - ][ + - ]: 2 : isc_throw(isc::OutOfRange, "Element " << name << " does not exist");
[ + - ]
86 : : }
87 : : }
88 : :
89 : : // Constructor
90 : : // Initialize impl_
91 : 116 : CounterDictionary::CounterDictionary(const size_t items) :
92 [ + + ]: 116 : impl_(new CounterDictionaryImpl(items))
93 : 115 : {}
94 : :
95 : : // Destructor
96 : : // impl_ will be freed automatically with scoped_ptr
97 : 115 : CounterDictionary::~CounterDictionary() {}
98 : :
99 : : void
100 : 124 : CounterDictionary::addElement(const std::string& name) {
101 : 124 : impl_->addElement(name);
102 : 123 : }
103 : :
104 : : void
105 : 2 : CounterDictionary::deleteElement(const std::string& name) {
106 : 2 : impl_->deleteElement(name);
107 : 1 : }
108 : :
109 : : Counter&
110 : 3 : CounterDictionary::getElement(const std::string& name) const {
111 : 3 : return (impl_->getElement(name));
112 : : }
113 : :
114 : : Counter&
115 : 30 : CounterDictionary::operator[](const std::string& name) const {
116 : 30 : return (impl_->getElement(name));
117 : : }
118 : :
119 : : // Implementation detail class for CounterDictionary::ConstIterator
120 : : class CounterDictionaryConstIteratorImpl {
121 : : public:
122 : : CounterDictionaryConstIteratorImpl();
123 : : ~CounterDictionaryConstIteratorImpl();
124 : : CounterDictionaryConstIteratorImpl(
125 : : const CounterDictionaryConstIteratorImpl &other);
126 : : CounterDictionaryConstIteratorImpl &operator=(
127 : : const CounterDictionaryConstIteratorImpl &source);
128 : : CounterDictionaryConstIteratorImpl(
129 : : DictionaryMap::const_iterator iterator);
130 : : public:
131 : : void increment();
132 : : const CounterDictionary::ConstIterator::value_type&
133 : : dereference() const;
134 : : bool equal(const CounterDictionaryConstIteratorImpl& other) const;
135 : : private:
136 : : DictionaryMap::const_iterator iterator_;
137 : : };
138 : :
139 : 1 : CounterDictionaryConstIteratorImpl::CounterDictionaryConstIteratorImpl() {}
140 : :
141 : 8 : CounterDictionaryConstIteratorImpl::~CounterDictionaryConstIteratorImpl() {}
142 : :
143 : : // Copy constructor: deep copy of iterator_
144 : 4 : CounterDictionaryConstIteratorImpl::CounterDictionaryConstIteratorImpl(
145 : : const CounterDictionaryConstIteratorImpl &other) :
146 : 4 : iterator_(other.iterator_)
147 : 4 : {}
148 : :
149 : : // Assignment operator: deep copy of iterator_
150 : : CounterDictionaryConstIteratorImpl &
151 : 1 : CounterDictionaryConstIteratorImpl::operator=(
152 : : const CounterDictionaryConstIteratorImpl &source)
153 : : {
154 : 1 : iterator_ = source.iterator_;
155 : 1 : return (*this);
156 : : }
157 : :
158 : : // Constructor from implementation detail DictionaryMap::const_iterator
159 : 3 : CounterDictionaryConstIteratorImpl::CounterDictionaryConstIteratorImpl(
160 : : DictionaryMap::const_iterator iterator) :
161 : 3 : iterator_(iterator)
162 : 3 : {}
163 : :
164 : : CounterDictionaryConstIteratorImpl
165 : 2 : CounterDictionaryImpl::begin() const {
166 : 2 : return (CounterDictionaryConstIteratorImpl(dictionary_.begin()));
167 : : }
168 : :
169 : : CounterDictionaryConstIteratorImpl
170 : 1 : CounterDictionaryImpl::end() const {
171 : 1 : return (CounterDictionaryConstIteratorImpl(dictionary_.end()));
172 : : }
173 : :
174 : : void
175 : 4 : CounterDictionaryConstIteratorImpl::increment() {
176 : 4 : ++iterator_;
177 : 4 : return;
178 : : }
179 : :
180 : : const CounterDictionary::ConstIterator::value_type&
181 : 2 : CounterDictionaryConstIteratorImpl::dereference() const {
182 : 2 : return (iterator_->first);
183 : : }
184 : :
185 : : bool
186 : 12 : CounterDictionaryConstIteratorImpl::equal(
187 : : const CounterDictionaryConstIteratorImpl& other) const
188 : : {
189 : 12 : return (iterator_ == other.iterator_);
190 : : }
191 : :
192 : : CounterDictionary::ConstIterator
193 : 2 : CounterDictionary::begin() const {
194 : : return (CounterDictionary::ConstIterator(
195 [ + - ]: 2 : CounterDictionaryConstIteratorImpl(impl_->begin())));
196 : : }
197 : :
198 : : CounterDictionary::ConstIterator
199 : 1 : CounterDictionary::end() const {
200 : : return (CounterDictionary::ConstIterator(
201 [ + - ]: 1 : CounterDictionaryConstIteratorImpl(impl_->end())));
202 : : }
203 : :
204 : 1 : CounterDictionary::ConstIterator::ConstIterator() :
205 [ + - ]: 1 : impl_(new CounterDictionaryConstIteratorImpl())
206 : 1 : {}
207 : :
208 : 5 : CounterDictionary::ConstIterator::~ConstIterator() {}
209 : :
210 : : // Copy constructor: deep copy of impl_
211 : 1 : CounterDictionary::ConstIterator::ConstIterator(
212 : : const CounterDictionary::ConstIterator& source) :
213 [ + - ]: 1 : impl_(new CounterDictionaryConstIteratorImpl(*(source.impl_)))
214 : 1 : {}
215 : :
216 : : // Assignment operator: deep copy of impl_
217 : : CounterDictionary::ConstIterator &
218 : 1 : CounterDictionary::ConstIterator::operator=(
219 : : const CounterDictionary::ConstIterator &source)
220 : : {
221 : 1 : *impl_ = *source.impl_;
222 : 1 : return (*this);
223 : : }
224 : :
225 : : // The constructor from implementation detail
226 : 3 : CounterDictionary::ConstIterator::ConstIterator(
227 : : const CounterDictionaryConstIteratorImpl& source) :
228 [ + - ]: 3 : impl_(new CounterDictionaryConstIteratorImpl(source))
229 : 3 : {}
230 : :
231 : : const CounterDictionary::ConstIterator::value_type&
232 : 2 : CounterDictionary::ConstIterator::dereference() const
233 : : {
234 : 2 : return (impl_->dereference());
235 : : }
236 : :
237 : : bool
238 : 12 : CounterDictionary::ConstIterator::equal(
239 : : CounterDictionary::ConstIterator const& other) const
240 : : {
241 : 12 : return (impl_->equal(*(other.impl_)));
242 : : }
243 : :
244 : : void
245 : 4 : CounterDictionary::ConstIterator::increment() {
246 : 4 : impl_->increment();
247 : 4 : return;
248 : : }
249 : :
250 : : } // namespace statistics
251 : 8 : } // namespace isc
|