Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<class T>
- class WeakPtr;
- struct Block {
- size_t strong_count;
- size_t weak_count;
- };
- template<class T>
- class SharedPtr {
- explicit SharedPtr(const WeakPtr<T>& weak);
- SharedPtr() : pointer_(nullptr), block_(new Block{1, 0}) {}
- explicit SharedPtr(T* pointer) : pointer_(pointer), block_(new Block{1, 0}) {}
- SharedPtr(const SharedPtr& other) : pointer_(other.pointer_), block_(other.block_) {
- ++block_->strong_count;
- }
- void Delete() {
- --block_->strong_count;
- if (block_->strong_count == 0) {
- delete pointer_;
- if (block_->weak_count == 0) {
- delete block_;
- }
- }
- }
- SharedPtr& operator=(const SharedPtr& other) {
- Delete();
- pointer_ = other.pointer_;
- block_ = other.block_;
- ++block_->strong_count;
- return *this;
- }
- explicit SharedPtr(SharedPtr&& other) : pointer_(other.pointer_), block_(other.block_) {
- other.pointer_ = nullptr;
- other.block_ = nullptr;
- }
- void Swap(SharedPtr& other) {
- std::swap(pointer_, other.pointer_);
- std::swap(block_, other.block_);
- }
- SharedPtr& operator=(SharedPtr&& other) {
- Swap(other);
- return *this;
- }
- ~SharedPtr() {
- Delete();
- }
- T operator*() const {
- return *pointer_;
- }
- T* Get() const {
- return pointer_;
- }
- Block* GetBlock() const {
- return block_;
- }
- void Reset() {
- Delete();
- pointer_ = nullptr;
- block_ = new Block{1, 0};
- }
- private:
- T* pointer_;
- Block* block_;
- };
- template <class T>
- class WeakPtr {
- explicit WeakPtr(const SharedPtr<T>& shared) : pointer_(shared.Get()), block_(shared.GetBlock()) {
- ++block_->weak_count;
- }
- WeakPtr() : pointer_(nullptr), block_(new Block{0, 1}) {}
- explicit WeakPtr(const WeakPtr& other) : pointer_(other.pointer_), block_(other.block_) {
- ++block_->weak_count;
- }
- void Delete() {
- --block_->weak_count;
- if (block_->strong_count + block_->weak_count == 0) {
- delete block_;
- }
- }
- WeakPtr& operator=(const WeakPtr& other) {
- Delete();
- pointer_ = other.pointer_;
- block_ = other.block_;
- ++block_->weak_count;
- return *this;
- }
- WeakPtr(WeakPtr&& other) : pointer_(other.pointer_), block_(other.block_) {
- other.pointer_ = nullptr;
- other.block_ = nullptr;
- }
- void Swap(WeakPtr& other) {
- std::swap(pointer_, other.pointer_);
- std::swap(block_, other.block_);
- }
- WeakPtr& operator=(WeakPtr&& other) {
- Swap(other);
- return *this;
- }
- ~WeakPtr() {
- Delete();
- }
- T* Get() const {
- return pointer_;
- }
- Block* GetBlock() const {
- return block_;
- }
- bool IsExpired() const {
- return block_->strong_count == 0;
- }
- SharedPtr<T> Lock() const {
- if (IsExpired()) {
- return {};
- }
- return {*this};
- }
- private:
- T* pointer_;
- Block* block_;
- };
- template <class T>
- SharedPtr<T>::SharedPtr(const WeakPtr<T>& other) {
- if (other.IsExpired()) {
- pointer_ = nullptr;
- } else {
- pointer_ = other.pointer_;
- }
- block_ = other.block_;
- ++block_->strong_count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement