Skip to content

Commit

Permalink
Little changes for array #defines.
Browse files Browse the repository at this point in the history
  • Loading branch information
nseam authored and kenorb committed May 1, 2023
1 parent 8b18790 commit 7f16aa1
Showing 1 changed file with 75 additions and 5 deletions.
80 changes: 75 additions & 5 deletions Std.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#include <vector>
#endif

#ifndef __MQL__
#define __FUNCSIG__ __FUNCTION__
#endif

#ifdef __MQL__
#define ASSIGN_TO_THIS(TYPE, VALUE) ((TYPE)this) = ((TYPE)VALUE)
#else
Expand All @@ -52,6 +56,7 @@
#define MAKE_REF_FROM_PTR(TYPE, NAME, PTR) TYPE* NAME = PTR
#define nullptr NULL
#define REF_DEREF .Ptr().
#define int64 long
#else
#define THIS_ATTR this->
#define THIS_PTR (this)
Expand All @@ -62,6 +67,7 @@
#define PTR_TO_REF(PTR) (*PTR)
#define MAKE_REF_FROM_PTR(TYPE, NAME, PTR) TYPE& NAME = PTR
#define REF_DEREF .Ptr()->
#define int64 long long
#endif

// References.
Expand Down Expand Up @@ -92,7 +98,9 @@
* @usage
* ARRAY_REF(<type of the array items>, <name of the variable>)
*/
#define ARRAY_TYPE(T) T[]
#define ARRAY_REF(T, N) REF(T) N ARRAY_DECLARATION_BRACKETS
#define FIXED_ARRAY_REF(T, N, S) ARRAY_REF(T, N)

#define CONST_ARRAY_REF(T, N) const N ARRAY_DECLARATION_BRACKETS

Expand All @@ -117,7 +125,9 @@
* @usage
* ARRAY_REF(<type of the array items>, <name of the variable>)
*/
#define ARRAY_REF(T, N) _cpp_array<T>& N
#define ARRAY_TYPE(T) _cpp_array<T>
#define ARRAY_REF(T, N) ARRAY_TYPE(T)& N
#define FIXED_ARRAY_REF(T, N, S) T(&N)[S]

#define CONST_ARRAY_REF(T, N) const _cpp_array<T>& N

Expand Down Expand Up @@ -166,6 +176,10 @@ class _cpp_array {
m_isSeries = r.m_isSeries;
}

std::vector<T>& str() { return m_data; }

void push(const T& value) { m_data.push_back(value); }

void operator=(const _cpp_array& r) {
m_data = r.m_data;
m_isSeries = r.m_isSeries;
Expand Down Expand Up @@ -196,6 +210,17 @@ class _cpp_array {
*/
int size() const { return (int)m_data.size(); }

void resize(int new_size, int reserve_size = 0) {
// E.g., size = 10, new_size = 90, reserve_size = 50
// thus: new_reserve_size = new_size + reserve_size - (new_size % reserve_size)
// which is: 90 + reserve_size - (90 % reserve_size) = 90 + 50 - 40 = 100.
if (reserve_size > 0) {
new_size = reserve_size - (new_size % reserve_size);
}
m_data.reserve(new_size);
m_data.resize(new_size);
}

/**
* Checks whether
*/
Expand All @@ -209,6 +234,20 @@ class _cpp_array {
void setIsSeries(bool _isSeries) { m_isSeries = _isSeries; }
};

#ifdef EMSCRIPTEN
#include <emscripten/bind.h>

#define REGISTER_ARRAY_OF(N, T, D) \
EMSCRIPTEN_BINDINGS(N) { \
emscripten::register_vector<T>(D "CppVector"); \
emscripten::class_<_cpp_array<T>>(D) \
.constructor() \
.function("Push", &_cpp_array<T>::push) \
.function("Size", &_cpp_array<T>::size); \
}

#endif

template <typename T>
class _cpp_array;
#endif
Expand All @@ -219,7 +258,7 @@ class color {
unsigned int value;

public:
color(unsigned int _color) { value = _color; }
color(unsigned int _color = 0) { value = _color; }
color& operator=(unsigned int _color) {
value = _color;
return *this;
Expand Down Expand Up @@ -283,20 +322,47 @@ class InvalidEnumValue {
};

#ifndef __MQL__
struct _WRONG_VALUE {
template <typename T>
operator T() {
return (T)-1;
}
} WRONG_VALUE;

const char* _empty_string_c = "";
const string _empty_string = "";

// Converter of NULL_VALUE into expected type. e.g., "int x = NULL_VALUE" will end up with "x = 0".
struct _NULL_VALUE {
template <typename T>
explicit operator T() const {
operator T() const {
return (T)0;
}

} NULL_VALUE;

/**
* Converting an enumeration value of any type to a text form.
*
* @docs
* - https://www.mql5.com/en/docs/convert/enumtostring
*/
string EnumToString(int _value) {
std::stringstream ss;
// We really don't want to mess with type reflection here (if possible at all). So we are outputting the input
// integer.
ss << _value;
return ss.str();
}

template <>
inline _NULL_VALUE::operator const std::string() const {
return "";
_NULL_VALUE::operator string() const {
return _empty_string;
}
#define NULL_STRING ""
#else
#define NULL_VALUE NULL
#define NULL_STRING NULL
#endif

#ifndef __MQL__
Expand All @@ -308,3 +374,7 @@ inline _NULL_VALUE::operator const std::string() const {
extern ENUM_TIMEFRAMES Period();

#endif

#define RUNTIME_ERROR(MSG) \
Print(MSG); \
DebugBreak();

0 comments on commit 7f16aa1

Please sign in to comment.