cpp-bencoding
BItem.h
Go to the documentation of this file.
1 
8 #ifndef BENCODING_BITEM_H
9 #define BENCODING_BITEM_H
10 
11 #include <memory>
12 
13 namespace bencoding {
14 
15 class BItemVisitor;
16 
20 class BItem: public std::enable_shared_from_this<BItem> {
21 public:
22  virtual ~BItem() = 0;
23 
26 
37  virtual void accept(BItemVisitor *visitor) = 0;
38 
40 
46  template <typename T>
47  std::shared_ptr<T> as() {
48  static_assert(std::is_base_of<BItem, T>::value,
49  "T has to be a subclass of BItem");
50 
51  return std::dynamic_pointer_cast<T>(shared_from_this());
52  }
53 
54 protected:
55  BItem();
56 
57 private:
58  // Disable copy construction and assignment for this class and subclasses.
59  BItem(const BItem &) = delete;
60  BItem &operator=(const BItem &) = delete;
61 };
62 
63 } // namespace bencoding
64 
65 #endif
Base class for all items (integers, strings, etc.).
Definition: BItem.h:20
Base class for all visitors of the BItem subclasses.
Definition: BItemVisitor.h:25
virtual ~BItem()=0
Destructs the item.
std::shared_ptr< T > as()
Casts the item to the given subclass of BItem.
Definition: BItem.h:47
BItem & operator=(const BItem &)=delete
Main namespace of the bencoding library.
BItem()
Constructs the item.
virtual void accept(BItemVisitor *visitor)=0
Accepts the item by the given visitor.