1
0
Derivar 1
BasesParserSLan/Src/DomBaseHeader.mqh
Nique_372 e17fae1c7d
2026-07-21 10:57:52 -05:00

175 linhas
6,7 KiB
MQL5

//+------------------------------------------------------------------+
//| DomBaseHeader.mqh |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com/"
#property strict
#ifndef BASESPARSERSLAN_SRC_DOM_BASE_HEADER_MQH
#define BASESPARSERSLAN_SRC_DOM_BASE_HEADER_MQH
//+------------------------------------------------------------------+
//| Include |
//+------------------------------------------------------------------+
#include "Main.mqh"
#include <TSN\\GCol\\GenericHashes.mqh>
#include <TSN\\GCol\\HashMap.mqh>
namespace TSN
{
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CDomNodeManager;
#define TSN_SBL_BASE_DOM_NULL 7
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CDomNodeBase
{
public:
CDomNodeManager* m_ctx;
union Value
{
long iv;
double dv;
bool bv;
int ix;
} m_v;
uchar m_type;
//---
CDomNodeBase(CDomNodeManager* ctx, const uchar type, const double v)
: m_ctx(ctx), m_type(type) { m_v.dv = v; }
CDomNodeBase(CDomNodeManager* ctx, const uchar type, const long v)
: m_ctx(ctx), m_type(type) { m_v.iv = v; }
CDomNodeBase(CDomNodeManager* ctx, const uchar type, const int v)
: m_ctx(ctx), m_type(type) { m_v.iv = v; }
CDomNodeBase(CDomNodeManager* ctx, const uchar type, const bool v)
: m_ctx(ctx), m_type(type) { m_v.bv = v; }
CDomNodeBase(CDomNodeManager* ctx, const uchar type, const string& v)
: m_ctx(ctx), m_type(type)
{
m_v.ix = m_ctx.ReserveString();
m_ctx.m_string_stack[m_v.ix] = v;
}
CDomNodeBase(CDomNodeManager* ctx)
: m_ctx(ctx), m_type(TSN_SBL_BASE_DOM_NULL) {}
CDomNodeBase(CDomNodeManager* ctx, const uchar type)
: m_ctx(ctx), m_type(type) {}
//---
CDomNodeBase() : m_ctx(NULL), m_type(TSN_SBL_BASE_DOM_NULL) {}
~CDomNodeBase();
//--- Keys
__forceinline int GetKeys(string& out[]) const;
__forceinline bool HasKey(const string& key) const;
bool Remove(const string& key); // Elimina una key
//--- Acceso obj
CDomNodeBase* const operator[](const string& key); // si no existe lo crea..
// la idea es llenear el hashmap con un nodo dado..
// dado qeu normalte operator siempre te crea un nodo.. siempre.. si no existe crea para que tu lo rellenes
// entonces si le das un nodo de otro manager no lo limpiaria
// para eso esta este metodo
bool Set(const string& key, CDomNodeBase* val);
// versiones extra (Devuele null si falla)
CDomNodeBase* const Get(const string& key) const;
CDomNodeBase* const Get(const ulong fnv164_hash) const;
//--- Acceso array
CDomNodeBase* const At(const int idx) const; // deuvelve el puntero pero no se peude cambiar de direccion
CDomNodeBase* const AtSafe(const int idx) const; // NULL si no esta en rango
bool Ats(const int idx, CDomNodeBase* val);
//--- Arr
template <typename T>
__forceinline int ToArray(T& array[]) const;
__forceinline int Size() const;
__forceinline bool InRange(const int index) const;
__forceinline bool RemoveItem(const int index); // elimina un index del array
bool AddItem(CDomNodeBase* node); // añade un item al array
//--- Comun entre arr y obj
void Clear(); // Elimina todos los elemetnos de array u obj
//--- Asignacion
bool As(const string v);
bool operator=(CDomNodeBase* ptr);
bool operator=(CDomNodeBase& ref);
bool operator=(const long v);
bool operator=(const double v);
bool operator=(const bool v);
bool operator=(const string& v);
void NewObj(int inital_hash_map_size);
void NewArr(int inital_reserve);
//--- Is
__forceinline bool IsNull() const { return m_type == TSN_SBL_BASE_DOM_NULL; }
__forceinline bool IsArray() const { return m_type == TSN_SBL_BASE_TYPE_ARR; }
__forceinline bool IsObject() const { return m_type == TSN_SBL_BASE_TYPE_OBJ; }
//--- To
//- Int
long ToInt(const long def) const;
bool ToIntSafe(long& val) const;
//- Double
double ToDouble(const double def) const;
bool ToDoubleSafe(double& val) const;
//--- Bool
bool ToBool(const bool def) const;
bool ToBoolSafe(bool& val) const;
//--- String
string ToString(const string def = "") const;
int StrLenRaw() const;
//---
__forceinline CHashMapFast(string, CDomNodeBase*)* HashMap() { return m_type == TSN_SBL_BASE_TYPE_OBJ ? m_ctx.m_objs[m_v.ix] : NULL; }
__forceinline CHashMapFastIterator(string, CDomNodeBase*) BenginIteration();
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CDomNodeBase::~CDomNodeBase()
{
switch(m_type)
{
case TSN_SBL_BASE_TYPE_OBJ:
m_ctx.FreeObj(m_v.ix);
break;
case TSN_SBL_BASE_TYPE_ARR:
m_ctx.FreeArr(m_v.ix);
break;
case TSN_SBL_BASE_TYPE_INT:
case TSN_SBL_BASE_TYPE_FLT:
case TSN_SBL_BASE_TYPE_BOL:
break;
case TSN_SBL_BASE_TYPE_STR:
m_ctx.FreeString(m_v.ix);
break;
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
__forceinline CHashMapFastIterator(string, CDomNodeBase*) CDomNodeBase::BenginIteration()
{
if(m_type != TSN_SBL_BASE_TYPE_OBJ)
return CHashMapFastIterator(string, CDomNodeBase*)();
return m_ctx.m_objs[m_v.ix].BeginIteration();
}
}
#endif // BASESPARSERSLAN_SRC_DOM_BASE_HEADER_MQH
//+------------------------------------------------------------------+