/*
 * $Id$
 *
 * Created: 5.2.2007
 * Author: jedi
 *
 * Just testing a couple of several configurations of Loki's Singleton
 */


#include <iostream>
#include <loki/Singleton.h>


using namespace std;
using namespace Loki;


/////////////////////////////////////////////////////////////////////////////
// class to keep track of the running time of the application
class runtime
{
   public:
      runtime()  { cout << "runtime instantiated" << endl; start(); }
      ~runtime() { cout << "runtime destroyed" << endl; }

      void start() { start_time_ = time(NULL); }
      time_t get_running_time() { return time(NULL) - start_time_; }

   private:
      time_t start_time_;
};
// declare singleton for runtime. runtime must have longer lifetime than logger
typedef SingletonHolder
<
    runtime, CreateUsingNew, SingletonWithLongevity
> running_time;
// runtime's longevity is 2
inline unsigned int GetLongevity(runtime*) {return 2;}



/////////////////////////////////////////////////////////////////////////////
// logger class that prints messages to stdout
class logging
{
   public:
      logging() { cout << "logging instantiated" << endl; }

      ~logging()
      {
         // oops. what if runtime has been deleted already?
         cout << running_time::Instance().get_running_time()
              << ": " << "logging destroyed" << endl;
      }

      void log(const string& msg)
      {
         cout << running_time::Instance().get_running_time()
              << ": " << msg << endl;
      }

   private:
      time_t start_time_;
};
// declare singleton for logger. logger must have shorter lifetime than runtime
typedef SingletonHolder
<
    logging, CreateUsingNew, SingletonWithLongevity
> logger;
// logger's longevity is 1
inline unsigned int GetLongevity(logging*) {return 1;}


/////////////////////////////////////////////////////////////////////////////
int main()
{
   logger::Instance();
   running_time::Instance();

   logging& l = logger::Instance();
   l.log("hello");

   return 0;
}