Skip to content

Commit

Permalink
Initial implementation of String vars in the Cpp runtime
Browse files Browse the repository at this point in the history
Belonging to [master]:
  - OpenModelica/OMCompiler#2992
  • Loading branch information
atrosinenko authored and OpenModelica-Hudson committed Apr 9, 2019
1 parent 963b33e commit 9a506ce
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
23 changes: 23 additions & 0 deletions SimulationRuntime/cpp/Core/System/DiscreteEvents.cpp
Expand Up @@ -61,6 +61,15 @@ void DiscreteEvents::save(bool& var,double value)
_sim_vars->getPreVar(var) = value;
}

/**
Saves a variable in _sim_vars->_pre_string_vars vector
*/
void DiscreteEvents::save(std::string& var,const std::string& value)
{
_sim_vars->getPreVar(var) = value;
}


/**
Implementation of the Modelica pre operator
*/
Expand All @@ -85,6 +94,14 @@ bool& DiscreteEvents::pre(const bool& var)
return _sim_vars->getPreVar(var);
}

/**
Implementation of the Modelica pre operator
*/
std::string& DiscreteEvents::pre(const std::string& var)
{
return _sim_vars->getPreVar(var);
}

/**
Implementation of the Modelica edge operator
Returns true for a variable when it changes from false to true
Expand Down Expand Up @@ -157,5 +174,11 @@ bool DiscreteEvents::changeDiscreteVar(bool& var)
return var != pre_var;
}

bool DiscreteEvents::changeDiscreteVar(std::string& var)
{
std::string& pre_var = _sim_vars->getPreVar(var);
return var != pre_var;
}

/** @} */ // end of coreSystem

19 changes: 19 additions & 0 deletions SimulationRuntime/cpp/Core/System/SimVars.cpp
Expand Up @@ -49,9 +49,11 @@ void SimVars::create(size_t dim_real, size_t dim_int, size_t dim_bool, size_t di
//allocate memory for all model variables
if (dim_string > 0) {
_string_vars = new string[dim_string];
_pre_string_vars = new string[dim_string];
}
else {
_string_vars = NULL;
_pre_string_vars = NULL;
}
if (dim_bool > 0) {
_bool_vars = (bool*)alignedMalloc(sizeof(bool) * dim_bool, 64);
Expand Down Expand Up @@ -105,6 +107,8 @@ SimVars::~SimVars()
alignedFree(_bool_vars);
if(_string_vars)
delete [] _string_vars;
if(_pre_string_vars)
delete [] _pre_string_vars;
}

ISimVars* SimVars::clone()
Expand Down Expand Up @@ -203,6 +207,13 @@ const bool& SimVars::getBoolVar(size_t i)
else
throw std::runtime_error("Wrong variable index");
}
const std::string& SimVars::getStringVar(size_t i)
{
if (i < _dim_string)
return _string_vars[i];
else
throw std::runtime_error("Wrong variable index");
}

string& SimVars::initStringVar(size_t i)
{
Expand Down Expand Up @@ -445,6 +456,8 @@ void SimVars::savePreVariables()
std::copy(_int_vars, _int_vars + _dim_int, _pre_int_vars);
if (_dim_bool > 0)
std::copy(_bool_vars, _bool_vars + _dim_bool, _pre_bool_vars);
if (_dim_string > 0)
std::copy(_string_vars, _string_vars + _dim_string, _pre_string_vars);
}
/**
* \brief Initializes access to pre variables
Expand Down Expand Up @@ -473,6 +486,12 @@ bool& SimVars::getPreVar(const bool& var)
return _pre_bool_vars[i];
}

std::string& SimVars::getPreVar(const std::string& var)
{
size_t i = &var - _string_vars;
return _pre_string_vars[i];
}

/**\brief returns a pointer to a real simvar variable in simvar array
* \param [in] i index of simvar in simvar array
* \return pointer to simvar
Expand Down
3 changes: 3 additions & 0 deletions SimulationRuntime/cpp/Include/Core/System/DiscreteEvents.h
Expand Up @@ -21,13 +21,15 @@ class BOOST_EXTENSION_EVENTHANDLING_DECL DiscreteEvents
void save(double& var,double value);
void save(int& var,double value);
void save(bool& var,double value);
void save(std::string& var,const std::string& value);
//void savePreVars(double vars [], unsigned int n);


//Implementation of the Modelica pre operator
double& pre(const double& var);
int& pre(const int& var);
bool& pre(const bool& var);
std::string& pre(const std::string& var);
//Implementation of the Modelica edge operator
bool edge(double& var);
bool edge(int& var);
Expand All @@ -41,6 +43,7 @@ class BOOST_EXTENSION_EVENTHANDLING_DECL DiscreteEvents
bool changeDiscreteVar(double& var);
bool changeDiscreteVar(int& var);
bool changeDiscreteVar(bool& var);
bool changeDiscreteVar(std::string& var);
//getCondition_type getCondition;

private:
Expand Down
2 changes: 2 additions & 0 deletions SimulationRuntime/cpp/Include/Core/System/ISimVars.h
Expand Up @@ -53,6 +53,7 @@ class ISimVars
virtual const double& getRealVar(size_t i) = 0;
virtual const int& getIntVar(size_t i)= 0;
virtual const bool& getBoolVar(size_t i)= 0;
virtual const std::string& getStringVar(size_t i) = 0;

/*Methods for initialize scalar model variables in simvars memory*/
virtual double& initRealVar(size_t i) = 0;
Expand All @@ -67,5 +68,6 @@ class ISimVars
virtual double& getPreVar(const double& var)=0;
virtual int& getPreVar(const int& var)=0;
virtual bool& getPreVar(const bool& var)=0;
virtual std::string& getPreVar(const std::string& var)=0;
};
/** @} */ // end of coreSystem
3 changes: 3 additions & 0 deletions SimulationRuntime/cpp/Include/Core/System/SimVars.h
Expand Up @@ -93,6 +93,7 @@ class BOOST_EXTENSION_SIMVARS_DECL SimVars: public ISimVars
virtual const double& getRealVar(size_t i);
virtual const int& getIntVar(size_t i);
virtual const bool& getBoolVar(size_t i);
virtual const std::string& getStringVar(size_t i);
virtual double* initRealArrayVar(size_t size, size_t start_index);
virtual int* initIntArrayVar(size_t size, size_t start_index);
virtual bool* initBoolArrayVar(size_t size, size_t start_index);
Expand All @@ -110,6 +111,7 @@ class BOOST_EXTENSION_SIMVARS_DECL SimVars: public ISimVars
virtual double& getPreVar(const double& var);
virtual int& getPreVar(const int& var);
virtual bool& getPreVar(const bool& var);
virtual std::string& getPreVar(const std::string& var);

virtual size_t getDimString() const;
virtual size_t getDimBool() const;
Expand Down Expand Up @@ -145,6 +147,7 @@ class BOOST_EXTENSION_SIMVARS_DECL SimVars: public ISimVars
double* _pre_real_vars;
int* _pre_int_vars;
bool* _pre_bool_vars;
std::string* _pre_string_vars;
};

/** @} */ // end of coreSystem
Expand Down

0 comments on commit 9a506ce

Please sign in to comment.