forked from nique_372/MQLArticles
140 lines
9.8 KiB
MQL5
140 lines
9.8 KiB
MQL5
//+------------------------------------------------------------------+
| |
//| AutoDelete.mqh |
| |
//| Copyright 2025, Niquel Mendoza. |
| |
//| https://www.mql5.com/es/users/nique_372 |
| |
//+------------------------------------------------------------------+
| |
#property copyright "Copyright 2025, Niquel Mendoza."
| |
#property link "https://www.mql5.com/es/users/nique_372"
| |
#property strict
| |
| |
| |
#ifndef MQLARTICLES_UTILS_FA_AUTODELETE_MQH
| |
#define MQLARTICLES_UTILS_FA_AUTODELETE_MQH
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#include "SimpleLogger.mqh"
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
typedef void(*FinalCleanFunction)(const int reason);
| |
#define MQLARTICLES_AUTOCLEANER_FLAG_PTR (1)
| |
#define MQLARTICLES_AUTOCLEANER_FLAG_FUNCTION (2)
| |
#define MQLARTICLES_AUTOCLEANER_ALL (MQLARTICLES_AUTOCLEANER_FLAG_PTR|MQLARTICLES_AUTOCLEANER_FLAG_FUNCTION)
| |
| |
//---
| |
//#define MQLARTICLES_AUTOCLENER_DEBUG
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
class CAutoCleaner
| |
{
| |
private:
| |
static void* s_ptrs[];
| |
static int s_ptrs_size;
| |
static FinalCleanFunction s_c_functions[];
| |
static int s_c_functions_size;
| |
| |
public:
| |
| |
CAutoCleaner() {}
| |
~CAutoCleaner() {}
| |
| |
//--- Inicializar
| |
static void Init()
| |
{
| |
s_ptrs_size = ArrayResize(s_ptrs, 0);
| |
s_c_functions_size = ArrayResize(s_c_functions, 0);
| |
}
| |
| |
//--- Punteros
| |
// Añadir
| |
static void AddPtr(void* ptr)
| |
{
| |
if(CheckPointer(ptr) == POINTER_DYNAMIC)
| |
{
| |
s_ptrs[ArrayResize(s_ptrs, ++s_ptrs_size) - 1] = ptr;
| |
}
| |
}
| |
| |
// Remover
| |
static void RemovePtr(void* ptr)
| |
{
| |
for(int i = 0; i < s_ptrs_size; i++)
| |
{
| |
if(s_ptrs[i] == ptr)
| |
{
| |
s_ptrs[i] = s_ptrs[--s_ptrs_size];
| |
ArrayResize(s_ptrs, s_ptrs_size);
| |
break;
| |
}
| |
}
| |
}
| |
| |
//--- Funciones
| |
// Añadir
| |
static void AddFunction(FinalCleanFunction f)
| |
{
| |
s_c_functions[ArrayResize(s_c_functions, (++s_c_functions_size)) - 1] = f;
| |
}
| |
| |
//--- Totales
| |
static __forceinline int TotalPtr() { return s_ptrs_size; }
| |
static __forceinline int TotalFunctions() { return s_c_functions_size; }
| |
| |
//--- Funciones principal clean
| |
static void Deinit(const int reason, int flags = MQLARTICLES_AUTOCLEANER_ALL)
| |
{
| |
//--- Ptrs
| |
if((flags & MQLARTICLES_AUTOCLEANER_FLAG_PTR) != 0)
| |
{
| |
for(int i = 0; i < s_ptrs_size; i++)
| |
{
| |
#ifdef MQLARTICLES_AUTOCLENER_DEBUG
| |
FastLog(FUNCION_ACTUAL, INFO_TEXT, StringFormat("Iniciado eliminacion de puntero en posicion[%d], tipo = %s", i, EnumToString(CheckPointer(s_ptrs[i]))));
| |
#endif
| |
| |
if(CheckPointer(s_ptrs[i]) == POINTER_DYNAMIC)
| |
delete s_ptrs[i];
| |
| |
#ifdef MQLARTICLES_AUTOCLENER_DEBUG
| |
FastLog(FUNCION_ACTUAL, INFO_TEXT, StringFormat("Finalizando eliminacion de puntero en posicion[%d]", i));
| |
#endif
| |
}
| |
}
| |
| |
//--- Eliminacion de funciones (DESPUES) debido aque qui eestan funciones de manager o clases de eventos
| |
if((flags & MQLARTICLES_AUTOCLEANER_FLAG_FUNCTION) != 0)
| |
{
| |
for(int i = 0; i < s_c_functions_size; i++)
| |
{
| |
#ifdef MQLARTICLES_AUTOCLENER_DEBUG
| |
FastLog(FUNCION_ACTUAL, INFO_TEXT, StringFormat("Iniciado ejecucion de funcion[%d]", i));
| |
#endif
| |
s_c_functions[i](reason);
| |
#ifdef MQLARTICLES_AUTOCLENER_DEBUG
| |
FastLog(FUNCION_ACTUAL, INFO_TEXT, StringFormat("Finalizando ejecucion de funcion[%d]", i));
| |
#endif
| |
}
| |
}
| |
| |
s_c_functions_size = ArrayResize(s_c_functions, 0);
| |
s_ptrs_size = ArrayResize(s_ptrs, 0);
| |
| |
#ifdef MQLARTICLES_AUTOCLENER_DEBUG
| |
FastLog(FUNCION_ACTUAL, INFO_TEXT, "Fin de la limpieza");
| |
#endif
| |
}
| |
};
| |
//+------------------------------------------------------------------+
| |
//| Definición de miembros estáticos |
| |
//+------------------------------------------------------------------+
| |
void* CAutoCleaner::s_ptrs[];
| |
int CAutoCleaner::s_ptrs_size = 0;
| |
FinalCleanFunction CAutoCleaner::s_c_functions[];
| |
int CAutoCleaner::s_c_functions_size = 0;
| |
| |
//+------------------------------------------------------------------+
| |
#endif // MQLARTICLES_UTILS_FA_AUTODELETE_MQH
|