2021-03-19 19:37:24 +01:00
|
|
|
/*
|
|
|
|
*
|
2021-04-28 20:30:34 +02:00
|
|
|
* Copyright (c) 2020 Jief
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2021-03-19 19:37:24 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CPP_LIB_DEF_TYPES_H_
|
|
|
|
#define CPP_LIB_DEF_TYPES_H_
|
|
|
|
|
2023-11-18 11:04:54 +01:00
|
|
|
#include "../cpp_foundation/XBool.h"
|
|
|
|
|
2021-03-19 19:37:24 +01:00
|
|
|
template <class T>
|
|
|
|
class undefinable
|
|
|
|
{
|
|
|
|
protected:
|
2021-09-28 15:54:31 +02:00
|
|
|
XBool m_defined = false;
|
2021-03-22 13:40:01 +01:00
|
|
|
T m_value = T();
|
2021-03-19 19:37:24 +01:00
|
|
|
|
|
|
|
public:
|
2021-09-28 15:54:31 +02:00
|
|
|
XBool isDefined() const { return m_defined; }
|
|
|
|
void setDefined(XBool b) { m_defined = b; }
|
2021-03-19 19:37:24 +01:00
|
|
|
|
2021-09-28 11:14:59 +02:00
|
|
|
// T& value() { return m_value; } // never allow to return a modifiable value. Breaks encapsulation.
|
2021-03-22 13:40:01 +01:00
|
|
|
const T& value() const {
|
|
|
|
if ( !isDefined() ) {
|
|
|
|
panic("get value of an undefined undefinable type");
|
2021-09-28 11:14:59 +02:00
|
|
|
}
|
2021-03-22 13:40:01 +01:00
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
const T& dgetValue() const { return m_value; } // if !m_defined, m_value = T()
|
|
|
|
|
2021-03-26 10:43:15 +01:00
|
|
|
explicit operator const T&() const {
|
2021-03-22 13:40:01 +01:00
|
|
|
if ( !isDefined() ) panic("get value of an undefined undefinable type");
|
2021-03-19 19:37:24 +01:00
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
undefinable<T>& operator = (T value) {
|
|
|
|
setDefined(true);
|
|
|
|
m_value = value;
|
|
|
|
return *this;
|
|
|
|
}
|
2021-04-11 07:18:52 +02:00
|
|
|
|
2021-09-28 15:54:31 +02:00
|
|
|
XBool operator ==(const undefinable<T>& other) const
|
2021-04-11 07:18:52 +02:00
|
|
|
{
|
|
|
|
if ( !(m_defined == other.m_defined ) ) return false;
|
|
|
|
if ( m_defined && !(m_value == other.m_value ) ) return false; // we don't test value if this is not defined.
|
|
|
|
return true;
|
|
|
|
}
|
2021-09-28 15:54:31 +02:00
|
|
|
XBool operator !=(const undefinable<T>& other) const { return !(*this == other); }
|
2021-03-19 19:37:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class undefinable_bool : public undefinable<bool>
|
|
|
|
{
|
|
|
|
using super = undefinable<bool>;
|
|
|
|
public:
|
2021-03-26 10:43:15 +01:00
|
|
|
undefinable_bool() { }
|
2021-03-27 19:53:30 +01:00
|
|
|
explicit undefinable_bool(bool newValue) { super::operator=(newValue); }
|
2021-03-22 13:40:01 +01:00
|
|
|
undefinable_bool& operator = (bool newValue) { super::operator=(newValue); return *this; }
|
2021-04-28 20:30:34 +02:00
|
|
|
};
|
|
|
|
|
2021-09-28 15:54:31 +02:00
|
|
|
class undefinable_xbool : public undefinable<XBool>
|
|
|
|
{
|
|
|
|
using super = undefinable<XBool>;
|
|
|
|
public:
|
|
|
|
undefinable_xbool() { }
|
|
|
|
explicit undefinable_xbool(XBool newValue) { super::operator=(newValue); }
|
|
|
|
undefinable_xbool& operator = (XBool newValue) { super::operator=(newValue); return *this; }
|
|
|
|
};
|
|
|
|
|
2021-04-28 20:30:34 +02:00
|
|
|
class undefinable_uint8 : public undefinable<uint8_t>
|
|
|
|
{
|
|
|
|
using super = undefinable<uint8_t>;
|
|
|
|
public:
|
|
|
|
undefinable_uint8() { }
|
2021-05-08 11:34:17 +02:00
|
|
|
explicit undefinable_uint8(uint8_t newValue) { super::operator=(newValue); }
|
2021-04-28 20:30:34 +02:00
|
|
|
undefinable_uint8& operator = (uint8_t newValue) { super::operator=(newValue); return *this; }
|
2021-03-19 19:37:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class undefinable_uint16 : public undefinable<uint16_t>
|
|
|
|
{
|
|
|
|
using super = undefinable<uint16_t>;
|
|
|
|
public:
|
2021-03-26 10:43:15 +01:00
|
|
|
undefinable_uint16() { }
|
2021-04-03 12:55:25 +02:00
|
|
|
explicit undefinable_uint16(uint16_t newValue) { super::operator=(newValue); }
|
2021-03-22 13:40:01 +01:00
|
|
|
undefinable_uint16& operator = (uint16_t newValue) { super::operator=(newValue); return *this; }
|
2021-03-19 19:37:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class undefinable_uint32 : public undefinable<uint32_t>
|
|
|
|
{
|
|
|
|
using super = undefinable<uint32_t>;
|
|
|
|
public:
|
2021-03-26 10:43:15 +01:00
|
|
|
undefinable_uint32() { }
|
2021-03-27 19:53:30 +01:00
|
|
|
explicit undefinable_uint32(uint32_t newValue) { super::operator=(newValue); }
|
2021-03-22 13:40:01 +01:00
|
|
|
undefinable_uint32& operator = (uint32_t newValue) { super::operator=(newValue); return *this; }
|
2021-03-19 19:37:24 +01:00
|
|
|
};
|
|
|
|
|
2021-03-22 13:40:01 +01:00
|
|
|
class undefinable_XString8 : public undefinable<XString8>
|
|
|
|
{
|
|
|
|
using super = undefinable<XString8>;
|
|
|
|
public:
|
2021-03-27 19:53:30 +01:00
|
|
|
undefinable_XString8() { }
|
|
|
|
explicit undefinable_XString8(const XString8& newValue) { super::operator=(newValue); }
|
2021-03-22 13:40:01 +01:00
|
|
|
undefinable_XString8& operator = (XString8 newValue) { super::operator=(newValue); return *this; }
|
|
|
|
};
|
2021-03-19 19:37:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
#endif /* CPP_LIB_DEF_TYPES_H_ */
|