Files
pip/pivariable.h

98 lines
2.6 KiB
C++

#ifndef PIVARIABLE_H
#define PIVARIABLE_H
#include "piconfig.h"
class PIVariable {
public:
PIVariable() {;}
PIVariable(const PIString & str) {setVariable(str);}
~PIVariable() {;}
enum Type {Char, Short, Int, Long, LLong, UChar, UShort, UInt, ULong, ULLong, Float, Double, LDouble, Bool};
typedef struct {
union {
char vChar;
short vShort;
int vInt;
long vLong;
llong vLLong;
uchar vUChar;
ushort vUShort;
uint vUInt;
ulong vULong;
ullong vULLong;
float vFloat;
double vDouble;
ldouble vLDouble;
bool vBool;
};
PIVariable::Type type;
} PIVariant;
void setVariable(const PIString & str);
void writeVariable(char * dest);
inline void readVariable(const char * var_ptr) {value_ = PIVariable::variableValue((char * )((int)var_ptr + offset), type_);}
inline PIVariable::Type type() const {return type_;}
inline uint size() const {return size_;}
inline const PIString & name() {return name_;}
inline void setName(const PIString & str) {name_ = str;}
inline double value() const {return value_;}
inline void setValue(const double & val) {value_ = val;}
int offset;
static PIVariable::Type fromString(const PIString & str);
static PIString toString(const PIVariable::Type & var);
static uint variableSize(const PIVariable::Type & var);
static double variableValue(const char * var_ptr, const PIVariable::Type & var);
private:
PIVariable::Type type_;
uint size_;
PIString name_;
double value_;
};
/*
* PIStruct is abstract structure, described by *.conf file with format of each line:
* "<name> = <type> #<n|f|b> <start_value>".
* e.g. "pi = double #f 3.1418"
*
* You can write or read binary content of this struct
* by functions "writeData" and "readData", e.g.
* "char * data = new char[struct.size()];
* struct.writeData(data);"
*
* Access to each variable in struct is looks like
* "double value = struct["pi"].value();"
*/
class PIStruct {
public:
PIStruct() {;}
PIStruct(const PIString & str) {parseFile(str);}
void parseFile(const PIString & file);
void readData(const char * data);
void writeData(char * data);
inline void clear() {vars.clear(); size_ = 0;}
inline uint count() const {return vars.size();}
inline uint size() const {return size_;}
inline const PIString & name() {return name_;}
inline void setName(const PIString & str) {name_ = str;}
inline PIVariable & operator[](const uint & index) {return vars[index];}
inline PIVariable & operator[](const PIString & name) {for (uint i = 0; i < vars.size(); ++i) if (vars[i].name() == name) return vars[i];}
private:
uint size_;
PIString name_;
vector<PIVariable> vars;
};
#endif // PIVARIABLE_H