Exemple #1
0
int main(int, char **)
{
	LuaGlue state;
	
	state.
		Class<Shared>("Shared").
			ctor("new").
			method("getRef", &Shared::getRef).
			method("putRef", &Shared::putRef).
			method("getValue", &Shared::getValue).
			end().
		open().glue();
	
	printf("running lua script!\n");
	if(!state.doFile("shared_ptr.lua"))
	{
		printf("failed to dofile: shared_ptr.lua\n");
		printf("err: %s\n", state.lastError().c_str());
	}
	
	Shared *shared = new Shared(123);
	std::shared_ptr<Shared> ptr(shared);

	printf("shared == %p\n", shared);
	state.invokeVoidFunction("test_ptr", ptr);
	state.invokeVoidFunction("test_ptr", ptr);
	state.invokeVoidFunction("test_ptr", ptr);
	
	printf("done!\n");
	
	//lua_gc(state.state(), LUA_GCCOLLECT, 0);

	return 0;
}
Exemple #2
0
int main(int, char **)
{
	LuaGlue state;
	
	auto &Class = state.Class<Invoke>("Invoke").
		ctor("new").
		method("invoke", &Invoke::invoke).
		method("invokeObj", &Invoke::invokeObj).
		method("objInvoke", &Invoke::objInvoke).
		method("fromlua", &Invoke::fromlua);
		
	state.open().glue();
	
	printf("running lua script!\n");
	if(!state.doFile("invoke.lua"))
	{
		printf("failed to dofile: invoke.lua\n");
		printf("err: %s\n", state.lastError().c_str());
	}
	
	printf("testing invoking methods from C++\n");
	Invoke *test_obj = new Invoke();
	test_obj->abc = 123;
	
	Class.invokeVoidMethod("invoke", test_obj, 1, 2.0, "three");
	Class.invokeVoidMethod("invoke_lua", test_obj, 2, 3.0, "four");
	Class.invokeVoidMethod("invokeObj", test_obj, test_obj);
	Invoke *ret_obj = Class.invokeMethod<Invoke *>("objInvoke", test_obj);
	
	if(ret_obj != test_obj)
	{
		printf("ret_obj(%p) != test_obj(%p) ! :o\n", ret_obj, test_obj);
	}

	printf("test_obj.abc: %i, ret_obj.abc: %i\n", test_obj->abc, ret_obj->abc);
	
	state.invokeVoidFunction("from_c");
	int ret = state.invokeFunction<int>("from_c_ret");
	printf("from_c_ret ret: %i\n", ret);
	
	int ret2 = state.invokeFunction<int>("from_c_args", 1, 2.0, "three");
	printf("from_c_args ret: %i\n", ret2);
	
	return 0;
}
wxString Model_Report::get_html(const Data* r)
{
    mm_html_template report(r->TEMPLATECONTENT);
    r->to_template(report);

    loop_t contents;

    loop_t errors;
    row_t error;

    wxSQLite3ResultSet q;
    int columnCount = 0;
    wxString sql = r->SQLCONTENT;
    std::map <wxString, wxString> rep_params;
    try
    {
        PrepareSQL(sql, rep_params);

        wxSQLite3Statement stmt = this->db_->PrepareStatement(sql);
        if (!stmt.IsReadOnly())
        {
            return wxString::Format(_("The SQL script:\n%s \nwill modify database! aborted!"), r->SQLCONTENT);
        }
        else
        {
            q = stmt.ExecuteQuery();
            columnCount = q.GetColumnCount();
        }
    }
    catch (const wxSQLite3Exception& e)
    {
        return e.GetMessage();
    }

    std::map <std::wstring, int> colHeaders;

    loop_t columns;
    for (int i = 0; i < columnCount; ++i)
    {
        int col_type = q.GetColumnType(i);
        const std::wstring col_name = q.GetColumnName(i).ToStdWstring();
        colHeaders[col_name] = col_type;
        row_t row;
        row(L"COLUMN") = col_name;
        columns += row;
    }
    report(L"COLUMNS") = columns;

    LuaGlue state;
    state.
        Class<Record>("Record").
        ctor("new").
        method("get", &Record::get).
        method("set", &Record::set).
        end().open().glue();

    bool skip_lua = r->LUACONTENT.IsEmpty();
    bool lua_status = state.doString(std::string(r->LUACONTENT.ToUTF8()));
    if (!skip_lua && !lua_status)
    {
        error(L"ERROR") = wxString("failed to doString : ") + r->LUACONTENT + wxString(" err: ") + wxString(state.lastError());
        errors += error;
    }

    while (q.NextRow())
    {
        Record rec;
        for (int i = 0; i < columnCount; ++i)
        {
            const wxString column_name = q.GetColumnName(i);
            rec[column_name.ToStdWstring()] = q.GetAsString(i);
        }

        if (lua_status && !skip_lua)
        {
            try
            {
                state.invokeVoidFunction("handle_record", &rec);
            }
            catch (const std::runtime_error& e)
            {
                error(L"ERROR") = wxString("failed to call handle_record : ") + wxString(e.what());
                errors += error;
            }
            catch (const std::exception& e)
            {
                error(L"ERROR") = wxString("failed to call handle_record : ") + wxString(e.what());
                errors += error;
            }
            catch (...)
            {
                error(L"ERROR") = L"failed to call handle_record ";
                errors += error;
            }
        }
        row_t row;
        for (const auto& item : rec)
        {
            row(item.first) = item.second;
        }
        contents += row;

    }
    q.Finalize();

    Record result;
    if (lua_status && !skip_lua)
    {
        try
        {
            state.invokeVoidFunction("complete", &result);
        }
        catch (const std::runtime_error& e)
        {
            error(L"ERROR") = wxString("failed to call complete: ") + wxString(e.what());
            errors += error;
        }
        catch (const std::exception& e)
        {
            error(L"ERROR") = wxString("failed to call complete: ") + wxString(e.what());
            errors += error;
        }
        catch (...)
        {
            error(L"ERROR") = L"failed to call complete";
            errors += error;
        }
    }

    for (const auto& item : result)
        report(item.first) = item.second;

    report(L"CONTENTS") = contents;
    {
        for (const auto& item : rep_params)
        {
            report(item.first.Upper().ToStdWstring()) = item.second;
        }
        auto p = mmex::getPathAttachment(mmAttachmentManage::InfotablePathSetting());
        //javascript does not handle backslashs
        p.Replace("\\", "\\\\");
        report(L"ATTACHMENTSFOLDER") = p;
        auto s = wxString(wxFileName::GetPathSeparator());
        s.Replace("\\", "\\\\");
        report(L"FILESEPARATOR") = s;
        report(L"LANGUAGE") = Option::instance().LanguageISO6391();
        report(L"HTMLSCALE") = wxString::Format("%d", Option::instance().HtmlFontSize());
    }
    report(L"ERRORS") = errors;

    wxString out = wxEmptyString;
    try 
    {
        out = report.Process();
    }
    catch (const syntax_ex& e)
    {
        return e.what();
    }
    catch (...)
    {
        return _("Caught exception");
    }

    return out;
}
wxString Model_Report::get_html(const Data* r)
{
    mm_html_template report(r->TEMPLATECONTENT);
    r->to_template(report);

    loop_t contents;
    json::Array jsoncontents;

    loop_t errors;
    row_t error;

    wxSQLite3ResultSet q;
    int columnCount = 0;
    try
    {
        wxString sql = r->SQLCONTENT;
        sql.Trim();
        if (!sql.empty() && sql.Last() != ';') sql += ';';
        wxSQLite3Statement stmt = this->db_->PrepareStatement(sql);
        if (!stmt.IsReadOnly())
        {
            return wxString::Format(_("The SQL script:\n%s \nwill modify database! aborted!"), r->SQLCONTENT);
        }
        else
        {
            q = stmt.ExecuteQuery();
            columnCount = q.GetColumnCount();
        }
    }
    catch (const wxSQLite3Exception& e)
    {
        return e.GetMessage();
    }

    std::map <std::wstring, int> colHeaders;

    loop_t columns;
    for (int i = 0; i < columnCount; ++i)
    {
        int col_type = q.GetColumnType(i);
        const std::wstring col_name = q.GetColumnName(i).ToStdWstring();
        colHeaders[col_name] = col_type;
        row_t row;
        row(L"COLUMN") = col_name;
        columns += row;
    }
    report(L"COLUMNS") = columns;

    LuaGlue state;
    state.
        Class<Record>("Record").
        ctor("new").
        method("get", &Record::get).
        method("set", &Record::set).
        end().open().glue();

    bool skip_lua = r->LUACONTENT.IsEmpty();
    bool lua_status = state.doString(std::string(r->LUACONTENT.ToUTF8()));
    if (!skip_lua && !lua_status)
    {
        error(L"ERROR") = wxString("failed to doString : ") + r->LUACONTENT + wxString(" err: ") + wxString(state.lastError());
        errors += error;
    }

    while (q.NextRow())
    {
        Record rec;
        for (int i = 0; i < columnCount; ++i)
        {
            const wxString column_name = q.GetColumnName(i);
            rec[column_name.ToStdWstring()] = q.GetAsString(i);
        }

        if (lua_status && !skip_lua)
        {
            try
            {
                state.invokeVoidFunction("handle_record", &rec);
            }
            catch (const std::runtime_error& e)
            {
                error(L"ERROR") = wxString("failed to call handle_record : ") + wxString(e.what());
                errors += error;
            }
            catch (const std::exception& e)
            {
                error(L"ERROR") = wxString("failed to call handle_record : ") + wxString(e.what());
                errors += error;
            }
            catch (...)
            {
                error(L"ERROR") = L"failed to call handle_record ";
                errors += error;
            }
        }
        row_t row;
        json::Object o;
        for (const auto& item : rec)
        {
            row(item.first) = item.second;

            double v;
            if ((colHeaders[item.first] == WXSQLITE_INTEGER || colHeaders[item.first] == WXSQLITE_FLOAT)
                && wxString(item.second).ToDouble(&v))
            {
                o[wxString(item.first).ToStdWstring()] = json::Number(v);
            }
            else
                o[wxString(item.first).ToStdWstring()] = json::String(wxString(item.second).ToStdWstring());
        }
        contents += row;
        jsoncontents.Insert(o);

    }
    q.Finalize();

    Record result;
    if (lua_status && !skip_lua)
    {
        try
        {
            state.invokeVoidFunction("complete", &result);
        }
        catch (const std::runtime_error& e)
        {
            error(L"ERROR") = wxString("failed to call complete: ") + wxString(e.what());
            errors += error;
        }
        catch (const std::exception& e)
        {
            error(L"ERROR") = wxString("failed to call complete: ") + wxString(e.what());
            errors += error;
        }
        catch (...)
        {
            error(L"ERROR") = L"failed to call complete";
            errors += error;
        }
    }

    for (const auto& item : result)
        report(item.first) = item.second;

    report(L"CONTENTS") = contents;
    {
        std::wstringstream ss;
        json::Writer::Write(jsoncontents, ss);
        report(L"JSONCONTENTS") = wxString(ss.str());
        auto p = mmex::getPathAttachment(mmAttachmentManage::InfotablePathSetting());
        //javascript does not handle backslashs
        p.Replace("\\", "\\\\");
        report(L"ATTACHMENTSFOLDER") = p;
        auto s = wxString(wxFileName::GetPathSeparator());
        s.Replace("\\", "\\\\");
        report(L"FILESEPARATOR") = s;
        report(L"LANGUAGE") = Model_Setting::instance().GetStringSetting(LANGUAGE_PARAMETER, "english");
        report(L"HTMLSCALE") = wxString::Format("%d", mmIniOptions::instance().html_font_size_);
    }
    report(L"ERRORS") = errors;

    wxString out = wxEmptyString;
    try 
    {
        out = report.Process();
    }
    catch (const syntax_ex& e)
    {
        out = e.what();
    }
    catch (...)
    {
        // TODO
    }

    outputReportFile(out);
    return out;
}