QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
observablevalue.hpp
Go to the documentation of this file.
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
20/*! \file observablevalue.hpp
21 \brief observable and assignable proxy to concrete value
22*/
23
24#ifndef quantlib_observable_value_hpp
25#define quantlib_observable_value_hpp
26
28
29namespace QuantLib {
30
31 //! %observable and assignable proxy to concrete value
32 /*! Observers can be registered with instances of this class so
33 that they are notified when a different value is assigned to
34 such instances. Client code can copy the contained value or
35 pass it to functions via implicit conversion.
36 \note it is not possible to call non-const method on the
37 returned value. This is by design, as this possibility
38 would necessarily bypass the notification code; client
39 code should modify the value via re-assignment instead.
40 */
41 template <class T>
42 class ObservableValue { // NOLINT(cppcoreguidelines-special-member-functions)
43 public:
48 ~ObservableValue() = default;
49 //! \name controlled assignment
50 //@{
54 //@}
55 //! implicit conversion
56 operator T() const;
57 operator ext::shared_ptr<Observable>() const;
58 //! explicit inspector
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>
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 &)
const DefaultType & t
Definition: any.hpp:35
STL namespace.
observer/observable pattern