Skip to content

Commit

Permalink
WIP. More C++ compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
nseam committed Feb 4, 2022
1 parent 66d9918 commit d75e229
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 79 deletions.
28 changes: 14 additions & 14 deletions DictStruct.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class DictStruct : public DictBase<K, V> {
Clear();
Resize(right.GetSlotCount());
for (unsigned int i = 0; i < (unsigned int)ArraySize(right._DictSlots_ref.DictSlots); ++i) {
this PTR_DEREF _DictSlots_ref PTR_DEREF DictSlots[i] = right._DictSlots_ref.DictSlots[i];
THIS_ATTR _DictSlots_ref.DictSlots[i] = right._DictSlots_ref.DictSlots[i];
}
THIS_ATTR _DictSlots_ref._num_used = right._DictSlots_ref._num_used;
THIS_ATTR _current_id = right._current_id;
Expand All @@ -78,17 +78,17 @@ class DictStruct : public DictBase<K, V> {

void Clear() {
for (unsigned int i = 0; i < (unsigned int)ArraySize(THIS_ATTR _DictSlots_ref.DictSlots); ++i) {
THIS_ATTR _DictSlots_ref PTR_DEREF DictSlots[i].SetFlags(0);
THIS_ATTR _DictSlots_ref.DictSlots[i].SetFlags(0);
}

THIS_ATTR _DictSlots_ref PTR_DEREF _num_used = 0;
THIS_ATTR _DictSlots_ref._num_used = 0;
}

DictStructIterator<K, V> Begin() {
// Searching for first item index.
for (unsigned int i = 0; i < (unsigned int)ArraySize(THIS_ATTR _DictSlots_ref.DictSlots); ++i) {
if (THIS_ATTR _DictSlots_ref.DictSlots[i].IsValid() && THIS_ATTR _DictSlots_ref.DictSlots[i].IsUsed()) {
DictStructIterator<K, V> iter(this, i);
DictStructIterator<K, V> iter(THIS_REF, i);
return iter;
}
}
Expand Down Expand Up @@ -265,7 +265,7 @@ class DictStruct : public DictBase<K, V> {
}

unsigned int position;
DictSlot<K, V>* keySlot = GetSlotByKey(dictSlotsRef, key, position);
DictSlot<K, V>* keySlot = THIS_ATTR GetSlotByKey(dictSlotsRef, key, position);

if (keySlot == NULL && !THIS_ATTR IsGrowUpAllowed()) {
// Resize is prohibited.
Expand All @@ -278,16 +278,16 @@ class DictStruct : public DictBase<K, V> {
return false;
}
// We now have new positions of slots, so we have to take the corrent slot again.
keySlot = GetSlotByKey(dictSlotsRef, key, position);
keySlot = THIS_ATTR GetSlotByKey(dictSlotsRef, key, position);
}

if (keySlot == NULL && dictSlotsRef._num_used == ArraySize(dictSlotsRef.DictSlots)) {
// No DictSlotsRef.DictSlots available.
if (THIS_ATTR overflow_listener != NULL) {
if (!overflow_listener(DICT_OVERFLOW_REASON_FULL, dictSlotsRef._num_used, 0)) {
if (!THIS_ATTR overflow_listener(DICT_OVERFLOW_REASON_FULL, dictSlotsRef._num_used, 0)) {
// Overwriting slot pointed exactly by key's position in the hash table (we don't check for possible
// conflicts).
keySlot = &dictSlotsRef.DictSlots[Hash(key) % ArraySize(dictSlotsRef.DictSlots)];
keySlot = &dictSlotsRef.DictSlots[THIS_ATTR Hash(key) % ArraySize(dictSlotsRef.DictSlots)];
}
}

Expand All @@ -298,10 +298,10 @@ class DictStruct : public DictBase<K, V> {
}

if (keySlot == NULL) {
position = Hash(key) % ArraySize(dictSlotsRef.DictSlots);
position = THIS_ATTR Hash(key) % ArraySize(dictSlotsRef.DictSlots);

unsigned int _starting_position = position;
int _num_conflicts = 0;
unsigned int _num_conflicts = 0;
bool _overwrite_slot = false;

// Searching for empty DictSlot<K, V> or used one with the matching key. It skips used, hashless DictSlots.
Expand All @@ -310,7 +310,8 @@ class DictStruct : public DictBase<K, V> {
if (THIS_ATTR overflow_listener_max_conflicts != 0 &&
++_num_conflicts == THIS_ATTR overflow_listener_max_conflicts) {
if (THIS_ATTR overflow_listener != NULL) {
if (!overflow_listener(DICT_OVERFLOW_REASON_TOO_MANY_CONFLICTS, dictSlotsRef._num_used, _num_conflicts)) {
if (!THIS_ATTR overflow_listener(DICT_OVERFLOW_REASON_TOO_MANY_CONFLICTS, dictSlotsRef._num_used,
_num_conflicts)) {
// Overflow listener returned false so we won't search for further empty slot.
_overwrite_slot = true;
break;
Expand Down Expand Up @@ -381,7 +382,7 @@ class DictStruct : public DictBase<K, V> {
*/
bool GrowUp(int percent = DICT_GROW_UP_PERCENT_DEFAULT) {
return Resize(
MathMax(10, (int)((float)ArraySize(THIS_ATTR THIS_ATTR_DictSlots_ref.DictSlots) * ((float)(percent + 100) / 100.0f))));
MathMax(10, (int)((float)ArraySize(THIS_ATTR _DictSlots_ref.DictSlots) * ((float)(percent + 100) / 100.0f))));
}

/**
Expand Down Expand Up @@ -409,8 +410,7 @@ class DictStruct : public DictBase<K, V> {

if (THIS_ATTR _DictSlots_ref.DictSlots[i].HasKey()) {
if (!InsertInto(new_DictSlots, THIS_ATTR _DictSlots_ref.DictSlots[i].key,
THIS_ATTR _DictSlots_ref.DictSlots[i].value,
false))
THIS_ATTR _DictSlots_ref.DictSlots[i].value, false))
return false;
} else {
if (!InsertInto(new_DictSlots, THIS_ATTR _DictSlots_ref.DictSlots[i].value)) return false;
Expand Down
2 changes: 1 addition & 1 deletion Serializer.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ class Serializer {
if (Enter(SerializerEnterArray, name)) {
num_items = ArraySize(array);
for (int i = 0; i < num_items; ++i) {
PassStruct(this, "", array[i]);
PassStruct(THIS_REF, "", array[i]);
}
Leave();
}
Expand Down
6 changes: 3 additions & 3 deletions Task/Task.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Task : public Taskable<TaskEntry> {
/**
* Class copy constructor.
*/
Task(Task &_task) { tasks = _task.GetTasks(); }
Task(Task &_task) { tasks = PTR_TO_REF(_task.GetTasks()); }

/**
* Class deconstructor.
Expand Down Expand Up @@ -267,7 +267,7 @@ class Task : public Taskable<TaskEntry> {
* @return
* Returns true when the condition is met.
*/
bool CheckCondition(ENUM_TASK_CONDITION _cond, DataParamEntry &_args[]) {
bool CheckCondition(ENUM_TASK_CONDITION _cond, ARRAY_REF(DataParamEntry, _args)) {
switch (_cond) {
case TASK_COND_IS_ACTIVE:
// Is active;
Expand Down Expand Up @@ -301,7 +301,7 @@ class Task : public Taskable<TaskEntry> {
* @return
* Returns true when the action has been executed successfully.
*/
bool ExecuteAction(ENUM_TASK_ACTION _action, DataParamEntry &_args[]) {
bool ExecuteAction(ENUM_TASK_ACTION _action, ARRAY_REF(DataParamEntry, _args)) {
bool _result = true;
switch (_action) {
case TASK_ACTION_PROCESS:
Expand Down
6 changes: 3 additions & 3 deletions Task/Task.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ struct TaskEntry {
public:
// Constructors.
TaskEntry() { Init(); }
TaskEntry(TaskActionEntry &_action, TaskConditionEntry &_cond) : action(_action), cond(_cond) { Init(); }
TaskEntry(const TaskActionEntry &_action, const TaskConditionEntry &_cond) : action(_action), cond(_cond) { Init(); }
template <typename AE, typename CE>
TaskEntry(AE _aid, CE _cid) : action(_aid), cond(_cid) {
Init();
Expand All @@ -89,7 +89,7 @@ struct TaskEntry {
bool Get(STRUCT_ENUM(TaskActionEntry, ENUM_TASK_ACTION_ENTRY_FLAG) _flag) { return action.Get(_flag); };
template <typename T>
bool Get(STRUCT_ENUM(TaskActionEntry, ENUM_TASK_ACTION_ENTRY_PROP) _prop) {
return action.Get(_prop);
return action.Get<bool>(_prop);
};
// bool Get(ENUM_TASK_ENTRY_FLAGS _flag) { return HasFlag(_flag); }
TaskActionEntry GetActionEntry() { return action; }
Expand All @@ -115,7 +115,7 @@ struct TaskEntry {
// Flag methods.
bool HasFlag(unsigned char _flag) { return bool(flags & _flag); }
void AddFlags(unsigned char _flags) { flags |= _flags; }
void RemoveFlags(unsigned char _flags) { flags &= ~_flags; }
void RemoveFlags(unsigned char _flags) { flags &= (unsigned char)~_flags; }
void SetFlag(ENUM_TASK_ENTRY_FLAGS _flag, bool _value) {
if (_value)
AddFlags(_flag);
Expand Down
24 changes: 11 additions & 13 deletions Task/TaskAction.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,19 @@ struct TaskActionEntry {
protected:
ARRAY(DataParamEntry, args); /* TaskAction arguments. */
unsigned char flags; /* TaskAction flags. */
int freq; /* How often to run (0 for no limit). */
int id; /* TaskAction's enum ID. */
datetime time_last_run; /* Time of the successful run. */
short tries; /* Number of retries left (-1 for unlimited). */
int freq; /* How often to run (0 for no limit). */
int id; /* TaskAction's enum ID. */
datetime time_last_run; /* Time of the successful run. */
short tries; /* Number of retries left (-1 for unlimited). */
protected:
// Protected methods.
void Init() {
SetFlag(STRUCT_ENUM(TaskActionEntry, TASK_ACTION_ENTRY_FLAG_IS_INVALID),
id == InvalidEnumValue<int>::value());
SetFlag(STRUCT_ENUM(TaskActionEntry, TASK_ACTION_ENTRY_FLAG_IS_INVALID), id == InvalidEnumValue<int>::value());
}

public:
// Constructors.
TaskActionEntry() : flags(0), freq(60), id(InvalidEnumValue<int>::value()), time_last_run(0), tries(-1) {
Init();
}
TaskActionEntry() : flags(0), freq(60), id(InvalidEnumValue<int>::value()), time_last_run(0), tries(-1) { Init(); }
TaskActionEntry(int _id)
: flags(STRUCT_ENUM(TaskActionEntry, TASK_ACTION_ENTRY_FLAG_IS_ACTIVE)),
freq(60),
Expand All @@ -84,7 +81,7 @@ struct TaskActionEntry {
tries(-1) {
Init();
}
TaskActionEntry(TaskActionEntry &_ae) { THIS_REF = _ae; }
TaskActionEntry(const TaskActionEntry &_ae) { THIS_REF = _ae; }
// Flag methods.
bool HasFlag(unsigned char _flag) const { return bool(flags & _flag); }
void AddFlags(unsigned char _flags) { flags |= _flags; }
Expand Down Expand Up @@ -128,7 +125,9 @@ struct TaskActionEntry {
DataParamEntry GetArg(int _index) const { return args[_index]; }
int GetId() const { return id; }
// Setters.
void TriesDec() { if(tries > 0) --tries; }
void TriesDec() {
if (tries > 0) --tries;
}
void Set(STRUCT_ENUM(TaskActionEntry, ENUM_TASK_ACTION_ENTRY_FLAG) _flag, bool _value = true) {
SetFlag(_flag, _value);
}
Expand All @@ -143,8 +142,7 @@ struct TaskActionEntry {
return;
case TASK_ACTION_ENTRY_ID:
id = (int)_value;
SetFlag(STRUCT_ENUM(TaskActionEntry, TASK_ACTION_ENTRY_FLAG_IS_INVALID),
id == InvalidEnumValue<int>::value());
SetFlag(STRUCT_ENUM(TaskActionEntry, TASK_ACTION_ENTRY_FLAG_IS_INVALID), id == InvalidEnumValue<int>::value());
return;
case TASK_ACTION_ENTRY_TRIES:
tries = (short)_value;
Expand Down
25 changes: 14 additions & 11 deletions Task/TaskCondition.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ struct TaskConditionEntry {
};

protected:
unsigned char flags; // Condition flags.
datetime last_check; // Time of the latest check.
datetime last_success; // Time of the last success.
int freq; // How often to run (0 for no limit).
int id; // Condition ID.
short tries; // Number of successful tries left (-1 for unlimited).
ARRAY(DataParamEntry, args); // Task's condition arguments.
unsigned char flags; // Condition flags.
int freq; // How often to run (0 for no limit).
int id; // Condition ID.
datetime last_check; // Time of the latest check.
datetime last_success; // Time of the last success.
short tries; // Number of successful tries left (-1 for unlimited).
// ENUM_TASK_CONDITION_STATEMENT next_statement; // Statement type of the next condition.
// ENUM_TASK_CONDITION_TYPE type; // Task's condition type.
ARRAY(DataParamEntry, args); // Task's condition arguments.
protected:
// Protected methods.
void Init() {
Expand All @@ -87,7 +87,7 @@ struct TaskConditionEntry {
tries(-1) {
Init();
}
TaskConditionEntry(TaskConditionEntry &_ae) { THIS_REF = _ae; }
TaskConditionEntry(const TaskConditionEntry &_ae) { THIS_REF = _ae; }
// Deconstructor.
~TaskConditionEntry() {}
// Getters.
Expand Down Expand Up @@ -116,7 +116,9 @@ struct TaskConditionEntry {
DataParamEntry GetArg(int _index) const { return args[_index]; }
int GetId() const { return id; }
// Setters.
void TriesDec() { tries -= tries > 0 ? 1 : 0; }
void TriesDec() {
if (tries > 0) --tries;
}
void Set(STRUCT_ENUM(TaskConditionEntry, ENUM_TASK_CONDITION_ENTRY_FLAGS) _flag, bool _value = true) {
SetFlag(_flag, _value);
}
Expand All @@ -131,7 +133,8 @@ struct TaskConditionEntry {
return;
case TASK_CONDITION_ENTRY_ID:
id = (int)_value;
SetFlag(STRUCT_ENUM(TaskConditionEntry, TASK_CONDITION_ENTRY_FLAG_IS_INVALID), id == InvalidEnumValue<int>::value());
SetFlag(STRUCT_ENUM(TaskConditionEntry, TASK_CONDITION_ENTRY_FLAG_IS_INVALID),
id == InvalidEnumValue<int>::value());
return;
case TASK_CONDITION_ENTRY_TRIES:
tries = (short)_value;
Expand All @@ -151,7 +154,7 @@ struct TaskConditionEntry {
// Flag methods.
bool HasFlag(unsigned char _flag) const { return bool(flags & _flag); }
void AddFlags(unsigned char _flags) { flags |= _flags; }
void RemoveFlags(unsigned char _flags) { flags &= ~_flags; }
void RemoveFlags(unsigned char _flags) { flags &= (unsigned char)~_flags; }
void SetFlag(ENUM_TASK_CONDITION_ENTRY_FLAGS _flag, bool _value) {
if (_value)
AddFlags(_flag);
Expand Down
36 changes: 23 additions & 13 deletions Task/TaskGetter.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct TaskGetterEntry {
TASK_GETTER_ENTRY_ID,
TASK_GETTER_ENTRY_TRIES,
TASK_GETTER_ENTRY_TIME_LAST_GET,
TASK_GETTER_ENTRY_TIME_LAST_RUN,
};
// Defines action entry flags.
enum ENUM_TASK_GETTER_ENTRY_FLAG {
Expand All @@ -58,12 +59,13 @@ struct TaskGetterEntry {
};

protected:
unsigned char flags; /* TaskGetter flags. */
datetime time_last_get; /* Time of the successful get. */
int freq; /* How often to run (0 for no limit). */
int id; /* TaskGetter's enum ID. */
short tries; /* Number of retries left (-1 for unlimited). */
ARRAY(DataParamEntry, args); /* TaskGetter arguments. */
ARRAY(DataParamEntry, args); /* TaskGetter arguments. */
unsigned char flags; /* TaskGetter flags. */
int freq; /* How often to run (0 for no limit). */
int id; /* TaskGetter's enum ID. */
datetime time_last_get; /* Time of the successful get. */
datetime time_last_run; /* Time of the successful run. */
short tries; /* Number of retries left (-1 for unlimited). */
protected:
// Protected methods.
void Init() {
Expand All @@ -72,20 +74,24 @@ struct TaskGetterEntry {

public:
// Constructors.
TaskGetterEntry() : flags(0), freq(60), id(InvalidEnumValue<int>::value()), time_last_get(0), tries(-1) { Init(); }
TaskGetterEntry()
: flags(0), freq(60), id(InvalidEnumValue<int>::value()), time_last_get(0), time_last_run(0), tries(-1) {
Init();
}
TaskGetterEntry(int _id)
: flags(STRUCT_ENUM(TaskGetterEntry, TASK_GETTER_ENTRY_FLAG_IS_ACTIVE)),
id(_id),
freq(60),
id(_id),
time_last_get(0),
time_last_run(0),
tries(-1) {
Init();
}
TaskGetterEntry(TaskGetterEntry &_ae) { THIS_REF = _ae; }
// Flag methods.
bool HasFlag(unsigned char _flag) const { return bool(flags & _flag); }
void AddFlags(unsigned char _flags) { flags |= _flags; }
void RemoveFlags(unsigned char _flags) { flags &= ~_flags; }
void RemoveFlags(unsigned char _flags) { flags &= (unsigned char)~_flags; }
void SetFlag(STRUCT_ENUM(TaskGetterEntry, ENUM_TASK_GETTER_ENTRY_FLAG) _flag, bool _value) {
if (_value) {
AddFlags(_flag);
Expand Down Expand Up @@ -114,17 +120,21 @@ struct TaskGetterEntry {
return (T)id;
case TASK_GETTER_ENTRY_TRIES:
return (T)tries;
case TASK_GETTER_ENTRY_TIME_LAST_GET:
return (T)time_last_get;
case TASK_GETTER_ENTRY_TIME_LAST_RUN:
return (T)time_last_run;
default:
break;
}
SetUserError(ERR_INVALID_PARAMETER);
return WRONG_VALUE;
return InvalidEnumValue<T>::value();
}
int GetId() const { return id; }
// Setters.
void TriesDec() { tries -= tries > 0 ? 1 : 0; }
void TriesDec() {
if (tries > 0) --tries;
}
void Set(STRUCT_ENUM(TaskGetterEntry, ENUM_TASK_GETTER_ENTRY_FLAG) _flag, bool _value = true) {
SetFlag(_flag, _value);
}
Expand All @@ -139,7 +149,7 @@ struct TaskGetterEntry {
return;
case TASK_GETTER_ENTRY_ID:
id = (int)_value;
SetFlag(STRUCT_ENUM(TaskGetterEntry, TASK_GETTER_ENTRY_FLAG_IS_INVALID), id == WRONG_VALUE);
SetFlag(STRUCT_ENUM(TaskGetterEntry, TASK_GETTER_ENTRY_FLAG_IS_INVALID), id == InvalidEnumValue<int>::value());
return;
case TASK_GETTER_ENTRY_TRIES:
tries = (short)_value;
Expand All @@ -165,7 +175,7 @@ struct TaskGetterEntry {
s.Pass(THIS_REF, "time_last_get", time_last_get);
s.Pass(THIS_REF, "tries", tries);
s.PassEnum(THIS_REF, "freq", freq);
s.PassArray(this, "args", args);
s.PassArray(THIS_REF, "args", args);
return SerializerNodeObject;
}

Expand Down
2 changes: 1 addition & 1 deletion Task/TaskGetterBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class TaskGetterBase {
/**
* Gets a copy of structure.
*/
virtual TS Get(const TaskGetterEntry &_entry) = NULL;
virtual TS Get(const TaskGetterEntry &_entry) = 0;
};

#endif // TASK_GETTER_BASE_H
Loading

0 comments on commit d75e229

Please sign in to comment.