Skip to content

Commit

Permalink
WIP. Writing stubs/most important methods' implementations for Emscri…
Browse files Browse the repository at this point in the history
…pten.
  • Loading branch information
nseam committed Nov 29, 2022
1 parent 5d9031f commit 5287625
Show file tree
Hide file tree
Showing 12 changed files with 628 additions and 357 deletions.
48 changes: 38 additions & 10 deletions Array.extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,64 @@
#ifndef __MQL__
#pragma once

#include "Common.extern.h"
#include "Std.h"
#include "String.extern.h"

template <typename T>
extern int ArraySize(const ARRAY_REF(T, _array));
int ArraySize(const ARRAY_REF(T, _array)) {
return _array.size();
}

template <typename T, int size>
extern constexpr int ArraySize(const T REF(_array)[size]);
constexpr int ArraySize(const T REF(_array)[size]);

template <typename T>
extern int ArrayResize(ARRAY_REF(T, _array), int _new_size, int _reserve_size = 0);
int ArrayResize(ARRAY_REF(T, _array), int _new_size, int _reserve_size = 0) {
_array.resize(_new_size, _reserve_size);
return _new_size;
}

template <typename T>
extern bool ArraySetAsSeries(ARRAY_REF(T, _array), bool _flag);
bool ArraySetAsSeries(ARRAY_REF(T, _array), bool _flag) {
_array.setIsSeries(_flag);
return true;
}

template <typename T>
extern int ArrayMaximum(const ARRAY_REF(T, _array), int _start = 0, unsigned int _count = WHOLE_ARRAY);
int ArrayMaximum(const ARRAY_REF(T, _array), int _start = 0, unsigned int _count = WHOLE_ARRAY) {
Print("Not yet implemented: ", __FUNCTION__, " returns 0.");
return 0;
}

template <typename T>
extern int ArrayMinimum(const ARRAY_REF(T, _array), int _start = 0, unsigned int _count = WHOLE_ARRAY);
int ArrayMinimum(const ARRAY_REF(T, _array), int _start = 0, unsigned int _count = WHOLE_ARRAY) {
Print("Not yet implemented: ", __FUNCTION__, " returns 0.");
return 0;
}

template <typename T>
extern int ArrayFree(const ARRAY_REF(T, _array));
int ArrayFree(ARRAY_REF(T, _array)) {
_array.resize(0, 0);
return 0;
}

template <typename T>
extern int ArrayReverse(const ARRAY_REF(T, _array));
bool ArrayReverse(ARRAY_REF(T, _array)) {
_array.reverse();
return true;
}

template <typename T>
extern int ArrayInitialize(ARRAY_REF(T, array), char value);
extern int ArrayInitialize(ARRAY_REF(T, array), char value) {
Print("Not yet implemented: ", __FUNCTION__, " returns 0.");
return 0;
}

template <typename T>
extern int ArraySort(ARRAY_REF(T, array));
extern int ArraySort(ARRAY_REF(T, array)) {
Print("Not yet implemented: ", __FUNCTION__, " returns 0.");
return 0;
}

#endif
10 changes: 8 additions & 2 deletions Common.extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@
// Define external global functions.
#ifndef __MQL__
#pragma once
#include <csignal>

#include "Chart.enum.h"
#include "DateTime.enum.h"
#include "Terminal.define.h"

void DebugBreak() { raise(SIGTRAP); }

int _LastError = 0;

extern void DebugBreak();
// Errors.
extern void SetUserError(unsigned short user_error);
void SetUserError(unsigned short user_error) { _LastError = ERR_USER_ERROR_FIRST + user_error; }
// Exceptions.
extern int NotImplementedException();
// Print-related functions.
Expand Down
2 changes: 2 additions & 0 deletions Convert.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "Account/Account.extern.h"
#include "Array.mqh"
#include "Convert.extern.h"
#include "DateTime.extern.h"
#include "DateTime.mqh"
#include "Math.extern.h"
#include "Order.enum.h"
#include "SymbolInfo.enum.h"
Expand Down
171 changes: 171 additions & 0 deletions DateTime.entry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
//+------------------------------------------------------------------+
//| 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/>.
*
*/

