
#include "Future.hh"

#include <iostream>
#include <unistd.h>
#include <boost/shared_ptr.hpp>

using namespace std;
using namespace bneijt;

struct Number
{
  int val;
  Number(int v)
  :
    val(v)
  {}
};

boost::shared_ptr<Number> something()
{
  size_t i = 10;
  while(--i)
  {
    cout << "Future thread working\n";
    sleep(1);
  }
  boost::shared_ptr<Number> n(new Number(9));
  return n;
}

int main()
{
  //Test the future
  boost::function0< boost::shared_ptr<Number> > z(something);
  cout << "Creating future\n";
  Future< boost::shared_ptr<Number> > f(z);
  //Call future (wait on result)
  cout << "Main waiting for 3 seconds\n";
  sleep(3);
  cout << "Calling upon future to return\n";
  //Future is being worked out now
  boost::shared_ptr<Number> r = f();
  cout << "Future returned: " << r->val << endl;  
  return 0;  
}

