Skip to content

Commit

Permalink
fix #47: MQL5 update makes calling static methods of template type pa…
Browse files Browse the repository at this point in the history
…rameters impossible
  • Loading branch information
dingmaotu committed May 14, 2020
1 parent bfe9f7a commit 1d00831
Showing 1 changed file with 145 additions and 41 deletions.
186 changes: 145 additions & 41 deletions Lang/GlobalVariable.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,57 @@
class GlobalVariable
{
public:
static int total() {return GlobalVariablesTotal();}
static string name(int index) {return GlobalVariableName(index);}
static void flush() {GlobalVariablesFlush();}
static int total()
{
return GlobalVariablesTotal();
}
static string name(int index)
{
return GlobalVariableName(index);
}
static void flush()
{
GlobalVariablesFlush();
}

static bool exists(string name) {return GlobalVariableCheck(name);}
static datetime lastAccess(string name) {return GlobalVariableTime(name);}
static bool exists(string name)
{
return GlobalVariableCheck(name);
}
static datetime lastAccess(string name)
{
return GlobalVariableTime(name);
}

static bool makeTemp(string name) {return GlobalVariableTemp(name);}
static double get(string name) {return GlobalVariableGet(name);}
static bool get(string name,double &value) {return (bool)GlobalVariableGet(name,value);}
static bool set(string name,double value) {return (bool)GlobalVariableSet(name,value);}
static bool setOn(string name,double value,double check) {return GlobalVariableSetOnCondition(name,value,check);}
static bool makeTemp(string name)
{
return GlobalVariableTemp(name);
}
static double get(string name)
{
return GlobalVariableGet(name);
}
static bool get(string name,double &value)
{
return (bool)GlobalVariableGet(name,value);
}
static bool set(string name,double value)
{
return (bool)GlobalVariableSet(name,value);
}
static bool setOn(string name,double value,double check)
{
return GlobalVariableSetOnCondition(name,value,check);
}

static bool remove(string name) {return (bool)GlobalVariableDel(name);}
static bool removeAll(string prefix=NULL,datetime before=0) {return (bool)GlobalVariablesDeleteAll(prefix,before);}
static bool remove(string name)
{
return (bool)GlobalVariableDel(name);
}
static bool removeAll(string prefix=NULL,datetime before=0)
{
return (bool)GlobalVariablesDeleteAll(prefix,before);
}
};
//+------------------------------------------------------------------+
//| TempVar is a variable whose life time is the same as the program |
Expand All @@ -57,14 +93,38 @@ public:
GlobalVariable::makeTemp(name);
}
}
~TempVar() {if(m_owned && isValid()) {GlobalVariable::remove(m_name);}}
~TempVar()
{
if(m_owned && isValid())
{
GlobalVariable::remove(m_name);
}
}

bool isValid() const {return GlobalVariable::exists(m_name);}
string getName() const {return m_name;}
bool set(double value) {return GlobalVariable::set(m_name,value);}
double get() const {return GlobalVariable::get(m_name);}
bool setOn(double value,double check) {return GlobalVariable::setOn(m_name,value,check);}
datetime lastAccess() const {return GlobalVariable::lastAccess(m_name);}
bool isValid() const
{
return GlobalVariable::exists(m_name);
}
string getName() const
{
return m_name;
}
bool set(double value)
{
return GlobalVariable::set(m_name,value);
}
double get() const
{
return GlobalVariable::get(m_name);
}
bool setOn(double value,double check)
{
return GlobalVariable::setOn(m_name,value,check);
}
datetime lastAccess() const
{
return GlobalVariable::lastAccess(m_name);
}
};
//+------------------------------------------------------------------+
//| |
Expand All @@ -77,7 +137,10 @@ public:
set(initial);
}
long increment(long by=1);
long decrement(long by=1) {return increment(-by);}
long decrement(long by=1)
{
return increment(-by);
}
};
//+------------------------------------------------------------------+
//| |
Expand All @@ -103,7 +166,10 @@ private:
TempVar m_var;
public:
Semaphore(string name,long initial=0);
bool isValid() const {return m_var.isValid();}
bool isValid() const
{
return m_var.isValid();
}
bool acquire();
void release();
};
Expand All @@ -127,7 +193,8 @@ bool Semaphore::acquire(void)
do
{
long value=(long)m_var.get();
if(value == 0) return false;
if(value == 0)
return false;
success=m_var.setOn(value-1,value);
}
while(!success && !IsStopped());
Expand Down Expand Up @@ -168,22 +235,46 @@ class CriticalSection
private:
const string m_name;
public:
CriticalSection(string name):m_name(name){}
CriticalSection(string name):m_name(name) {}

