Skip to content

Commit

Permalink
Stop using std::shared_ptr<X>::unique()
Browse files Browse the repository at this point in the history
  • Loading branch information
bredelings committed Jul 25, 2024
1 parent c5722fd commit 5ae1a59
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 20 deletions.
10 changes: 0 additions & 10 deletions src/computation/object.H
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,6 @@ template<> std::string Box<Matrix>::print() const;

template<> std::string Box<std::vector<std::pair<int,int>>>::print() const;

template <typename T>
bool unshare(object_ptr<T>& ptr)
{
if (ptr.unique()) return false;

ptr = object_ptr<T>(ptr->clone());

return true;
}

template <typename T>
using Vector = Box<std::vector<T>>;

Expand Down
13 changes: 3 additions & 10 deletions src/util/include/util/cow-ptr.H
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ private:
// create a new copy of data = make a direct copy otherwise
void copy()
{
if (not data.unique()) {
// Note that shared_ptr<X>::use_count() isn't thread-safe.
if (data.use_count() > 1) {
X* data_old = data.get();
data = std::shared_ptr<X>(new X(*data_old));
}
Expand Down Expand Up @@ -101,10 +102,6 @@ public:
return data.use_count();
}

bool unique() const {
return data.unique();
}

void swap(cow_ptr<X>& other) {std::swap(data,other.data);}

cow_ptr& operator=(const cow_ptr& r)
Expand Down Expand Up @@ -167,7 +164,7 @@ private:
// create a new copy of data = make a direct copy otherwise
void copy()
{
if (not data.unique()) {
if (data.use_count() > 1) {
X* data_old = data.get();
data = std::shared_ptr<X>(data_old->clone());
}
Expand Down Expand Up @@ -216,10 +213,6 @@ public:
return data.use_count();
}

bool unique() const {
return data.unique();
}

void swap(polymorphic_cow_ptr<X>& other) {std::swap(data,other.data);}

polymorphic_cow_ptr& operator=(const polymorphic_cow_ptr& r)
Expand Down

0 comments on commit 5ae1a59

Please sign in to comment.