Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <memory>
  4. #include "Test.hpp"
  5.  
  6. namespace smart_pointer {
  7. // обьявление класса "исключение"
  8.     class exception : std::exception {
  9.         using base_class = std::exception;
  10.         using base_class::base_class;
  11.     };
  12.  
  13. // обьявление класса "умный указатель"
  14.     template<
  15.             typename T,
  16.             typename Allocator
  17.     >
  18.     class SmartPointer {
  19.         // эта строчка нужно, ее не удалять
  20.         ENABLE_CLASS_TESTS;
  21.     public:
  22.         using value_type = T;
  23.  
  24.         SmartPointer(value_type * = nullptr);
  25.  
  26.         // конструктор копирования
  27.         SmartPointer(const SmartPointer &);
  28.  
  29.         // конструктор перемещения? (move)
  30.         SmartPointer(SmartPointer &&);
  31.  
  32.         // присвоение
  33.         SmartPointer &operator=(const SmartPointer &);
  34.  
  35.         // присвоение с rval
  36.         SmartPointer &operator=(SmartPointer &&);
  37.  
  38.         //
  39.         SmartPointer &operator=(value_type *);
  40.  
  41.         ~SmartPointer();
  42.  
  43.         // возвращаем ссылку на объект класса / типа T
  44.         // если SmartPointer указывает на (или содержит) nullptr, кидаем throw `SmartPointer::exception`
  45.         value_type &operator*();
  46.         const value_type &operator*() const;
  47.  
  48.         // возвращает указатель на объект класса / типа T
  49.         value_type *operator->() const;
  50.  
  51.         value_type *get() const;
  52.  
  53.         // если указатель == nullptr => return false
  54.         operator bool() const;
  55.  
  56.         // если указатели оба указывают на один и тот же адрес или оба на nullptr  => return true
  57.         template<typename U, typename AnotherAllocator>
  58.         bool operator==(const SmartPointer<U, AnotherAllocator> &) const;
  59.  
  60.         // если указатели оба указывают на один и тот же адрес или оба на nullptr  => return false
  61.         template<typename U, typename AnotherAllocator>
  62.         bool operator!=(const SmartPointer<U, AnotherAllocator> &) const;
  63.  
  64.         // если умный указатель содержит (или указывает)  НЕ nullptr => return количество владельцев (count owners)
  65.         // если умный указатель содержит (или указывает) nullptr => return 0
  66.         std::size_t count_owners() const;
  67.  
  68.     private:
  69.         class Core;
  70.         Core *core;
  71.     };
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement