More changes!
Since I need to protect each variable i've defined a template class to create a single protected variable:
#ifndef PROTECTEDVARIABLE_H
#define PROTECTEDVARIABLE_H
#include <QReadWriteLock>
template <class T> class ProtectedVariable
{
public:
ProtectedVariable(T=0);
void set(T);
T get();
private:
T value;
};
template <class T> ProtectedVariable<T>::ProtectedVariable(T data)
{
set(data);
}
template <class T> void ProtectedVariable<T>::set(T value)
{
this->value=value;
}
template <class T> T ProtectedVariable<T>::get()
{
return value;
}
#endif // PROTECTEDVARIABLE_H
#ifndef PROTECTEDVARIABLE_H
#define PROTECTEDVARIABLE_H
#include <QReadWriteLock>
template <class T> class ProtectedVariable
{
public:
ProtectedVariable(T=0);
void set(T);
T get();
private:
T value;
QReadWriteLock lock;
};
template <class T> ProtectedVariable<T>::ProtectedVariable(T data)
{
set(data);
}
template <class T> void ProtectedVariable<T>::set(T value)
{
QWriteLocker wlocker(&lock);
this->value=value;
}
template <class T> T ProtectedVariable<T>::get()
{
QReadLocker rlocker(&lock);
return value;
}
#endif // PROTECTEDVARIABLE_H
To copy to clipboard, switch view to plain text mode
Then in the data container I create the different variables.
#include <protectedvariable.h>
class DataTable
{
public:
ProtectedVariable<int> data;
ProtectedVariable<double> differentData;
}
#include <protectedvariable.h>
class DataTable
{
public:
ProtectedVariable<int> data;
ProtectedVariable<double> differentData;
}
To copy to clipboard, switch view to plain text mode
Now the question, when I access the variables as
DataTable dt;
dt.data.set(5);
DataTable dt;
dt.data.set(5);
To copy to clipboard, switch view to plain text mode
Is my variable still really protected?
Bookmarks