BasesParserSLan/Src/DOM/DomBaseHeader.mqh
Nique_372 73df7c0d6c
2026-10-08 13:38:10 -05:00

253 lines
9.4 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_DOM_BASE_HEADER_MQH
#define BASESPARSERSLAN_SRC_DOM_DOM_BASE_HEADER_MQH
//+------------------------------------------------------------------+
//| Include |
//+------------------------------------------------------------------+
//--- baseclass
#include "..\\BaseClass\\Main.mqh"
//--- hasmap
#include <TSN\\GCol\\GenericHashes.mqh>
#include <TSN\\GCol\\HashMap.mqh>
//--- def
#include "..\\ExtraDef\\Pointer\\Def.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CDomNodeManager;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#define BASESPARSERSLAN_DOMNODE_FREE \
switch(m_type) \
{ \
case TSN_SBL_BASE_TYPE_ARR: \
m_ctx.FreeArr(m_v.ix); \
break; \
case TSN_SBL_BASE_TYPE_OBJ: \
m_ctx.FreeObj(m_v.ix); \
break; \
case TSN_SBL_BASE_TYPE_UINT: \
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; \
default: \
break; \
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CDomNodeBase
{
public:
CDomNodeManager* m_ctx;
union Value
{
ulong uv; // uinteger (no-signed) value
long iv; // integer (signed) value
double dv; // double value
bool bv; // bool value
int ix; // indice en stack
} m_v;
uchar m_type;
//--- Basicos
CDomNodeBase(CDomNodeManager* ctx, const double v)
: m_ctx(ctx), m_type(TSN_SBL_BASE_TYPE_FLT) { m_v.dv = v; }
CDomNodeBase(CDomNodeManager* ctx, const ulong v)
: m_ctx(ctx), m_type(TSN_SBL_BASE_TYPE_UINT) { m_v.uv = v; }
CDomNodeBase(CDomNodeManager* ctx, const long v)
: m_ctx(ctx), m_type(TSN_SBL_BASE_TYPE_INT) { m_v.iv = v; }
CDomNodeBase(CDomNodeManager* ctx, const int v)
: m_ctx(ctx), m_type(TSN_SBL_BASE_TYPE_INT) { m_v.iv = v; }
CDomNodeBase(CDomNodeManager* ctx, const bool v)
: m_ctx(ctx), m_type(TSN_SBL_BASE_TYPE_BOL) { m_v.bv = v; }
CDomNodeBase(CDomNodeManager* ctx, const string& v)
: m_ctx(ctx), m_type(TSN_SBL_BASE_TYPE_STR)
{
m_v.ix = m_ctx.ReserveString();
m_ctx.m_string_stack[m_v.ix] = v;
}
//--- Especificamdo el tipo inicial
CDomNodeBase(uchar tt, CDomNodeManager* ctx, int default_els_in_arrojb = 32);
//--- Por defecto vacio
CDomNodeBase(CDomNodeManager* ctx)
: m_ctx(ctx), m_type(TSN_SBL_BASE_DOM_NULL) {}
//--- Sin mas
CDomNodeBase() : m_ctx(NULL), m_type(TSN_SBL_BASE_DOM_NULL) {}
//---
~CDomNodeBase();
//----
template <typename TValue>
void Load(TValue& other)
{
//---
BASESPARSERSLAN_DOMNODE_FREE
//---
m_type = TSN_SBL_BASE_DOM_NULL;
//---
TValue::ToTsnDomNode(this, other);
}
//---
template <typename TValue>
TValue GetAs()
{
return TValue(&this);
}
//--- 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
// version fast no comprueba limites
CDomNodeBase* const At(const int idx) const; // deuvelve el puntero pero no se peude cambiar de direccion
// chekea limites
CDomNodeBase* const AtSafe(const int idx) const; // NULL si no esta en rango
bool Ats(const int idx, CDomNodeBase* val);
int PySlicing(CDomNodeBase* &out[], int dst_start = 0, int start = -1,
int end = -1, int step = 1) const;
int CopyTo(CDomNodeBase *&out[], int dst_start) const;
//--- Json pointer (opcionales)
CDomNodeBase* operator[](BSFLPointerQuery& query);
//CDomNodeBase* PointerSet(BSFLPointerQuery& query);
//--- 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 int v);
bool operator=(const long v);
bool operator=(const double v);
bool operator=(const bool v);
bool operator=(const string& v);
bool operator=(const ulong v);
void NewObj(int inital_hash_map_size);
void NewArr(int inital_reserve);
//--- cambiar el tipo
void Type(const uchar tt, int default_els_in_arrojb = 32);
//--- 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; }
//---
__forceinline uint GetFlag() const { return 1 << m_type; }
//--- To
//- Int
long ToInt(const long def) const;
bool ToIntSafe(long& val) const;
//- Uint
ulong ToUint(const ulong def) const;
bool ToUintSafe(ulong& val) const;
//- Double
double ToDouble(const double def) 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(uchar tt, CDomNodeManager *ctx, int default_els_in_arrojb)
// nul por mientras para que en el free no entre al switch
: m_type(TSN_SBL_BASE_DOM_NULL), m_ctx(ctx)
{
//---
Type(tt, default_els_in_arrojb);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CDomNodeBase::~CDomNodeBase()
{
BASESPARSERSLAN_DOMNODE_FREE
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
__forceinline CHashMapFastIterator(string, CDomNodeBase*) CDomNodeBase::BenginIteration()
{
if(m_type != TSN_SBL_BASE_TYPE_OBJ)
return CHashMapFastIterator(string, CDomNodeBase*)(); // empty
return m_ctx.m_objs[m_v.ix].BeginIteration();
}
}
#endif // BASESPARSERSLAN_SRC_DOM_DOM_BASE_HEADER_MQH
//+------------------------------------------------------------------+