MQLplus/lib_structures/lib_objects.mqh

294 lines
10 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 16:09:52 +02:00
#ifndef LIB_MQLPLUS_OBJ_BASE_TPL_MQH_INCLUDED
#define LIB_MQLPLUS_OBJ_BASE_TPL_MQH_INCLUDED
#property version "1.0";
/**********************************************************************************
* Copyright (C) 2010-2022 Dominik Egert <info@freie-netze.de>
*
* This file is the objects include file.
*
* MQLplus, including this file may not be copied and/or distributed
* without explecit permit by the author.
* Author Dominik Egert / Freie Netze UG.
**********************************************************************************
*
* Version: 1.0
* State: production
*
* File information
* ================
*
*
*
*/
#ifdef DBG_MSG_TRACE_FILE_LOADER
DBG_MSG_TRACE_FILE_LOADER;
#endif
/*********************************************************************************************************************************************************/
/* */
/* MQLplus data structures */
/* */
/*********************************************************************************************************************************************************/
///////////////////////////////////////
//
// Serial number base object
//
struct s_serial_nr
{
public:
// Local storage
const ulong serial_nr;
ulong copy_of_sn;
// Constructor
s_serial_nr() :
serial_nr(LIB_NAMESPACE(__objects_serial_counter, serial_counter)++),
copy_of_sn(NULL)
{};
// Copy constructor
s_serial_nr(const s_serial_nr& p_in)
{ operator=(p_in); }
// Assignment operator
s_serial_nr operator=(const s_serial_nr& p_in) { copy_of_sn = p_in.serial_nr; return(this); };
// Comparisson operators
const bool operator==(const s_serial_nr& p_in) const { return(serial_nr == p_in.serial_nr); };
const bool operator!=(const s_serial_nr& p_in) const { return(serial_nr != p_in.serial_nr); };
const bool operator>=(const s_serial_nr& p_in) const { return(serial_nr >= p_in.serial_nr); };
const bool operator<=(const s_serial_nr& p_in) const { return(serial_nr <= p_in.serial_nr); };
const bool operator>(const s_serial_nr& p_in) const { return(serial_nr > p_in.serial_nr); };
const bool operator<(const s_serial_nr& p_in) const { return(serial_nr < p_in.serial_nr); };
};
class c_serial_nr
{
public:
// Local storage
const ulong serial_nr;
ulong copy_of_sn;
// Constructor
c_serial_nr() :
serial_nr(LIB_NAMESPACE(__objects_serial_counter, serial_counter)++),
copy_of_sn(NULL)
{};
// Copy constructor
c_serial_nr(const c_serial_nr& p_in)
{ operator=(p_in); }
// Assignment operator
c_serial_nr operator=(const c_serial_nr& p_in)
{ copy_of_sn = p_in.serial_nr; return(this); };
// Comparisson operators
const bool operator==(const c_serial_nr& p_in) const { return(serial_nr == p_in.serial_nr); };
const bool operator!=(const c_serial_nr& p_in) const { return(serial_nr != p_in.serial_nr); };
const bool operator>=(const c_serial_nr& p_in) const { return(serial_nr >= p_in.serial_nr); };
const bool operator<=(const c_serial_nr& p_in) const { return(serial_nr <= p_in.serial_nr); };
const bool operator>(const c_serial_nr& p_in) const { return(serial_nr > p_in.serial_nr); };
const bool operator<(const c_serial_nr& p_in) const { return(serial_nr < p_in.serial_nr); };
};
#ifndef __MQL4_COMPATIBILITY_CODE__
namespace __objects_serial_counter {
#endif
static ulong LIB_NAMESPACE_DEF(__objects_serial_counter, serial_counter) = 1;
#ifndef __MQL4_COMPATIBILITY_CODE__
}; // End Namespace
#endif
///////////////////////////////////////
//
// Sorting capable object
//
template <typename T, typename U>
struct object_sortable
{
public:
// Local storage
T obj;
U sort_value;
// Constructor
object_sortable()
{ };
object_sortable(const object_sortable& p_in)
{
obj = p_in.obj;
sort_value = p_in.sort_value;
}
// Add element
void add(const T& obj_in, const U sort_by_value_in)
{
obj = obj_in;
sort_value = sort_by_value_in;
}
// Assignment operator
const object_sortable operator=(const object_sortable& p_in)
{
obj = p_in.obj;
sort_value = p_in.sort_value;
return(this);
}
// Radix sort bit-shift operator
const long operator>>(const int p_in) const { return(sort_value >> p_in); }
// Comparison operator
const bool operator==(const object_sortable& p_in) const { return(sort_value == p_in.sort_value); }
const bool operator!=(const object_sortable& p_in) const { return(sort_value != p_in.sort_value); }
const bool operator>=(const object_sortable& p_in) const { return(sort_value >= p_in.sort_value); }
const bool operator<=(const object_sortable& p_in) const { return(sort_value <= p_in.sort_value); }
const bool operator>(const object_sortable& p_in) const { return(sort_value > p_in.sort_value); }
const bool operator<(const object_sortable& p_in) const { return(sort_value < p_in.sort_value); }
};
///////////////////////////////////////
//
// Mean series object
//
template <typename T>
struct object_mean
{
private:
// Local storage
T _ring_buffer[];
T _sum_value;
int ptr;
int _size;
bool is_full;
public:
// Constructors
object_mean() :
_sum_value (NULL),
ptr (-1),
_size (NULL),
is_full (false)
{ };
object_mean(const int size)
{ reset(size); };
object_mean(const object_mean& p_in)
{ operator=(p_in); };
// Operators
void operator=(const object_mean& p_in)
{
_sum_value = p_in._sum_value;
ptr = p_in.ptr;
_size = p_in._size;
is_full = p_in.is_full;
::ArrayCopy(_ring_buffer, p_in._ring_buffer);
};
const T operator[](const int p_in) const
{ return(_ring_buffer[p_in]); };
// Buffer functions
const bool reset(const int size)
{
_sum_value = NULL;
ptr = -1;
is_full = false;
_size = ::ArrayResize(_ring_buffer, size);
return(::ArrayInitialize(_ring_buffer, NULL) == size);
};
// Mean functions
void add(const T p_in)
{
ptr++;
is_full |= (ptr == _size);
ptr = ptr % _size;
update(p_in);
};
void update(const T p_in)
{
_sum_value += (p_in - _ring_buffer[ptr]);
_ring_buffer[ptr] = p_in;
};
const T get() const
{ return(_sum_value / ((is_full) ? _size : (ptr + 1))); };
// Min/Max functions
const int max() const
{ int idx = ptr; for(int cnt = (is_full) ? (_size - 1) : ptr; (cnt >= NULL) && !_StopFlag; cnt--) { idx = (_ring_buffer[idx] < _ring_buffer[cnt]) ? cnt : idx; } return(idx); };
const int min() const
{ int idx = ptr; for(int cnt = (is_full) ? (_size - 1) : ptr; (cnt >= NULL) && !_StopFlag; cnt--) { idx = (_ring_buffer[idx] > _ring_buffer[cnt]) ? cnt : idx; } return(idx); };
};
//
// END MQL data structures */
//*********************************************************************************************************************************************************/
#endif // LIB_MQLPLUS_OBJECTS_TEMPLATES_MQH_INCLUDED