Coverage Report

Created: 2025-08-28 06:30

/src/quantlib/ql/utilities/observablevalue.hpp
Line
Count
Source (jump to first uncovered line)
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
27
#include <ql/patterns/observable.hpp>
28
29
namespace 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:
44
        ObservableValue();
45
        ObservableValue(T&&);
46
        ObservableValue(const T&);
47
        ObservableValue(const ObservableValue<T>&);
48
2
        ~ObservableValue() = default;
49
        //! \name controlled assignment
50
        //@{
51
        ObservableValue<T>& operator=(T&&);
52
        ObservableValue<T>& operator=(const T&);
53
        ObservableValue<T>& operator=(const ObservableValue<T>&);
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:
61
        T value_;
62
        ext::shared_ptr<Observable> observable_;
63
    };
64
65
66
    // template definition
67
68
    template <class T>
69
    ObservableValue<T>::ObservableValue()
70
    : value_(), observable_(new Observable) {}
71
72
    template <class T>
73
    ObservableValue<T>::ObservableValue(T&& t)
74
2
    : value_(std::move(t)), observable_(new Observable) {}
75
76
    template <class T>
77
    ObservableValue<T>::ObservableValue(const T& t)
78
    : value_(t), observable_(new Observable) {}
79
80
    template <class T>
81
    ObservableValue<T>::ObservableValue(const ObservableValue<T>& t)
82
    : value_(t.value_), observable_(new Observable) {}
83
84
    template <class T>
85
    ObservableValue<T>& ObservableValue<T>::operator=(T&& t) {
86
        value_ = std::move(t);
87
        observable_->notifyObservers();
88
        return *this;
89
    }
90
91
    template <class T>
92
0
    ObservableValue<T>& ObservableValue<T>::operator=(const T& t) {
93
0
        value_ = t;
94
0
        observable_->notifyObservers();
95
0
        return *this;
96
0
    }
97
98
    template <class T>
99
    ObservableValue<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>
107
    ObservableValue<T>::operator T() const {
108
        return value_;
109
    }
110
111
    template <class T>
112
684k
    ObservableValue<T>::operator ext::shared_ptr<Observable>() const {
113
684k
        return observable_;
114
684k
    }
115
116
    template <class T>
117
511k
    const T& ObservableValue<T>::value() const {
118
511k
        return value_;
119
511k
    }
120
121
}
122
123
#endif