/*! \file pip_lua.h
 * \ingroup Lua
 * \~\brief
 * \~english Conversions between PIP types and LuaBridge
 * \~russian Преобразования между типами PIP и LuaBridge
 */
/*
    PIP - Platform Independent Primitives
    PIP Lua bindings
    Andrey Bychkov work.a.b@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/>.
*/
//! \defgroup Lua Lua
//! \~\brief
//! \~english Lua programs support
//! \~russian Поддержка программ Lua
//!
//! \~\details
//! \~english \section cmake_module_Lua Building with CMake
//! \~russian \section cmake_module_Lua Сборка с использованием CMake
//!
//! \~\code
//! find_package(PIP REQUIRED)
//! target_link_libraries([target] PIP::Lua)
//! \endcode
//!
//! \~english \par Common
//! \~russian \par Общее
//!
//! \~english
//! These files provide execution of Lua programs inside C++ code.
//!
//! \~russian
//! Эти файлы обеспечивают возможность выполнения программ Lua внутри C++ кода.
//!
//! \~\authors
//! \~english
//! Ivan Pelipenko peri4ko@yandex.ru;
//! Andrey Bychkov work.a.b@yandex.ru;
//! \~russian
//! Иван Пелипенко peri4ko@yandex.ru;
//! Андрей Бычков work.a.b@yandex.ru;
//!

#ifndef PIP_LUA_H
#define PIP_LUA_H

#include "lua.hpp"
#include "pistring.h"

#include <LuaBridge/LuaBridge.h>

namespace luabridge {

//! \~\ingroup Lua
//! \~\brief
//! \~english LuaBridge stack adapter for %PIString.
//! \~russian Адаптер стека LuaBridge для %PIString.
template<>
struct Stack<PIString> {
	//! \~english Pushes a UTF-8 representation of the string onto the Lua stack.
	//! \~russian Помещает UTF-8 представление строки в стек Lua.
	static void push(lua_State * L, PIString const & str) { lua_pushstring(L, str.dataUTF8()); }

	//! \~english Reads a Lua value as %PIString, converting non-string values through Lua.
	//! \~russian Читает значение Lua как %PIString, при необходимости преобразуя нестроковые значения средствами Lua.
	static PIString get(lua_State * L, int index) {
		if (lua_type(L, index) == LUA_TSTRING) {
			const char * str = lua_tostring(L, index);
			return PIString::fromUTF8(str);
		}

		lua_pushvalue(L, index);
		const char * str = lua_tostring(L, -1);
		PIString string  = PIString::fromUTF8(str);
		lua_pop(L, 1);
		return string;
	}

	//! \~english Returns whether the value at \a index is already a Lua string.
	//! \~russian Возвращает, является ли значение по \a index уже строкой Lua.
	static bool isInstance(lua_State * L, int index) { return lua_type(L, index) == LUA_TSTRING; }
};

} // namespace luabridge
#endif // PIP_LUA_H
