Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <utility>
- using namespace std;
- struct ref_counter
- {
- int c;
- ref_counter() noexcept
- : c(0)
- {
- }
- ref_counter(int c) noexcept
- : c(c)
- {
- }
- void inc_count() noexcept {
- ++c;
- }
- void dec_count() noexcept {
- --c;
- }
- };
- template<typename T>
- class smart_ptr
- {
- T* data;
- ref_counter *ref_count;
- public:
- template<typename U> friend class smart_ptr;
- smart_ptr() noexcept
- : data(nullptr), ref_count(nullptr)
- {
- }
- smart_ptr(T* data) noexcept
- : data(data), ref_count(new ref_counter(1))
- {
- }
- smart_ptr(const smart_ptr& a) noexcept
- : data(a.data), ref_count(a.ref_count)
- {
- if (ref_count) {
- ref_count->inc_count();
- }
- }
- template<typename U>
- smart_ptr(const smart_ptr<U>& a) noexcept
- : data(a.data), ref_count(a.ref_count)
- {
- if (ref_count) {
- ref_count->inc_count();
- }
- }
- smart_ptr(smart_ptr&& a) noexcept
- {
- data = exchange(a.data, nullptr);
- ref_count = exchange(a.ref_count, nullptr);
- }
- template<typename U>
- smart_ptr(smart_ptr<U>&& a) noexcept
- {
- data = exchange(a.data, nullptr);
- ref_count = exchange(a.ref_count, nullptr);
- }
- smart_ptr& operator=(const smart_ptr& a) noexcept {
- if (this == &a) {
- return *this;
- }
- if (data != nullptr) {
- ref_count->dec_count();
- if (!ref_count->c) {
- delete data;
- }
- }
- data = a.data;
- ref_count = a.ref_count;
- ref_count->inc_count();
- return *this;
- }
- smart_ptr& operator=(smart_ptr&& a) noexcept {
- if (this == &a) {
- return *this;
- }
- if (data != nullptr) {
- ref_count->dec_count();
- if (!ref_count->c) {
- delete data;
- }
- }
- data = exchange(a.data, nullptr);
- ref_count = exchange(a.ref_count, nullptr);
- return *this;
- }
- template<typename U>
- smart_ptr& operator=(const smart_ptr<U>& a) noexcept {
- if (this == &a) {
- return *this;
- }
- if (data != nullptr) {
- ref_count->dec_count();
- if (!ref_count->c) {
- delete data;
- }
- }
- data = a.data;
- ref_count = a.ref_count;
- ref_count->inc_count();
- return *this;
- }
- template<typename U>
- smart_ptr& operator=(smart_ptr<U>&& a) noexcept {
- if (this == &a) {
- return *this;
- }
- if (data != nullptr) {
- ref_count->dec_count();
- if (!ref_count->c) {
- delete data;
- }
- }
- data = exchange(a.data, nullptr);
- ref_count = exchange(a.ref_count, nullptr);
- return *this;
- }
- T& operator*() const noexcept {
- return *data;
- }
- T* operator->() const noexcept {
- return data;
- }
- T* get() const noexcept {
- return data;
- }
- int use_count() const noexcept {
- return ref_count == nullptr ? 0 : ref_count->c;
- }
- explicit operator bool() const noexcept {
- return data != nullptr;
- }
- bool unique() const noexcept {
- return ref_count != nullptr && ref_count->c == 1;
- }
- ~smart_ptr() noexcept {
- if (data) {
- ref_count->dec_count();
- if (ref_count->c == 0) {
- delete data;
- delete ref_count;
- }
- }
- }
- };
- template<typename T, typename... Args>
- smart_ptr<T>& make_smart(Args&&... args) {
- return smart_ptr<T>(forwad<Args>(args)...);
- }
- int main()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement