1 line
19 KiB
MQL5
1 line
19 KiB
MQL5
|
// MQL4&5-code
#define __TYPETOBYTES__
template <typename T>
class CASTING
{
public:
template <typename T1>
static const T Casting( const T1 &Value )
{
#ifdef TYPETOBYTES_FULL_SLOW
static const bool IsScript = ((ENUM_PROGRAM_TYPE)::MQLInfoInteger(MQL_PROGRAM_TYPE) == PROGRAM_SCRIPT);
static bool FirstRun = true;
T Data = {0};
if (FirstRun)
{
if (IsScript)
{
const int handle = ::FileOpen("Casting.tmp", FILE_READ | FILE_WRITE | FILE_BIN);
if (handle != INVALID_HANDLE)
{
::FileWriteStruct(handle, Value);
::FileSeek(handle, 0, SEEK_SET);
::FileReadStruct(handle, Data);
::FileClose(handle);
}
}
else
{
::Alert("TYPETOBYTES_FULL_SLOW-mode only for Scripts!");
FirstRun = false;
}
}
return(Data);
#else // TYPETOBYTES_FULL_SLOW
union CAST
{
T1 Value1;
const T Value2;
CAST( const T1 &Value)
{
this.Value1 = Value; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
};
const CAST Union(Value);
return(Union.Value2);
#endif // TYPETOBYTES_FULL_SLOW
}
};
#define _C(A, B) CASTING<A>::Casting(B)
#define _R(A) TYPETOBYTES::Read(A)
#define _W(A) A=TYPETOBYTES::Write(A)
#define _OFFSET(A,B) TYPETOBYTES::Offset(A, A.B)
#define _WRONG_ASSIGN_OPERATOR(A) TYPETOBYTES::IsWrongAssignOperator<A>()
#define EQUAL_DEFINE(A) \
template <typename T1> \
bool operator ==( const T1 &A ) const \
{ \
return(::ArrayCompare(this.Bytes, \
_R(Value).Bytes) == 0); \
} \
\
template <typename T1> \
bool operator !=( const T1 &A ) const \
{ \
return(!(this == Value)); \
}
#define CONSTRUCTOR_DEFINE(A) \
template <typename T1> \
T1 operator []( const T1 Pos ) const \
{ \
STRUCT_TYPE<T1> Res = {0}; \
const int iPos = (int)Pos; \
\
if (iPos + sizeof(T1) <= ::ArraySize(this.Bytes)) \
{ \
STRUCT_READ<T1> Tmp = {0}; \
\
::ArrayCopy(Tmp.Bytes, this.Bytes, 0, iPos, sizeof(T1)); \
\
Res = _C(STRUCT_TYPE<T1>, Tmp); \
} \
\
return(Res.Value); \
} \
\
string operator []( const string Pos ) const \
{ \
string Res = NULL; \
const int iPos = (int)Pos; \
const int Size = ::ArraySize(this.Bytes); \
int iEnd = iPos; \
\
while ((iEnd < Size) && (this.Bytes[iEnd] != 0)) \
iEnd++; \
\
if (iPos < Size) \
{ \
uchar TmpBytes[]; \
\
::ArrayCopy(TmpBytes,
|