Skip to content

Commit

Permalink
WIP. Trying to make smart pointers to work in JS.
Browse files Browse the repository at this point in the history
  • Loading branch information
nseam committed Dec 15, 2022
1 parent e36c9c9 commit 42409e3
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 61 deletions.
4 changes: 2 additions & 2 deletions Convert.extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#pragma once

// Includes.
#include <sstream>
#include <cmath>
#include <sstream>

// Define external global functions.
double NormalizeDouble(double value, int digits) { return std::round(value / digits) * digits; }
Expand All @@ -45,7 +45,7 @@ string DoubleToString(double value, int digits = 8) {

string ShortToString(unsigned short symbol_code) {
std::stringstream ss;
ss << symbol_code;
ss << (char)symbol_code;
return ss.str();
}
#endif
4 changes: 2 additions & 2 deletions DateTime.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ class DateTime {
}

#ifdef __debug_verbose__
string _passed =
"time now " + (string)dt_curr.GetTimestamp() + ", time last " + (string)dt_last.GetTimestamp() + " ";
string _passed = "time now " + TimeToString(dt_curr.GetTimestamp()) + ", time last " +
TimeToString(dt_last.GetTimestamp()) + " ";

if (_update) {
_passed += "updating time ";
Expand Down
2 changes: 2 additions & 0 deletions DictSlot.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class DictSlot {

DictSlot(unsigned char flags = 0) : _flags(flags) {}

DictSlot(const DictSlot& r) : _flags(r._flags), key(r.key), value(r.value) {}

bool IsValid() { return !bool(_flags & DICT_SLOT_INVALID); }

bool HasKey() { return bool(_flags & DICT_SLOT_HAS_KEY); }
Expand Down
17 changes: 4 additions & 13 deletions DictStruct.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,6 @@ class DictStruct : public DictBase<K, V> {
THIS_ATTR _mode = right._mode;
}

void operator=(DictStruct<K, V>& right) {
Clear();
Resize(right.GetSlotCount());
for (unsigned int i = 0; i < (unsigned int)ArraySize(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;
THIS_ATTR _mode = right._mode;
}

void Clear() {
for (unsigned int i = 0; i < (unsigned int)ArraySize(THIS_ATTR _DictSlots_ref.DictSlots); ++i) {
THIS_ATTR _DictSlots_ref.DictSlots[i].SetFlags(0);
Expand All @@ -124,7 +113,7 @@ class DictStruct : public DictBase<K, V> {
/**
* Inserts value using hashless key.
*/
bool Push(V& value) {
bool Push(const V& value) {
if (!InsertInto(THIS_ATTR _DictSlots_ref, value)) return false;
return true;
}
Expand All @@ -136,6 +125,8 @@ class DictStruct : public DictBase<K, V> {

/**
* Inserts value using hashless key.
*
* @todo Get rid of this method.
*/
#ifdef __MQL__
template <>
Expand Down Expand Up @@ -385,7 +376,7 @@ class DictStruct : public DictBase<K, V> {
/**
* Inserts hashless value into given array of DictSlots.
*/
bool InsertInto(DictSlotsRef<K, V>& dictSlotsRef, V& value) {
bool InsertInto(DictSlotsRef<K, V>& dictSlotsRef, const V& value) {
if (THIS_ATTR _mode == DictModeUnknown)
THIS_ATTR _mode = DictModeList;
else if (THIS_ATTR _mode != DictModeList) {
Expand Down
9 changes: 8 additions & 1 deletion Object.extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,12 @@
* Includes external declarations related to objects.
*/
#ifndef __MQL__
extern void *GetPointer(void *anyobject);
template <typename X>
X* GetPointer(X& value) {
return &value;
}
template <typename X>
X* GetPointer(X* ptr) {
return ptr;
}
#endif
56 changes: 43 additions & 13 deletions Refs.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@
#endif

// Includes.
#include <type_traits>

#include "Refs.rc.h"
#include "Std.h"

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

class Dynamic;
// Forward class declaration.
template <typename X>
Expand Down Expand Up @@ -87,6 +93,10 @@ struct SimpleRef {
}
};

template <typename T>
using base_type =
typename std::remove_cv<typename std::remove_reference<typename std::remove_pointer<T>::type>::type>::type;

/**
* Class used to hold strong reference to reference-counted object.
*/
Expand All @@ -97,21 +107,34 @@ struct Ref {
*/
X* ptr_object;

#ifdef EMSCRIPTEN
typedef X element_type;
#endif

public:
/**
* Constructor.
*/
Ref(X* _ptr) { THIS_REF = _ptr; }
Ref(X* _ptr) {
ptr_object = nullptr;
THIS_REF = _ptr;
}

/**
* Constructor.
*/
Ref(Ref<X>& ref) { THIS_REF = ref.Ptr(); }
Ref(const Ref<X>& ref) {
ptr_object = nullptr;
Set(ref.Ptr());
}

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

/**
* Constructor.
Expand All @@ -126,7 +149,11 @@ struct Ref {
/**
* Returns pointer to target object.
*/
X* Ptr() { return ptr_object; }
X* Ptr() const { return ptr_object; }

#ifdef EMSCRIPTEN
X* get() const { return ptr_object; }
#endif

/**
* Checks whether any object is referenced.
Expand Down Expand Up @@ -208,7 +235,11 @@ struct Ref {
/**
* Makes a strong reference to the given object.
*/
X* operator=(X* _ptr) {
X* operator=(X* _ptr) { return Set(_ptr); }
/**
* Makes a strong reference to the given object.
*/
X* Set(X* _ptr) {
if (ptr_object == _ptr) {
// Assigning the same object.
return Ptr();
Expand Down Expand Up @@ -240,25 +271,24 @@ struct Ref {
/**
* Makes a strong reference to the given weakly-referenced object.
*/
X* operator=(WeakRef<X>& right) {
THIS_REF = right.Ptr();
return Ptr();
}
X* operator=(const WeakRef<X>& right) { return Set((X*)right.Ptr()); }

/**
* Makes a strong reference to the strongly-referenced object.
*/
X* operator=(Ref<X>& right) {
THIS_REF = right.Ptr();
return Ptr();
}
X* operator=(const Ref<X>& right) { return Set((X*)right.Ptr()); }

/**
* Equality operator.
*/
bool operator==(const Ref<X>& r) { return ptr_object != NULL && ptr_object == r.ptr_object; }
};

template <typename X>
Ref<X> make_ref() {
return Ref<X>();
}

/**
* Class used to hold weak reference to reference-counted object.
*/
Expand Down
16 changes: 11 additions & 5 deletions Serializer/SerializerConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class SerializerNode;
#include "SerializerDict.h"
#include "SerializerNode.h"

#ifdef __debug__
#include "SerializerJson.h"
#endif

class SerializerConverter {
public:
SerializerNode* root_node;
Expand Down Expand Up @@ -70,8 +74,9 @@ class SerializerConverter {
SerializerConverter _converter(_serializer.GetRoot(), serializer_flags);
#ifdef __debug__
Print("FromObject(): serializer flags: ", serializer_flags);
Print("FromObject(): result: ",
_serializer.GetRoot() != NULL ? _serializer.GetRoot().ToString(SERIALIZER_JSON_NO_WHITESPACES) : "NULL");
Print("FromObject(): result: ", _serializer.GetRoot() != NULL
? _serializer.GetRoot() PTR_DEREF ToString(SERIALIZER_JSON_NO_WHITESPACES)
: "NULL");
#endif
return _converter;
}
Expand All @@ -84,8 +89,9 @@ class SerializerConverter {
SerializerConverter _converter(_serializer.GetRoot(), serializer_flags);
#ifdef __debug__
Print("FromObject(): serializer flags: ", serializer_flags);
Print("FromObject(): result: ",
_serializer.GetRoot() != NULL ? _serializer.GetRoot().ToString(SERIALIZER_JSON_NO_WHITESPACES) : "NULL");
Print("FromObject(): result: ", _serializer.GetRoot() != NULL
? _serializer.GetRoot() PTR_DEREF ToString(SERIALIZER_JSON_NO_WHITESPACES)
: "NULL");
#endif
return _converter;
}
Expand Down Expand Up @@ -115,7 +121,7 @@ class SerializerConverter {
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");
_converter.Node() != NULL ? _converter.Node() PTR_DEREF ToString(SERIALIZER_JSON_NO_WHITESPACES) : "NULL");
#endif
return _converter;
}
Expand Down
26 changes: 15 additions & 11 deletions Serializer/SerializerJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class SerializerJson {

#ifdef __debug__
Print("SerializerJson: Value \"" + extracted + "\" for key " +
(key != NULL ? ("\"" + key.ToString() + "\"") : "<none>"));
(key != NULL ? ("\"" + key PTR_DEREF ToString() + "\"") : "<none>"));
#endif

expectingValue = false;
Expand All @@ -217,7 +217,8 @@ class SerializerJson {
}

#ifdef __debug__
Print("SerializerJson: Entering object for key " + (key != NULL ? ("\"" + key.ToString() + "\"") : "<none>"));
Print("SerializerJson: Entering object for key " +
(key != NULL ? ("\"" + key PTR_DEREF ToString() + "\"") : "<none>"));
#endif

node = new SerializerNode(SerializerNodeObject, current, key);
Expand All @@ -237,16 +238,18 @@ class SerializerJson {
}

#ifdef __debug__
Print("SerializerJson: Leaving object for key " + (current != NULL && current.GetKeyParam() != NULL
? ("\"" + current.GetKeyParam().ToString() + "\"")
: "<none>"));
Print("SerializerJson: Leaving object for key " +
(current != NULL && current PTR_DEREF GetKeyParam() != NULL
? ("\"" + current PTR_DEREF GetKeyParam() PTR_DEREF ToString() + "\"")
: "<none>"));
#endif

current = PTR_ATTRIB(current, GetParent());
expectingValue = false;
} else if (ch == '[') {
#ifdef __debug__
Print("SerializerJson: Entering list for key " + (key != NULL ? ("\"" + key.ToString() + "\"") : "<none>"));
Print("SerializerJson: Entering list for key " +
(key != NULL ? ("\"" + key PTR_DEREF ToString() + "\"") : "<none>"));
#endif

if (expectingKey) {
Expand All @@ -264,7 +267,8 @@ class SerializerJson {
key = NULL;
} else if (ch == ']') {
#ifdef __debug__
Print("SerializerJson: Leaving list for key " + (key != NULL ? ("\"" + key.ToString() + "\"") : "<none>"));
Print("SerializerJson: Leaving list for key " +
(key != NULL ? ("\"" + key PTR_DEREF ToString() + "\"") : "<none>"));
#endif

if (expectingKey || expectingValue || PTR_ATTRIB(current, GetType()) != SerializerNodeArray) {
Expand All @@ -285,8 +289,8 @@ class SerializerJson {
value = StringFind(extracted, ".") != -1 ? SerializerNodeParam::FromValue(StringToDouble(extracted))
: SerializerNodeParam::FromValue(StringToInteger(extracted));
#ifdef __debug__
Print("SerializerJson: Value " + value.AsString() + " for key " +
(key != NULL ? ("\"" + key.ToString() + "\"") : "<none>"));
Print("SerializerJson: Value " + value PTR_DEREF AsString() + " for key " +
(key != NULL ? ("\"" + key PTR_DEREF ToString() + "\"") : "<none>"));
#endif

PTR_ATTRIB(current, AddChild(new SerializerNode(PTR_ATTRIB(current, GetType()) == SerializerNodeObject
Expand All @@ -306,8 +310,8 @@ class SerializerJson {
value = SerializerNodeParam::FromValue(ch == 't' ? true : false);

#ifdef __debug__
Print("SerializerJson: Value " + (value.ToBool() ? "true" : "false") + " for key " +
(key != NULL ? ("\"" + key.ToString() + "\"") : "<none>"));
Print(string("SerializerJson: Value ") + (value PTR_DEREF ToBool() ? "true" : "false") + " for key " +
(key != NULL ? ("\"" + key PTR_DEREF ToString() + "\"") : "<none>"));
#endif

// Skipping value.
Expand Down
2 changes: 2 additions & 0 deletions String.extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ void PrintTo(std::ostream& out, Arg&& arg, Args&&... args) {
out << std::forward<Arg>(arg);
using expander = int[];
(void)expander{0, (void(out << std::forward<Args>(args)), 0)...};
out << "\n";
out.flush();
}

template <typename Arg, typename... Args>
Expand Down
Loading

0 comments on commit 42409e3

Please sign in to comment.