Libraries: TypeToBytes - page 3

 
yes
 
Three lines of the same thing
Color = (color)ColorToARGB(Color, Alpha);

Color = (color)((Color & 0xFFFFFF) + (Alpha << 24));

_W(Color)[3] = Alpha;
 

Forum on trading, automated trading systems and testing trading strategies

Any newbie questions on MQL4, help and discussion on algorithms and codes

fxsaber, 2017.03.07 13:55

#include <TypeToBytes.mqh>

template <typename T>
void Swap( T &Value1, T &Value2 )
{
  const T Tmp = Value1;
  
  Value1 = Value2;
  Value2 = Tmp;
}

// Sort an array of any simple type
template <typename T>
bool MyArraySort( T &Array[] )
{
  if (!ArraySort(Array))
  {
    const int Size = ArraySize(Array);
    
    for (int i = 0; i < Size - 1; i++)
    {
      const T Tmp = Array[i];
      
      for (int j = i + 1; j < Size; j++)
        if (_R(Tmp) == Array[j]) // TypeToBytes.mqh
        {
          Swap(Array[i + 1], Array[j]);
          
          i++;
        }
    }      
  }
  
  return(true);
}
 
Added byte-by-byte operation with arrays as well
// Working with arrays
  short Array[] = {1, 2, 3};
  ArrayPrint(_R(Array).Bytes);           // Print byte-by-byte representation of Array array

  PRINT((_R(Array) == _R(Array).Bytes))  // Make sure that byte-by-byte array comparison works.

  _W(Color) = Array;                     // Byte-by-byte write Array array to Color
  PRINT(Color)                           // Make sure Color is now C'1,0,2'.
  ArrayPrint(_R(Color).Bytes);           // Print byte-by-byte representation of Color

  uchar Array2[];
  ArrayCopy(Array2, _R(Tick).Bytes);     // Write the byte representation of Tick to Array2
  PRINT((_R(Tick) == Array2))            // Make sure that Tick and Array2 match byte by by byte

  MqlTick Tick2 = {0};
  _W(Tick2) = Array2;                    // Write Array2 array to Tick2
  PRINT((_R(Tick) == Tick2))             // Make sure that Tick and Tick2 match.

  int Array3[] = {INT_MIN, INT_MAX};
  ArrayPrint(_R(Array3).Bytes);          // Print byte-by-byte representation of Array3 array
  ArrayPrint(_R(Array).Bytes);           // Print byte-by-byte representation of Array array

  _ArrayCopy(Array3, Array);             // Copy Array array to Array3 array byte by by byte
  ArrayPrint(_R(Array3).Bytes);          // Make sure that the byte representation of the Array3 array matches the
 
Added byte-by-byte operation with strings as well
// Working with strings
  string Str = "abcd";

  _W(i) = Str;                           // Write the string to (int)i byte by byte

  PRINT((_R(i) == Str))                  // Byte-by-byte comparison of int and sring

  ArrayPrint(_R(i).Bytes);               // Looked up bytes i
  ArrayPrint(_R(Str).Bytes);             // Looked at Str bytes

  PRINT(_R(Str)[(short)1])               // Took the short-value by mix 1 in Str

  PRINT(Str)
  _W(Str)[2] = "98765";                  // Byte-by-byte writing of a string to a string at offset 2
  PRINT(Str)

  string StrArray[] = {"123", "45", "6789"};
  _W(Str) = StrArray;                    // Write a string array to a string
  PRINT(Str)

  _W(Str)[3] = (uchar)0;                 // A zero is written to byte at offset 3, thus cutting off the string (length - 3 ANSI characters (4 bytes)).
  PRINT(Str);
 

Forum on trading, automated trading systems and testing trading strategies

Libraries: File Mapping without DLL

fxsaber, 2017.04.03 16:07

Thanks to the author for the library!

I made functions for transferring any data. Below script shows their work on the example of ticks

#include <MemMapLib.mqh>
#include <TypeToBytes.mqh>

// Allocates memory of the specified length for the data 
template <typename T>
bool GetFileMemory( CMemMapFile* &FileMemory, const int Amount, const string FileName = "Local\\test" )
{
  FileMemory = new CMemMapFile;
    
  return(FileMemory.Open(FileName, sizeof(T) * Amount + sizeof(int) + HEAD_MEM, modeCreate) == 0);
}

// Writes data to memory
template <typename T>
void DataSave( CMemMapFile* FileMemory, const T &Data[], const bool FromBegin = true  )
{
  const int Size = ArraySize(Data) * sizeof(T);
  uchar Bytes[];
  
  _ArrayCopy(Bytes, _R(Size).Bytes);              // Recorded the quantity 
  _ArrayCopy(Bytes, _R(Data).Bytes, sizeof(int)); // Recorded the data

  if (FromBegin)
    FileMemory.Seek(0, SEEK_SET);

  FileMemory.Write(Bytes, ArraySize(Bytes)); // Dumped everything into memory
  
  return;
}