/**
* @file
* Includes DateTime's structs.
*/

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

// Includes.
#include "DateTime.static.h"
#include "PlatformTime.h"

struct DateTimeEntry : MqlDateTime {
int week_of_year;
// Struct constructors.
DateTimeEntry() { Set(); }
DateTimeEntry(datetime _dt) { Set(_dt); }
DateTimeEntry(MqlDateTime& _dt) {
Set(_dt);
#ifndef __MQL__
throw NotImplementedException();
#endif
}
// Getters.
int GetDayOfMonth() { return day; }
int GetDayOfWeek() {
// Returns the zero-based day of week.
// (0-Sunday, 1-Monday, ... , 6-Saturday).
return day_of_week;
}
int GetDayOfYear() { return day_of_year + 1; } // Zero-based day of year (1st Jan = 0).
int GetHour() { return hour; }
int GetMinute() { return min; }
int GetMonth() { return mon; }
int GetSeconds() { return sec; }
// int GetWeekOfYear() { return week_of_year; } // @todo
int GetValue(ENUM_DATETIME_UNIT _unit) {
int _result = -1;
switch (_unit) {
case DATETIME_SECOND:
return GetSeconds();
case DATETIME_MINUTE:
return GetMinute();
case DATETIME_HOUR:
return GetHour();
case DATETIME_DAY:
return GetDayOfMonth();
case DATETIME_WEEK:
return -1; // return WeekOfYear(); // @todo
case DATETIME_MONTH:
return GetMonth();
case DATETIME_YEAR:
return GetYear();
default:
break;
}
return _result;
}
unsigned int GetValue(unsigned int _unit) {
if ((_unit & (DATETIME_DAY | DATETIME_WEEK)) != 0) {
return GetDayOfWeek();
} else if ((_unit & (DATETIME_DAY | DATETIME_MONTH)) != 0) {
return GetDayOfMonth();
} else if ((_unit & (DATETIME_DAY | DATETIME_YEAR)) != 0) {
return GetDayOfYear();
}
return GetValue((ENUM_DATETIME_UNIT)_unit);
}
int GetYear() { return year; }
datetime GetTimestamp() { return StructToTime(THIS_REF); }
// Setters.
void Set() {
TimeToStruct(PlatformTime::CurrentTimestamp(), THIS_REF);
// @fixit Should also set day of week.
}
void SetGMT() {
TimeToStruct(::TimeGMT(), THIS_REF);
// @fixit Should also set day of week.
}
// Set date and time.
void Set(datetime _time) {
TimeToStruct(_time, THIS_REF);
// @fixit Should also set day of week.
}
// Set date and time.
void Set(MqlDateTime& _time) {
THIS_REF = _time;
// @fixit Should also set day of week.
}
void SetDayOfMonth(int _value) {
day = _value;
day_of_week = DateTimeStatic::DayOfWeek(); // Zero-based day of week.
day_of_year = DateTimeStatic::DayOfYear(); // Zero-based day of year.
}
void SetDayOfYear(int _value) {
day_of_year = _value - 1; // Sets zero-based day of year.
day = DateTimeStatic::Month(); // Sets day of month (1..31).
day_of_week = DateTimeStatic::DayOfWeek(); // Zero-based day of week.
}
void SetHour(int _value) { hour = _value; }
void SetMinute(int _value) { min = _value; }
void SetMonth(int _value) { mon = _value; }
void SetSeconds(int _value) { sec = _value; }
void SetWeekOfYear(int _value) {
week_of_year = _value;
// day = @todo;
// day_of_week = @todo;
// day_of_year = @todo;
}
void SetValue(ENUM_DATETIME_UNIT _unit, int _value) {
switch (_unit) {
case DATETIME_SECOND:
SetSeconds(_value);
break;
case DATETIME_MINUTE:
SetMinute(_value);
break;
case DATETIME_HOUR:
SetHour(_value);
break;
case DATETIME_DAY:
SetDayOfMonth(_value);
break;
case DATETIME_WEEK:
SetWeekOfYear(_value);
break;
case DATETIME_MONTH:
SetMonth(_value);
break;
case DATETIME_YEAR:
SetYear(_value);
break;
default:
break;
}
}
void SetValue(unsigned short _unit, int _value) {
if ((_unit & (DATETIME_DAY | DATETIME_MONTH)) != 0) {
SetDayOfMonth(_value);
} else if ((_unit & (DATETIME_DAY | DATETIME_YEAR)) != 0) {
SetDayOfYear(_value);
} else {
SetValue((ENUM_DATETIME_UNIT)_unit, _value);
}
}
void SetYear(int _value) { year = _value; }
};
33 changes: 20 additions & 13 deletions DateTime.extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,18 @@ class datetime {
time_t dt;

public:
datetime();
datetime(const long& _time);
datetime() { dt = 0; }
datetime(const long& _time) { dt = _time; }
datetime(const int& _time);
bool operator==(const int _time) const;
bool operator==(const datetime& _time) const;
bool operator<(const int _time) const;
bool operator>(const int _time) const;
bool operator<(const datetime& _time);
bool operator>(const datetime& _time);
operator long() const;
bool operator==(const int _time) const = delete;
bool operator==(const datetime& _time) const { return dt == _time; }
bool operator<(const int _time) const = delete;
bool operator>(const int _time) const = delete;
bool operator<(const datetime& _time) const { return dt < _time; }
bool operator>(const datetime& _time) const { return dt > _time; }
operator long() const { return dt; }
};

