Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- //#include <memory.h>
- #define DEBUG
- using namespace std;
- template <typename Derived>
- class RelOps {
- public:
- friend bool operator > (const Derived & left, const Derived &right) {
- return right < left;
- }
- friend bool operator <= (const Derived & left, const Derived &right) {
- return !(right < left);
- }
- friend bool operator >= (const Derived & left, const Derived &right) {
- return right <= left;
- }
- friend bool operator == (const Derived & left, const Derived &right) {
- return left <= right && left >= right;
- }
- friend bool operator != (const Derived & left, const Derived &right) {
- !(left == right);
- }
- };
- class Standard_deleter {
- public:
- Standard_deleter() {}
- template <class T>
- void operator()(T* p) {
- delete p;
- }
- };
- template <typename T, typename Deleter = Standard_deleter>
- struct Proxy {
- T* ptr;
- Deleter dltr;
- int ref_cnt = 0;
- Proxy() {
- };
- Proxy(T* ptr) {
- dltr = Deleter();
- ref_cnt++;
- this->ptr = ptr;
- #ifdef DEBUG
- cout << *(this->ptr) << endl;
- #endif
- };
- ~Proxy() {
- dltr(ptr);
- };
- };
- template <typename T>
- class Shared_ptr_concept {
- public:
- Proxy<T>* proxy;
- virtual ~Shared_ptr_concept() = default;
- virtual bool operator < (const Shared_ptr_concept& other) const = 0;
- virtual explicit operator bool() const = 0;
- virtual void swap(Shared_ptr_concept& other) noexcept = 0;
- virtual T* operator->() = 0;
- virtual T& operator*() = 0;
- virtual Shared_ptr_concept& operator = (Shared_ptr_concept&& other) noexcept = 0;
- virtual Shared_ptr_concept& operator = (Shared_ptr_concept& other) noexcept = 0;
- };
- template <typename T>
- class Shared_ptr_model: public Shared_ptr_concept<T>, RelOps<Shared_ptr_model<T>> {
- public:
- Proxy<T>* proxy;
- explicit Shared_ptr_model(T* obj) {
- proxy = new Proxy<T>(obj);
- #ifdef DEBUG
- cout << "shared_ptr_model construcetor complete. object:" << *obj << endl;
- cout << *(proxy->ptr) << endl;
- #endif
- }
- ~Shared_ptr_model() {
- proxy->ref_cnt--;
- if (proxy->ref_cnt == 0)
- delete proxy;
- }
- virtual bool operator < (const Shared_ptr_concept<T>& other) const override {
- return proxy->ptr < other.proxy->ptr;
- }
- explicit operator bool() const {
- return proxy->ptr != 0;
- }
- virtual void swap(Shared_ptr_concept<T>& other) noexcept override{
- std::swap(proxy->ptr, other.proxy->ptr);
- }
- T* operator->() { return proxy->ptr; }
- T& operator* () { return *(proxy->ptr); }
- Shared_ptr_model& operator = (Shared_ptr_concept<T>&& other) noexcept override{
- swap(other);
- other.~Shared_ptr_concept();//seriously
- return *this;
- }
- virtual Shared_ptr_model& operator = (Shared_ptr_concept<T>& other) noexcept override{
- proxy = other.proxy;
- proxy->ref_cnt++;
- return *this;
- }
- };
- template <typename T>
- class Shared_ptr {
- private:
- std::unique_ptr<Shared_ptr_concept<T>> object_ptr;
- public:
- Shared_ptr(T* obj):object_ptr(new Shared_ptr_model<T>(obj)) {
- #ifdef DEBUG
- cout << "shared_ptr constructor comlete" << endl;
- cout << *(object_ptr->proxy->ptr) << endl;
- #endif
- };
- bool operator < (const Shared_ptr_concept<T>& other) const {
- return object_ptr->operator < (other);
- // return object_ptr->proxy->ptr < other->proxy->ptr;
- // ???? опять не понятно как < вызвать
- }
- explicit operator bool() const {
- // return object_ptr->;
- // ???? непонятно
- }
- void swap(Shared_ptr_concept<T>& other) noexcept {
- return object_ptr->swap(other);
- }
- T* operator ->() {
- // return object_ptr->->;
- // ???? непонятно
- }
- T& operator*() {
- return *(object_ptr->proxy->ptr);
- // return (object_ptr->*);
- // ???? непонятно
- }
- Shared_ptr_concept<T>& operator = (Shared_ptr_concept<T>& other) noexcept {
- // object_ptr->= other;
- }
- };
- int main() {
- int* a = new int(44);
- Shared_ptr<int> sh_p(a);
- cout << *sh_p << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement