// ******************************************************************************** 
// ** Utils.h
// ** General purpose type definitions and common classes
// **
// ** Copyright (C) 2012-2017 3D at Depth Inc. All Rights Reserved
// ** www.3DatDepth.com  This software can not be copied and/or distributed without 
// ** the express permission of 3D at Depth Inc.
// ******************************************************************************** 
#pragma once

// ************  General type definitions ********************** 
typedef signed char					INT8;
typedef signed short					INT16;
typedef signed int					INT32;
typedef unsigned char				UINT8;
typedef unsigned short				UINT16;
typedef unsigned int					UINT32;
typedef signed __int64           INT64;
typedef unsigned __int64         UINT64;


// ************  General purpose mod class ********************** 
// Always name variable accordingly, to limit confusion when using this type
class UtilIntMod
{
public:
   UtilIntMod(int value, int mod)  : m_value(value), m_mod(mod)  {}
   void SetMod(int mod)       { m_mod = mod; m_value %= mod; }

   operator int() const       { return m_value; }
   int operator=(int v)       { m_value = v % m_mod; return m_value; }
   int operator++()           { m_value = (m_value+1) % m_mod; return m_value; }
   int operator++(int)        { int v = m_value; m_value = (m_value+1) % m_mod; return v; }
   int operator+=(int x)      { m_value = (m_value+x) % m_mod; return m_value; }
   int operator+(int x) const { return (m_value+x) % m_mod; }
   int operator--()           { m_value = (m_value+m_mod-1) % m_mod; return m_value; }
   int operator--(int)        { int v = m_value; m_value = (m_value+m_mod-1) % m_mod; return v; }
   int operator-=(int x)      { m_value = (m_value+m_mod-x) % m_mod; return m_value; }
   int operator-(int x) const { return (m_value+m_mod-x) % m_mod; }
   bool operator>(int x) const { int y = (m_value+m_mod-x) % m_mod; return y && y < m_mod/2; }
   bool operator<(int x) const { int y = (m_value+m_mod-x) % m_mod; return y && y > m_mod/2; }

private:
   int m_value;
   int m_mod;
};

// ************  General purpose string copy class ********************** 
class UtilStrCpy
{
public:
	UtilStrCpy() {}
	errno_t strcpy_s( char *dest, size_t sizeDest, char *src )
	{
		if( strlen( src ) > sizeDest-1 ) { src[sizeDest-1] =  0; }
		errno_t err = ::strcpy_s( dest, sizeDest, src );
		return err;
	}
};
