Skip to content

Commit

Permalink
move check of program options to precompiled OMCFactory
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25621 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
rfranke committed Apr 19, 2015
1 parent 8f85408 commit d8d901e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 50 deletions.
46 changes: 2 additions & 44 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -1934,19 +1934,15 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
}
#endif

#include <boost/algorithm/string.hpp>

#if defined(_MSC_VER) || defined(__MINGW32__)
#include <tchar.h>
int _tmain(int argc, const _TCHAR* argv[])
#else
int main(int argc, const char* argv[])
#endif
{


// default program options
std::map<std::string, std::string> opts;
std::map<std::string, std::string>::const_iterator it;
opts["-s"] = "<%start%>";
opts["-e"] = "<%end%>";
opts["-f"] = "<%stepsize%>";
Expand All @@ -1957,44 +1953,6 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
opts["-m"] = "<%moLib%>";
opts["-R"] = "<%simulationResults(getRunningTestsuite(),simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)%>";
opts["-o"] = "<%settings.outputFormat%>";
std::vector<const char *> optv;
optv.push_back(argv[0]);
std::string override; // OMEdit override option
for (int i = 1; i < argc; i++) {
if ((it = opts.find(argv[i])) != opts.end() && i < argc - 1)
opts[it->first] = argv[++i]; // regular override
else if (strncmp(argv[i], "-override=", 10) == 0) {
std::map<std::string, std::string> supported = map_list_of
("startTime", "-s")("stopTime", "-e")("stepSize", "-f")
("tolerance", "-y")("solver", "-i")("outputFormat", "-o");
std::vector<std::string> strs;
boost::split(strs, argv[i], boost::is_any_of(",="));
for (int j = 1; j < strs.size(); j++) {
if ((it = supported.find(strs[j])) != supported.end()
&& j < strs.size() - 1) {
opts[it->second] = strs[++j];
}
else {
// leave untreated overrides
if (override.size() > 0)
override += ",";
else
override = "-override=";
override += strs[j];
if (j < strs.size() - 1)
override += "=" + strs[++j];
}
}
if (override.size() > 10)
optv.push_back(override.c_str());
}
else
optv.push_back(argv[i]); // pass through
}
for (it = opts.begin(); it != opts.end(); it++) {
optv.push_back(it->first.c_str());
optv.push_back(it->second.c_str());
}

<%
match(getConfigString(PROFILING_LEVEL))
Expand Down Expand Up @@ -2048,7 +2006,7 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
//SimController to start simulation


std::pair<boost::shared_ptr<ISimController>, SimSettings> simulation = _factory->createSimulation(optv.size(), &optv[0]);
std::pair<boost::shared_ptr<ISimController>, SimSettings> simulation = _factory->createSimulation(argc, argv, opts);

<%if boolNot(stringEq(getConfigString(PROFILING_LEVEL),"none")) then
<<
Expand Down
Expand Up @@ -12,20 +12,28 @@ struct SimSettings;
class OMCFactory
{
public:
OMCFactory();
OMCFactory();
OMCFactory(PATH library_path, PATH modelicasystem_path);
virtual ~OMCFactory();

void UnloadAllLibs(void);
LOADERRESULT LoadLibrary(string libName, type_map& current_map);
LOADERRESULT UnloadLibrary(shared_library lib);

virtual std::pair<boost::shared_ptr<ISimController>,SimSettings> createSimulation(int argc, const char* argv[]);
/**
* Create SimController and SimSettings.
* @param argc number of command line arguments
* @param argv command line arguments of main function
* @param opts default options that are overridden with argv
*/
virtual std::pair<boost::shared_ptr<ISimController>,SimSettings>
createSimulation(int argc, const char* argv[],
std::map<std::string, std::string> &opts);

protected:
SimSettings ReadSimulationParameter(int argc, const char* argv[]);
//boost::shared_ptr<ISimController> _simController;
std::map<string,shared_library> _modules;
PATH _library_path;
PATH _modelicasystem_path;
};
};
48 changes: 45 additions & 3 deletions SimulationRuntime/cpp/SimCoreFactory/OMCFactory/OMCFactory.cpp
Expand Up @@ -2,7 +2,7 @@
#include <SimCoreFactory/Policies/FactoryConfig.h>
#include <SimCoreFactory/OMCFactory/OMCFactory.h>
#include <Core/SimController/ISimController.h>

#include <boost/algorithm/string.hpp>

OMCFactory::OMCFactory(PATH library_path, PATH modelicasystem_path)
: _library_path(library_path)
Expand Down Expand Up @@ -201,9 +201,51 @@ SimSettings OMCFactory::ReadSimulationParameter(int argc, const char* argv[])
return settings;

}
std::pair<boost::shared_ptr<ISimController>,SimSettings> OMCFactory::createSimulation(int argc, const char* argv[])
std::pair<boost::shared_ptr<ISimController>,SimSettings>
OMCFactory::createSimulation(int argc, const char* argv[],
std::map<std::string, std::string> &opts)
{
SimSettings settings = ReadSimulationParameter(argc,argv);
std::map<std::string, std::string>::const_iterator oit;
std::vector<const char *> optv;
optv.push_back(argv[0]);
std::string override; // OMEdit override option
for (int i = 1; i < argc; i++) {
if ((oit = opts.find(argv[i])) != opts.end() && i < argc - 1)
opts[oit->first] = argv[++i]; // regular override
else if (strncmp(argv[i], "-override=", 10) == 0) {
std::map<std::string, std::string> supported = map_list_of
("startTime", "-s")("stopTime", "-e")("stepSize", "-f")
("tolerance", "-y")("solver", "-i")("outputFormat", "-o");
std::vector<std::string> strs;
boost::split(strs, argv[i], boost::is_any_of(",="));
for (int j = 1; j < strs.size(); j++) {
if ((oit = supported.find(strs[j])) != supported.end()
&& j < strs.size() - 1) {
opts[oit->second] = strs[++j];
}
else {
// leave untreated overrides
if (override.size() > 0)
override += ",";
else
override = "-override=";
override += strs[j];
if (j < strs.size() - 1)
override += "=" + strs[++j];
}
}
if (override.size() > 10)
optv.push_back(override.c_str());
}
else
optv.push_back(argv[i]); // pass through
}
for (oit = opts.begin(); oit != opts.end(); oit++) {
optv.push_back(oit->first.c_str());
optv.push_back(oit->second.c_str());
}

SimSettings settings = ReadSimulationParameter(optv.size(), &optv[0]);
type_map simcontroller_type_map;
PATH simcontroller_path = _library_path;
PATH simcontroller_name(SIMCONTROLLER_LIB);
Expand Down

0 comments on commit d8d901e

Please sign in to comment.