Browse Source

Added a default constructor to shared_ptr

master
Ludovic 'Archivist' Lagouardette 3 years ago
parent
commit
99ced331c0
1 changed files with 16 additions and 4 deletions
  1. +16
    -4
      include/gp/utils/pointers.hpp

+ 16
- 4
include/gp/utils/pointers.hpp View File

@ -4,6 +4,7 @@
#include "gp/algorithms/move.hpp" #include "gp/algorithms/move.hpp"
#include "gp/containers/buffer.hpp" #include "gp/containers/buffer.hpp"
#include "gp/functional/function.hpp" #include "gp/functional/function.hpp"
#include "gp/utils/allocators/dummy.hpp"
#include <atomic> #include <atomic>
#include <concepts> #include <concepts>
@ -78,7 +79,7 @@ namespace gp {
class shared_ptr { class shared_ptr {
T* data; T* data;
std::atomic_int* refcounter; std::atomic_int* refcounter;
gp::allocator&; owner;
gp::optional<gp::reference_wrapper<gp::allocator>>; owner;
shared_ptr(T* _data, gp::allocator& _owner) shared_ptr(T* _data, gp::allocator& _owner)
: data(_data) : data(_data)
@ -101,8 +102,8 @@ namespace gp {
if(refcounter->fetch_sub(1, std::memory_order::acq_rel) == 0) { if(refcounter->fetch_sub(1, std::memory_order::acq_rel) == 0) {
if(data) { if(data) {
data->~T(); data->~T();
owner.deallocate(refcounter);
owner.deallocate(data);
owner.value().get().deallocate(refcounter);
owner.value().get().deallocate(data);
} }
} }
} }
@ -130,6 +131,12 @@ namespace gp {
return data != nullptr; return data != nullptr;
} }
shared_ptr()
: data{nullptr}
, owner{gp::nullopt}
, refcounter(nullptr)
{}
shared_ptr(shared_ptr& oth) { shared_ptr(shared_ptr& oth) {
oth.refcounter->fetch_add(1, std::memory_order::acquire); oth.refcounter->fetch_add(1, std::memory_order::acquire);
@ -152,13 +159,18 @@ namespace gp {
data = oth.data; data = oth.data;
refcounter = oth.refcounter; refcounter = oth.refcounter;
owner = oth.owner;
owner = oth.owner;
return *this;
} }
shared_ptr& operator=(shared_ptr&& oth) { shared_ptr& operator=(shared_ptr&& oth) {
dirty_clear(); dirty_clear();
(*oth.refcounter)++;
data = oth.data; data = oth.data;
refcounter = oth.refcounter;
owner = oth.owner; owner = oth.owner;
return *this;
} }
~shared_ptr() { ~shared_ptr() {

Loading…
Cancel
Save