Skip to content

Commit

Permalink
utils::updateable_value: add operator=(T)
Browse files Browse the repository at this point in the history
Allow assigning a const value.
  • Loading branch information
denesb committed Feb 27, 2020
1 parent 091d80e commit d1194da
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions utils/updateable_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ updateable_value_base::operator=(updateable_value_base&& v) noexcept {
return *this;
}

updateable_value_base&
updateable_value_base::updateable_value_base::operator=(nullptr_t) {
if (_source) {
_source->del_ref(this);
_source = nullptr;
}
return *this;
}

void
updateable_value_source_base::for_each_ref(std::function<void (updateable_value_base* ref)> func) {
for (auto ref : _refs) {
Expand Down
9 changes: 9 additions & 0 deletions utils/updateable_value.hh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public:
updateable_value_base& operator=(const updateable_value_base&);
updateable_value_base(updateable_value_base&&) noexcept;
updateable_value_base& operator=(updateable_value_base&&) noexcept;
updateable_value_base& operator=(nullptr_t);

friend class updateable_value_source_base;
};
Expand All @@ -78,6 +79,7 @@ public:
explicit updateable_value(T value) : _value(std::move(value)) {}
explicit updateable_value(const updateable_value_source<T>& source);
updateable_value(const updateable_value& v);
updateable_value& operator=(T value);
updateable_value& operator=(const updateable_value&);
updateable_value(updateable_value&&) noexcept;
updateable_value& operator=(updateable_value&&) noexcept;
Expand Down Expand Up @@ -170,6 +172,13 @@ template <typename T>
updateable_value<T>::updateable_value(const updateable_value& v) : updateable_value_base(v), _value(v._value) {
}

template <typename T>
updateable_value<T>& updateable_value<T>::operator=(T value) {
updateable_value_base::operator=(nullptr);
_value = std::move(value);
return *this;
}

template <typename T>
updateable_value<T>& updateable_value<T>::operator=(const updateable_value& v) {
if (this != &v) {
Expand Down

0 comments on commit d1194da

Please sign in to comment.