evolution/src/SubIndiBase.mqh
Vladislav Boyko Vladislav Boyko cd50d18f99 style: align pointer and reference symbols with type
In modern C++ and OOP practices, pointer and reference qualifiers are treated as an intrinsic part of the data type rather than the variable name. Aligning these symbols with the type reinforces this semantic distinction, improving conceptual clarity and consistency with modern conventions.
2026-08-28 12:32:39 -07:00
74 lines
2.6 KiB
MQL5
//+------------------------------------------------------------------+
//| Copyright (C) 2026, boyvlad |
//| https://www.mql5.com/en/users/boyvlad |
//+------------------------------------------------------------------+
#include "SubIndiRegistry.mqh"
//+------------------------------------------------------------------+
//| Base class for all sub-indicators. Extracts common traits and |
//| handles routine boilerplate |
//+------------------------------------------------------------------+
class CSubIndiBase
{
private:
//--- Subclass hook for clearing all buffers at a given bar index
virtual void clearBuffersAt(int barIdx) = 0;
//--- Ensures barsRequired is valid
static int correctBarsRequired(int barsRequired)
{
if(barsRequired >= 1)
return barsRequired;
const int prevValue = barsRequired;
barsRequired = 1;
PrintFormat(__FUNCTION__": invalid barsRequired value (%i); corrected to %i", prevValue, barsRequired);
return barsRequired;
}
protected:
//--- Determines calculation start index; clears buffers on full calculation
int prepareCalculation(const int rates_total, const int prev_calculated)
{
int calcStartIdx;
switch(rates_total - prev_calculated)
{
case 0:
case 1:
calcStartIdx = prev_calculated - 1;
break;
default:
calcStartIdx = drawBegin;
clearLeftFromDrawBegin();
break;
}
return calcStartIdx;
}
//--- Overload for recurrent/stateful indicators: reports if drawBegin is calculated
int prepareCalculation(const int rates_total, const int prev_calculated, bool& isFullCalculation)
{
const int calcStartIdx = prepareCalculation(rates_total, prev_calculated);
isFullCalculation = calcStartIdx <= drawBegin;
return calcStartIdx;
}
//--- Clears all buffers at each bar before drawBegin
void clearLeftFromDrawBegin()
{
for(int i = 0; i < drawBegin; i++)
clearBuffersAt(i);
}
public:
const int barsRequired; // minimum bars required for valid calculation
const int drawBegin; // index of first valid value (barsRequired - 1)
const double emptyValue; // placeholder for bars with no value
protected:
CSubIndiBase(int a_barsRequired, double a_emptyValue)
: barsRequired(correctBarsRequired(a_barsRequired)),
drawBegin(barsRequired - 1),
emptyValue(a_emptyValue)
{
CSubIndiRegistry::register(this);
}
};