QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
observablevalue.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2005, 2006 StatPro Italia srl
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
24#ifndef quantlib_observable_value_hpp
25#define quantlib_observable_value_hpp
26
27#include <ql/patterns/observable.hpp>
28
29namespace QuantLib {
30
32
41 template <class T>
42 class ObservableValue { // NOLINT(cppcoreguidelines-special-member-functions)
43 public:
46 ObservableValue(const T&);
48 ~ObservableValue() = default;
50
55
56 operator T() const;
57 operator ext::shared_ptr<Observable>() const;
59 const T& value() const;
60 private:
62 ext::shared_ptr<Observable> observable_;
63 };
64
65
66 // template definition
67
68 template <class T>
70 : value_(), observable_(new Observable) {}
71
72 template <class T>
74 : value_(std::move(t)), observable_(new Observable) {}
75
76 template <class T>
78 : value_(t), observable_(new Observable) {}
79
80 template <class T>
82 : value_(t.value_), observable_(new Observable) {}
83
84 template <class T>
86 value_ = std::move(t);
87 observable_->notifyObservers();
88 return *this;
89 }
90
91 template <class T>
93 value_ = t;
94 observable_->notifyObservers();
95 return *this;
96 }
97
98 template <class T>
100 ObservableValue<T>::operator=(const ObservableValue<T>& t) { // NOLINT(bugprone-unhandled-self-assignment)
101 value_ = t.value_;
102 observable_->notifyObservers();
103 return *this;
104 }
105
106 template <class T>
108 return value_;
109 }
110
111 template <class T>
112 ObservableValue<T>::operator ext::shared_ptr<Observable>() const {
113 return observable_;
114 }
115
116 template <class T>
117 const T& ObservableValue<T>::value() const {
118 return value_;
119 }
120
121}
122
123#endif
Object that notifies its changes to a set of observers.
Definition: observable.hpp:62
observable and assignable proxy to concrete value
ObservableValue(const ObservableValue< T > &)
ext::shared_ptr< Observable > observable_
ObservableValue< T > & operator=(T &&)
ObservableValue< T > & operator=(const ObservableValue< T > &)
const T & value() const
explicit inspector
ObservableValue< T > & operator=(const T &)
Definition: any.hpp:35
STL namespace.