// Reads data from memory
template <typename T>
int DataLoad( CMemMapFile* FileMemory, T &Data[], const bool FromBegin = true )
{
  if (FromBegin)
    FileMemory.Seek(0, SEEK_SET);

  uchar Bytes[];
          
  FileMemory.Read(Bytes, sizeof(int));  // Read the amount of data from memory 
  FileMemory.Read(Bytes, _R(Bytes)[0]); // Got the data itself

  _ArrayCopy(Data, Bytes);              // Dumped the data into an array
  
  return(ArraySize(Data));
}

#define  AMOUNT 1000

#define  TOSTRING(A) #A + " = " + (string)(A) + " "

// Example of tick transmission
void OnStart()
{  
  CMemMapFile* FileMemory;
  
  if (GetFileMemory<MqlTick>(FileMemory, AMOUNT))
  {
    MqlTick Ticks4Save[];    
    CopyTicks(_Symbol, Ticks4Save, COPY_TICKS_INFO, 0, AMOUNT);
    
    DataSave(FileMemory, Ticks4Save);
    
    MqlTick Ticks4Load[];    
    
    if (DataLoad(FileMemory, Ticks4Load) > 0)    
      Print(TOSTRING((_R(Ticks4Save) == Ticks4Load)) +
            TOSTRING(ArraySize(Ticks4Save)) +
            TOSTRING(ArraySize(Ticks4Load)));
     
    FileMemory.Close();   
  }
  
  delete FileMemory;
}


Result

(_R(Ticks4Save)==Ticks4Load) = true ArraySize(Ticks4Save) = 1000 ArraySize(Ticks4Load) = 1000

An example of possible practical application of innovations.

 
#include <TypeToBytes.mqh>

// Merge all array strings into one string
string StringArrayConcatenate( const string &StrArray[] )
{
  string Str = "";
  const int Size = ArraySize(StrArray);
  
  for (int i = 0; i < Size; i++)
    Str += StrArray[i];
    
  return(Str);
}

void OnStart()
{
  string StrArray[] = {"abcd", "123", "zxc", "890", "qwert"};
  string Str1, Str2;

// Merge all array strings into one string 
  Str1 = StringArrayConcatenate(StrArray);
  _W(Str2) = StrArray;
  
  Print(Str1);
  Print(Str2); 
}

Highlighted - the same result in vastly different ways

abcd123zxc890qwert
abcd123zxc890qwert
 
After the update, it will be available
// Set a custom structure
struct STRUCT
{
  int i;

  // Set the assignment operator
  void operator =( const STRUCT& ) {}
};

// Demonstration of the result of bypassing the custom assignment operator of the _R structure by the library function
  STRUCT Struct;                         // Created an object with a custom assignment operator
  Struct.i = 1;

  ArrayPrint(_R(Struct).Bytes);          // Make sure that the assignment operator
  PRINT(_R(Struct) == Struct)            // does not affect the _R functionality of the library

// Checking the "correctness" of the type assignment operator
  PRINT(_WRONG_ASSIGN_OPERATOR(int))     // Correct
  PRINT(_WRONG_ASSIGN_OPERATOR(MqlTick)) // Correct
  PRINT(_WRONG_ASSIGN_OPERATOR(STRUCT))  // Not correct
 
  • Определение корректности оператора присваивания структур.

An example of how this feature can be useful for detecting potential errors.

Write and run the script.

#include <TypeToBytes.mqh>

struct STRUCT
{
  int i;
  
  STRUCT() : i(0) {}
  
  template <typename T>
  void operator =( T& ) {}
};

#define  PRINT(A) ::Print(#A + " = " + (string)(A));

void OnStart()
{
  PRINT(_WRONG_ASSIGN_OPERATOR(STRUCT))
}


Result.

_WRONG_ASSIGN_OPERATOR(STRUCT) = true

This indicates that the assignment operator will not copy a structure into a structure of the same type.


If we add more to the structure,

  void operator =( STRUCT &Value ) { this.i = 0; }

the result will be the same.


It would seem that by fixing this operator to

  void operator =( STRUCT &Value ) { this.i = Value.i; }

should make everything correct, but the library says otherwise.


Perhaps, this is the most subtle point of this example.

We fix it to

#include <TypeToBytes.mqh>

struct STRUCT
{
  int i;
  
  STRUCT() : i(0) {}
  
  template <typename T>
  void operator =( T& ) {}

  void operator =( const STRUCT &Value ) { this.i = Value.i; }
};

#define  PRINT(A) ::Print(#A + " = " + (string)(A));

void OnStart()
{
  PRINT(_WRONG_ASSIGN_OPERATOR(STRUCT))
}

and we get the result

_WRONG_ASSIGN_OPERATOR(STRUCT) = false


Now the copy operator is written correctly!

You can check the correctness of assignment/copy operators of any simple structures in a similar way.

 
What is the difference between a NULL string and an empty string?
#include <TypeToBytes.mqh>

#define  PRINT(A) ::Print(#A + " = " + (string)(A));

void OnStart()
{  
  const string StrNull = NULL;
  
  PRINT(ArraySize(_R(StrNull).Bytes))
  PRINT(ArraySize(_R("").Bytes))
}

Result

ArraySize(_R(StrNull).Bytes) = 0
ArraySize(_R().Bytes) = 1

A NULL string has zero length in bytes. An empty string is 1 byte long (where zero is the end of the string).