Skip to content

Commit

Permalink
Nelson Engine API for C (compatible with MEX Engine)
Browse files Browse the repository at this point in the history
--minimize option added
  • Loading branch information
Nelson-numerical-software committed Oct 20, 2020
1 parent 58d0b8a commit 80a4f10
Show file tree
Hide file tree
Showing 44 changed files with 1,460 additions and 288 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# 0.4.10 (2020-10-XX)

* extends dlgeneratemake to generate also executable.
* Nelson Engine API for C (compatible with MEX Engine).

* extends mex function to generate also executable.

* extends dlgeneratemake function to generate also executable.

* --minimize command line argument added. minimize main GUI Window at startup.


Bug Fixes:
---------
Expand Down
4 changes: 3 additions & 1 deletion modules/dynamic_link/resources/template_executable_cmake.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ __C_CPP_FILES__
)
#==============================================================================
add_executable(__EXECUTABLE_NAME__ ${MODULE_SRC})
#==============================================================================
#==============================================================================
target_link_libraries(__EXECUTABLE_NAME__ __EXTERNAL_LIBRARIES__)
#==============================================================================
5 changes: 5 additions & 0 deletions modules/engine/help/en_US/xml/executable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
<param_description>disable the user script file executed at startup after the main startup file.</param_description>
</param_input_item>

<param_input_item>
<param_name>--minimize</param_name>
<param_description>minimize main GUI Windows (GUI mode only).</param_description>
</param_input_item>

