Line data Source code
1 : /**
2 : * @file BDictionary.cpp
3 : * @copyright (c) 2014 by Petr Zemek (s3rvac@gmail.com) and contributors
4 : * @license BSD, see the @c LICENSE file for more details
5 : * @brief Implementation of the BDictionary class.
6 : */
7 :
8 : #include "BDictionary.h"
9 :
10 : #include <cassert>
11 :
12 : #include "BItemVisitor.h"
13 : #include "BString.h"
14 :
15 : namespace bencoding {
16 :
17 : /**
18 : * @brief Checks if <tt>lhs->value() < rhs->value()</tt>.
19 : *
20 : * @return @c true if <tt>lhs->value() < rhs->value()</tt>, @c false otherwise.
21 : */
22 24 : bool BDictionary::BStringByValueComparator::operator()(
23 : const std::shared_ptr<BString> &lhs,
24 : const std::shared_ptr<BString> &rhs) const {
25 24 : return lhs->value() < rhs->value();
26 : }
27 :
28 : /**
29 : * @brief Constructs an empty dictionary.
30 : */
31 : BDictionary::BDictionary() = default;
32 :
33 : /**
34 : * @brief Constructs a dictionary from the given items.
35 : */
36 2 : BDictionary::BDictionary(std::initializer_list<value_type> items):
37 2 : itemMap(items) {}
38 :
39 : /**
40 : * @brief Creates and returns a new dictionary.
41 : */
42 22 : std::unique_ptr<BDictionary> BDictionary::create() {
43 22 : return std::unique_ptr<BDictionary>(new BDictionary());
44 : }
45 :
46 : /**
47 : * @brief Creates and returns a new dictionary containing the given @a items.
48 : */
49 2 : std::unique_ptr<BDictionary> BDictionary::create(
50 : std::initializer_list<value_type> items) {
51 2 : return std::unique_ptr<BDictionary>(new BDictionary(items));
52 : }
53 :
54 : /**
55 : * @brief Returns the number of items in the dictionary.
56 : */
57 7 : BDictionary::size_type BDictionary::size() const {
58 7 : return itemMap.size();
59 : }
60 :
61 : /**
62 : * @brief Checks if the dictionary is empty.
63 : *
64 : * @return @c true if the dictionary is empty, @c false otherwise.
65 : */
66 8 : bool BDictionary::empty() const {
67 8 : return itemMap.empty();
68 : }
69 :
70 : /**
71 : * @brief Accesses the specified element.
72 : *
73 : * @returns A reference to the value that is mapped to a key equivalent to key.
74 : *
75 : * If there is no value mapped to @a key, an insertion of a null pointer is
76 : * automatically performed, and a reference to this null pointer is returned.
77 : */
78 21 : BDictionary::mapped_type &BDictionary::operator[](const key_type &key) {
79 21 : return itemMap[key];
80 : }
81 :
82 : /**
83 : * @brief Returns an iterator to the beginning of the dictionary.
84 : */
85 13 : BDictionary::iterator BDictionary::begin() {
86 13 : return itemMap.begin();
87 : }
88 :
89 : /**
90 : * @brief Returns an iterator to the end of the dictionary.
91 : */
92 9 : BDictionary::iterator BDictionary::end() {
93 9 : return itemMap.end();
94 : }
95 :
96 : /**
97 : * @brief Returns a constant iterator to the beginning of the dictionary.
98 : */
99 1 : BDictionary::const_iterator BDictionary::begin() const {
100 1 : return itemMap.begin();
101 : }
102 :
103 : /**
104 : * @brief Returns a constant iterator to the end of the dictionary.
105 : */
106 1 : BDictionary::const_iterator BDictionary::end() const {
107 1 : return itemMap.end();
108 : }
109 :
110 : /**
111 : * @brief Returns a constant iterator to the beginning of the dictionary.
112 : */
113 1 : BDictionary::const_iterator BDictionary::cbegin() const {
114 1 : return itemMap.cbegin();
115 : }
116 :
117 : /**
118 : * @brief Returns a constant iterator to the end of the dictionary.
119 : */
120 1 : BDictionary::const_iterator BDictionary::cend() const {
121 1 : return itemMap.cend();
122 : }
123 :
124 7 : void BDictionary::accept(BItemVisitor *visitor) {
125 7 : visitor->visit(this);
126 7 : }
127 :
128 : } // namespace bencoding
|