/*! \file piobject.h
 * \brief Base object
 * 
 * This file declare PIObject class and associated macros
*/
/*
    PIP - Platform Independent Primitives
    Object, base class of some PIP classes, provide EVENT -> EVENT_HANDLER mechanism
    Copyright (C) 2014  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 PIOBJECT_H
#define PIOBJECT_H

#include "pivariant.h"
#include "pimutex.h"


#ifdef DOXYGEN


/// \relatesalso PIObject \brief you should use this macro after class declaration to use EVENT and EVENT_HANDLER and correct piCoutObj output
#define PIOBJECT(name)


/// \relatesalso PIObject \brief declare event handler \"event\" with name \"name\" and return type \"ret\", ret name()
#define EVENT_HANDLER0(ret, name) ret name()

/// \relatesalso PIObject \brief declare event handler \"event\" with name \"name\" and return type \"ret\", ret name(type0 var0)
#define EVENT_HANDLER1(ret, name, type0, var0) ret name(type0 var0)

/// \relatesalso PIObject \brief declare event handler \"event\" with name \"name\" and return type \"ret\", ret name(type0 var0, type1 var1)
#define EVENT_HANDLER2(ret, name, type0, var0, type1, var1) ret name(type0 var0, type1 var1)

/// \relatesalso PIObject \brief declare event handler \"event\" with name \"name\" and return type \"ret\", ret name(type0 var0, type1 var1, type2 var2)
#define EVENT_HANDLER3(ret, name, type0, var0, type1, var1, type2, var2) ret name(type0 var0, type1 var1, type2 var2)

/// \relatesalso PIObject \brief declare event handler \"event\" with name \"name\" and return type \"ret\", ret name(type0 var0, type1 var1, type2 var2, type3 var3)
#define EVENT_HANDLER4(ret, name, type0, var0, type1, var1, type2, var2, type3, var3) ret name(type0 var0, type1 var1, type2 var2, type3 var3)

/// \relatesalso PIObject \brief EVENT_HANDLER is synonym of EVENT_HANDLER0
#define EVENT_HANDLER EVENT_HANDLER0


/// \relatesalso PIObject \brief declare virtual event handler \"event\" with name \"name\" and return type \"ret\", virtual ret name()
#define EVENT_VHANDLER0(ret, name) virtual ret name()

/// \relatesalso PIObject \brief declare virtual event handler \"event\" with name \"name\" and return type \"ret\", virtual ret name(type0 var0)
#define EVENT_VHANDLER1(ret, name, type0, var0) virtual ret name(type0 var0)

/// \relatesalso PIObject \brief declare virtual event handler \"event\" with name \"name\" and return type \"ret\", virtual ret name(type0 var0, type1 var1)
#define EVENT_VHANDLER2(ret, name, type0, var0, type1, var1) virtual ret name(type0 var0, type1 var1)

/// \relatesalso PIObject \brief declare virtual event handler \"event\" with name \"name\" and return type \"ret\", virtual ret name(type0 var0, type1 var1, type2 var2)
#define EVENT_VHANDLER3(ret, name, type0, var0, type1, var1, type2, var2) virtual ret name(type0 var0, type1 var1, type2 var2)

/// \relatesalso PIObject \brief declare virtual event handler \"event\" with name \"name\" and return type \"ret\", virtual ret name(type0 var0, type1 var1, type2 var2, type3 var3)
#define EVENT_VHANDLER4(ret, name, type0, var0, type1, var1, type2, var2, type3, var3) virtual ret name(type0 var0, type1 var1, type2 var2, type3 var3)

/// \relatesalso PIObject \brief EVENT_VHANDLER is synonym of EVENT_VHANDLER0
#define EVENT_VHANDLER EVENT_VHANDLER0


/// \relatesalso PIObject \brief declare event \"event\" with name \"name\", void name();
#define EVENT0(name) void name();

/// \relatesalso PIObject \brief declare event \"event\" with name \"name\", void name(type0 var0);
#define EVENT1(name, type0, var0) void name(type0 var0);

/// \relatesalso PIObject \brief declare event \"event\" with name \"name\", void name(type0 var0, type1 var1);
#define EVENT2(name, type0, var0, type1, var1) void name(type0 var0, type1 var1);

/// \relatesalso PIObject \brief declare event \"event\" with name \"name\", void name(type0 var0, type1 var1, type2 var2);
#define EVENT3(name, type0, var0, type1, var1, type2, var2) void name(type0 var0, type1 var1, type2 var2);

/// \relatesalso PIObject \brief declare event \"event\" with name \"name\", void name(type0 var0, type1 var1, type2 var2, type3 var3);
#define EVENT4(name, type0, var0, type1, var1, type2, var2, type3, var3) void name(type0 var0, type1 var1, type2 var2, type3 var3);

/// \relatesalso PIObject \brief EVENT is synonym of EVENT0
#define EVENT EVENT0


#define RAISE_EVENT0(src, event)
#define RAISE_EVENT1(src, event, v0)
#define RAISE_EVENT2(src, event, v0, v1)
#define RAISE_EVENT3(src, event, v0, v1, v2)
#define RAISE_EVENT4(src, event, v0, v1, v2, v3)
#define RAISE_EVENT RAISE_EVENT0


/// \relatesalso PIObject \brief connect event \"event\" from object \"src\"  to  event handler \"handler\" with return type \"ret\" from object \"dest\" with check of event and handler exists
#define CONNECT0(ret, src, event, dest, handler)

/// \relatesalso PIObject \brief connect event \"event\" from object \"src\"  to  event handler \"handler\" with return type \"ret\" from object \"dest\" with check of event and handler exists
#define CONNECT1(ret, type0, src, event, dest, handler)

/// \relatesalso PIObject \brief connect event \"event\" from object \"src\"  to  event handler \"handler\" with return type \"ret\" from object \"dest\" with check of event and handler exists
#define CONNECT2(ret, type0, type1, src, event, dest, handler)

/// \relatesalso PIObject \brief connect event \"event\" from object \"src\"  to  event handler \"handler\" with return type \"ret\" from object \"dest\" with check of event and handler exists
#define CONNECT3(ret, type0, type1, type2, src, event, dest, handler)

/// \relatesalso PIObject \brief connect event \"event\" from object \"src\"  to  event handler \"handler\" with return type \"ret\" from object \"dest\" with check of event and handler exists
#define CONNECT4(ret, type0, type1, type2, type3, src, event, dest, handler)

/// \relatesalso PIObject \brief CONNECT is synonym of CONNECT0
#define CONNECT CONNECT0


/// \relatesalso PIObject \brief connect event \"event\" from object \"src\"  to  event handler \"handler\" with return type \"ret\" from object \"dest\" without check of event exists
#define WEAK_CONNECT0(ret, src, event, dest, handler)

/// \relatesalso PIObject \brief connect event \"event\" from object \"src\"  to  event handler \"handler\" with return type \"ret\" from object \"dest\" without check of event exists
#define WEAK_CONNECT1(ret, type0, src, event, dest, handler)

/// \relatesalso PIObject \brief connect event \"event\" from object \"src\"  to  event handler \"handler\" with return type \"ret\" from object \"dest\" without check of event exists
#define WEAK_CONNECT2(ret, type0, type1, src, event, dest, handler)

/// \relatesalso PIObject \brief connect event \"event\" from object \"src\"  to  event handler \"handler\" with return type \"ret\" from object \"dest\" without check of event exists
#define WEAK_CONNECT3(ret, type0, type1, type2, src, event, dest, handler)

/// \relatesalso PIObject \brief connect event \"event\" from object \"src\"  to  event handler \"handler\" with return type \"ret\" from object \"dest\" without check of event exists
#define WEAK_CONNECT4(ret, type0, type1, type2, type3, src, event, dest, handler)

/// \relatesalso PIObject \brief WEAK_CONNECT is synonym of WEAK_CONNECT0
#define WEAK_CONNECT WEAK_CONNECT0


/// \relatesalso PIObject \brief piDisconnect event \"event\" from object \"src\"  from  event handler \"handler\" with return type \"ret\" from object \"dest\"
#define DISCONNECT0(ret, src, event, dest, handler)

/// \relatesalso PIObject \brief piDisconnect event \"event\" from object \"src\"  from  event handler \"handler\" with return type \"ret\" from object \"dest\"
#define DISCONNECT1(ret, type0, src, event, dest, handler)

/// \relatesalso PIObject \brief piDisconnect event \"event\" from object \"src\"  from  event handler \"handler\" with return type \"ret\" from object \"dest\"
#define DISCONNECT2(ret, type0, type1, src, event, dest, handler)

/// \relatesalso PIObject \brief piDisconnect event \"event\" from object \"src\"  from  event handler \"handler\" with return type \"ret\" from object \"dest\"
#define DISCONNECT3(ret, type0, type1, type2, src, event, dest, handler)

/// \relatesalso PIObject \brief piDisconnect event \"event\" from object \"src\"  from  event handler \"handler\" with return type \"ret\" from object \"dest\"
#define DISCONNECT4(ret, type0, type1, type2, type3, src, event, dest, handler)

/// \relatesalso PIObject \brief DISCONNECT is synonym of DISCONNECT0
#define DISCONNECT DISCONNECT0


/// \relatesalso PIObject \brief Returns pointer to events handler \"handler\"
#define HANDLER(handler)


#else


#define PIOBJECT(obj) typedef obj __PIObject__; public: virtual const char * className() const {return #obj;} private:

#define EVENT_HANDLER0(ret, name) static ret __stat_eh_##name##__(void * o) {return ((__PIObject__*)o)->name();} ret name()
#define EVENT_HANDLER1(ret, name, a0, n0) static ret __stat_eh_##name##__(void * o, a0 n0) {return ((__PIObject__*)o)->name(n0);} ret name(a0 n0)
#define EVENT_HANDLER2(ret, name, a0, n0, a1, n1) static ret __stat_eh_##name##__(void * o, a0 n0, a1 n1) {return ((__PIObject__*)o)->name(n0, n1);} ret name(a0 n0, a1 n1)
#define EVENT_HANDLER3(ret, name, a0, n0, a1, n1, a2, n2) static ret __stat_eh_##name##__(void * o, a0 n0, a1 n1, a2 n2) {return ((__PIObject__*)o)->name(n0, n1, n2);} ret name(a0 n0, a1 n1, a2 n2)
#define EVENT_HANDLER4(ret, name, a0, n0, a1, n1, a2, n2, a3, n3) static ret __stat_eh_##name##__(void * o, a0 n0, a1 n1, a2 n2, a3 n3) {return ((__PIObject__*)o)->name(n0, n1, n2, n3);} ret name(a0 n0, a1 n1, a2 n2, a3 n3)
#define EVENT_HANDLER EVENT_HANDLER0

#define EVENT_VHANDLER0(ret, name) static ret __stat_eh_##name##__(void * o) {return ((__PIObject__*)o)->name();} virtual ret name()
#define EVENT_VHANDLER1(ret, name, a0, n0) static ret __stat_eh_##name##__(void * o, a0 n0) {return ((__PIObject__*)o)->name(n0);} virtual ret name(a0 n0)
#define EVENT_VHANDLER2(ret, name, a0, n0, a1, n1) static ret __stat_eh_##name##__(void * o, a0 n0, a1 n1) {return ((__PIObject__*)o)->name(n0, n1);} virtual ret name(a0 n0, a1 n1)
#define EVENT_VHANDLER3(ret, name, a0, n0, a1, n1, a2, n2) static ret __stat_eh_##name##__(void * o, a0 n0, a1 n1, a2 n2) {return ((__PIObject__*)o)->name(n0, n1, n2);} virtual ret name(a0 n0, a1 n1, a2 n2)
#define EVENT_VHANDLER4(ret, name, a0, n0, a1, n1, a2, n2, a3, n3) static ret __stat_eh_##name##__(void * o, a0 n0, a1 n1, a2 n2, a3 n3) {return ((__PIObject__*)o)->name(n0, n1, n2, n3);} virtual ret name(a0 n0, a1 n1, a2 n2, a3 n3)
#define EVENT_VHANDLER EVENT_VHANDLER0

#define EVENT0(name) EVENT_HANDLER0(void, name) {PIObject::raiseEvent(this, #name);}
#define EVENT1(name, a0, n0) EVENT_HANDLER1(void, name, a0, n0) {PIObject::raiseEvent(this, #name, n0);}
#define EVENT2(name, a0, n0, a1, n1) EVENT_HANDLER2(void, name, a0, n0, a1, n1) {PIObject::raiseEvent(this, #name, n0, n1);}
#define EVENT3(name, a0, n0, a1, n1, a2, n2) EVENT_HANDLER3(void, name, a0, n0, a1, n1, a2, n2) {PIObject::raiseEvent(this, #name, n0, n1, n2);}
#define EVENT4(name, a0, n0, a1, n1, a2, n2, a3, n3) EVENT_HANDLER4(void, name, a0, n0, a1, n1, a2, n2, a3, n3) {PIObject::raiseEvent(this, #name, n0, n1, n2, n3);}
#define EVENT EVENT0

#define RAISE_EVENT0(src, event) (src)->event();
#define RAISE_EVENT1(src, event, v0) (src)->event(v0);
#define RAISE_EVENT2(src, event, v0, v1) (src)->event(v0, v1);
#define RAISE_EVENT3(src, event, v0, v1, v2) (src)->event(v0, v1, v2);
#define RAISE_EVENT4(src, event, v0, v1, v2, v3) (src)->event(v0, v1, v2, v3);
#define RAISE_EVENT RAISE_EVENT0

#define CONNECT0(ret, src, event, dest, handler) PIObject::piConnect(src, #event, dest, (void*)(ret(*)(void*))(&(dest)->__stat_eh_##handler##__), (void*)(void(*)(void*))(&(src)->__stat_eh_##event##__));
#define CONNECT1(ret, a0, src, event, dest, handler) PIObject::piConnect(src, #event, dest, (void*)(ret(*)(void*, a0))(&(dest)->__stat_eh_##handler##__), (void*)(void(*)(void*, a0))(&(src)->__stat_eh_##event##__));
#define CONNECT2(ret, a0, a1, src, event, dest, handler) PIObject::piConnect(src, #event, dest, (void*)(ret(*)(void*, a0, a1))(&(dest)->__stat_eh_##handler##__), (void*)(void(*)(void*, a0, a1))(&(src)->__stat_eh_##event##__));
#define CONNECT3(ret, a0, a1, a2, src, event, dest, handler) PIObject::piConnect(src, #event, dest, (void*)(ret(*)(void*, a0, a1, a2))(&(dest)->__stat_eh_##handler##__), (void*)(void(*)(void*, a0, a1, a2))(&(src)->__stat_eh_##event##__));
#define CONNECT4(ret, a0, a1, a2, a3, src, event, dest, handler) PIObject::piConnect(src, #event, dest, (void*)(ret(*)(void*, a0, a1, a2, a3))(&(dest)->__stat_eh_##handler##__), (void*)(void(*)(void*, a0, a1, a2, a3))(&(src)->__stat_eh_##event##__));
#define CONNECT CONNECT0

#define WEAK_CONNECT0(ret, src, event, dest, handler) PIObject::piConnect(src, #event, dest, (void*)(ret(*)(void*))(&(dest)->__stat_eh_##handler##__));
#define WEAK_CONNECT1(ret, a0, src, event, dest, handler) PIObject::piConnect(src, #event, dest, (void*)(ret(*)(void*, a0))(&(dest)->__stat_eh_##handler##__));
#define WEAK_CONNECT2(ret, a0, a1, src, event, dest, handler) PIObject::piConnect(src, #event, dest, (void*)(ret(*)(void*, a0, a1))(&(dest)->__stat_eh_##handler##__));
#define WEAK_CONNECT3(ret, a0, a1, a2, src, event, dest, handler) PIObject::piConnect(src, #event, dest, (void*)(ret(*)(void*, a0, a1, a2))(&(dest)->__stat_eh_##handler##__));
#define WEAK_CONNECT4(ret, a0, a1, a2, a3, src, event, dest, handler) PIObject::piConnect(src, #event, dest, (void*)(ret(*)(void*, a0, a1, a2, a3))(&(dest)->__stat_eh_##handler##__));
#define WEAK_CONNECT WEAK_CONNECT0

#define DISCONNECT0(ret, src, event, dest, handler) PIObject::piDisconnect(src, #event, dest, (void*)(ret(*)(void*))(&(dest)->__stat_eh_##handler##__));
#define DISCONNECT1(ret, a0, src, event, dest, handler) PIObject::piDisconnect(src, #event, dest, (void*)(ret(*)(void*, a0))(&(dest)->__stat_eh_##handler##__));
#define DISCONNECT2(ret, a0, a1, src, event, dest, handler) PIObject::piDisconnect(src, #event, dest, (void*)(ret(*)(void*, a0, a1))(&(dest)->__stat_eh_##handler##__));
#define DISCONNECT3(ret, a0, a1, a2, src, event, dest, handler) PIObject::piDisconnect(src, #event, dest, (void*)(ret(*)(void*, a0, a1, a2))(&(dest)->__stat_eh_##handler##__));
#define DISCONNECT4(ret, a0, a1, a2, a3, src, event, dest, handler) PIObject::piDisconnect(src, #event, dest, (void*)(ret(*)(void*, a0, a1, a2, a3))(&(dest)->__stat_eh_##handler##__));
#define DISCONNECT DISCONNECT0

#define HANDLER(handler) __stat_eh_##handler##__

#endif

typedef void (*Handler)(void * );

class PIP_EXPORT PIObject
{
	friend class PIObjectManager;
public:
	
	//! Contructs PIObject with name "name"
	PIObject(const PIString & name = PIString());
	
	virtual ~PIObject() {piMonitor.objects--; objects.removeAll(this);}
	
	
	//! Returns object name
	PIString name() const {return property("name").toString();}
	
	//! Returns object class name
	virtual const char * className() const {return "PIObject";}
	
	//! Return if debug of this object is active
	bool debug() const {return property("debug").toBool();}
	
	
	//! Set object name
	void setName(const PIString & name) {setProperty("name", name);}
	
	//! Set object debug active
	void setDebug(bool debug) {setProperty("debug", debug);}
	
	//! Returns properties of the object
	const PIMap<PIString, PIVariant> & properties() const {return properties_;}
	
	//! Returns properties count of the object
	int propertiesCount() const {return properties_.size_s();}
	
	//! Returns property with name "name"
	PIVariant property(const PIString & name) const {if (!properties_.contains(name)) return PIVariant(); return properties_.value(name);}
	
	//! Set property with name "name" to "value". If there is no such property in object it will be added
	void setProperty(const PIString & name, const PIVariant & value) {properties_[name] = value; propertyChanged(name);}
	
	//! Returns if property with name "name" exists
	bool isPropertyExists(const PIString & name) const {return properties_.contains(name);}
	
	void setThreadSafe(bool yes) {thread_safe_ = yes;}
	bool isThreadSafe() const {return thread_safe_;}
	
	/*
	PIStringList events();
	PIStringList eventHandlers();

	/// Events
	void addEvent(const PIString & name) {
		signals_.insert(NamedFunction(0, name, PIStringList()), name);
	}
	template <typename T0>
	void addEvent(const PIString & name) {
		signals_.insert(NamedFunction(0, name, PIStringList(typeid(T0).name())), name);
	}
	template <typename T0, typename T1>
	void addEvent(const PIString & name) {
		signals_.insert(NamedFunction(0, name, PIStringList(typeid(T0).name(), typeid(T1).name())), name);
	}
	template <typename T0, typename T1, typename T2>
	void addEvent(const PIString & name) {
		signals_.insert(NamedFunction(0, name, PIStringList(typeid(T0).name(), typeid(T1).name(), typeid(T2).name())), name);
	}
	template <typename T0, typename T1, typename T2, typename T3>
	void addEvent(const PIString & name) {
		signals_.insert(NamedFunction(0, name, PIStringList(typeid(T0).name(), typeid(T1).name(), typeid(T2).name(), typeid(T3).name())), name);
	}

	/// Event handlers
	void addEventHandler(const PIString & name, void * func) {
		slots_.insert(NamedFunction(func, name, PIStringList()), name);
	}
	template <typename T0>
	void addEventHandler(const PIString & name, void * func) {
		slots_.insert(NamedFunction(func, name, PIStringList(typeid(T0).name())), name);
	}
	template <typename T0, typename T1>
	void addEventHandler(const PIString & name, void * func) {
		slots_.insert(NamedFunction(func, name, PIStringList(typeid(T0).name(), typeid(T1).name())), name);
	}
	template <typename T0, typename T1, typename T2>
	void addEventHandler(const PIString & name, void * func) {
		slots_.insert(NamedFunction(func, name, PIStringList(typeid(T0).name(), typeid(T1).name(), typeid(T2).name())), name);
	}
	template <typename T0, typename T1, typename T2, typename T3>
	void addEventHandler(const PIString & name, void * func) {
		slots_.insert(NamedFunction(func, name, PIStringList(typeid(T0).name(), typeid(T1).name(), typeid(T2).name(), typeid(T3).name())), name);
	}
	*/
	// / Direct connect
	static void piConnect(PIObject * src, const PIString & sig, void * dest, void * ev_h) {src->connections << Connection(ev_h, 0, sig, dest);}
	static void piConnect(PIObject * src, const PIString & sig, void * dest, void * ev_h, void * e_h) {src->connections << Connection(ev_h, e_h, sig, dest);}
	
	static void piConnect(const PIString & src, const PIString & sig, void * dest, void * ev_h);
	static void piConnect(PIObject * src, const PIString & sig, const PIString & dest, void * ev_h);
	static void piConnect(const PIString & src, const PIString & sig, const PIString & dest, void * ev_h);
	
	static void piDisconnect(PIObject * src, const PIString & sig, void * dest, void * ev_h) {
		for (int i = 0; i < src->connections.size_s(); ++i) {
			Connection & cc(src->connections[i]);
			if (cc.event == sig && cc.dest == dest && cc.slot == ev_h) {
				src->connections.remove(i);
				i--;
			}
		}
	}
	static void piDisconnect(PIObject * src, const PIString & sig, void * dest) {
		for (int i = 0; i < src->connections.size_s(); ++i) {
			Connection & cc(src->connections[i]);
			if (cc.event == sig && cc.dest == dest) {
				src->connections.remove(i);
				i--;
			}
		}
	}
	
	//! Disconnect object "src" from all connections with event name "sig"
	static void piDisconnect(PIObject * src, const PIString & sig) {
		for (int i = 0; i < src->connections.size_s(); ++i) {
			Connection & cc(src->connections[i]);
			if (cc.event == sig) {
				src->connections.remove(i);
				i--;
			}
		}
	}
	
	//! Disconnect object "src" from all connections, i.e. all connections where object "src" is emitter
	static void piDisconnect(PIObject * src) {src->connections.clear();}
	//static void piConnect(PIObject & src, const PIString & sig, PIObject * dest, void * ev_h) {src.connections << Connection(ev_h, sig, dest);}
	//static void piConnect(PIObject * src, const PIString & sig, PIObject & dest, void * ev_h) {src->connections << Connection(ev_h, sig, &dest);}
	//static void piConnect(PIObject & src, const PIString & sig, PIObject & dest, void * ev_h) {src.connections << Connection(ev_h, sig, &dest);}

	/*/// Connect through manager
	static bool piConnect(const PIString & srcObject, const PIString & event, const PIString & destObject, const PIString & handler, bool force = false) {
		PIObject * src = findByName(srcObject);
		if (src == 0) {
			cout << "PIObject::piConnect: can`t find PIObject with \"" << srcObject << "\" name!" << endl;
			return false;
		}
		PIObject * dest = findByName(destObject);
		if (dest == 0) {
			cout << "PIObject::piConnect: can`t find PIObject with \"" << destObject << "\" name!" << endl;
			return false;
		}
		return PIObject::piConnect(src, event, dest, handler, force);
	}*/

	// / Raise events
	static void raiseEvent(PIObject * sender, const PIString & event) {
		for (int j = 0; j < sender->connections.size_s(); ++j) {
			Connection & i(sender->connections[j]);
			if (i.event != event) continue;
			if (sender->thread_safe_) ((PIObject*)(i.dest))->mutex_.lock();
			((PIObject*)(i.dest))->emitter_ = sender;
			((void(*)(void * ))i.slot)(i.dest);
			((PIObject*)(i.dest))->emitter_ = 0;
			if (sender->thread_safe_) ((PIObject*)(i.dest))->mutex_.unlock();
		}
	}
	template <typename T0>
	static void raiseEvent(PIObject * sender, const PIString & event, const T0 & v0 = T0()) {
		for (int j = 0; j < sender->connections.size_s(); ++j) {
			Connection & i(sender->connections[j]);
			if (i.event != event) continue;
			if (sender->thread_safe_) ((PIObject*)(i.dest))->mutex_.lock();
			((PIObject*)(i.dest))->emitter_ = sender;
			((void(*)(void * , T0))i.slot)(i.dest, v0);
			((PIObject*)(i.dest))->emitter_ = 0;
			if (sender->thread_safe_) ((PIObject*)(i.dest))->mutex_.unlock();
		}
	}
	template <typename T0, typename T1>
	static void raiseEvent(PIObject * sender, const PIString & event, const T0 & v0 = T0(), const T1 & v1 = T1()) {
		for (int j = 0; j < sender->connections.size_s(); ++j) {
			Connection & i(sender->connections[j]);
			if (i.event != event) continue;
			if (sender->thread_safe_) ((PIObject*)(i.dest))->mutex_.lock();
			((PIObject*)(i.dest))->emitter_ = sender;
			((void(*)(void * , T0, T1))i.slot)(i.dest, v0, v1);
			((PIObject*)(i.dest))->emitter_ = 0;
			if (sender->thread_safe_) ((PIObject*)(i.dest))->mutex_.unlock();
		}
	}
	template <typename T0, typename T1, typename T2>
	static void raiseEvent(PIObject * sender, const PIString & event, const T0 & v0 = T0(), const T1 & v1 = T1(), const T2 & v2 = T2()) {
		for (int j = 0; j < sender->connections.size_s(); ++j) {
			Connection & i(sender->connections[j]);
			if (i.event != event) continue;
			if (sender->thread_safe_) ((PIObject*)(i.dest))->mutex_.lock();
			((PIObject*)(i.dest))->emitter_ = sender;
			((void(*)(void * , T0, T1, T2))i.slot)(i.dest, v0, v1, v2);
			((PIObject*)(i.dest))->emitter_ = 0;
			if (sender->thread_safe_) ((PIObject*)(i.dest))->mutex_.unlock();
		}
	}
	template <typename T0, typename T1, typename T2, typename T3>
	static void raiseEvent(PIObject * sender, const PIString & event, const T0 & v0 = T0(), const T1 & v1 = T1(), const T2 & v2 = T2(), const T3 & v3 = T3()) {
		for (int j = 0; j < sender->connections.size_s(); ++j) {
			Connection & i(sender->connections[j]);
			if (i.event != event) continue;
			if (sender->thread_safe_) ((PIObject*)(i.dest))->mutex_.lock();
			((PIObject*)(i.dest))->emitter_ = sender;
			((void(*)(void * , T0, T1, T2, T3))i.slot)(i.dest, v0, v1, v2, v3);
			((PIObject*)(i.dest))->emitter_ = 0;
			if (sender->thread_safe_) ((PIObject*)(i.dest))->mutex_.unlock();
		}
	}
	/*
	/// Raise events (static)
	static void raiseEvent(PIObject * destObject, const PIString & name) {
		destObject->raiseEvent(name);
	}
	template <typename T0>
	static void raiseEvent(PIObject * destObject, const PIString & name, const T0 & v0 = T0()) {
		destObject->raiseEvent<T0>(name, v0);
	}
	template <typename T0, typename T1>
	static void raiseEvent(PIObject * destObject, const PIString & name, const T0 & v0 = T0(), const T1 & v1 = T1()) {
		destObject->raiseEvent<T0, T1>(name, v0, v1);
	}
	template <typename T0, typename T1, typename T2>
	static void raiseEvent(PIObject * destObject, const PIString & name, const T0 & v0 = T0(), const T1 & v1 = T1(), const T2 & v2 = T2()) {
		destObject->raiseEvent<T0, T1, T2>(name, v0, v1, v2);
	}
	template <typename T0, typename T1, typename T2, typename T3>
	static void raiseEvent(PIObject * destObject, const PIString & name, const T0 & v0 = T0(), const T1 & v1 = T1(), const T2 & v2 = T2(), const T3 & v3 = T3()) {
		destObject->raiseEvent<T0, T1, T2, T3>(name, v0, v1, v2, v3);
	}
	*/
	// / Raise events through manager
	static void raiseEvent(const PIString & destObject, const PIString & name) {
		PIObject * dest = findByName(destObject);
		if (dest == 0) {
			cout << "PIObject::piConnect: can`t find PIObject with \"" << destObject << "\" name!" << endl;
			return;
		}
		raiseEvent(dest, name);
	}
	template <typename T0>
	static void raiseEvent(const PIString & destObject, const PIString & name, const T0 & v0 = T0()) {
		PIObject * dest = findByName(destObject);
		if (dest == 0) {
			cout << "PIObject::piConnect: can`t find PIObject with \"" << destObject << "\" name!" << endl;
			return;
		}
		raiseEvent<T0>(dest, name, v0);
	}
	template <typename T0, typename T1>
	static void raiseEvent(const PIString & destObject, const PIString & name, const T0 & v0 = T0(), const T1 & v1 = T1()) {
		PIObject * dest = findByName(destObject);
		if (dest == 0) {
			cout << "PIObject::piConnect: can`t find PIObject with \"" << destObject << "\" name!" << endl;
			return;
		}
		raiseEvent<T0, T1>(dest, name, v0, v1);
	}
	template <typename T0, typename T1, typename T2>
	static void raiseEvent(const PIString & destObject, const PIString & name, const T0 & v0 = T0(), const T1 & v1 = T1(), const T2 & v2 = T2()) {
		PIObject * dest = findByName(destObject);
		if (dest == 0) {
			cout << "PIObject::piConnect: can`t find PIObject with \"" << destObject << "\" name!" << endl;
			return;
		}
		raiseEvent<T0, T1, T2>(name, dest, v0, v1, v2);
	}
	template <typename T0, typename T1, typename T2, typename T3>
	static void raiseEvent(const PIString & destObject, const PIString & name, const T0 & v0 = T0(), const T1 & v1 = T1(), const T2 & v2 = T2(), const T3 & v3 = T3()) {
		PIObject * dest = findByName(destObject);
		if (dest == 0) {
			cout << "PIObject::piConnect: can`t find PIObject with \"" << destObject << "\" name!" << endl;
			return;
		}
		raiseEvent<T0, T1, T2, T3>(name,dest , v0, v1, v2, v3);
	}
	
	//! Returns PIObject* with name "name" or 0, if there is no object found
	static PIObject * findByName(const PIString & name) {
		piForeach (PIObject * i, PIObject::objects) {
			if (i->name() != name) continue;
			return i;
		}
		return 0;
	};

protected:
	
	//! Returns PIObject* which has raised an event. This value is correct only in definition of some event handler
	PIObject * emitter() const {return emitter_;}
	
	//! Virtual function executes after property with name "name" has been changed
	virtual void propertyChanged(const PIString & name) {}

private:
	struct Connection {
		Connection(void * sl = 0, void * si = 0, const PIString & e = PIString(), void * o = 0) {slot = sl; signal = si; event = e; dest = o;}
		void * slot;
		void * signal;
		PIString event;
		void * dest;
	};
	
	PIVector<Connection> connections;
	PIMap<PIString, PIVariant> properties_;
	
	static PIVector<PIObject * > objects;
	PIMutex mutex_;
	PIObject * emitter_;
	bool thread_safe_;

};

#endif // PIOBJECT_H
