/* "CodeWorker":	a scripting language for parsing and generating text.

Copyright (C) 1996-1997, 1999-2003 Cédric Lemaire

This library 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 2.1 of the License, or (at your option) any later version.

This library 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 library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

To contact the author: codeworker@free.fr
*/

#ifndef _DynPackage_h_
#define _DynPackage_h_



#ifdef CODEWORKER_DOT_NET
#	pragma unmanaged
#endif

#include <string>
#include <map>

#include "GrfFunction.h"

namespace CodeWorker {
	typedef void* LIBRARY_HANDLE;
	typedef void* Parameter;

	class Interpreter;


	class DynFunction : public GrfFunction {
	private:
		Interpreter* _pInterpreter;
		std::string _sModuleName;
		void* _pfFunctionCall;

	public:
		inline DynFunction(Interpreter* pInterpreter, const std::string& sName) : GrfFunction(0L, sName, "", false), _pInterpreter(pInterpreter) { init(); }
		DynFunction(Interpreter* pInterpreter, const char* tcName, bool bIsP1Node);
		DynFunction(Interpreter* pInterpreter, const char* tcName, bool bIsP1Node, bool bIsP2Node);
		DynFunction(Interpreter* pInterpreter, const char* tcName, bool bIsP1Node, bool bIsP2Node, bool bIsP3Node);
		DynFunction(Interpreter* pInterpreter, const char* tcName, bool bIsP1Node, bool bIsP2Node, bool bIsP3Node, bool bIsP4Node);
		DynFunction(Interpreter* pInterpreter, const char* tcName, int iNbParams,  int* tbNodeParams);
		virtual ~DynFunction();

		inline void setFunctionCall(void* pfFunctionCall) { _pfFunctionCall = pfFunctionCall; }

		virtual std::string launchExecution(DtaScriptVariable& visibility, const ExprScriptFunction& functionCall, const std::string& sInstantiationKey = "");

	private:
		void init();

		std::string call(DtaScriptVariable& pVisibility);
		std::string call(DtaScriptVariable& pVisibility, Parameter p1);
		std::string call(DtaScriptVariable& pVisibility, Parameter p1, Parameter p2);
		std::string call(DtaScriptVariable& pVisibility, Parameter p1, Parameter p2, Parameter p3);
		std::string call(DtaScriptVariable& pVisibility, Parameter p1, Parameter p2, Parameter p3, Parameter p4);
		std::string call(DtaScriptVariable& pVisibility, Parameter* tParams);
	};


	class DynPackage {
	private:
		static std::map<std::string, DynPackage*> _packages;

	private:
		std::string _sPackage;
		GrfBlock* _pBlock;
		LIBRARY_HANDLE _hHandle;
		Interpreter* _pInterpreter;
		std::map<std::string, DynFunction*> _functions;

	public:
		~DynPackage();

		inline const std::string& getName() const { return _sPackage; }
		inline GrfBlock* getBlock() const { return _pBlock; }

		DynFunction* getFunction(const std::string& sName) const;
		bool addFunction(const char* tcName);
		bool addFunction(const char* tcName, bool bIsP1Node);
		bool addFunction(const char* tcName, bool bIsP1Node, bool bIsP2Node);
		bool addFunction(const char* tcName, bool bIsP1Node, bool bIsP2Node, bool bIsP3Node);
		bool addFunction(const char* tcName, bool bIsP1Node, bool bIsP2Node, bool bIsP3Node, bool bIsP4Node);
		bool addFunction(const char* tcName, int  iNbParams, int* tbNodeParams);

		static DynPackage* addPackage(const std::string& sPackage, GrfBlock& block);
		static DynPackage* getPackage(const std::string& sPackage);

		static LIBRARY_HANDLE loadLibrary(const std::string& sLibrary);
		static LIBRARY_HANDLE loadPackage(const std::string& sLibrary);
		static void freeLibrary(LIBRARY_HANDLE hHandle);
		static void* findFunction(LIBRARY_HANDLE hHandle, const std::string& sFunction);

	private:
		DynPackage(const std::string& sPackage, GrfBlock& block);
		bool addFunction(DynFunction* pFunction);
	};

}

#endif
