Page MenuHomec4science

syncvar.h
No OneTemporary

File Metadata

Created
Thu, May 16, 14:15

syncvar.h

#ifndef SYNCVAR_H
#define SYNCVAR_H
#include <stdexcept>
#include <QString>
#include <QList>
#include "../Firmware/src/definitions.h"
typedef comm_VarType VarType;
typedef comm_VarAccess VarAccess;
template<typename T>
class SyncVarPointer;
template<typename T>
T& operator*(SyncVarPointer<T> &svp);
class HriBoard;
/** @defgroup SyncVar SyncVar
* @brief Set of classes to handle SyncVar operations conveniently.
*
* First, get the list of the SyncVars from the HRI board, by creating a
* HriBoard object and calling openLink(). The given slot function will be
* called when the SyncVars list will be received. Then, it is possible to
* create and associate SyncVarPointers to manipulate these SyncVars.
*
* A local SyncVar can be accessed from a SyncVarPointer, simply using the *
* operator. To actually synchronize this value with the one of the board, call
* writeRemoteVar() or readRemoteVar().
*
* @addtogroup SyncVar
* @{
*/
/**
* @brief Variable that can be synchronized with its HRI board counterpart.
*/
class SyncVar
{
template<typename T>
friend T& operator*(SyncVarPointer<T> &svp);
public:
SyncVar(int index, QString name,
VarType type, VarAccess access);
int getIndex() const;
QString getName() const;
int getSize() const;
QByteArray getData() const;
void setData(QByteArray newData);
bool isUpToDate() const;
void setOutOfDate();
private:
QByteArray data; ///< SyncVar value, stored as raw bytes.
int index; ///< Index of the SyncVar in the list.
QString name; ///< Name describing the SyncVar.
VarType type; ///< Type of variable.
VarAccess access; ///< Access rights of the variable.
bool upToDate; ///< Indicates whether the local value is up-to-date or not.
};
/**
* @brief Generic version of SyncVarPointer.
* @remark This generic version of SyncVarPointer should not be instanced, use
* SyncVarPointer instead.
*/
class SyncVarPointerBase
{
public:
SyncVarPointerBase();
void associate(HriBoard *hriBoard, SyncVar *syncVar);
bool isValid() const;
SyncVar* getVar() const;
void readValue();
void writeValue();
protected:
HriBoard* hriBoard; ///< Pointer to the HRI board object.
SyncVar* syncVar; ///< Pointer to the associated SyncVar.
};
/**
* @brief Typed handle to a SyncVar, for easy value manipulation.
* Using the value of the SyncVars is tedious, since their data array must be
* casted. SyncVarPointer is a typed object to handle easily a SyncVar value.
*
* First, create SyncVarPointer of the same type that the SyncVar to be
* manipulated. Then, associate it to the desired variable with associate().
*
* To access the SyncVar value, just use the * operator, like a pointer.
* To synchronize the local SyncVar value with the one on the board, call
* readValue() or writeValue().
*/
template<typename T>
class SyncVarPointer : public SyncVarPointerBase
{
};
/**
* @brief Gets a typed reference to the SyncVar data, from a SyncVarPointer.
* @param svp the SyncVarPointer to get the SyncVar value from.
* @return a reference to the value of the SyncVar.
*/
template<typename T>
T& operator*(SyncVarPointer<T> &svp)
{
if (svp.isValid())
return (T&)*(T*)svp.getVar()->data.data();
else
throw std::runtime_error("The SyncVar pointer does not have an associated SyncVar.");
}
/**
* @}
*/
#endif // SYNCVAR_H

Event Timeline