<param_input_item>
<param_name>--noipc</param_name>
<param_description>disable interprocess features (files association, ipc builtin).</param_description>
Expand Down
4 changes: 2 additions & 2 deletions modules/engine/src/cpp/MainEvaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace Nelson {
static Evaluator* mainEvaluator = nullptr;
//=============================================================================
Evaluator*
createMainEvaluator(NELSON_ENGINE_MODE _mode, const std::wstring& lang)
createMainEvaluator(NELSON_ENGINE_MODE _mode, const std::wstring& lang, bool minimizeWindow)
{
setDefaultMaxNumCompThreads();
if (mainEvaluator == nullptr) {
Expand Down Expand Up @@ -124,7 +124,7 @@ createMainEvaluator(NELSON_ENGINE_MODE _mode, const std::wstring& lang)
case GUI: {
InitGuiObjectsDynamic();
mainEvaluator
= static_cast<Evaluator*>(CreateGuiEvaluatorDynamic((void*)context, _mode));
= static_cast<Evaluator*>(CreateGuiEvaluatorDynamic((void*)context, _mode, minimizeWindow));
} break;
default: {
std::string _msg = _("unknow engine.\n");
Expand Down
6 changes: 3 additions & 3 deletions modules/engine/src/cpp/MainGuiObjectDynamic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ InitGuiObjectsDynamic()
}
//===================================================================================
void*
CreateGuiEvaluatorDynamic(void* vcontext, NELSON_ENGINE_MODE _mode)
CreateGuiEvaluatorDynamic(void* vcontext, NELSON_ENGINE_MODE _mode, bool minimizeWindow)
{
using PROC_CreateGuiEvaluator = void* (*)(void*, NELSON_ENGINE_MODE);
using PROC_CreateGuiEvaluator = void* (*)(void*, NELSON_ENGINE_MODE, bool);
static PROC_CreateGuiEvaluator CreateGuiEvaluatorPtr = nullptr;
initGuiDynamicLibrary();
if (CreateGuiEvaluatorPtr == nullptr) {
Expand All @@ -112,7 +112,7 @@ CreateGuiEvaluatorDynamic(void* vcontext, NELSON_ENGINE_MODE _mode)
exit(1);
}
}
return CreateGuiEvaluatorPtr(vcontext, _mode);
return CreateGuiEvaluatorPtr(vcontext, _mode, minimizeWindow);
}
//===================================================================================
void
Expand Down
19 changes: 19 additions & 0 deletions modules/engine/src/cpp/ProgramOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ ProgramOptions::ProgramOptions(wstringVector args, NELSON_ENGINE_MODE mode)
_userstartup = false;
_usermodules = false;
_quietmode = false;
_minimize = false;
_ipc = false;
_error.clear();
_file.clear();
Expand All @@ -138,6 +139,7 @@ ProgramOptions::~ProgramOptions()
_usermodules = false;
_quietmode = false;
_ipc = false;
_minimize = false;
_error.clear();
_file.clear();
_command.clear();
Expand Down Expand Up @@ -250,6 +252,7 @@ ProgramOptions::parse()
Option nostartupOption(L"nostartup", L"", _W("no main startup file"), false, false);
Option nouserstartupOption(L"nouserstartup", L"", _W("no user startup file"), false, false);
Option nousermodulesOption(L"nousermodules", L"", _W("no user modules loaded"), false, false);
Option minimizeOption(L"minimize", L"", _W("minimize main window (GUI only)"), false, false);
Option noIpcOption(L"noipc", L"", _W("no ipc features"), false, false);
Option commandtoexecuteOption(L"execute", L"e", _W("command to execute"), false, true);
Option filetoexecuteOption(L"file", L"f", _W("file to execute in an new process"), false, true);
Expand All @@ -269,6 +272,9 @@ ProgramOptions::parse()
_options = _options + nouserstartupOption.getFullDescription() + L"\n";
_options = _options + nousermodulesOption.getFullDescription() + L"\n";
_options = _options + noIpcOption.getFullDescription() + L"\n";
if (_mode == NELSON_ENGINE_MODE::GUI) {
_options = _options + minimizeOption.getFullDescription() + L"\n";
}
_options = _options + commandtoexecuteOption.getFullDescription() + L"\n";
_options = _options + filetoexecuteOption.getFullDescription() + L"\n";
_options = _options + filetoexecuteIPCOption.getFullDescription() + L"\n";
Expand All @@ -280,11 +286,15 @@ ProgramOptions::parse()
_options = _options + timeoutOption.getFullDescription() + L"\n";
_options = _options + openFilesOption.getFullDescription() + L"\n";
_options = _options + loadFilesOption.getFullDescription() + L"\n";

bRes = parseOption(helpOption, _ishelp);
bRes = bRes && parseOption(versionOption, _isversion);
bRes = bRes && parseOption(nostartupOption, _startup);
bRes = bRes && parseOption(nouserstartupOption, _userstartup);
bRes = bRes && parseOption(nousermodulesOption, _usermodules);
if (_mode == NELSON_ENGINE_MODE::GUI) {
bRes = bRes && parseOption(minimizeOption, _minimize);
}
bRes = bRes && parseOption(noIpcOption, _ipc);
bRes = bRes && parseOptionWithValues(openFilesOption, _filesToOpen);
bRes = bRes && parseOptionWithValues(loadFilesOption, _filesToLoad);
Expand Down Expand Up @@ -342,6 +352,15 @@ ProgramOptions::haveOptionsHelp()
}
//=============================================================================
bool
ProgramOptions::haveOptionsMinimize()
{
if (_isvalid) {
return _minimize;
}
return false;
}
//=============================================================================
bool
ProgramOptions::haveVersion()
{
if (_isvalid) {
Expand Down
8 changes: 6 additions & 2 deletions modules/engine/src/cpp/StartNelson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#endif
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <clocale>
#include <sstream>
#include "StartNelson.h"
Expand Down Expand Up @@ -64,6 +65,7 @@
#include "MxCall.h"
#include "NelsonConfiguration.hpp"
#include "FilesAssociation.hpp"
#include "NelsonReadyNamedMutex.hpp"
//=============================================================================
static void
ErrorCommandLineMessage_startup_exclusive(NELSON_ENGINE_MODE _mode)
Expand Down Expand Up @@ -239,6 +241,7 @@ NelsonMainStates(Evaluator* eval, bool haveNoStartup, bool haveNoUserStartup,
Interface* io = eval->getInterface();
io->errorMessage(e.getMessage());
}
eval->isReadyToUse = true;
OpenFilesAssociated((NELSON_ENGINE_MODE)eval->getNelsonEngineMode(), filesToOpen, false);
LoadFilesAssociated((NELSON_ENGINE_MODE)eval->getNelsonEngineMode(), filesToLoad, false);
while (eval->getState() != NLS_STATE_QUIT) {
Expand All @@ -248,6 +251,7 @@ NelsonMainStates(Evaluator* eval, bool haveNoStartup, bool haveNoUserStartup,
eval->resetState();
eval->evalCLI();
}
closeIsReadyNelsonMutex((int)boost::interprocess::ipcdetail::get_current_process_id());
eval->resetState();
FINISH:
if (!haveNoStartup) {
Expand Down Expand Up @@ -383,8 +387,8 @@ StartNelsonInternal(wstringVector args, NELSON_ENGINE_MODE _mode)
boost::filesystem::path full_p = boost::filesystem::complete(p);
fileToExecute = full_p.generic_wstring();
}

Evaluator* eval = createMainEvaluator(_mode, lang);
Evaluator* eval = createMainEvaluator(_mode, lang, po.haveOptionsMinimize());
if (eval != nullptr) {
setWarningEvaluator(eval);
setErrorEvaluator(eval);
Expand Down
2 changes: 1 addition & 1 deletion modules/engine/src/include/MainEvaluator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//=============================================================================
namespace Nelson {
NLSENGINE_IMPEXP Evaluator*
createMainEvaluator(NELSON_ENGINE_MODE _mode, const std::wstring& lang);
createMainEvaluator(NELSON_ENGINE_MODE _mode, const std::wstring& lang, bool minimizeWindow);
NLSENGINE_IMPEXP Evaluator*
getMainEvaluator();
NLSENGINE_IMPEXP bool
Expand Down
2 changes: 1 addition & 1 deletion modules/engine/src/include/MainGuiObjectDynamic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Nelson {
void
InitGuiObjectsDynamic();
void*
CreateGuiEvaluatorDynamic(void* vcontext, NELSON_ENGINE_MODE _mode);
CreateGuiEvaluatorDynamic(void* vcontext, NELSON_ENGINE_MODE _mode, bool minimizeWindow);
void
DestroyMainGuiObjectDynamic(void* term);
void*
Expand Down
4 changes: 4 additions & 0 deletions modules/engine/src/include/NelsonNamedMutex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@
#pragma once
//=============================================================================
namespace Nelson {
//=============================================================================
bool
openNelsonMutex();
//=============================================================================
bool
closeNelsonMutex();
//=============================================================================
bool
haveNelsonMutex();
//=============================================================================
} // namespace Nelson
//=============================================================================
3 changes: 3 additions & 0 deletions modules/engine/src/include/ProgramOptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class ProgramOptions
wstringVector
getFilesToLoad();
bool
haveOptionsMinimize();
bool
haveOptionsHelp();
bool
haveVersion();
Expand Down Expand Up @@ -135,6 +137,7 @@ class ProgramOptions
bool _usermodules;
bool _quietmode;
bool _ipc;
bool _minimize;
std::wstring _error;
std::wstring _file;
std::wstring _fileIPC;
Expand Down
4 changes: 2 additions & 2 deletions modules/gui/src/cpp/MainGuiObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ InitGuiObjects(void)
}
//===================================================================================
void*
CreateGuiEvaluator(void* vcontext, NELSON_ENGINE_MODE _mode)
CreateGuiEvaluator(void* vcontext, NELSON_ENGINE_MODE _mode, bool minimizeWindow)
{
CreateConsole();
try {
NelSonQtMainWindow = new QtMainWindow();
NelSonQtMainWindow = new QtMainWindow(minimizeWindow);
} catch (std::bad_alloc&) {
NelSonQtMainWindow = nullptr;
}
Expand Down
7 changes: 6 additions & 1 deletion modules/gui/src/cpp/QtMainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ QtMainWindow::~QtMainWindow()
}
}
//=============================================================================
QtMainWindow::QtMainWindow()
QtMainWindow::QtMainWindow(bool minimized)
{
nelsonPath = Nelson::wstringToQString(Nelson::GetRootPath());
QWidget* widget = new QWidget;
Expand All @@ -148,6 +148,11 @@ QtMainWindow::QtMainWindow()
setWindowTitle(TR("Nelson"));
setMinimumSize(640, 480);
resize(840, 600);
if (minimized) {
showMinimized();
} else {
show();
}
show();
qtTerminal->show();
bClosed = false;
Expand Down
2 changes: 1 addition & 1 deletion modules/gui/src/cpp/QtMainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class QtMainWindow : public QMainWindow
Q_OBJECT

public:
QtMainWindow();
QtMainWindow(bool minimized = false);
~QtMainWindow() override;

QtTerminal*
Expand Down
2 changes: 1 addition & 1 deletion modules/gui/src/include/MainGuiObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extern "C"
NLSGUI_IMPEXP void
InitGuiObjects(void);
NLSGUI_IMPEXP void*
CreateGuiEvaluator(void* vcontext, NELSON_ENGINE_MODE _mode);
CreateGuiEvaluator(void* vcontext, NELSON_ENGINE_MODE _mode, bool minimizeWindow);
NLSGUI_IMPEXP void
DestroyMainGuiObject(void* term);
NLSGUI_IMPEXP void*
Expand Down
2 changes: 2 additions & 0 deletions modules/interpreter/src/c/nlsInterpreter.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@
<ClCompile Include="..\cpp\MacroFunctionDef.cpp" />
<ClCompile Include="..\cpp\MtimesOperator.cpp" />
<ClCompile Include="..\cpp\NelsonLexer.cpp" />
<ClCompile Include="..\cpp\NelsonReadyNamedMutex.cpp" />
<ClCompile Include="..\cpp\NeOperator.cpp" />
<ClCompile Include="..\cpp\NotOperator.cpp" />
<ClCompile Include="..\cpp\Operators.cpp" />
Expand Down Expand Up @@ -344,6 +345,7 @@
<ClInclude Include="..\include\LocalFunctionsTable.hpp" />
<ClInclude Include="..\include\MacroFunctionDef.hpp" />
<ClInclude Include="..\include\NelsonGateway.hpp" />
<ClInclude Include="..\include\NelsonReadyNamedMutex.hpp" />
<ClInclude Include="..\include\nlsInterpreter_exports.h" />
<ClInclude Include="..\include\Operators.hpp" />
<ClInclude Include="..\include\OverloadCache.hpp" />
Expand Down
6 changes: 6 additions & 0 deletions modules/interpreter/src/c/nlsInterpreter.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@
<ClCompile Include="..\cpp\CallMexBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\cpp\NelsonReadyNamedMutex.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\..\module.iss" />
Expand Down Expand Up @@ -355,6 +358,9 @@
<ClInclude Include="..\cpp\CallMexBuiltin.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\NelsonReadyNamedMutex.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\CMakeLists.txt" />
Expand Down
15 changes: 14 additions & 1 deletion modules/interpreter/src/cpp/Evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
//=============================================================================
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdio>
#include <cerrno>
#include <iostream>
Expand Down Expand Up @@ -109,6 +110,7 @@
#include "ProfilerHelpers.hpp"
#include "ClassToString.hpp"
#include "IsValidVariableName.hpp"
#include "NelsonReadyNamedMutex.hpp"
//=============================================================================
#ifdef _MSC_VER
#define strdup _strdup
Expand All @@ -124,7 +126,7 @@ class endData
ArrayOf endArray;
int index = 0;
size_t count = 0;
endData(ArrayOf p, int ndx, size_t cnt) : endArray(p), index(ndx), count(cnt) {}
endData(ArrayOf p, int ndx, size_t cnt) : endArray(p), index(ndx), count(cnt) { }
~endData() = default;
;
};
Expand Down Expand Up @@ -4086,6 +4088,13 @@ Evaluator::buildPrompt()
return prompt;
}
//=============================================================================
static bool doOnce = true;
void
setNamedMutexNelsonReady()
{
openIsReadyNelsonMutex((int)boost::interprocess::ipcdetail::get_current_process_id());
}
//=============================================================================
void
Evaluator::evalCLI()
{
Expand All @@ -4101,6 +4110,10 @@ Evaluator::evalCLI()
std::wstring commandLine;
commandQueue.get(commandLine);
if (commandLine.empty()) {
if (doOnce) {
setNamedMutexNelsonReady();
doOnce = false;
}
commandLine = io->getLine(buildPrompt());
if (commandLine.empty()) {
InCLI = false;
Expand Down
Loading

0 comments on commit 80a4f10

Please sign in to comment.