extern datetime TimeCurrent();
extern datetime TimeCurrent(MqlDateTime& dt_struct);

extern int CopyTime(string symbol_name, ENUM_TIMEFRAMES timeframe, int start_pos, int count,
ARRAY_REF(datetime, time_array));

Expand All @@ -73,10 +70,20 @@ extern datetime TimeGMT(MqlDateTime& dt_struct);
extern datetime TimeTradeServer();
extern datetime TimeTradeServer(MqlDateTime& dt_struct);
extern datetime StringToTime(const string& value);
extern string TimeToString(datetime value, int mode = TIME_DATE | TIME_MINUTES);
string TimeToString(datetime value, int mode = TIME_DATE | TIME_MINUTES) {
/*
auto now = std::chrono::time_point();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
*/
std::stringstream ss;
ss << __FUNCTION__ << " is not yet implemented!";
// ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
return ss.str();
}

template <char... T>
extern datetime operator"" _D();

#define DATETIME_LITERAL(STR) _D " ## STR ## "

#endif
19 changes: 17 additions & 2 deletions DateTime.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ struct DataParamEntry;
// Includes class enum and structs.
#include "Array.mqh"
#include "Data.struct.h"
#include "DateTime.entry.h"
#include "DateTime.enum.h"
#include "DateTime.extern.h"
#include "DateTime.static.h"
#include "DateTime.struct.h"
#include "PlatformTime.h"

#ifndef __MQL4__
// Defines global functions (for MQL4 backward compatibility).
Expand All @@ -61,7 +64,7 @@ class DateTime {
/**
* Class constructor.
*/
DateTime() { TimeToStruct(TimeCurrent(), dt_curr); }
DateTime() { TimeToStruct(PlatformTime::CurrentTimestamp(), dt_curr); }
DateTime(DateTime &r) : dt_curr(r.dt_curr), dt_last(r.dt_last) {}
DateTime(DateTimeEntry &_dt) { dt_curr = _dt; }
DateTime(MqlDateTime &_dt) { dt_curr = _dt; }
Expand Down Expand Up @@ -198,7 +201,7 @@ class DateTime {
/**
* Updates datetime to the current one.
*/
void Update() { dt_curr.Set(TimeCurrent()); }
void Update() { dt_curr.Set(PlatformTime::CurrentTimestamp()); }

/* Conditions */

Expand Down Expand Up @@ -238,4 +241,16 @@ class DateTime {
return DateTime::CheckCondition(_cond, _args);
}
};

#ifndef __MQL__

datetime TimeCurrent() { return PlatformTime::CurrentTimestamp(); }

datetime TimeCurrent(MqlDateTime &dt_struct) {
dt_struct = PlatformTime::CurrentTime();
return PlatformTime::CurrentTimestamp();
}

#endif

#endif // DATETIME_MQH
Loading

0 comments on commit 5287625

Please sign in to comment.