/*
    PIP - Platform Independent Primitives
    Variable, Struct (simple serialization)
    Copyright (C) 2013  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 <http://www.gnu.org/licenses/>.
*/

#ifndef PIVARIABLE_H
#define PIVARIABLE_H

#include "piconfig.h"

class PIP_EXPORT 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);}

	void setValue(const char * v) {setValue(PIString(v));}
	void setValue(const bool & v) {type = PIVariant::Bool; vBool = v;}
	void setValue(const char & v) {type = PIVariant::Char; vChar = v;}
	void setValue(const short & v) {type = PIVariant::Short; vShort = v;}
	void setValue(const int & v) {type = PIVariant::Int; vInt = v;}
	void setValue(const long & v) {type = PIVariant::Long; vLong = v;}
	void setValue(const llong & v) {type = PIVariant::LLong; vLLong = v;}
	void setValue(const uchar & v) {type = PIVariant::UChar; vUChar = v;}
	void setValue(const ushort & v) {type = PIVariant::UShort; vUShort = v;}
	void setValue(const uint & v) {type = PIVariant::UInt; vUInt = v;}
	void setValue(const ulong & v) {type = PIVariant::ULong; vULong = v;}
	void setValue(const ullong & v) {type = PIVariant::ULLong; vULLong = v;}
	void setValue(const float & v) {type = PIVariant::Float; vFloat = v;}
	void setValue(const double & v) {type = PIVariant::Double; vDouble = v;}
	void setValue(const ldouble & v) {type = PIVariant::LDouble; vLDouble = v;}
	void setValue(const PIString & v) {type = PIVariant::String; vString = v;}
	void setValue(const PIStringList & v) {type = PIVariant::StringList; vStringList = v;}
	void setValueOnly(const PIString & v);
	PIString typeName() const {return PIVariant::toString(type);}
	double doubleValue() const {return PIVariant::variableValue(&vChar, type);}
	PIString stringValue() const;
	void typeFromString(const PIString & str) {type = PIVariant::fromString(str);}
	PIString typeToString() const {return PIVariant::toString(type);}
	uint size() {if (type != PIVariant::String && type != PIVariant::StringList) return PIVariant::variableSize(type); if (type == PIVariant::String) return vString.size(); else return vStringList.contentSize();}
	PIString writeToString() const {return typeName() + ":" + stringValue();}

#ifdef QNX
	void operator =(const PIVariant & v) {type = v.type; vLDouble = v.vLDouble; vString = v.vString; vStringList = v.vStringList;}
#endif
	void operator =(const char * v) {setValue(PIString(v));}
	void operator =(const bool & v) {type = PIVariant::Bool; vBool = v;}
	void operator =(const char & v) {type = PIVariant::Char; vChar = v;}
	void operator =(const short & v) {type = PIVariant::Short; vShort = v;}
	void operator =(const int & v) {type = PIVariant::Int; vInt = v;}
	void operator =(const long & v) {type = PIVariant::Long; vLong = v;}
	void operator =(const llong & v) {type = PIVariant::LLong; vLLong = v;}
	void operator =(const uchar & v) {type = PIVariant::UChar; vUChar = v;}
	void operator =(const ushort & v) {type = PIVariant::UShort; vUShort = v;}
	void operator =(const uint & v) {type = PIVariant::UInt; vUInt = v;}
	void operator =(const ulong & v) {type = PIVariant::ULong; vULong = v;}
	void operator =(const ullong & v) {type = PIVariant::ULLong; vULLong = v;}
	void operator =(const float & v) {type = PIVariant::Float; vFloat = v;}
	void operator =(const double & v) {type = PIVariant::Double; vDouble = v;}
	void operator =(const ldouble & v) {type = PIVariant::LDouble; vLDouble = v;}
	void operator =(const PIString & v) {type = PIVariant::String; vString = v;}
	void operator =(const PIStringList & v) {type = PIVariant::StringList; vStringList = v;}

	bool operator ==(const PIVariant & v) const;
	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 void * var_ptr, const PIVariant::Type & var);

};

inline std::ostream & operator <<(std::ostream & s, const PIVariant & v) {s << v.typeName() << ": " << v.stringValue(); return s;}

class PIP_EXPORT PIVariable {
public:
	PIVariable() {;}
	PIVariable(const PIString & str) {setVariable(str);}
	~PIVariable() {;}

	void setVariable(const PIString & str);
	void writeVariable(void * dest);
	void readVariable(const void * var_ptr) {value_ = PIVariant::variableValue((char * )((long)var_ptr + offset), type_);}
	PIVariant::Type type() const {return type_;}
	uint size() const {return size_;}
	const PIString & name() {return name_;}
	void setName(const PIString & str) {name_ = str;}
	double value() const {return value_;}
	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:
 * "<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 PIP_EXPORT PIStruct {
public:
	PIStruct() {;}
	PIStruct(const PIString & str) {parseFile(str);}

	void parseFile(const PIString & file);
	void readData(const void * data);
	void writeData(void * data);
	void clear() {vars.clear(); size_ = 0;}
	uint count() const {return vars.size();}
	uint size() const {return size_;}
	const PIString & name() {return name_;}
	void setName(const PIString & str) {name_ = str;}
	PIVariable & operator[](const uint & index) {return vars[index];}
	PIVariable & operator[](const PIString & name) {for (uint i = 0; i < vars.size(); ++i) if (vars[i].name() == name) return vars[i]; return def;}

private:
	uint size_;
	PIString name_;
	PIVariable def;
	PIVector<PIVariable> vars;

};

#endif // PIVARIABLE_H
