Skip to content

Commit

Permalink
WIP. Fixing MQL/C++ errors. Moving serialization methods to separate …
Browse files Browse the repository at this point in the history
…files.
  • Loading branch information
nseam committed Nov 11, 2022
1 parent 4cdd0d2 commit fc3de69
Show file tree
Hide file tree
Showing 23 changed files with 503 additions and 382 deletions.
2 changes: 1 addition & 1 deletion Chart.struct.tf.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ struct ChartTf {
* _tf ENUM_TIMEFRAMES Specify timeframe enum.
*/
static ENUM_TIMEFRAMES_INDEX TfToIndex(ENUM_TIMEFRAMES _tf) {
_tf = (_tf == 0 || _tf == PERIOD_CURRENT) ? (ENUM_TIMEFRAMES)Platform::Period() : _tf;
_tf = (_tf == 0 || _tf == PERIOD_CURRENT) ? (ENUM_TIMEFRAMES)Period() : _tf;
for (int i = 0; i < ArraySize(TIMEFRAMES_LIST); i++) {
if (TIMEFRAMES_LIST[i] == _tf) {
return (ENUM_TIMEFRAMES_INDEX)i;
Expand Down
151 changes: 151 additions & 0 deletions Convert.basic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+

/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

// Prevents processing this includes file for the second time.
#ifndef __MQL__
#pragma once
#endif

// Includes.
#include "Array.mqh"
#include "Common.extern.h"
#include "DateTime.mqh"
#include "Std.h"
#include "String.mqh"

/**
* Class to provide conversion methods.
*/
class ConvertBasic {
public:
/**
* Convert integer to hex.
*/
static string IntToHex(long long_number) {
string result;
int integer_number = (int)long_number;
for (int i = 0; i < 4; i++) {
int byte = (integer_number >> (i * 8)) & 0xff;
result += StringFormat("%02x", byte);
}
return result;
}

/**
* Convert character into integer.
*/
static int CharToInt(ARRAY_REF(int, _chars)) {
return ((_chars[0]) | (_chars[1] << 8) | (_chars[2] << 16) | (_chars[3] << 24));
}

/**
* Assume: len % 4 == 0.
*/
static int String4ToIntArray(ARRAY_REF(int, output), string in) {
int len;
int i, j;
len = StringLen(in);
if (len % 4 != 0) len = len - len % 4;
int size = ArraySize(output);
if (size < len / 4) {
ArrayResize(output, len / 4);
}
for (i = 0, j = 0; j < len; i++, j += 4) {
output[i] = (StringGetCharacter(in, j)) | ((StringGetCharacter(in, j + 1)) << 8) |
((StringGetCharacter(in, j + 2)) << 16) | ((StringGetCharacter(in, j + 3)) << 24);
}
return (len / 4);
}

static void StringToType(string _value, bool& _out) {
#ifdef __MQL__
_out = _value != "" && _value != NULL && _value != "0" && _value != "false";
#else
_out = _value != "" && _value != "0" && _value != "false";
#endif
}

static void StringToType(string _value, int& _out) { _out = (int)StringToInteger(_value); }
static void StringToType(string _value, unsigned int& _out) { _out = (unsigned int)StringToInteger(_value); }
static void StringToType(string _value, char& _out) { _out = (char)_value[0]; }
static void StringToType(string _value, unsigned char& _out) { _out = (unsigned char)_value[0]; }
static void StringToType(string _value, long& _out) { _out = StringToInteger(_value); }
static void StringToType(string _value, unsigned long& _out) { _out = StringToInteger(_value); }
static void StringToType(string _value, short& _out) { _out = (short)StringToInteger(_value); }
static void StringToType(string _value, unsigned short& _out) { _out = (unsigned short)StringToInteger(_value); }
static void StringToType(string _value, float& _out) { _out = (float)StringToDouble(_value); }
static void StringToType(string _value, double& _out) { _out = StringToDouble(_value); }
static void StringToType(string _value, string& _out) { _out = _value; }
static void StringToType(string _value, color& _out) { _out = 0; }
static void StringToType(string _value, datetime& _out) {
#ifdef __MQL4__
_out = StrToTime(_value);
#else
_out = StringToTime(_value);
#endif
}

static void BoolToType(bool _value, bool& _out) { _out = _value; }
static void BoolToType(bool _value, char& _out) { _out = (char)_value; }
static void BoolToType(bool _value, unsigned char& _out) { _out = (unsigned char)_value; }
static void BoolToType(bool _value, int& _out) { _out = (int)_value; }
static void BoolToType(bool _value, unsigned int& _out) { _out = (unsigned int)_value; }
static void BoolToType(bool _value, long& _out) { _out = (long)_value; }
static void BoolToType(bool _value, unsigned long& _out) { _out = (unsigned long)_value; }
static void BoolToType(bool _value, short& _out) { _out = (short)_value; }
static void BoolToType(bool _value, unsigned short& _out) { _out = (unsigned short)_value; }
static void BoolToType(bool _value, float& _out) { _out = (float)_value; }
static void BoolToType(bool _value, double& _out) { _out = (double)_value; }
static void BoolToType(bool _value, string& _out) { _out = _value ? "1" : "0"; }
static void BoolToType(bool _value, color& _out) { _out = 0; }
static void BoolToType(bool _value, datetime& _out) {}

static void LongToType(long _value, bool& _out) { _out = (bool)_value; }
static void LongToType(long _value, char& _out) { _out = (char)_value; }
static void LongToType(long _value, unsigned char& _out) { _out = (unsigned char)_value; }
static void LongToType(long _value, int& _out) { _out = (int)_value; }
static void LongToType(long _value, unsigned int& _out) { _out = (unsigned int)_value; }
static void LongToType(long _value, long& _out) { _out = (long)_value; }
static void LongToType(long _value, unsigned long& _out) { _out = (unsigned long)_value; }
static void LongToType(long _value, short& _out) { _out = (short)_value; }
static void LongToType(long _value, unsigned short& _out) { _out = (unsigned short)_value; }
static void LongToType(long _value, float& _out) { _out = (float)_value; }
static void LongToType(long _value, double& _out) { _out = (double)_value; }
static void LongToType(long _value, string& _out) { _out = _value ? "1" : "0"; }
static void LongToType(long _value, color& _out) { _out = 0; }
static void LongToType(long _value, datetime& _out) {}

static void DoubleToType(double _value, bool& _out) { _out = (bool)_value; }
static void DoubleToType(double _value, char& _out) { _out = (char)_value; }
static void DoubleToType(double _value, unsigned char& _out) { _out = (unsigned char)_value; }
static void DoubleToType(double _value, int& _out) { _out = (int)_value; }
static void DoubleToType(double _value, unsigned int& _out) { _out = (unsigned int)_value; }
static void DoubleToType(double _value, long& _out) { _out = (long)_value; }
static void DoubleToType(double _value, unsigned long& _out) { _out = (unsigned long)_value; }
static void DoubleToType(double _value, short& _out) { _out = (short)_value; }
static void DoubleToType(double _value, unsigned short& _out) { _out = (unsigned short)_value; }
static void DoubleToType(double _value, float& _out) { _out = (float)_value; }
static void DoubleToType(double _value, double& _out) { _out = (double)_value; }
static void DoubleToType(double _value, string& _out) { _out = _value ? "1" : "0"; }
static void DoubleToType(double _value, color& _out) { _out = 0; }
static void DoubleToType(double _value, datetime& _out) {}
};
118 changes: 0 additions & 118 deletions Convert.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
*
*/

// Prevents processing this includes file for the second time.
#ifndef CONVERT_MQH
#define CONVERT_MQH

// Prevents processing this includes file for the second time.
#ifndef __MQL__
#pragma once
Expand Down Expand Up @@ -280,118 +276,4 @@ class Convert {
return prefix ? CharToString(sign) + DoubleToString(value, digits)
: DoubleToString(value, digits) + CharToString(sign);
}

/**
* Convert integer to hex.
*/
static string IntToHex(long long_number) {
string result;
int integer_number = (int)long_number;
for (int i = 0; i < 4; i++) {
int byte = (integer_number >> (i * 8)) & 0xff;
result += StringFormat("%02x", byte);
}
return result;
}

/**
* Convert character into integer.
*/
static int CharToInt(ARRAY_REF(int, _chars)) {
return ((_chars[0]) | (_chars[1] << 8) | (_chars[2] << 16) | (_chars[3] << 24));
}

/**
* Assume: len % 4 == 0.
*/
static int String4ToIntArray(ARRAY_REF(int, output), string in) {
int len;
int i, j;
len = StringLen(in);
if (len % 4 != 0) len = len - len % 4;
int size = ArraySize(output);
if (size < len / 4) {
ArrayResize(output, len / 4);
}
for (i = 0, j = 0; j < len; i++, j += 4) {
output[i] = (StringGetCharacter(in, j)) | ((StringGetCharacter(in, j + 1)) << 8) |
((StringGetCharacter(in, j + 2)) << 16) | ((StringGetCharacter(in, j + 3)) << 24);
}
return (len / 4);
}

static void StringToType(string _value, bool& _out) {
#ifdef __MQL__
_out = _value != "" && _value != NULL && _value != "0" && _value != "false";
#else
_out = _value != "" && _value != "0" && _value != "false";
#endif
}

static void StringToType(string _value, int& _out) { _out = (int)StringToInteger(_value); }
static void StringToType(string _value, unsigned int& _out) { _out = (unsigned int)StringToInteger(_value); }
static void StringToType(string _value, char& _out) { _out = (char)_value[0]; }
static void StringToType(string _value, unsigned char& _out) { _out = (unsigned char)_value[0]; }
static void StringToType(string _value, long& _out) { _out = StringToInteger(_value); }
static void StringToType(string _value, unsigned long& _out) { _out = StringToInteger(_value); }
static void StringToType(string _value, short& _out) { _out = (short)StringToInteger(_value); }
static void StringToType(string _value, unsigned short& _out) { _out = (unsigned short)StringToInteger(_value); }
static void StringToType(string _value, float& _out) { _out = (float)StringToDouble(_value); }
static void StringToType(string _value, double& _out) { _out = StringToDouble(_value); }
static void StringToType(string _value, string& _out) { _out = _value; }
static void StringToType(string _value, color& _out) { _out = 0; }
static void StringToType(string _value, datetime& _out) {
#ifdef __MQL4__
_out = StrToTime(_value);
#else
_out = StringToTime(_value);
#endif
}

static void BoolToType(bool _value, bool& _out) { _out = _value; }
static void BoolToType(bool _value, char& _out) { _out = (char)_value; }
static void BoolToType(bool _value, unsigned char& _out) { _out = (unsigned char)_value; }
static void BoolToType(bool _value, int& _out) { _out = (int)_value; }
static void BoolToType(bool _value, unsigned int& _out) { _out = (unsigned int)_value; }
static void BoolToType(bool _value, long& _out) { _out = (long)_value; }
static void BoolToType(bool _value, unsigned long& _out) { _out = (unsigned long)_value; }
static void BoolToType(bool _value, short& _out) { _out = (short)_value; }
static void BoolToType(bool _value, unsigned short& _out) { _out = (unsigned short)_value; }
static void BoolToType(bool _value, float& _out) { _out = (float)_value; }
static void BoolToType(bool _value, double& _out) { _out = (double)_value; }
static void BoolToType(bool _value, string& _out) { _out = _value ? "1" : "0"; }
static void BoolToType(bool _value, color& _out) { _out = 0; }
static void BoolToType(bool _value, datetime& _out) {}

static void LongToType(long _value, bool& _out) { _out = (bool)_value; }
static void LongToType(long _value, char& _out) { _out = (char)_value; }
static void LongToType(long _value, unsigned char& _out) { _out = (unsigned char)_value; }
static void LongToType(long _value, int& _out) { _out = (int)_value; }
static void LongToType(long _value, unsigned int& _out) { _out = (unsigned int)_value; }
static void LongToType(long _value, long& _out) { _out = (long)_value; }
static void LongToType(long _value, unsigned long& _out) { _out = (unsigned long)_value; }
static void LongToType(long _value, short& _out) { _out = (short)_value; }
static void LongToType(long _value, unsigned short& _out) { _out = (unsigned short)_value; }
static void LongToType(long _value, float& _out) { _out = (float)_value; }
static void LongToType(long _value, double& _out) { _out = (double)_value; }
static void LongToType(long _value, string& _out) { _out = _value ? "1" : "0"; }
static void LongToType(long _value, color& _out) { _out = 0; }
static void LongToType(long _value, datetime& _out) {}

static void DoubleToType(double _value, bool& _out) { _out = (bool)_value; }
static void DoubleToType(double _value, char& _out) { _out = (char)_value; }
static void DoubleToType(double _value, unsigned char& _out) { _out = (unsigned char)_value; }
static void DoubleToType(double _value, int& _out) { _out = (int)_value; }
static void DoubleToType(double _value, unsigned int& _out) { _out = (unsigned int)_value; }
static void DoubleToType(double _value, long& _out) { _out = (long)_value; }
static void DoubleToType(double _value, unsigned long& _out) { _out = (unsigned long)_value; }
static void DoubleToType(double _value, short& _out) { _out = (short)_value; }
static void DoubleToType(double _value, unsigned short& _out) { _out = (unsigned short)_value; }
static void DoubleToType(double _value, float& _out) { _out = (float)_value; }
static void DoubleToType(double _value, double& _out) { _out = (double)_value; }
static void DoubleToType(double _value, string& _out) { _out = _value ? "1" : "0"; }
static void DoubleToType(double _value, color& _out) { _out = 0; }
static void DoubleToType(double _value, datetime& _out) {}
};

#endif // CONVERT_MQH
5 changes: 3 additions & 2 deletions Data.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ struct MqlRates;

// Includes.
#include "Data.enum.h"
#include "DateTime.mqh"
#include "DateTime.extern.h"
#include "Serializer/Serializer.enum.h"
#include "Serializer/SerializerNode.enum.h"
#include "Std.h"
#include "String.mqh"

#ifndef __MQL__
/**
Expand Down Expand Up @@ -188,7 +189,7 @@ struct DataParamEntry : public MqlParam {
case TYPE_CHAR:
case TYPE_STRING:
case TYPE_UCHAR:
return (T)::StringToDouble(string_value);
return (T)StringToDouble(string_value);
case TYPE_DOUBLE:
case TYPE_FLOAT:
return (T)ToDouble(THIS_REF);
Expand Down
9 changes: 6 additions & 3 deletions DateTime.extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@
*
*/

// Includes.
#include "DateTime.enum.h"

/**
* @file
* Includes external declarations related to date and time.
*/
#ifndef __MQL__
#pragma once

// Includes.
#include <time.h>
#include "DateTime.enum.h"
#include "String.mqh"

// Forward declarations.
struct MqlDateTime;

Expand Down
4 changes: 2 additions & 2 deletions Dict.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#ifndef DICT_MQH
#define DICT_MQH

#include "Convert.mqh"
#include "Convert.basic.h"
#include "DictBase.mqh"
#include "Matrix.mqh"
#include "Serializer/Serializer.h"
Expand Down Expand Up @@ -404,7 +404,7 @@ class Dict : public DictBase<K, V> {
if (i.HasKey()) {
// Converting key to a string.
K key;
Convert::StringToType(i.Key(), key);
ConvertBasic::StringToType(i.Key(), key);

// Note that we're retrieving value by a key (as we are in an
// object!).
Expand Down
Loading

0 comments on commit fc3de69

Please sign in to comment.