#ifndef _xbBOOL_
#define _xbBOOL_
/*<<<***************************************************************************
The MIT License (MIT)

Copyright(C) 2013 - 2015 Marc Mirti Mancinelli, Christophe Lardau and Philippe Pamart (xdcube.org)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
#Title: xbBool
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<01/09/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/

#ifndef _xbSTRING_
#	include "xbString.h"
#endif

enum xbBoolState
{
	xbBool_False = 0,
	xbBool_True = 1,
	xbBool_Default = 2
};

class xbBool
{
public:
	xbBool(bool bDefaultVal = false)
	{
		m_state = xbBool_Default;
		m_bDefaultVal = bDefaultVal;
	}
	xbBool(xbBoolState state,bool bDefaultVal = false)
	{
		m_state = state;
		m_bDefaultVal = bDefaultVal;
	}
	xbBool(const xbBool &b)
	{
		m_state = b.m_state;
		m_bDefaultVal = b.m_bDefaultVal;
	}
	xbBool &operator=(const xbBool &b)
	{
		m_state = b.m_state;
		m_bDefaultVal = b.m_bDefaultVal;
		return *this;
	}
	xbBool &operator=(bool b)
	{
		m_state = b?xbBool_True:xbBool_False;
		return *this;
	}
	xbBool &operator=(xbBoolState b)
	{
		m_state = b;
		return *this;
	}
	operator bool() const
	{
		if (m_state == xbBool_True) return true;
		if (m_state == xbBool_False) return false;
		return m_bDefaultVal;
	}
	xbString ToString() const
	{
		if (m_state == xbBool_True) return "True";
		if (m_state == xbBool_False) return "False";
		return "Default";
	}
	operator xbBoolState() const
	{
		return m_state;
	}
	bool IsDefault() const
	{
		return m_state == xbBool_Default;
	}
	void Default()
	{
		m_state = xbBool_Default;
	}
	bool IsEmpty() const
	{
		return m_state == xbBool_Default;
	}
	bool IsValid() const
	{
		return m_state != xbBool_Default;
	}
	void Empty()
	{
		m_state = xbBool_Default;
	}
	bool Set(const xbBool &b)
	{
		if (m_state == b.m_state && m_bDefaultVal == b.m_bDefaultVal) return false;
		m_state = b.m_state;
		m_bDefaultVal = b.m_bDefaultVal;
		return true;
	}
	bool Set(xbBoolState b)
	{
		if (m_state == b) return false;
		m_state = b;
		return true;
	}
	bool Set(bool b)
	{
		if (operator bool() == b) return false;
		m_state = b?xbBool_True:xbBool_False;
		return true;
	}
	bool SetDefaultValue(bool b)
	{
		if (m_bDefaultVal == b) return false;
		m_bDefaultVal = b;
		return true;
	}
	bool Set(const xbString &str)
	{
		if (str.IsEmpty()) return Set(xbBool_Default);
		int ch = toupper(str.First());

		if (ch == 'D') return Set(xbBool_Default);
		
		if (ch != 'T' && ch != 'Y' && ch != '1') 
			return Set(xbBool_False);

		return Set(xbBool_True);
	}
	xbBoolState Get() const
	{
		return m_state;
	}
	bool GetDefaultValue() const
	{
		return m_bDefaultVal;
	}
	bool operator==(bool b) const
	{
		return (operator bool() == b);
	}
	bool operator!=(bool b) const
	{
		return (operator bool() != b);
	}
	bool operator==(xbBoolState b) const
	{
		return (m_state == b);
	}
	bool operator!=(xbBoolState b) const
	{
		return (m_state != b);
	}
	bool operator==(const xbBool &b) const
	{
		return (m_state == b.m_state && m_bDefaultVal == b.m_bDefaultVal);
	}
	bool operator!=(const xbBool &b) const
	{
		return (m_state != b.m_state || m_bDefaultVal != b.m_bDefaultVal);
	}
protected:
	xbBoolState	m_state;
	bool		m_bDefaultVal;
};

#endif
