Furrent
bencode_value.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <memory>
5 #include <string>
6 #include <vector>
7 
9 namespace fur::bencode {
10 
12 enum class BencodeType { Integer, String, List, Dict };
13 
14 class BencodeValue {
15  public:
17  [[nodiscard]] virtual std::string to_string() const = 0;
19  [[nodiscard]] virtual BencodeType get_type() const = 0;
20  virtual ~BencodeValue() = default;
21 };
22 
23 class BencodeInt : public BencodeValue {
24  private:
26  int64_t _val;
27 
28  public:
29  explicit BencodeInt(int64_t data);
30  [[nodiscard]] std::string to_string() const override;
31  [[nodiscard]] BencodeType get_type() const override;
33  [[nodiscard]] int64_t value() const;
34 };
35 
36 class BencodeString : public BencodeValue {
37  private:
39  std::string _val;
40 
41  public:
42  explicit BencodeString(std::string data);
43  [[nodiscard]] std::string to_string() const override;
44  [[nodiscard]] BencodeType get_type() const override;
46  [[nodiscard]] std::string& value();
47 };
48 
49 class BencodeList : public BencodeValue {
50  private:
52  std::vector<std::unique_ptr<BencodeValue>> _val;
53 
54  public:
55  explicit BencodeList(std::vector<std::unique_ptr<BencodeValue>> data);
56  [[nodiscard]] std::string to_string() const override;
57  [[nodiscard]] BencodeType get_type() const override;
59  [[nodiscard]] std::vector<std::unique_ptr<BencodeValue>>& value();
60 };
61 
62 class BencodeDict : public BencodeValue {
63  private:
65  std::map<std::string, std::unique_ptr<BencodeValue>> _val;
66 
67  public:
69  explicit BencodeDict(
70  std::map<std::string, std::unique_ptr<BencodeValue>> data);
71  [[nodiscard]] std::string to_string() const override;
72  [[nodiscard]] BencodeType get_type() const override;
75  [[nodiscard]] std::map<std::string, std::unique_ptr<BencodeValue>>& value();
76 };
77 
78 } // namespace fur::bencode
fur::bencode::BencodeDict::to_string
std::string to_string() const override
Returns the string representation of the bencode value.
Definition: bencode_value.cpp:67
fur::bencode::BencodeList::value
std::vector< std::unique_ptr< BencodeValue > > & value()
Returns the list of BencodeValue objects that are contained in the list.
Definition: bencode_value.cpp:56
fur::bencode::BencodeType
BencodeType
Enumeration for the different types of bencode data.
Definition: bencode_value.hpp:12
fur::bencode
Contains the structure for decoding and encoding bencode data.
Definition: bencode_parser.cpp:7
fur::bencode::BencodeString::value
std::string & value()
Returns the string value of the bencode value.
Definition: bencode_value.cpp:35
fur::bencode::BencodeValue::~BencodeValue
virtual ~BencodeValue()=default
fur::bencode::BencodeInt::value
int64_t value() const
Returns the integer value of the bencode value.
Definition: bencode_value.cpp:21
fur::bencode::BencodeValue::get_type
virtual BencodeType get_type() const =0
Returns the type of the bencode value as a BencodeType enum.
fur::bencode::BencodeString::get_type
BencodeType get_type() const override
Returns the type of the bencode value as a BencodeType enum.
Definition: bencode_value.cpp:33
fur::bencode::BencodeInt::get_type
BencodeType get_type() const override
Returns the type of the bencode value as a BencodeType enum.
Definition: bencode_value.cpp:19
fur::bencode::BencodeDict::value
std::map< std::string, std::unique_ptr< BencodeValue > > & value()
Definition: bencode_value.cpp:82
fur::bencode::BencodeDict::BencodeDict
BencodeDict(std::map< std::string, std::unique_ptr< BencodeValue >> data)
Constructs a BencodeDict object from a map of strings and BencodeValue.
Definition: bencode_value.cpp:63
fur::bencode::BencodeInt::BencodeInt
BencodeInt(int64_t data)
Definition: bencode_value.cpp:12
fur::bencode::BencodeDict::get_type
BencodeType get_type() const override
Returns the type of the bencode value as a BencodeType enum.
Definition: bencode_value.cpp:80
fur::bencode::BencodeList::BencodeList
BencodeList(std::vector< std::unique_ptr< BencodeValue >> data)
Definition: bencode_value.cpp:40
fur::bencode::BencodeString
Definition: bencode_value.hpp:36
fur::bencode::BencodeType::String
@ String
fur::bencode::BencodeString::BencodeString
BencodeString(std::string data)
Definition: bencode_value.cpp:26
fur::bencode::BencodeType::List
@ List
fur::bencode::BencodeType::Integer
@ Integer
fur::bencode::BencodeString::to_string
std::string to_string() const override
Returns the string representation of the bencode value.
Definition: bencode_value.cpp:28
fur::bencode::BencodeInt
Definition: bencode_value.hpp:23
fur::bencode::BencodeDict
Definition: bencode_value.hpp:62
fur::bencode::BencodeValue
Definition: bencode_value.hpp:14
fur::bencode::BencodeList::to_string
std::string to_string() const override
Returns the string representation of the bencode value.
Definition: bencode_value.cpp:44
fur::bencode::BencodeType::Dict
@ Dict
fur::bencode::BencodeInt::to_string
std::string to_string() const override
Returns the string representation of the bencode value.
Definition: bencode_value.cpp:14
fur::bencode::BencodeValue::to_string
virtual std::string to_string() const =0
Returns the string representation of the bencode value.
fur::bencode::BencodeList
Definition: bencode_value.hpp:49
fur::bencode::BencodeList::get_type
BencodeType get_type() const override
Returns the type of the bencode value as a BencodeType enum.
Definition: bencode_value.cpp:54