| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2007 Ferdinando Ametrano |
| 5 | Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl |
| 6 | |
| 7 | This file is part of QuantLib, a free-software/open-source library |
| 8 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 9 | |
| 10 | QuantLib is free software: you can redistribute it and/or modify it |
| 11 | under the terms of the QuantLib license. You should have received a |
| 12 | copy of the license along with this program; if not, please email |
| 13 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 14 | <http://quantlib.org/license.shtml>. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 19 | */ |
| 20 | |
| 21 | /*! \file simplequote.hpp |
| 22 | \brief simple quote class |
| 23 | */ |
| 24 | |
| 25 | #ifndef quantlib_simple_quote_hpp |
| 26 | #define quantlib_simple_quote_hpp |
| 27 | |
| 28 | #include <ql/quote.hpp> |
| 29 | |
| 30 | namespace QuantLib { |
| 31 | |
| 32 | //! market element returning a stored value |
| 33 | class SimpleQuote : public Quote { |
| 34 | public: |
| 35 | SimpleQuote(Real value = Null<Real>()); |
| 36 | //! \name Quote interface |
| 37 | //@{ |
| 38 | Real value() const override; |
| 39 | bool isValid() const override; |
| 40 | //@} |
| 41 | //! \name Modifiers |
| 42 | //@{ |
| 43 | //! returns the difference between the new value and the old value |
| 44 | Real setValue(Real value = Null<Real>()); |
| 45 | void reset(); |
| 46 | //@} |
| 47 | private: |
| 48 | Real value_; |
| 49 | }; |
| 50 | |
| 51 | // inline definitions |
| 52 | |
| 53 | inline SimpleQuote::SimpleQuote(Real value) |
| 54 | : value_(value) {} |
| 55 | |
| 56 | inline Real SimpleQuote::value() const { |
| 57 | QL_ENSURE(isValid(), "invalid SimpleQuote" ); |
| 58 | return value_; |
| 59 | } |
| 60 | |
| 61 | inline bool SimpleQuote::isValid() const { |
| 62 | return value_!=Null<Real>(); |
| 63 | } |
| 64 | |
| 65 | inline Real SimpleQuote::setValue(Real value) { |
| 66 | Real diff = value-value_; |
| 67 | if (diff != 0.0) { |
| 68 | value_ = value; |
| 69 | notifyObservers(); |
| 70 | } |
| 71 | return diff; |
| 72 | } |
| 73 | |
| 74 | inline void SimpleQuote::reset() { |
| 75 | setValue(Null<Real>()); |
| 76 | } |
| 77 | |
| 78 | } |
| 79 | |
| 80 | #endif |
| 81 | |