/****************************************************************************/
/* Copyright (c) 2015 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : FastTime.h                                                     */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 03/07/2015                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#ifndef _FastTime_H
#define _FastTime_H

// set this to 0 to disable FastTime compilation
#if 1
#define FASTTIME

#include <time.h>
#include <stdlib.h>
#include "ourTypes.h"
#include "Syslog.h"
#include "SharedData.h"
#include "TimeIF.h"

/*
CLASS 
FastTime

DESCRIPTION
Artificial time for use in simulation 

AUTHOR
Henthorn
*/

// FastTime enable env var name
#define FAST_TIME_ENV "FAST_TIME_EN"
// FastTime env var enabled value (clear when disabled)
#define FAST_TIME_EN "Y"
#define FAST_TIME_DI "N"
// set FastTime enable environment variable
#define FAST_TIME_SET_EN "FAST_TIME_EN=Y"
// clear FastTime enable environment variable
#define FAST_TIME_SET_DI "FAST_TIME_EN="


class Simulator;
class Supervisor;

class FastTime {
  friend Simulator;
  friend Supervisor;

public:

   void getFastTime(struct  timespec *ts);
   void getFastTime(TimeIF::TimeSpec *ts);
   Boolean fastTimeOn();
    // constructor
    FastTime();
    // virtual destructor
    virtual ~FastTime();
    static void Destroy();

private:

   // Initialize the simulation time. Default to current system time.
   void initialize(TimeIF::TimeSpec *ts = NULL);
   void incrFastTime(int milliseconds);
    
class MyTime : public SharedData {

  public:
    struct Data {
      TimeIF::TimeSpec ts;
    };

    virtual void MyTime::read()
    {
      SharedData::read((void *)&data);
    }

    virtual void MyTime::write()
    {
      SharedData::write((void *)&data);
    }
    
    // This ensures that only the Time class can instatiate and access
    // the simulation time.
    MyTime()
      : SharedData("FastTime", sizeof(MyTime::Data), SharedData::ReadWrite)
    {
    }
    
    // virtual destructor so that
    // SharedData destructor is called
    virtual ~MyTime(){
        Boolean debug = False;
        dprintf("**** MyTime destructor ****\n");
    }

    Data data;
  };

  MyTime  _time;

};

#endif
#endif
