MQLplus/lib_structures/lib_unions.mqh
super.admin 466f9ca5c5 convert
2025-05-30 16:09:52 +02:00

542 lines
32 KiB
MQL5

#ifndef LIB_MQLPLUS_STRUCTURE_UNIONS_MQH_INCLUDED
#define LIB_MQLPLUS_STRUCTURE_UNIONS_MQH_INCLUDED
#property version "1.2";
/**********************************************************************************
* Copyright (C) 2010-2022 Dominik Egert <info@freie-netze.de>
*
* This file is the unions data conversion include file.
*
* MQLplus, including this file may not be copied and/or distributed
* without explecit permit by the author.
* Author Dominik Egert / Freie Netze UG.
**********************************************************************************
*
* Version: 1.2
* State: production
*
* File information
* ================
*
*
*
*/
#ifdef DBG_MSG_TRACE_FILE_LOADER
DBG_MSG_TRACE_FILE_LOADER;
#endif
/*********************************************************************************************************************************************************/
/* */
/* MQLplus datatype conversion unions */
/* */
/*********************************************************************************************************************************************************/
// Predeclaration conversion union
union _clr_transform;
union _64_bit;
/// 32 Bit generic conversion union
/**
32 Bit conversion union data structure.
Allows binary conversion of int, uint, float and color.
Data can be accessed by uchar array on a byte level.
Structure does not interpret float binary storage format.
*/
union _32_bit
{
// Conversion data
int i; /*!< int type access varialbe i */
uint ui; /*!< unsigned int type access varialbe ui */
float f; /*!< float type access varialbe f */
color clr; /*!< color type access varialbe clr */
ushort us[2]; /*!< unsigned short type access array us[2] */
uchar uc[4]; /*!< unsigned char type access array uc[4] */
//! Constructors
/*!
Union structure constructors support input types for binary access.
Default constructor type.
Copy constructor type.
Convert 64 bit to lower 32 bit constructor type
int, unsigned int, float and color constructor types.
Byte constructor type
\param -none-
\param p_in as _32_bit& reference as copy constructor
\param p_in as _64_bit& reference as conversion to lower 32 bit binary
\param p_in as integer datatype
\param p_in as unsigned integer datatype
\param p_in as color datatype
\param b1 as first byte unsigned char datatype
\param b2 as second byte unsigned char datatype
\param b3 as third byte unsigned char datatype
\param b4 as fourth byte unsigned char datatype
*/
_32_bit() : i (NULL) { };
_32_bit(const _32_bit& p_in) : ui (p_in.ui) { };
_32_bit(const _64_bit& p_in) : ui ((uint)p_in.ul) { };
_32_bit(const int p_in) : i (p_in) { };
_32_bit(const uint p_in) : ui (p_in) { };
_32_bit(const float p_in) : f (p_in) { };
_32_bit(const color p_in) : clr (p_in) { };
_32_bit(const ushort s1,
const ushort s2 = NULL)
{
us[0] = s1;
us[1] = s2;
};
_32_bit(const ushort s1,
const uchar b3 = NULL,
const uchar b4 = NULL)
{
us[0] = s1;
uc[2] = b3;
uc[3] = b4;
};
_32_bit(const uchar b1,
const ushort s23 = NULL,
const uchar b4 = NULL)
{
uc[0] = b1;
uc[1] = (uchar)(s23 >> 8);
uc[2] = (uchar)(s23);
uc[3] = b4;
};
_32_bit(const uchar b1,
const uchar b2 = NULL,
const ushort s2 = NULL)
{
uc[0] = b1;
uc[1] = b2;
us[1] = s2;
};
_32_bit(const uchar b1,
const uchar b2 = NULL,
const uchar b3 = NULL,
const uchar b4 = NULL)
{
uc[0] = b1;
uc[1] = b2;
uc[2] = b3;
uc[3] = b4;
};
//! Assignment operators
/*!
Assignment operator supports as input
_clr_transform& reference to structure
_32_bit& reference to structure
_64_bit& reference to structure
integer type assignment
unsigned integer type assignment
float type assignment
color type assignment
\param in as 4 byte value to be assigned according to its type.
*/
void operator=(const _clr_transform& in) { ui = in.ui; };
void operator=(const _32_bit& in) { ui = in.ui; };
void operator=(const _64_bit& in) { ui = (uint)in.ul; };
void operator=(const int in) { i = in; };
void operator=(const uint in) { ui = in; };
void operator=(const float in) { f = in; };
void operator=(const color in) { clr = in; };
// Comparison operators
bool operator==(const _clr_transform& in) { return(ui == in.ui); };
bool operator==(const _32_bit& in) { return(ui == in.ui); };
bool operator==(const _64_bit& in) { return(ui == (uint)in.ul); };
bool operator==(const int in) { return(i == in); };
bool operator==(const uint in) { return(ui == in); };
bool operator==(const float in) { return(f == in); };
bool operator==(const color in) { return(clr == in); };
bool operator!=(const _clr_transform& in) { return(ui != in.ui); };
bool operator!=(const _32_bit& in) { return(ui != in.ui); };
bool operator!=(const _64_bit& in) { return(ui != (uint)in.ul); };
bool operator!=(const int in) { return(i != in); };
bool operator!=(const uint in) { return(ui != in); };
bool operator!=(const float in) { return(f != in); };
bool operator!=(const color in) { return(clr != in); };
bool operator<(const _clr_transform& in) { return(ui < in.ui); };
bool operator<(const _32_bit& in) { return(ui < in.ui); };
bool operator<(const _64_bit& in) { return(ui < (uint)in.ul); };
bool operator<(const int in) { return(i < in); };
bool operator<(const uint in) { return(ui < in); };
bool operator<(const float in) { return(f < in); };
bool operator<(const color in) { return(clr < in); };
bool operator>(const _clr_transform& in) { return(ui > in.ui); };
bool operator>(const _32_bit& in) { return(ui > in.ui); };
bool operator>(const _64_bit& in) { return(ui > (uint)in.ul); };
bool operator>(const int in) { return(i > in); };
bool operator>(const uint in) { return(ui > in); };
bool operator>(const float in) { return(f > in); };
bool operator>(const color in) { return(clr > in); };
bool operator<=(const _clr_transform& in) { return(ui <= in.ui); };
bool operator<=(const _32_bit& in) { return(ui <= in.ui); };
bool operator<=(const _64_bit& in) { return(ui <= (uint)in.ul); };
bool operator<=(const int in) { return(i <= in); };
bool operator<=(const uint in) { return(ui <= in); };
bool operator<=(const float in) { return(f <= in); };
bool operator<=(const color in) { return(clr <= in); };
bool operator>=(const _clr_transform& in) { return(ui >= in.ui); };
bool operator>=(const _32_bit& in) { return(ui >= in.ui); };
bool operator>=(const _64_bit& in) { return(ui >= (uint)in.ul); };
bool operator>=(const int in) { return(i >= in); };
bool operator>=(const uint in) { return(ui >= in); };
bool operator>=(const float in) { return(f >= in); };
bool operator>=(const color in) { return(clr >= in); };
};
// 32 Bit color structure
struct _basic_clr_rgb
{
// Local color storage
uchar red;
uchar green;
uchar blue;
uchar alpha;
};
// 32 Bit color conversion union
union _clr_transform
{
// Conversion data
int i;
uint ui;
float f;
color clr;
uchar uc[4];
// Conversion data
_basic_clr_rgb rgb;
// Constructors
_clr_transform() : i (NULL) { };
_clr_transform( const _32_bit& p_in) : ui (p_in.ui) { };
_clr_transform( const int p_in) : i (p_in) { };
_clr_transform( const uint p_in) : ui (p_in) { };
_clr_transform( const float p_in) : f (p_in) { };
_clr_transform( const color p_in) : clr (p_in) { };
_clr_transform( const uchar _red,
const uchar _green,
const uchar _blue,
const uchar _alpha = NULL) : clr ( (_red)
+ (_green << 8)
+ (_blue << 16)
+ (_alpha << 24)) { };
_clr_transform( const _clr_transform& p_in) : ui (p_in.ui) { };
// Assignment Operators
void operator=(const _clr_transform& in) { ui = in.ui; };
void operator=(const _32_bit& in) { ui = in.ui; };
void operator=(const int in) { i = in; };
void operator=(const uint in) { ui = in; };
void operator=(const float in) { f = in; };
void operator=(const color in) { clr = in; };
// Comparison operators
bool operator==(const _clr_transform& in) { return(ui == in.ui); };
bool operator==(const _32_bit& in) { return(ui == in.ui); };
bool operator==(const int in) { return(i == in); };
bool operator==(const uint in) { return(ui == in); };
bool operator==(const float in) { return(f == in); };
bool operator==(const color in) { return(clr == in); };
bool operator!=(const _clr_transform& in) { return(ui != in.ui); };
bool operator!=(const _32_bit& in) { return(ui != in.ui); };
bool operator!=(const int in) { return(i != in); };
bool operator!=(const uint in) { return(ui != in); };
bool operator!=(const float in) { return(f != in); };
bool operator!=(const color in) { return(clr != in); };
bool operator<(const _clr_transform& in) { return(ui < in.ui); };
bool operator<(const _32_bit& in) { return(ui < in.ui); };
bool operator<(const int in) { return(i < in); };
bool operator<(const uint in) { return(ui < in); };
bool operator<(const float in) { return(f < in); };
bool operator<(const color in) { return(clr < in); };
bool operator>(const _clr_transform& in) { return(ui > in.ui); };
bool operator>(const _32_bit& in) { return(ui > in.ui); };
bool operator>(const int in) { return(i > in); };
bool operator>(const uint in) { return(ui > in); };
bool operator>(const float in) { return(f > in); };
bool operator>(const color in) { return(clr > in); };
bool operator<=(const _clr_transform& in) { return(ui <= in.ui); };
bool operator<=(const _32_bit& in) { return(ui <= in.ui); };
bool operator<=(const int in) { return(i <= in); };
bool operator<=(const uint in) { return(ui <= in); };
bool operator<=(const float in) { return(f <= in); };
bool operator<=(const color in) { return(clr <= in); };
bool operator>=(const _clr_transform& in) { return(ui >= in.ui); };
bool operator>=(const _32_bit& in) { return(ui >= in.ui); };
bool operator>=(const int in) { return(i >= in); };
bool operator>=(const uint in) { return(ui >= in); };
bool operator>=(const float in) { return(f >= in); };
bool operator>=(const color in) { return(clr >= in); };
};
// 64 Bit conversion union
union _64_bit
{
// Conversion data
long l;
ulong ul;
double d;
datetime dt;
uint ui[2];
ushort us[4];
uchar uc[8];
// Constructors
_64_bit() : l (NULL) { };
_64_bit(const _32_bit& p_in) : ul (p_in.ui) { };
_64_bit(const _64_bit& p_in) : ul (p_in.ul) { };
_64_bit(const long p_in) : l (p_in) { };
_64_bit(const ulong p_in) : ul (p_in) { };
_64_bit(const double p_in) : d (p_in) { };
_64_bit(const datetime p_in) : dt (p_in) { };
_64_bit(const uchar b1,
const uchar b2 = NULL,
const uchar b3 = NULL,
const uchar b4 = NULL,
const uchar b5 = NULL,
const uchar b6 = NULL,
const uchar b7 = NULL,
const uchar b8 = NULL)
{
uc[0] = b1;
uc[1] = b2;
uc[2] = b3;
uc[3] = b4;
uc[4] = b5;
uc[5] = b6;
uc[6] = b7;
uc[7] = b8;
};
// Assignment operators
void operator=(const _32_bit& in) { ul = in.ui; };
void operator=(const _64_bit& in) { ul = in.ul; };
void operator=(const long in) { l = in; };
void operator=(const ulong in) { ul = in; };
void operator=(const double in) { d = in; };
void operator=(const datetime in) { dt = in; };
// Comparison operators
bool operator==(const _32_bit& in) { return(ul == (ulong)in.ui); };
bool operator==(const _64_bit& in) { return(ul == in.ul); };
bool operator==(const long in) { return(l == in); };
bool operator==(const ulong in) { return(ul == in); };
bool operator==(const double in) { return(d == in); };
bool operator==(const datetime in) { return(dt == in); };
bool operator!=(const _32_bit& in) { return(ul != (ulong)in.ui); };
bool operator!=(const _64_bit& in) { return(ul != in.ul); };
bool operator!=(const long in) { return(l != in); };
bool operator!=(const ulong in) { return(ul != in); };
bool operator!=(const double in) { return(d != in); };
bool operator!=(const datetime in) { return(dt != in); };
bool operator<(const _32_bit& in) { return(ul < (ulong)in.ui); };
bool operator<(const _64_bit& in) { return(ul < in.ul); };
bool operator<(const long in) { return(l < in); };
bool operator<(const ulong in) { return(ul < in); };
bool operator<(const double in) { return(d < in); };
bool operator<(const datetime in) { return(dt < in); };
bool operator>(const _32_bit& in) { return(ul > (ulong)in.ui); };
bool operator>(const _64_bit& in) { return(ul > in.ul); };
bool operator>(const long in) { return(l > in); };
bool operator>(const ulong in) { return(ul > in); };
bool operator>(const double in) { return(d > in); };
bool operator>(const datetime in) { return(dt > in); };
bool operator<=(const _32_bit& in) { return(ul <= (ulong)in.ui); };
bool operator<=(const _64_bit& in) { return(ul <= in.ul); };
bool operator<=(const long in) { return(l <= in); };
bool operator<=(const ulong in) { return(ul <= in); };
bool operator<=(const double in) { return(d <= in); };
bool operator<=(const datetime in) { return(dt <= in); };
bool operator>=(const _32_bit& in) { return(ul >= (ulong)in.ui); };
bool operator>=(const _64_bit& in) { return(ul >= in.ul); };
bool operator>=(const long in) { return(l >= in); };
bool operator>=(const ulong in) { return(ul >= in); };
bool operator>=(const double in) { return(d >= in); };
bool operator>=(const datetime in) { return(dt >= in); };
};
// Templated conversion union
template <typename T, typename U>
union _T_bit
{
// Conversion data
T _int;
unsigned T _uint;
U _flt;
uchar _uc[sizeof(T) > sizeof(U) ? sizeof(T) : sizeof(U)];
// Constructors
_T_bit() : _int (NULL) { };
_T_bit(const _T_bit& p_in) : _uint ((T)p_in._uint) { };
_T_bit(const _32_bit& p_in) : _uint ((T)p_in.ui) { };
_T_bit(const _64_bit& p_in) : _uint ((T)p_in.ul) { };
_T_bit(const char p_in) : _uint ((T)p_in) { };
_T_bit(const uchar p_in) : _uint ((unsigned T)p_in) { };
_T_bit(const short p_in) : _uint ((T)p_in) { };
_T_bit(const ushort p_in) : _uint ((unsigned T)p_in) { };
_T_bit(const int p_in) : _uint ((T)p_in) { };
_T_bit(const uint p_in) : _uint ((unsigned T)p_in) { };
_T_bit(const float p_in) : _flt ((U)p_in) { };
_T_bit(const color p_in) : _uint ((T)p_in) { };
_T_bit(const long p_in) : _uint ((T)p_in) { };
_T_bit(const ulong p_in) : _uint ((unsigned T)p_in) { };
_T_bit(const double p_in) : _flt ((U)p_in) { };
_T_bit(const datetime p_in) : _uint ((T)p_in) { };
_T_bit(const uchar& b_in[]) : _int (NULL) { ::ArrayCopy(_uc, b_in, 0, sizeof(T)); };
// Assignment operators
void operator=(const _32_bit& in) { _int = (T) in.ui; };
void operator=(const _64_bit& in) { _int = (T) in.ul; };
void operator=(const _T_bit& in) { _int = (T) in._int; };
void operator=(const char in) { _int = (T) in; };
void operator=(const uchar in) { _uint = (unsigned T)in; };
void operator=(const short in) { _int = (T) in; };
void operator=(const ushort in) { _uint = (unsigned T)in; };
void operator=(const int in) { _int = (T) in; };
void operator=(const uint in) { _uint = (unsigned T)in; };
void operator=(const float in) { _flt = (U) in; };
void operator=(const color in) { _int = (T) in; };
void operator=(const long in) { _int = (T) in; };
void operator=(const ulong in) { _uint = (unsigned T)in; };
void operator=(const double in) { _flt = (U) in; };
void operator=(const datetime in) { _int = (T) in; };
// Comparison operators
bool operator==(const _T_bit& in) { return(_int == in._int); };
bool operator==(const _32_bit& in) { return(_int == (T)in.i); };
bool operator==(const _64_bit& in) { return(_int == (T)in.l); };
bool operator==(const char in) { return((char)_int == in); };
bool operator==(const uchar in) { return((uchar)_uint == in); };
bool operator==(const short in) { return((short)_int == in); };
bool operator==(const ushort in) { return((ushort)_uint == in); };
bool operator==(const int in) { return((int)_int == in); };
bool operator==(const uint in) { return((uint)_uint == in); };
bool operator==(const float in) { return((float)_flt == in); };
bool operator==(const color in) { return((color)_int == in); };
bool operator==(const long in) { return((long)_int == in); };
bool operator==(const ulong in) { return((ulong)_uint == in); };
bool operator==(const double in) { return((double)_flt == in); };
bool operator==(const datetime in) { return((datetime)_int == in); };
bool operator!=(const _T_bit& in) { return(_int != in._int); };
bool operator!=(const _32_bit& in) { return(_int != (T)in.i); };
bool operator!=(const _64_bit& in) { return(_int != (T)in.l); };
bool operator!=(const char in) { return((char)_int != in); };
bool operator!=(const uchar in) { return((uchar)_uint != in); };
bool operator!=(const short in) { return((short)_int != in); };
bool operator!=(const ushort in) { return((ushort)_uint != in); };
bool operator!=(const int in) { return((int)_int != in); };
bool operator!=(const uint in) { return((uint)_uint != in); };
bool operator!=(const float in) { return((float)_flt != in); };
bool operator!=(const color in) { return((color)_int != in); };
bool operator!=(const long in) { return((long)_int != in); };
bool operator!=(const ulong in) { return((ulong)_uint != in); };
bool operator!=(const double in) { return((double)_flt != in); };
bool operator!=(const datetime in) { return((datetime)_int != in); };
bool operator<(const _T_bit& in) { return(_int < in._int); };
bool operator<(const _32_bit& in) { return(_int < (T)in.i); };
bool operator<(const _64_bit& in) { return(_int < (T)in.l); };
bool operator<(const char in) { return((char)_int < in); };
bool operator<(const uchar in) { return((uchar)_uint < in); };
bool operator<(const short in) { return((short)_int < in); };
bool operator<(const ushort in) { return((ushort)_uint < in); };
bool operator<(const int in) { return((int)_int < in); };
bool operator<(const uint in) { return((uint)_uint < in); };
bool operator<(const float in) { return((float)_flt < in); };
bool operator<(const color in) { return((color)_int < in); };
bool operator<(const long in) { return((long)_int < in); };
bool operator<(const ulong in) { return((ulong)_uint < in); };
bool operator<(const double in) { return((double)_flt < in); };
bool operator<(const datetime in) { return((datetime)_int < in); };
bool operator>(const _T_bit& in) { return(_int > in._int); };
bool operator>(const _32_bit& in) { return(_int > (T)in.i); };
bool operator>(const _64_bit& in) { return(_int > (T)in.l); };
bool operator>(const char in) { return((char)_int > in); };
bool operator>(const uchar in) { return((uchar)_uint > in); };
bool operator>(const short in) { return((short)_int > in); };
bool operator>(const ushort in) { return((ushort)_uint > in); };
bool operator>(const int in) { return((int)_int > in); };
bool operator>(const uint in) { return((uint)_uint > in); };
bool operator>(const float in) { return((float)_flt > in); };
bool operator>(const color in) { return((color)_int > in); };
bool operator>(const long in) { return((long)_int > in); };
bool operator>(const ulong in) { return((ulong)_uint > in); };
bool operator>(const double in) { return((double)_flt > in); };
bool operator>(const datetime in) { return((datetime)_int > in); };
bool operator<=(const _T_bit& in) { return(_int <= in._int); };
bool operator<=(const _32_bit& in) { return(_int <= (T)in.i); };
bool operator<=(const _64_bit& in) { return(_int <= (T)in.l); };
bool operator<=(const char in) { return((char)_int <= in); };
bool operator<=(const uchar in) { return((uchar)_uint <= in); };
bool operator<=(const short in) { return((short)_int <= in); };
bool operator<=(const ushort in) { return((ushort)_uint <= in); };
bool operator<=(const int in) { return((int)_int <= in); };
bool operator<=(const uint in) { return((uint)_uint <= in); };
bool operator<=(const float in) { return((float)_flt <= in); };
bool operator<=(const color in) { return((color)_int <= in); };
bool operator<=(const long in) { return((long)_int <= in); };
bool operator<=(const ulong in) { return((ulong)_uint <= in); };
bool operator<=(const double in) { return((double)_flt <= in); };
bool operator<=(const datetime in) { return((datetime)_int <= in); };
bool operator>=(const _T_bit& in) { return(_int >= in._int); };
bool operator>=(const _32_bit& in) { return(_int >= (T)in.i); };
bool operator>=(const _64_bit& in) { return(_int >= (T)in.l); };
bool operator>=(const char in) { return((char)_int >= in); };
bool operator>=(const uchar in) { return((uchar)_uint >= in); };
bool operator>=(const short in) { return((short)_int >= in); };
bool operator>=(const ushort in) { return((ushort)_uint >= in); };
bool operator>=(const int in) { return((int)_int >= in); };
bool operator>=(const uint in) { return((uint)_uint >= in); };
bool operator>=(const float in) { return((float)_flt >= in); };
bool operator>=(const color in) { return((color)_int >= in); };
bool operator>=(const long in) { return((long)_int >= in); };
bool operator>=(const ulong in) { return((ulong)_uint >= in); };
bool operator>=(const double in) { return((double)_flt >= in); };
bool operator>=(const datetime in) { return((datetime)_int >= in); };
};
//
// END MQL data structures */
//*********************************************************************************************************************************************************/
#endif // LIB_MQLPLUS_STRUCTURE_UNIONS_MQH_INCLUDED