8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-02-01 07:23:02 +01:00
firebird-mirror/src/common/classes/TriState.h

185 lines
3.5 KiB
C
Raw Normal View History

2007-09-04 10:22:48 +02:00
/*
* PROGRAM: Firebird aux classes.
2007-09-05 04:26:47 +02:00
* MODULE: TriState.h
2007-09-04 10:22:48 +02:00
* DESCRIPTION: Firebird's SQL tri-state emulation class.
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Claudio Valderrama on 28-Aug-2007
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2007 Claudio Valderrama
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*
*/
2007-09-05 04:26:47 +02:00
#ifndef CLASSES_TRISTATE_H
#define CLASSES_TRISTATE_H
2007-09-04 10:22:48 +02:00
#include "firebird.h"
// Do not have constructor to allow usage in unions (used in the parser).
template <typename T> class TriStateRawType
{
public:
static TriStateRawType<T> val(const T& v)
{
TriStateRawType<T> triState;
triState.value = v;
triState.specified = true;
return triState;
}
static TriStateRawType<T> empty()
{
TriStateRawType<T> triState;
triState.value = (T) 0;
triState.specified = false;
return triState;
}
void operator =(const T& newValue)
{
value = newValue;
specified = true;
}
bool operator ==(const TriStateRawType<T>& o) const
{
return (!specified && !o.specified) || (specified == o.specified && value == o.value);
}
public:
T value;
bool specified;
};
template <typename T> class TriStateType : public TriStateRawType<T>
{
public:
TriStateType<T>(const T& v)
{
this->value = v;
this->specified = true;
}
TriStateType<T>(const TriStateType<T>& o)
{
this->value = o.value;
this->specified = o.specified;
}
TriStateType<T>()
{
this->value = 0;
this->specified = false;
}
public:
void operator =(const TriStateRawType<T>& o)
{
this->value = o.value;
this->specified = o.specified;
}
};
2007-09-04 10:22:48 +02:00
class TriState
{
public:
TriState();
explicit TriState(bool input);
2007-09-05 04:26:47 +02:00
2007-09-04 10:22:48 +02:00
void operator=(bool input);
2007-09-05 04:26:47 +02:00
2007-09-04 10:22:48 +02:00
bool asBool() const;
void reset();
bool assignOnce(bool input);
bool isUnknown() const;
bool isAssigned() const;
bool toggle();
2007-09-05 04:26:47 +02:00
2007-09-04 10:22:48 +02:00
private:
bool m_init, m_val;
};
// The var is left uninitialized.
2007-09-05 11:41:54 +02:00
inline TriState::TriState()
2007-09-04 10:22:48 +02:00
: m_init(false), m_val(false)
{
}
// The var is initialized to the explicit value.
2007-09-05 11:41:54 +02:00
inline TriState::TriState(bool input)
2007-09-04 10:22:48 +02:00
: m_init(true), m_val(input)
{
}
// The var receives a T/F value.
2007-09-05 11:41:54 +02:00
inline void TriState::operator=(bool input)
2007-09-04 10:22:48 +02:00
{
m_init = true;
m_val = input;
}
// The var is coerced to a T/F value as result.
2007-09-05 11:41:54 +02:00
inline bool TriState::asBool() const
2007-09-04 10:22:48 +02:00
{
return m_init && m_val;
}
// The var is returned to its uninitialized state.
2007-09-05 11:41:54 +02:00
inline void TriState::reset()
2007-09-04 10:22:48 +02:00
{
m_init = m_val = false;
}
// The assignment succeeds only if the var is uninitialized.
2007-09-05 11:41:54 +02:00
inline bool TriState::assignOnce(bool input)
2007-09-04 10:22:48 +02:00
{
if (m_init)
return false;
m_init = true;
m_val = input;
return true;
}
// Tests whether the var is uninitialized.
2007-09-05 11:41:54 +02:00
inline bool TriState::isUnknown() const
2007-09-04 10:22:48 +02:00
{
return !m_init;
}
// Tests whether the var is initialized.
2007-09-05 11:41:54 +02:00
inline bool TriState::isAssigned() const
2007-09-04 10:22:48 +02:00
{
return m_init;
}
// The var is toggled between T and F only if it's already initialized.
2007-09-05 11:41:54 +02:00
inline bool TriState::toggle()
2007-09-04 10:22:48 +02:00
{
if (!m_init)
return false;
2008-12-05 01:56:15 +01:00
2007-09-04 10:22:48 +02:00
m_val = !m_val;
return true;
}
2007-09-05 04:26:47 +02:00
#endif // CLASSES_TRISTATE_H