/*******************************************************************************
** Copyright (c) 2005 MaK Technologies, Inc.
** All rights reserved.
*******************************************************************************/
/*******************************************************************************
** $RCSfile: logicalTimeIntervalImplInteger.h,v $ $Revision: 1.3 $ $State: Exp $
*******************************************************************************/

#ifndef RTI_LogicalTimeIntervalImplInteger_H_
#define RTI_LogicalTimeIntervalImplInteger_H_

#include <RTI/LogicalTimeInterval.h>

#if VMS
#include <limits>
#define MAXINT64       numeric_limits<long long>::max()
#else
#define MAXINT64       9223372036854775807LL
#endif

#define POSITIVE_INFINITY (MAXINT64)
#define EPSILON (1)

//! ------------- Implementation for a long long (64 bytes unsigned)
#if DT_HAVE_64BIT_LONGS

//! A 64 bit  integer value.
typedef long DtLtInt64;

#else
#ifdef _WIN32

//! A 64 bit unsigned integer value.
typedef _int64 DtLtInt64;

#else

//! A 64 bit unsigned integer value.
typedef long long DtLtInt64;

#endif
#endif

class RTI_EXPORT_FEDTIME LogicalTimeIntervalImplInteger
   : public rti1516::LogicalTimeInterval
{
public:
   static std::wstring theImplementationName;

public:

   // Constructors
   LogicalTimeIntervalImplInteger();
   LogicalTimeIntervalImplInteger(const DtLtInt64&);
   LogicalTimeIntervalImplInteger(rti1516::LogicalTimeInterval const &);
   LogicalTimeIntervalImplInteger(const LogicalTimeIntervalImplInteger&);

   // Destructor
   virtual ~LogicalTimeIntervalImplInteger()
   throw ();

   // Basic accessors/mutators
   virtual void setZero();
   virtual bool isZero() const;

   virtual void setEpsilon();
   virtual bool isEpsilon() const;

   // Assignment
   virtual
   rti1516::LogicalTimeInterval&
   operator= (rti1516::LogicalTimeInterval const & value)
      throw (
         rti1516::InvalidLogicalTimeInterval);

    // Set self to the difference between two LogicalTimes
    virtual
    void
    setToDifference(rti1516::LogicalTime const & minuend,
      rti1516::LogicalTime const& subtrahend)
      throw (rti1516::InvalidLogicalTime);
   
   virtual
   rti1516::LogicalTimeInterval&
   operator+= (rti1516::LogicalTimeInterval const & addend)
      throw (
         rti1516::InvalidLogicalTimeInterval);

   virtual
   rti1516::LogicalTimeInterval&
   operator-= (rti1516::LogicalTimeInterval const & subtrahend)
      throw (
         rti1516::InvalidLogicalTimeInterval);

   virtual
   bool
   operator> (rti1516::LogicalTimeInterval const & value) const
      throw (
         rti1516::InvalidLogicalTimeInterval);

   virtual
   bool
   operator< (rti1516::LogicalTimeInterval const & value) const
      throw (
         rti1516::InvalidLogicalTimeInterval);

   virtual
   bool
   operator== (rti1516::LogicalTimeInterval const & value) const
      throw (
         rti1516::InvalidLogicalTimeInterval);

   virtual
   bool
   operator>= (rti1516::LogicalTimeInterval const & value) const
      throw (
         rti1516::InvalidLogicalTimeInterval);

   virtual
   bool
   operator<= (rti1516::LogicalTimeInterval const & value) const
      throw (
         rti1516::InvalidLogicalTimeInterval);

   // Generates an encoded value that can be used to send
   // LogicalTimeIntervals to other federates in updates or interactions
   virtual rti1516::VariableLengthData encode() const;

   // Alternate encode for directly filling a buffer
   virtual unsigned long encodedLength() const;
   virtual unsigned long encode(void* buffer, unsigned long bufferSize) const
       throw (rti1516::CouldNotEncode);

   // Decode encodedValue into self
   virtual void decode(rti1516::VariableLengthData const & encodedValue)
      throw (
         rti1516::InternalError,
         rti1516::CouldNotDecode);

   // Alternate decode that reads directly from a buffer
   virtual void decode(void* buffer, unsigned long bufferSize)
      throw (
         rti1516::InternalError,
         rti1516::CouldNotDecode);

   virtual std::wstring toString() const;

   // Returns the name of the implementation, as needed by
   // createFederationExecution.
   virtual std::wstring implementationName() const;

public:
   //-----------------------------------------------------------------
   // Implementation functions
   //-----------------------------------------------------------------
   virtual DtLtInt64 getInterval() const;

   //-----------------------------------------------------------------
   // Implementation operators
   //-----------------------------------------------------------------
   virtual bool operator== (const DtLtInt64&) const
   throw (rti1516::InvalidLogicalTimeInterval);

   virtual bool operator!= (rti1516::LogicalTimeInterval const & value) const
      throw (
         rti1516::InvalidLogicalTimeInterval);

   virtual bool operator!= (LogicalTimeIntervalImplInteger const & value) const
      throw (
         rti1516::InvalidLogicalTimeInterval);

   virtual bool operator!= (const DtLtInt64&) const
   throw (rti1516::InvalidLogicalTimeInterval);

   virtual LogicalTimeIntervalImplInteger& operator= (
      const LogicalTimeIntervalImplInteger& value)
   throw (rti1516::InvalidLogicalTimeInterval);

   virtual rti1516::LogicalTimeInterval& operator= (const DtLtInt64&)
   throw (rti1516::InvalidLogicalTimeInterval);

   virtual rti1516::LogicalTimeInterval& operator+= (const DtLtInt64&)
   throw (rti1516::InvalidLogicalTimeInterval);

   virtual rti1516::LogicalTimeInterval& operator-= (const DtLtInt64&)
   throw (rti1516::InvalidLogicalTimeInterval);

private:
   //-----------------------------------------------------------------
   // Implementation member variables
   //-----------------------------------------------------------------
   DtLtInt64 myInterval;
   DtLtInt64 myZero;
   DtLtInt64 myEpsilon;
   DtLtInt64 myPositiveInfinity;
};

#endif // RTI_LogicalTimeIntervalImplInteger_H_

