cpp-bencoding
BDictionary.h
Go to the documentation of this file.
1 
8 #ifndef BENCODING_BDICTIONARY_H
9 #define BENCODING_BDICTIONARY_H
10 
11 #include <initializer_list>
12 #include <map>
13 #include <memory>
14 
15 #include "BItem.h"
16 
17 namespace bencoding {
18 
19 class BString;
20 
39 class BDictionary: public BItem {
40 private:
48  public:
49  bool operator()(const std::shared_ptr<BString> &lhs,
50  const std::shared_ptr<BString> &rhs) const;
51  };
52 
53 private:
55  // See the class description for the reason why a custom comparator is
56  // used instead of @c std::less<>.
57  using BItemMap = std::map<std::shared_ptr<BString>,
58  std::shared_ptr<BItem>, BStringByValueComparator>;
59 
60 public:
62  using key_type = BItemMap::key_type;
63 
65  using mapped_type = BItemMap::mapped_type;
66 
68  using value_type = BItemMap::value_type;
69 
71  using size_type = BItemMap::size_type;
72 
74  using reference = BItemMap::reference;
75 
77  using const_reference = BItemMap::const_reference;
78 
80  using iterator = BItemMap::iterator;
81 
83  using const_iterator = BItemMap::const_iterator;
84 
85 public:
86  static std::unique_ptr<BDictionary> create();
87  static std::unique_ptr<BDictionary> create(
88  std::initializer_list<value_type> items);
89 
92  size_type size() const;
93  bool empty() const;
95 
98  mapped_type &operator[](const key_type &key);
100 
103  iterator begin();
104  iterator end();
105  const_iterator begin() const;
106  const_iterator end() const;
107  const_iterator cbegin() const;
108  const_iterator cend() const;
110 
113  virtual void accept(BItemVisitor *visitor) override;
115 
116 private:
117  BDictionary();
118  explicit BDictionary(std::initializer_list<value_type> items);
119 
120 private:
123 };
124 
125 } // namespace bencoding
126 
127 #endif
BDictionary()
Constructs an empty dictionary.
Base class for all items (integers, strings, etc.).
Definition: BItem.h:20
std::map< std::shared_ptr< BString >, std::shared_ptr< BItem >, BStringByValueComparator > BItemMap
Mapping of strings into items.
Definition: BDictionary.h:58
iterator begin()
Returns an iterator to the beginning of the dictionary.
Definition: BDictionary.cpp:85
BItemMap::const_reference const_reference
Constant reference.
Definition: BDictionary.h:77
BItemMap::reference reference
Reference.
Definition: BDictionary.h:74
Base class for all visitors of the BItem subclasses.
Definition: BItemVisitor.h:25
virtual void accept(BItemVisitor *visitor) override
Accepts the item by the given visitor.
Definition: BDictionary.cpp:124
Representation of a dictionary.
Definition: BDictionary.h:39
const_iterator cbegin() const
Returns a constant iterator to the beginning of the dictionary.
Definition: BDictionary.cpp:113
iterator end()
Returns an iterator to the end of the dictionary.
Definition: BDictionary.cpp:92
Comparator of keys for the dictionary.
Definition: BDictionary.h:47
bool operator()(const std::shared_ptr< BString > &lhs, const std::shared_ptr< BString > &rhs) const
Checks if lhs->value() < rhs->value().
Definition: BDictionary.cpp:22
BItemMap::mapped_type mapped_type
Mapped type.
Definition: BDictionary.h:65
Main namespace of the bencoding library.
const_iterator cend() const
Returns a constant iterator to the end of the dictionary.
Definition: BDictionary.cpp:120
Base class for all items (integers, strings, etc.).
BItemMap itemMap
Underlying list of items.
Definition: BDictionary.h:122
BItemMap::size_type size_type
Size type.
Definition: BDictionary.h:71
BItemMap::iterator iterator
Iterator.
Definition: BDictionary.h:80
mapped_type & operator[](const key_type &key)
Accesses the specified element.
Definition: BDictionary.cpp:78
BItemMap::const_iterator const_iterator
Constant iterator.
Definition: BDictionary.h:83
size_type size() const
Returns the number of items in the dictionary.
Definition: BDictionary.cpp:57
BItemMap::key_type key_type
Key type.
Definition: BDictionary.h:62
static std::unique_ptr< BDictionary > create()
Creates and returns a new dictionary.
Definition: BDictionary.cpp:42
BItemMap::value_type value_type
Value type.
Definition: BDictionary.h:68
bool empty() const
Checks if the dictionary is empty.
Definition: BDictionary.cpp:66