/*! \file picodeinfo.h
 * \brief C++ code info structs
*/
/*
	PIP - Platform Independent Primitives
	C++ code info structs
	Ivan Pelipenko peri4ko@yandex.ru

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser 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 Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef PICODEINFO_H
#define PICODEINFO_H

#include "pistringlist.h"

class PIVariant;

namespace PICodeInfo {

enum TypeFlag {
	NoFlag,
	Const = 0x01,
	Static = 0x02,
	Mutable = 0x04,
	Volatile = 0x08,
	Inline = 0x10,
	Virtual = 0x20,
	Extern = 0x40
};

typedef PIFlags<PICodeInfo::TypeFlag> TypeFlags;
typedef PIMap<PIString, PIString> MetaMap;
typedef PIByteArray(*AccessValueFunction)(const void *, const char *);
typedef const char*(*AccessTypeFunction)(const char *);

struct PIP_EXPORT TypeInfo {
	TypeInfo(const PIString & n = PIString(), const PIString & t = PIString(), PICodeInfo::TypeFlags f = 0, int b = -1) {name = n; type = t; flags = f; bits = b;}
	bool isBitfield() const {return bits > 0;}
	MetaMap meta;
	PIString name;
	PIString type;
	PICodeInfo::TypeFlags flags;
	int bits;
};

struct PIP_EXPORT FunctionInfo {
	MetaMap meta;
	PIString name;
	TypeInfo return_type;
	PIVector<PICodeInfo::TypeInfo> arguments;
};

struct PIP_EXPORT ClassInfo {
	ClassInfo() {has_name = true;}
	MetaMap meta;
	bool has_name;
	PIString type;
	PIString name;
	PIStringList parents;
	PIVector<PICodeInfo::TypeInfo> variables;
	PIVector<PICodeInfo::FunctionInfo> functions;
	PIVector<PICodeInfo::ClassInfo * > children_info;
};

struct PIP_EXPORT EnumeratorInfo {
	EnumeratorInfo(const PIString & n = PIString(), int v = 0) {name = n; value = v;}
	MetaMap meta;
	PIString name;
	int value;
};

struct PIP_EXPORT EnumInfo {
	PIString memberName(int value) const;
	int memberValue(const PIString & name) const;
	MetaMap meta;
	PIString name;
	PIVector<PICodeInfo::EnumeratorInfo> members;
};


inline PICout operator <<(PICout s, const PICodeInfo::TypeInfo & v) {
	if (v.flags[Inline]) s << "inline ";
	if (v.flags[Virtual]) s << "virtual ";
	if (v.flags[Mutable]) s << "mutable ";
	if (v.flags[Volatile]) s << "volatile ";
	if (v.flags[Static]) s << "static ";
	if (v.flags[Const]) s << "const ";
	s << v.type;
	if (!v.name.isEmpty())
		s << " " << v.name;
	return s;
}

inline PICout operator <<(PICout s, const PICodeInfo::EnumeratorInfo & v) {s << v.name << " = " << v.value << " Meta" << v.meta; return s;}

inline PICout operator <<(PICout s, const PICodeInfo::ClassInfo & v) {
	s.setControl(0, true);
	s << "class " << v.name;
	if (!v.parents.isEmpty()) {
		s << ": ";
		bool first = true;
		piForeachC (PIString & i, v.parents) {
			if (first) first = false;
			else s << ", ";
			s << i;
		}
	}
	s << " Meta" << v.meta << " {\n";
	piForeachC (FunctionInfo & i, v.functions) {
		s << PICoutManipulators::Tab << i.return_type << " " << i.name << "(";
		bool fa = true;
		piForeachC (TypeInfo & a, i.arguments) {
			if (fa) fa = false;
			else s << ", ";
			s << a;
		}
		s << ") Meta" << i.meta << ";\n";
	}
	if (!v.functions.isEmpty() && !v.variables.isEmpty())
		s << "\n";
	piForeachC (TypeInfo & i, v.variables) {
		s << PICoutManipulators::Tab << i << " Meta" << i.meta << ";\n";
	}
	s << "}\n";
	s.restoreControl();
	return s;
}

inline PICout operator <<(PICout s, const PICodeInfo::EnumInfo & v) {
	s.setControl(0, true);
	s << "enum " << v.name << " Meta" << v.meta << " {\n";
	piForeachC (EnumeratorInfo & i, v.members) {
		bool f = true;
		if (f) f = false;
		else s << ", ";
		s << PICoutManipulators::Tab << i << "\n";
	}
	s << "}\n";
	s.restoreControl();
	return s;
}

extern PIP_EXPORT PIMap<PIString, PICodeInfo::ClassInfo * > * classesInfo;
extern PIP_EXPORT PIMap<PIString, PICodeInfo::EnumInfo * > * enumsInfo;
extern PIP_EXPORT PIMap<PIString, PICodeInfo::AccessValueFunction> * accessValueFunctions;
extern PIP_EXPORT PIMap<PIString, PICodeInfo::AccessTypeFunction> * accessTypeFunctions;

inline PIByteArray getMemberValue(const void * p, const char * class_name, const char * member_name) {
	if (!p || !class_name || !member_name || !accessValueFunctions) return PIByteArray();
	AccessValueFunction af = accessValueFunctions->value(PIStringAscii(class_name), (AccessValueFunction)0);
	if (!af) return PIByteArray();
	return af(p, member_name);
}

inline const char * getMemberType(const char * class_name, const char * member_name) {
	if (!class_name || !member_name || !accessTypeFunctions) return "";
	AccessTypeFunction af = accessTypeFunctions->value(PIStringAscii(class_name), (AccessTypeFunction)0);
	if (!af) return "";
	return af(member_name);
}

PIP_EXPORT PIVariant getMemberAsVariant(const void * p, const char * class_name, const char * member_name);


template<typename T, typename std::enable_if< std::is_assignable<T&, const T&>::value, int>::type = 0>
void serialize(PIByteArray & ret, const T & v) {ret << v;}

template<typename T, typename std::enable_if<!std::is_assignable<T&, const T&>::value, int>::type = 0>
void serialize(PIByteArray & ret, const T & v) {}

}

class PIP_EXPORT __PICodeInfoInitializer__ {
public:
	__PICodeInfoInitializer__() {
		if (_inited_) return;
		_inited_ = true;
		PICodeInfo::classesInfo = new PIMap<PIString, PICodeInfo::ClassInfo * >;
		PICodeInfo::enumsInfo = new PIMap<PIString, PICodeInfo::EnumInfo * >;
		PICodeInfo::accessValueFunctions = new PIMap<PIString, PICodeInfo::AccessValueFunction>;
		PICodeInfo::accessTypeFunctions = new PIMap<PIString, PICodeInfo::AccessTypeFunction>;
	}
	static bool _inited_;
};

static __PICodeInfoInitializer__ __picodeinfoinitializer__;

#endif // PICODEINFO_H
