Relinking Handles

Very simple example showing how to relink a Handle object in QuantLib:

/**
   Copyright (C) 2010 Bojan Nikolic <bojan@bnikolic.co.uk>

   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.

   Comments welcome at bojan@bnikolic.co.uk
*/
#include <iostream>
#include <ql/quantlib.hpp>

using namespace QuantLib;

/// Trivial structure which makes a copy of the handle passed to it
struct TkH
{
  // Note this is a copy not a reference
  Handle<Quote> _h;

  TkH(const Handle<Quote> &h):
    _h(h)
  {
  }

  void print(void)
  {
    std::cout<<(*_h)->value()
             <<std::endl;
  }

};

int main(void)
{
  boost::shared_ptr<Quote> s(new SimpleQuote(0.99));

  RelinkableHandle<Quote> h(s);

  TkH t(h);
  t.print();

  h.linkTo(boost::shared_ptr<Quote>(new SimpleQuote(0.5)));
  t.print();
}

The output is, unsurprisingly,

0.99
0.5