Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 9a506ce

Browse files
atrosinenkoOpenModelica-Hudson
authored andcommitted
Initial implementation of String vars in the Cpp runtime
Belonging to [master]: - #2992
1 parent 963b33e commit 9a506ce

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

SimulationRuntime/cpp/Core/System/DiscreteEvents.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ void DiscreteEvents::save(bool& var,double value)
6161
_sim_vars->getPreVar(var) = value;
6262
}
6363

64+
/**
65+
Saves a variable in _sim_vars->_pre_string_vars vector
66+
*/
67+
void DiscreteEvents::save(std::string& var,const std::string& value)
68+
{
69+
_sim_vars->getPreVar(var) = value;
70+
}
71+
72+
6473
/**
6574
Implementation of the Modelica pre operator
6675
*/
@@ -85,6 +94,14 @@ bool& DiscreteEvents::pre(const bool& var)
8594
return _sim_vars->getPreVar(var);
8695
}
8796

97+
/**
98+
Implementation of the Modelica pre operator
99+
*/
100+
std::string& DiscreteEvents::pre(const std::string& var)
101+
{
102+
return _sim_vars->getPreVar(var);
103+
}
104+
88105
/**
89106
Implementation of the Modelica edge operator
90107
Returns true for a variable when it changes from false to true
@@ -157,5 +174,11 @@ bool DiscreteEvents::changeDiscreteVar(bool& var)
157174
return var != pre_var;
158175
}
159176

177+
bool DiscreteEvents::changeDiscreteVar(std::string& var)
178+
{
179+
std::string& pre_var = _sim_vars->getPreVar(var);
180+
return var != pre_var;
181+
}
182+
160183
/** @} */ // end of coreSystem
161184

SimulationRuntime/cpp/Core/System/SimVars.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ void SimVars::create(size_t dim_real, size_t dim_int, size_t dim_bool, size_t di
4949
//allocate memory for all model variables
5050
if (dim_string > 0) {
5151
_string_vars = new string[dim_string];
52+
_pre_string_vars = new string[dim_string];
5253
}
5354
else {
5455
_string_vars = NULL;
56+
_pre_string_vars = NULL;
5557
}
5658
if (dim_bool > 0) {
5759
_bool_vars = (bool*)alignedMalloc(sizeof(bool) * dim_bool, 64);
@@ -105,6 +107,8 @@ SimVars::~SimVars()
105107
alignedFree(_bool_vars);
106108
if(_string_vars)
107109
delete [] _string_vars;
110+
if(_pre_string_vars)
111+
delete [] _pre_string_vars;
108112
}
109113

110114
ISimVars* SimVars::clone()
@@ -203,6 +207,13 @@ const bool& SimVars::getBoolVar(size_t i)
203207
else
204208
throw std::runtime_error("Wrong variable index");
205209
}
210+
const std::string& SimVars::getStringVar(size_t i)
211+
{
212+
if (i < _dim_string)
213+
return _string_vars[i];
214+
else
215+
throw std::runtime_error("Wrong variable index");
216+
}
206217

207218
string& SimVars::initStringVar(size_t i)
208219
{
@@ -445,6 +456,8 @@ void SimVars::savePreVariables()
445456
std::copy(_int_vars, _int_vars + _dim_int, _pre_int_vars);
446457
if (_dim_bool > 0)
447458
std::copy(_bool_vars, _bool_vars + _dim_bool, _pre_bool_vars);
459+
if (_dim_string > 0)
460+
std::copy(_string_vars, _string_vars + _dim_string, _pre_string_vars);
448461
}
449462
/**
450463
* \brief Initializes access to pre variables
@@ -473,6 +486,12 @@ bool& SimVars::getPreVar(const bool& var)
473486
return _pre_bool_vars[i];
474487
}
475488

489+
std::string& SimVars::getPreVar(const std::string& var)
490+
{
491+
size_t i = &var - _string_vars;
492+
return _pre_string_vars[i];
493+
}
494+
476495
/**\brief returns a pointer to a real simvar variable in simvar array
477496
* \param [in] i index of simvar in simvar array
478497
* \return pointer to simvar

SimulationRuntime/cpp/Include/Core/System/DiscreteEvents.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ class BOOST_EXTENSION_EVENTHANDLING_DECL DiscreteEvents
2121
void save(double& var,double value);
2222
void save(int& var,double value);
2323
void save(bool& var,double value);
24+
void save(std::string& var,const std::string& value);
2425
//void savePreVars(double vars [], unsigned int n);
2526

2627

2728
//Implementation of the Modelica pre operator
2829
double& pre(const double& var);
2930
int& pre(const int& var);
3031
bool& pre(const bool& var);
32+
std::string& pre(const std::string& var);
3133
//Implementation of the Modelica edge operator
3234
bool edge(double& var);
3335
bool edge(int& var);
@@ -41,6 +43,7 @@ class BOOST_EXTENSION_EVENTHANDLING_DECL DiscreteEvents
4143
bool changeDiscreteVar(double& var);
4244
bool changeDiscreteVar(int& var);
4345
bool changeDiscreteVar(bool& var);
46+
bool changeDiscreteVar(std::string& var);
4447
//getCondition_type getCondition;
4548

4649
private:

SimulationRuntime/cpp/Include/Core/System/ISimVars.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class ISimVars
5353
virtual const double& getRealVar(size_t i) = 0;
5454
virtual const int& getIntVar(size_t i)= 0;
5555
virtual const bool& getBoolVar(size_t i)= 0;
56+
virtual const std::string& getStringVar(size_t i) = 0;
5657

5758
/*Methods for initialize scalar model variables in simvars memory*/
5859
virtual double& initRealVar(size_t i) = 0;
@@ -67,5 +68,6 @@ class ISimVars
6768
virtual double& getPreVar(const double& var)=0;
6869
virtual int& getPreVar(const int& var)=0;
6970
virtual bool& getPreVar(const bool& var)=0;
71+
virtual std::string& getPreVar(const std::string& var)=0;
7072
};
7173
/** @} */ // end of coreSystem

SimulationRuntime/cpp/Include/Core/System/SimVars.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class BOOST_EXTENSION_SIMVARS_DECL SimVars: public ISimVars
9393
virtual const double& getRealVar(size_t i);
9494
virtual const int& getIntVar(size_t i);
9595
virtual const bool& getBoolVar(size_t i);
96+
virtual const std::string& getStringVar(size_t i);
9697
virtual double* initRealArrayVar(size_t size, size_t start_index);
9798
virtual int* initIntArrayVar(size_t size, size_t start_index);
9899
virtual bool* initBoolArrayVar(size_t size, size_t start_index);
@@ -110,6 +111,7 @@ class BOOST_EXTENSION_SIMVARS_DECL SimVars: public ISimVars
110111
virtual double& getPreVar(const double& var);
111112
virtual int& getPreVar(const int& var);
112113
virtual bool& getPreVar(const bool& var);
114+
virtual std::string& getPreVar(const std::string& var);
113115

114116
virtual size_t getDimString() const;
115117
virtual size_t getDimBool() const;
@@ -145,6 +147,7 @@ class BOOST_EXTENSION_SIMVARS_DECL SimVars: public ISimVars
145147
double* _pre_real_vars;
146148
int* _pre_int_vars;
147149
bool* _pre_bool_vars;
150+
std::string* _pre_string_vars;
148151
};
149152

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

0 commit comments

Comments
 (0)