/* PIP - Platform Independent Primitives Variable, Struct (simple serialization) Copyright (C) 2011 Ivan Pelipenko peri4ko@gmail.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef PIVARIABLE_H #define PIVARIABLE_H #include "piconfig.h" class PIVariant { friend class PIVariable; public: enum Type {Bool, Char, Short, Int, Long, LLong, UChar, UShort, UInt, ULong, ULLong, Float, Double, LDouble, String, StringList}; PIVariant() {setValue(0.);} PIVariant(const char * v) {setValue(v);} PIVariant(const bool & v) {setValue(v);} PIVariant(const char & v) {setValue(v);} PIVariant(const short & v) {setValue(v);} PIVariant(const int & v) {setValue(v);} PIVariant(const long & v) {setValue(v);} PIVariant(const llong & v) {setValue(v);} PIVariant(const uchar & v) {setValue(v);} PIVariant(const ushort & v) {setValue(v);} PIVariant(const uint & v) {setValue(v);} PIVariant(const ulong & v) {setValue(v);} PIVariant(const ullong & v) {setValue(v);} PIVariant(const float & v) {setValue(v);} PIVariant(const double & v) {setValue(v);} PIVariant(const ldouble & v) {setValue(v);} PIVariant(const PIString & v) {setValue(v);} PIVariant(const PIStringList & v) {setValue(v);} inline void setValue(const char * v) {setValue(PIString(v));} inline void setValue(const bool & v) {type = PIVariant::Bool; vBool = v;} inline void setValue(const char & v) {type = PIVariant::Char; vChar = v;} inline void setValue(const short & v) {type = PIVariant::Short; vShort = v;} inline void setValue(const int & v) {type = PIVariant::Int; vInt = v;} inline void setValue(const long & v) {type = PIVariant::Long; vLong = v;} inline void setValue(const llong & v) {type = PIVariant::LLong; vLLong = v;} inline void setValue(const uchar & v) {type = PIVariant::UChar; vUChar = v;} inline void setValue(const ushort & v) {type = PIVariant::UShort; vUShort = v;} inline void setValue(const uint & v) {type = PIVariant::UInt; vUInt = v;} inline void setValue(const ulong & v) {type = PIVariant::ULong; vULong = v;} inline void setValue(const ullong & v) {type = PIVariant::ULLong; vULLong = v;} inline void setValue(const float & v) {type = PIVariant::Float; vFloat = v;} inline void setValue(const double & v) {type = PIVariant::Double; vDouble = v;} inline void setValue(const ldouble & v) {type = PIVariant::LDouble; vLDouble = v;} inline void setValue(const PIString & v) {type = PIVariant::String; vString = v;} inline void setValue(const PIStringList & v) {type = PIVariant::StringList; vStringList = v;} void setValueOnly(const PIString & v); inline PIString typeName() const {return PIVariant::toString(type);} inline double doubleValue() const {return PIVariant::variableValue(&vChar, type);} PIString stringValue() const; inline void typeFromString(const PIString & str) {type = PIVariant::fromString(str);} inline PIString typeToString() const {return PIVariant::toString(type);} inline uint size() {if (type != PIVariant::String && type != PIVariant::StringList) return PIVariant::variableSize(type); if (type == PIVariant::String) return vString.size(); else return vStringList.contentSize();} inline PIString writeToString() const {return typeName() + ":" + stringValue();} #ifdef QNX inline void operator =(const PIVariant & v) {type = v.type; vLDouble = v.vLDouble; vString = v.vString; vStringList = v.vStringList;} #endif inline void operator =(const char * v) {setValue(PIString(v));} inline void operator =(const bool & v) {type = PIVariant::Bool; vBool = v;} inline void operator =(const char & v) {type = PIVariant::Char; vChar = v;} inline void operator =(const short & v) {type = PIVariant::Short; vShort = v;} inline void operator =(const int & v) {type = PIVariant::Int; vInt = v;} inline void operator =(const long & v) {type = PIVariant::Long; vLong = v;} inline void operator =(const llong & v) {type = PIVariant::LLong; vLLong = v;} inline void operator =(const uchar & v) {type = PIVariant::UChar; vUChar = v;} inline void operator =(const ushort & v) {type = PIVariant::UShort; vUShort = v;} inline void operator =(const uint & v) {type = PIVariant::UInt; vUInt = v;} inline void operator =(const ulong & v) {type = PIVariant::ULong; vULong = v;} inline void operator =(const ullong & v) {type = PIVariant::ULLong; vULLong = v;} inline void operator =(const float & v) {type = PIVariant::Float; vFloat = v;} inline void operator =(const double & v) {type = PIVariant::Double; vDouble = v;} inline void operator =(const ldouble & v) {type = PIVariant::LDouble; vLDouble = v;} inline void operator =(const PIString & v) {type = PIVariant::String; vString = v;} inline void operator =(const PIStringList & v) {type = PIVariant::StringList; vStringList = v;} bool operator ==(const PIVariant & v) const; inline bool operator !=(const PIVariant & v) const {return !(*this == v);} PIVariant::Type type; union { bool vBool; 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; }; PIString vString; PIStringList vStringList; static PIVariant readFromString(const PIString & s); private: static PIVariant::Type fromString(const PIString & str); static PIString toString(const PIVariant::Type & var); static uint variableSize(const PIVariant::Type & var); static double variableValue(const char * var_ptr, const PIVariant::Type & var); }; inline std::ostream & operator <<(std::ostream & s, const PIVariant & v) {s << v.typeName() << ": " << v.stringValue(); return s;} class PIVariable { public: PIVariable() {;} PIVariable(const PIString & str) {setVariable(str);} ~PIVariable() {;} void setVariable(const PIString & str); void writeVariable(char * dest); inline void readVariable(const char * var_ptr) {value_ = PIVariant::variableValue((char * )((long)var_ptr + offset), type_);} inline PIVariant::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; private: PIVariant::Type type_; uint size_; PIString name_; double value_; }; /* * PIStruct is abstract structure, described by *.conf file with format of each line: * " = # ". * 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_; PIVector vars; }; #endif // PIVARIABLE_H #/** PhEDIT attribute block #-11:16777215 #0:7338:monospace10:-3:-3:0 #** PhEDIT attribute block ends (-0000117)**/