bool isValid() const {return m_name!=NULL;}
string getName() const {return m_name;}
bool isValid() const
{
return m_name!=NULL;
}
string getName() const
{
return m_name;
}

void enter() { while(!GlobalVariable::makeTemp(m_name) && !IsStopped())Sleep(100); }
bool tryEnter() { return GlobalVariable::makeTemp(m_name); }
void leave() { GlobalVariable::remove(m_name);}
void enter()
{
while(!GlobalVariable::makeTemp(m_name) && !IsStopped())
Sleep(100);
}
bool tryEnter()
{
return GlobalVariable::makeTemp(m_name);
}
void leave()
{
GlobalVariable::remove(m_name);
}
};
//+------------------------------------------------------------------+
//| HandleManager should implement 2 methods: create & destroy |
//+------------------------------------------------------------------+
template<typename T>
interface HandleManager
{
T create();
void destroy(T);
};
//+------------------------------------------------------------------+
//| A reference counted global pointer (or handle) |
//| Generic type parameter T can be long or int depending the handle |
//| length (64bit or 32bit) |
//| HandleManager should implement 2 static methods: create & destroy|
//+------------------------------------------------------------------+
template<typename T,typename HandleManager>
template<typename T,typename HM>
class GlobalHandle
{
private:
Expand All @@ -192,12 +283,15 @@ private:
string m_counterName;
protected:
T m_ref;
HandleManager<T> *m_hm;
public:
GlobalHandle(string sharedKey=NULL):m_cs(sharedKey)
{
m_refName=m_cs.getName()+"_Ref";
m_counterName=m_cs.getName()+"_Count";
if(!m_cs.isValid()) m_ref=HandleManager::create();
m_hm = new HM;

This comment has been minimized.

Copy link
@hisham-ash

hisham-ash Jul 2, 2020

I'm getting an error here only. '=' - type mismatch GlobalVariable.mqh

Is there anyway to fix this. Sorry i really don't understand MQL.

This comment has been minimized.

Copy link
@dingmaotu

dingmaotu Jul 2, 2020

Author Owner

Hi @hisham-ash can you provide how did you use this lib? Better yet, you can create an issue, so I can help you. The error depends on what type you passed as the HM template parameter.

if(!m_cs.isValid())
m_ref = m_hm.create();
else
{
m_cs.enter();
Expand All @@ -208,7 +302,7 @@ public:
}
if(long(GlobalVariable::get(m_counterName))==0)
{
m_ref=HandleManager::create();
m_ref = m_hm.create();
if(!GlobalVariable::exists(m_refName))
{
GlobalVariable::makeTemp(m_refName);
Expand All @@ -225,18 +319,28 @@ public:
}
~GlobalHandle()
{
if(!m_cs.isValid()) {HandleManager::destroy(m_ref); return;}
m_cs.enter();
GlobalVariable::set(m_counterName,GlobalVariable::get(m_counterName)-1);
if(long(GlobalVariable::get(m_counterName))==0)
if(!m_cs.isValid())
{
HandleManager::destroy(m_ref);
GlobalVariable::remove(m_refName);
GlobalVariable::remove(m_counterName);
m_hm.destroy(m_ref);
}
m_cs.leave();
else
{
m_cs.enter();
GlobalVariable::set(m_counterName,GlobalVariable::get(m_counterName)-1);
if(long(GlobalVariable::get(m_counterName))==0)
{
m_hm.destroy(m_ref);
GlobalVariable::remove(m_refName);
GlobalVariable::remove(m_counterName);
}
m_cs.leave();
}
delete m_hm;
}

T ref() const {return m_ref;}
T ref() const
{
return m_ref;
}
};
//+------------------------------------------------------------------+

0 comments on commit 1d00831

Please sign in to comment.