/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2006 Ferdinando Ametrano
 Copyright (C) 2006 François du Vignaud
 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <https://www.quantlib.org/license.shtml>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

/*! \file derivedquote.hpp
    \brief market quote whose value depends on another quote
*/

#ifndef quantlib_derived_quote_hpp
#define quantlib_derived_quote_hpp

#include <ql/handle.hpp>
#include <ql/quote.hpp>
#include <ql/utilities/null.hpp>
#include <utility>

namespace QuantLib {

    //! market quote whose value depends on another quote
    /*! \test the correctness of the returned values is tested by
              checking them against numerical calculations.
    */
    template <class UnaryFunction>
    class DerivedQuote : public Quote, public Observer {
      public:
        DerivedQuote(Handle<Quote> element, UnaryFunction f);
        //! \name Quote interface
        //@{
        Real value() const override;
        bool isValid() const override;
        //@}
        //! \name Observer interface
        //@{
        void update() override;
        //@}
      private:
        Handle<Quote> element_;
        mutable Real value_ = Null<Real>();
        UnaryFunction f_;
    };

    //! creator method
    template <class UnaryFunction>
    DerivedQuote<UnaryFunction> makeDerivedQuote(Handle<Quote> element,
                                                 UnaryFunction f) {
        return DerivedQuote<UnaryFunction>(std::move(element), std::move(f));
    }

    // inline definitions
    template <class UnaryFunction>
    inline DerivedQuote<UnaryFunction>::DerivedQuote(Handle<Quote> element, UnaryFunction f)
    : element_(std::move(element)), f_(std::move(f)) {
        registerWith(element_);
    }

    template <class UnaryFunction>
    inline Real DerivedQuote<UnaryFunction>::value() const {
        if (value_ == Null<Real>()) {
            QL_ENSURE(isValid(), "invalid DerivedQuote");
            value_ = f_(element_->value());
        }
        return value_;
    }

    template <class UnaryFunction>
    inline bool DerivedQuote<UnaryFunction>::isValid() const {
        return !element_.empty() && element_->isValid();
    }

    template <class UnaryFunction>
    inline void DerivedQuote<UnaryFunction>::update() {
        value_ = Null<Real>();
        notifyObservers();
    }

}

#endif
