Skip to content

Commit

Permalink
WIP. Making Account and Exchange classes to work via Emscripten. Also…
Browse files Browse the repository at this point in the history
… introduced MemoryFileSystem for accessing temporary files written by MQL/C++ code.
  • Loading branch information
nseam committed Dec 21, 2022
1 parent 42409e3 commit da13c54
Show file tree
Hide file tree
Showing 26 changed files with 496 additions and 112 deletions.
2 changes: 1 addition & 1 deletion Account/Account.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#ifndef __MQL__
// Allows the preprocessor to include a header file when it is needed.
#pragma once
#include "../Serializer.enum.h"
#include "../Serializer/Serializer.enum.h"
#endif

// Forward class declaration.
Expand Down
3 changes: 2 additions & 1 deletion Account/AccountBase.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
#ifndef __MQL__
// Allows the preprocessor to include a header file when it is needed.
#pragma once
#include "../Serializer.enum.h"
#include "../Serializer/Serializer.enum.h"
#endif

// Forward class declaration.
class Serializer;

// Includes.
#include "../Serializer/Serializer.h"
#include "../Std.h"
#include "../Terminal.define.h"

// Struct for account entries.
Expand Down
6 changes: 3 additions & 3 deletions Array.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Array {
int i;
string result = "";
for (i = 0; i < ArraySize(arr); i++) {
result += StringFormat("%d:%d%s", i, arr[i], sep);
result += StringFormat("%d:%d%s", i, arr[i], C_STR(sep));
}
// Return text without last separator.
return StringSubstr(result, 0, StringLen(result) - StringLen(sep));
Expand All @@ -136,7 +136,7 @@ class Array {
int i;
string result = "";
for (i = 0; i < ArraySize(arr); i++) {
result += StringFormat("%d:%g%s", i, arr[i], sep);
result += StringFormat("%d:%g%s", i, arr[i], C_STR(sep));
}
// Return text without last separator.
return StringSubstr(result, 0, StringLen(result) - StringLen(sep));
Expand Down Expand Up @@ -404,7 +404,7 @@ static int GetLowestArrDoubleValue(double& arr[][], int key) {
int i;
string res = "";
for (i = 0; i < ArraySize(arr); i++) {
res += StringFormat("%g%s", NormalizeDouble(arr[i], digits), dlm);
res += StringFormat("%g%s", NormalizeDouble(arr[i], digits), C_STR(dlm));
}
res = StringSubstr(res, 0, StringLen(res) - StringLen(dlm));
return res;
Expand Down
4 changes: 2 additions & 2 deletions Bar.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ struct BarOHLC
BarOHLC() : open(0), high(0), low(0), close(0), time(0){};
BarOHLC(double _open, double _high, double _low, double _close, datetime _time = 0)
: time(_time), open(_open), high(_high), low(_low), close(_close) {
if (_time == 0) {
if (_time == (datetime)0) {
_time = TimeCurrent();
}
}
BarOHLC(ARRAY_REF(double, _prices), datetime _time = 0) : time(_time) {
_time = _time == 0 ? TimeCurrent() : _time;
_time = _time == (datetime)0 ? TimeCurrent() : _time;
int _size = ArraySize(_prices);
close = _prices[0];
open = _prices[_size - 1];
Expand Down
8 changes: 4 additions & 4 deletions BufferStruct.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ class BufferStruct : public DictStruct<long, TStruct> {
/**
* Constructor.
*/
BufferStruct() : max(INT_MIN), min(INT_MAX) { SetOverflowListener(BufferStructOverflowListener, 10); }
BufferStruct() : max(INT_MIN), min(INT_MAX) { THIS_ATTR SetOverflowListener(BufferStructOverflowListener, 10); }
BufferStruct(BufferStruct& _right) : max(INT_MIN), min(INT_MAX) {
this = _right;
SetOverflowListener(BufferStructOverflowListener, 10);
THIS_ATTR SetOverflowListener(BufferStructOverflowListener, 10);
}

/**
* Adds new value.
*/
void Add(TStruct& _value, long _dt = 0) {
_dt = _dt > 0 ? _dt : TimeCurrent();
_dt = _dt > 0 ? _dt : (long)TimeCurrent();
if (Set(_dt, _value)) {
min = _dt < min ? _dt : min;
max = _dt > max ? _dt : max;
Expand All @@ -87,7 +87,7 @@ class BufferStruct : public DictStruct<long, TStruct> {
min = INT_MAX;
max = INT_MIN;
if (_dt > 0) {
for (DictStructIterator<long, TStruct> iter(Begin()); iter.IsValid(); ++iter) {
for (DictStructIterator<long, TStruct> iter(THIS_ATTR Begin()); iter.IsValid(); ++iter) {
long _time = iter.Key();
if (_older && _time < _dt) {
Unset(iter.Key());
Expand Down
6 changes: 6 additions & 0 deletions Chart.struct.static.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@
* Includes Chart's static structs.
*/

#ifndef __MQL__
// Allows the preprocessor to include a header file when it is needed.
#pragma once
#endif

// Includes.
#include "Chart.define.h"
#include "Chart.symboltf.h"
#include "Platform.extern.h"
#include "Terminal.define.h"

/* Defines struct for chart static methods. */
Expand Down
54 changes: 46 additions & 8 deletions Data.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,36 +129,74 @@ struct DataParamEntry : public MqlParam {
string_value = _string_value;
}
DataParamEntry(const DataParamEntry &_r) { ASSIGN_TO_THIS(MqlParam, _r); }

DataParamEntry(bool _value) {
type = TYPE_BOOL;
integer_value = _value;
}
DataParamEntry(const datetime _value) {
type = TYPE_DATETIME;
integer_value = _value;
}
DataParamEntry(double _value) {
type = TYPE_DOUBLE;
double_value = _value;
}
DataParamEntry(int _value) {
type = TYPE_INT;
integer_value = _value;
}
DataParamEntry(const string _value) {
type = TYPE_STRING;
string_value = _value;
}
DataParamEntry(unsigned int _value) {
type = TYPE_UINT;
integer_value = _value;
}
DataParamEntry(long _value) {
type = TYPE_LONG;
integer_value = _value;
}
DataParamEntry(unsigned long _value) {
type = TYPE_ULONG;
integer_value = (long)_value;
}

// Struct operators.
void operator=(const bool _value) {
void operator=(bool _value) {
type = TYPE_BOOL;
integer_value = _value;
}
void operator=(const datetime _value) {
type = TYPE_DATETIME;
integer_value = _value;
}
void operator=(const double _value) {
void operator=(double _value) {
type = TYPE_DOUBLE;
double_value = _value;
}
void operator=(const int _value) {
void operator=(int _value) {
type = TYPE_INT;
integer_value = _value;
}
void operator=(const string _value) {
type = TYPE_STRING;
string_value = _value;
}
void operator=(const unsigned int _value) {
void operator=(unsigned int _value) {
type = TYPE_UINT;
integer_value = _value;
}
template <typename T>
void operator=(const T _value) {
type = TYPE_INT;
integer_value = (int)_value;
void operator=(long _value) {
type = TYPE_LONG;
integer_value = _value;
}
void operator=(unsigned long _value) {
type = TYPE_ULONG;
integer_value = (long)_value;
}

bool operator==(const DataParamEntry &_s) {
return type == _s.type && double_value == _s.double_value && integer_value == _s.integer_value &&
string_value == _s.string_value;
Expand Down
6 changes: 3 additions & 3 deletions DictStruct.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ class DictStruct : public DictBase<K, V> {
V operator[](K key) {
DictSlot<K, V>* slot;

int position;
unsigned int position;

if (THIS_ATTR _mode == DictModeList)
slot = THIS_ATTR GetSlot((unsigned int)key);
else
slot = GetSlotByKey(THIS_ATTR _DictSlots_ref, key, position);
slot = THIS_ATTR GetSlotByKey(THIS_ATTR _DictSlots_ref, key, position);

if (slot == NULL || !slot PTR_DEREF IsUsed()) {
Alert("Invalid DictStruct key \"", key, "\" (called by [] operator). Returning empty structure.");
Expand All @@ -174,7 +174,7 @@ class DictStruct : public DictBase<K, V> {
*/
V GetByKey(const K _key) {
unsigned int position;
DictSlot<K, V>* slot = GetSlotByKey(THIS_ATTR _DictSlots_ref, _key, position);
DictSlot<K, V>* slot = THIS_ATTR GetSlotByKey(THIS_ATTR _DictSlots_ref, _key, position);

if (!slot) {
static V _empty;
Expand Down
35 changes: 34 additions & 1 deletion File.define.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,46 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

// Defines.
#ifndef __MQL__
#pragma once

// File constants to read the whole value of char, short or int type.
#define CHAR_VALUE 1
#define INT_VALUE 4
#define SHORT_VALUE 2
// Used for checking file handles (see FileOpen() and FileFindFirst()).
#define INVALID_HANDLE -1

enum ENUM_FILE_PROPERTY_INTEGER {
FILE_EXISTS,
FILE_CREATE_DATE,
FILE_MODIFY_DATE,
FILE_ACCESS_DATE,
FILE_SIZE,
FILE_POSITION,
FILE_END,
FILE_LINE_END,
FILE_IS_COMMON,
FILE_IS_TEXT,
FILE_IS_BINARY,
FILE_IS_CSV,
FILE_IS_ANSI,
FILE_IS_READABLE,
FILE_IS_WRITABLE,
};
enum ENUM_FILE_OPEN_FLAGS {
FILE_READ = 1,
FILE_WRITE = 2,
FILE_BIN = 4,
FILE_CSV = 8,
FILE_TXT = 16,
FILE_ANSI = 32,
FILE_UNICODE = 64,
FILE_SHARE_READ = 128,
FILE_SHARE_WRITE = 256,
FILE_REWRITE = 512,
FILE_COMMON = 4096,
};

#endif
27 changes: 24 additions & 3 deletions File.extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,36 @@

// Includes.
#include "File.define.h"
#include "Storage/MemoryFileSystem.h"
#include "String.extern.h"
#include "Terminal.define.h"

// Define external global functions.
#ifndef __MQL__

MemoryFileSystem _memfs;

extern bool FileIsEnding(int file_handle);

extern bool FileIsExist(const string file_name, int common_flag = 0);
extern int FileClose(int file_handle);
extern int FileOpen(string file_name, int open_flags, short delimiter = '\t', unsigned int codepage = CP_ACP);

void FileClose(int file_handle) { _memfs.FileClose(file_handle); }

int FileOpen(string file_name, int open_flags, short delimiter = '\t', unsigned int codepage = CP_ACP) {
return _memfs.FileOpen(file_name, open_flags, delimiter, codepage);
}

extern int FileReadInteger(int file_handle, int size = INT_VALUE);

extern string FileReadString(int file_handle, int length = -1);
extern unsigned int FileWriteString(int file_handle, const string text_string, int length = -1);

unsigned int FileWriteString(int file_handle, const string text_string, int length = -1) {
return _memfs.FileWrite(file_handle, text_string);
}

template <typename Arg, typename... Args>
unsigned int FileWrite(int file_handle, Arg&& arg, Args&&... args) {
return _memfs.FileWrite(file_handle, arg, args...);
}

#endif
33 changes: 0 additions & 33 deletions File.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -37,39 +37,6 @@
#include "Terminal.enum.h"
#include "Terminal.extern.h"

#ifndef __MQL__
enum ENUM_FILE_PROPERTY_INTEGER {
FILE_EXISTS,
FILE_CREATE_DATE,
FILE_MODIFY_DATE,
FILE_ACCESS_DATE,
FILE_SIZE,
FILE_POSITION,
FILE_END,
FILE_LINE_END,
FILE_IS_COMMON,
FILE_IS_TEXT,
FILE_IS_BINARY,
FILE_IS_CSV,
FILE_IS_ANSI,
FILE_IS_READABLE,
FILE_IS_WRITABLE,
};
enum ENUM_FILE_OPEN_FLAGS {
FILE_READ = 1,
FILE_WRITE = 2,
FILE_BIN = 4,
FILE_CSV = 8,
FILE_TXT = 16,
FILE_ANSI = 32,
FILE_UNICODE = 64,
FILE_SHARE_READ = 128,
FILE_SHARE_WRITE = 256,
FILE_REWRITE = 512,
FILE_COMMON = 4096,
};
#endif

/**
* Class to provide a group of functions for working with files.
*/
Expand Down
5 changes: 5 additions & 0 deletions Flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
*
*/

#ifndef __MQL__
// Allows the preprocessor to include a header file when it is needed.
#pragma once
#endif

/**
* Flags manipulation helper.
*/
Expand Down
1 change: 1 addition & 0 deletions Log.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class DictStruct;
#include "Collection.mqh"
#include "DateTime.mqh"
#include "DictStruct.mqh"
#include "File.mqh"
#include "Object.mqh"

// Define assert macros.
Expand Down
5 changes: 0 additions & 5 deletions Object.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ class Object : public Dynamic {
}
bool IsDynamic() { return IsDynamic(obj); }

/**
* Returns text representation of the object.
*/
virtual string ToString() { return StringFormat("[Object #%04x]", GetPointer(this)); }

/**
* Returns text representation of the object.
*/
Expand Down
Loading

0 comments on commit da13c54

Please sign in to comment.