Skip to content

Commit

Permalink
Allow moving value into ObservableValue (#1601)
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Mar 14, 2023
2 parents 230ce83 + 851edb0 commit 5b494c6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
8 changes: 4 additions & 4 deletions ql/indexes/indexmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace QuantLib {
return data_[to_upper_copy(name)].value();
}

void IndexManager::setHistory(const string& name, const TimeSeries<Real>& history) {
data_[to_upper_copy(name)] = history;
void IndexManager::setHistory(const string& name, TimeSeries<Real> history) {
data_[to_upper_copy(name)] = std::move(history);
}

ext::shared_ptr<Observable> IndexManager::notifier(const string& name) const {
Expand All @@ -44,8 +44,8 @@ namespace QuantLib {
std::vector<string> IndexManager::histories() const {
std::vector<string> temp;
temp.reserve(data_.size());
for (history_map::const_iterator i = data_.begin(); i != data_.end(); ++i)
temp.push_back(i->first);
for (const auto& i : data_)
temp.push_back(i.first);
return temp;
}

Expand Down
5 changes: 2 additions & 3 deletions ql/indexes/indexmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace QuantLib {
//! returns the (possibly empty) history of the index fixings
const TimeSeries<Real>& getHistory(const std::string& name) const;
//! stores the historical fixings of the index
void setHistory(const std::string& name, const TimeSeries<Real>&);
void setHistory(const std::string& name, TimeSeries<Real> history);
//! observer notifying of changes in the index fixings
ext::shared_ptr<Observable> notifier(const std::string& name) const;
//! returns all names of the indexes for which fixings were stored
Expand All @@ -58,8 +58,7 @@ namespace QuantLib {
bool hasHistoricalFixing(const std::string& name, const Date& fixingDate) const;

private:
typedef std::map<std::string, ObservableValue<TimeSeries<Real> > > history_map;
mutable history_map data_;
mutable std::map<std::string, ObservableValue<TimeSeries<Real>>> data_;
};

}
Expand Down
14 changes: 14 additions & 0 deletions ql/utilities/observablevalue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ namespace QuantLib {
class ObservableValue {
public:
ObservableValue();
ObservableValue(T&&);
ObservableValue(const T&);
ObservableValue(const ObservableValue<T>&);
~ObservableValue() = default;
//! \name controlled assignment
//@{
ObservableValue<T>& operator=(T&&);
ObservableValue<T>& operator=(const T&);
ObservableValue<T>& operator=(const ObservableValue<T>&);
//@}
Expand All @@ -66,6 +69,10 @@ namespace QuantLib {
ObservableValue<T>::ObservableValue()
: value_(), observable_(new Observable) {}

template <class T>
ObservableValue<T>::ObservableValue(T&& t)
: value_(std::move(t)), observable_(new Observable) {}

template <class T>
ObservableValue<T>::ObservableValue(const T& t)
: value_(t), observable_(new Observable) {}
Expand All @@ -74,6 +81,13 @@ namespace QuantLib {
ObservableValue<T>::ObservableValue(const ObservableValue<T>& t)
: value_(t.value_), observable_(new Observable) {}

template <class T>
ObservableValue<T>& ObservableValue<T>::operator=(T&& t) {
value_ = std::move(t);
observable_->notifyObservers();
return *this;
}

template <class T>
ObservableValue<T>& ObservableValue<T>::operator=(const T& t) {
value_ = t;
Expand Down

0 comments on commit 5b494c6

Please sign in to comment.