Skip to content

Commit

Permalink
WIP. Fixed SerializerNode and SerializerNodeIterator's loop. Now i'm …
Browse files Browse the repository at this point in the history
…stuck with another complicated compiler error.
  • Loading branch information
nseam committed Feb 8, 2022
1 parent d75e229 commit ce62685
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 59 deletions.
2 changes: 2 additions & 0 deletions Array.extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#ifndef __MQL__
#pragma once

#include "Std.h"

template <typename T>
extern int ArraySize(const ARRAY_REF(T, _array));

Expand Down
1 change: 1 addition & 0 deletions Data.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct MqlRates;

// Includes.
#include "Data.enum.h"
#include "DateTime.mqh"
#include "Serializer.enum.h"
#include "SerializerNode.enum.h"
#include "Std.h"
Expand Down
6 changes: 3 additions & 3 deletions DateTime.extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class datetime {
datetime();
datetime(const long& _time);
datetime(const int& _time);
bool operator==(const int& _time) const;
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 int _time) const;
bool operator>(const int _time) const;
bool operator<(const datetime& _time);
bool operator>(const datetime& _time);
operator long() const;
Expand Down
2 changes: 0 additions & 2 deletions DateTime.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ class DateTime {
dt_last = dt_curr;
Update();
}
int _prev_secs = dt_last.GetSeconds();
int _curr_secs = dt_curr.GetSeconds();
if (dt_curr.GetSeconds() < dt_last.GetSeconds()) {
_result = true;
}
Expand Down
16 changes: 8 additions & 8 deletions DateTime.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct DateTimeStatic {
/**
* Returns the current day of the month (e.g. the day of month of the last known server time).
*/
static int Day(datetime dt = NULL) {
static int Day(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
Expand All @@ -80,7 +80,7 @@ struct DateTimeStatic {
/**
* Returns the current zero-based day of the week of the last known server time.
*/
static int DayOfWeek(datetime dt = NULL) {
static int DayOfWeek(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
Expand All @@ -96,7 +96,7 @@ struct DateTimeStatic {
/**
* Returns the current day of the year (e.g. the day of year of the last known server time).
*/
static int DayOfYear(datetime dt = NULL) {
static int DayOfYear(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
Expand All @@ -112,7 +112,7 @@ struct DateTimeStatic {
/**
* Returns the hour of the last known server time by the moment of the program start.
*/
static int Hour(datetime dt = NULL) {
static int Hour(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
Expand All @@ -137,7 +137,7 @@ struct DateTimeStatic {
/**
* Returns the current minute of the last known server time by the moment of the program start.
*/
static int Minute(datetime dt = NULL) {
static int Minute(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
Expand All @@ -153,7 +153,7 @@ struct DateTimeStatic {
/**
* Returns the current month as number (e.g. the number of month of the last known server time).
*/
static int Month(datetime dt = NULL) {
static int Month(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
Expand All @@ -169,7 +169,7 @@ struct DateTimeStatic {
/**
* Returns the amount of seconds elapsed from the beginning of the current minute of the last known server time.
*/
static int Seconds(datetime dt = NULL) {
static int Seconds(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
Expand Down Expand Up @@ -213,7 +213,7 @@ struct DateTimeStatic {
/**
* Returns the current year (e.g. the year of the last known server time).
*/
static int Year(datetime dt = NULL) {
static int Year(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
Expand Down
2 changes: 2 additions & 0 deletions Dict.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,9 @@ class Dict : public DictBase<K, V> {
}

public:
#ifdef __cplusplus
template <>
#endif
SerializerNodeType Serialize(Serializer& s) {
if (s.IsWriting()) {
for (DictIteratorBase<K, V> i(Begin()); i.IsValid(); ++i) {
Expand Down
2 changes: 1 addition & 1 deletion Refs.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Dynamic {
/**
* Destructor.
*/
~Dynamic() {
virtual ~Dynamic() {
if (ptr_ref_counter != NULL && PTR_ATTRIB(ptr_ref_counter, num_strong_refs) == 0 &&
PTR_ATTRIB(ptr_ref_counter, num_weak_refs) == 0) {
#ifdef __MQL__
Expand Down
3 changes: 3 additions & 0 deletions Refs.rc.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#pragma once
#endif

// Includes.
#include "String.mqh"

// Forward declarations.
class Dynamic;

Expand Down
21 changes: 16 additions & 5 deletions Refs.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ struct Ref {
/**
* Constructor.
*/
Ref(X* _ptr) { this = _ptr; }
Ref(X* _ptr) { THIS_REF = _ptr; }

/**
* Constructor.
*/
Ref(Ref<X>& ref) { this = ref.Ptr(); }
Ref(Ref<X>& ref) { THIS_REF = ref.Ptr(); }

/**
* Constructor.
*/
Ref(WeakRef<X>& ref) { this = ref.Ptr(); }
Ref(WeakRef<X>& ref) { THIS_REF = ref.Ptr(); }

/**
* Constructor.
Expand All @@ -138,11 +138,13 @@ struct Ref {
*/
void Unset() {
if (ptr_object != NULL) {
#ifdef __MQL__
if (CheckPointer(ptr_object) == POINTER_INVALID) {
// Double check the pointer for invalid references. Can happen in rare circumstances.
ptr_object = NULL;
return;
}
#endif
if (PTR_ATTRIB(ptr_object, ptr_ref_counter) == NULL) {
// Object is not reference counted. Maybe a stack-based one?
return;
Expand All @@ -155,6 +157,7 @@ struct Ref {

// No more strong references.
if (!PTR_ATTRIB2(ptr_object, ptr_ref_counter, num_weak_refs)) {
#ifdef __MQL__
if (CheckPointer(PTR_ATTRIB(ptr_object, ptr_ref_counter)) == POINTER_INVALID) {
// Serious problem.
#ifndef __MQL4__
Expand All @@ -163,6 +166,7 @@ struct Ref {
#endif
return;
}
#endif

// Also no more weak references.
delete PTR_ATTRIB(ptr_object, ptr_ref_counter);
Expand All @@ -175,6 +179,7 @@ struct Ref {
// Avoiding delete loop for cyclic references.
X* ptr_to_delete = ptr_object;

#ifdef __MQL__
if (CheckPointer(ptr_to_delete) == POINTER_INVALID) {
// Serious problem.
#ifndef __MQL4__
Expand All @@ -183,6 +188,7 @@ struct Ref {
#endif
return;
}
#endif

// Avoiding double deletion in Dynamic's destructor.
PTR_ATTRIB(ptr_object, ptr_ref_counter) = NULL;
Expand Down Expand Up @@ -213,7 +219,12 @@ struct Ref {
ptr_object = _ptr;

if (ptr_object != NULL) {
if (CheckPointer(ptr_object) == POINTER_INVALID || PTR_ATTRIB(ptr_object, ptr_ref_counter) == NULL) {
#ifdef __MQL__
if (CheckPointer(ptr_object) == POINTER_INVALID) {
return Ptr();
}
#endif
if (PTR_ATTRIB(ptr_object, ptr_ref_counter) == NULL) {
// Double check the pointer for invalid references. Can happen very rarely.
return Ptr();
}
Expand All @@ -238,7 +249,7 @@ struct Ref {
* Makes a strong reference to the strongly-referenced object.
*/
X* operator=(Ref<X>& right) {
this = right.Ptr();
THIS_REF = right.Ptr();
return Ptr();
}

Expand Down
30 changes: 29 additions & 1 deletion Serializer.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ class Serializer {
// Should not happen.
} else {
_node = parent PTR_DEREF GetChild(si.Index());
array[si.Index()] = si.Struct();
array[si.Index()] = Struct<VT>(si.Key());
}
}

Expand Down Expand Up @@ -434,6 +434,34 @@ class Serializer {

return NULL;
}

/**
* Returns next value or value by given key.
*/
template <typename X>
X Value(string key = "") {
X value;
Pass(THIS_REF, key, value);
return value;
}

/**
* Returns next structure or structure by given key.
*/
template <typename X>
X Struct(string key = "") {
X value;
PassStruct(THIS_REF, key, value);
return value;
}

/**
* Returns next object or object by given key.
*/
template <typename X>
X Object(string key = "") {
return Struct<X>(key);
}
};

#endif // End: SERIALIZER_MQH
2 changes: 1 addition & 1 deletion SerializerConverter.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class SerializerConverter {

template <typename C>
static SerializerConverter FromString(string arg) {
SerializerConverter _converter(((C*)NULL).Parse(arg), 0);
SerializerConverter _converter(((C*)NULL)PTR_DEREF Parse(arg), 0);
#ifdef __debug__
Print("FromString(): result: ",
_converter.Node() != NULL ? _converter.Node().ToString(SERIALIZER_JSON_NO_WHITESPACES) : "NULL");
Expand Down
34 changes: 0 additions & 34 deletions SerializerNodeIterator.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -109,40 +109,6 @@ class SerializerIterator : public SerializerNodeIterator {
*/
SerializerIterator(const SerializerIterator& r) : SerializerNodeIterator(r) { _serializer = r._serializer; }

/**
* Returns next value or value by given key.
*/
#ifdef __MQL__
template <>
#endif
X Value(string key = "") {
X value;
_serializer PTR_DEREF Pass(PTR_TO_REF(_serializer), key, value);
return value;
}

/**
* Returns next object or object by given key.
*/
#ifdef __MQL__
template <>
#endif
X Object(string key = "") {
return Struct(key);
}

/**
* Returns next structure or structure by given key.
*/
#ifdef __MQL__
template <>
#endif
X Struct(string key = "") {
X value;
_serializer PTR_DEREF PassStruct(PTR_TO_REF(_serializer), key, value);
return value;
}

SerializerNodeType ParentNodeType() { return _collection PTR_DEREF GetType(); }
};

Expand Down
2 changes: 1 addition & 1 deletion Std.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "Math.define.h"
#endif


// Data types.
#ifdef __cplusplus
#include <iomanip>
Expand All @@ -51,6 +50,7 @@
#define PTR_ATTRIB2(O, A, B) O.A.B
#define PTR_TO_REF(PTR) PTR
#define MAKE_REF_FROM_PTR(TYPE, NAME, PTR) TYPE* NAME = PTR
#define nullptr NULL
#else
#define THIS_ATTR this->
#define THIS_PTR (this)
Expand Down
1 change: 1 addition & 0 deletions String.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

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

Expand Down
3 changes: 3 additions & 0 deletions Task/TaskActionBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#ifndef TASK_ACTION_BASE_H
#define TASK_ACTION_BASE_H

// Includes.
#include "TaskAction.struct.h"

/**
* TaskActionBase class.
*/
Expand Down
3 changes: 3 additions & 0 deletions Task/TaskGetterBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#ifndef TASK_GETTER_BASE_H
#define TASK_GETTER_BASE_H

// Includes.
#include "TaskGetter.struct.h"

/**
* TaskGetterBase class.
*/
Expand Down
6 changes: 3 additions & 3 deletions Task/TaskManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class TaskManager {
* Adds new task.
*/
bool Add(Task *_task) {
Ref<Task> _ref = _task;
Ref<Task> _ref(_task);
return tasks.Push(_ref);
}

Expand All @@ -83,7 +83,7 @@ class TaskManager {
bool Add(string _entry_str) {
TaskEntry _entry;
SerializerConverter::FromString<SerializerJson>(_entry_str).ToObject(_entry);
Ref<Task> _task = new Task(_entry);
Ref<Task> _task(new Task(_entry));
return Add(_task.Ptr());
}

Expand All @@ -104,7 +104,7 @@ class TaskManager {
bool _result = true;
for (DictStructIterator<int, Ref<Task>> _iter = tasks.Begin(); _iter.IsValid(); ++_iter) {
Task *_task = _iter.Value().Ptr();
_result &= _task.Process();
_result &= _task PTR_DEREF Process();
}
return _result;
}
Expand Down

0 comments on commit ce62685

Please sign in to comment.