Skip to content
Permalink
Browse files

Generator: removed our use of templates.

  • Loading branch information...
agarny committed Feb 12, 2019
1 parent c6ad173 commit f859acb96ca13c80f32a04aebc7651094cdeaa01
Showing with 81 additions and 102 deletions.
  1. +1 −17 src/api/libcellml/generator.h
  2. +80 −85 src/generator.cpp
@@ -70,29 +70,13 @@ struct EnumClassHash
}
};

class LIBCELLML_EXPORT CXX
{
public:
enum class types {void_t, double_t, double_ct, double_pt, double_rt};

static std::string returnType(types t);
static std::string argType(types t);
static std::string argListOp() {return "(";}
static std::string argListCl() {return ")";}
static std::string funBodyOp() {return "{";}
static std::string funBodyCl() {return "}";}
static std::string instructionDelimiter() {return ";";}
static std::string dereferenceOp() {return "*";}
};

class LIBCELLML_EXPORT Generator : public Logger
{
public:
Generator();
virtual ~Generator();

template <typename L = CXX>
std::string generateCode(ModelPtr m);
std::string generateCode(ModelPtr m);
void writeCodeToFile(std::string filename);

private:
@@ -21,54 +21,32 @@ namespace libcellml{

using namespace libcellml::operators;

std::string CXX::returnType(types t)
{
static const std::unordered_map<types, std::string, EnumClassHash> returnTypes = {
{types::void_t,"void "},
{types::double_t, "double "},
{types::double_ct, "const double "},
{types::double_pt, "double *"},
{types::double_rt, "double &"}
};

return returnTypes.at(t);
}

std::string CXX::argType(types t)
{
static const std::unordered_map<types, std::string, EnumClassHash> argTypes = {
{types::void_t,"void "},
{types::double_t, "double "},
{types::double_ct, "const double "},
{types::double_pt, "double *"},
{types::double_rt, "double &"},
};

return argTypes.at(t);
}

struct Generator::GeneratorImpl
{
enum class types {void_t, double_t, double_ct, double_pt, double_rt};

Generator* mGenerator;

void findVOI(std::string math);
void findVOIHelper(XmlNodePtr node);
void findInitialValues(ComponentPtr c);
std::shared_ptr<libcellml::operators::Representable> parseMathML(std::string math);
std::shared_ptr<libcellml::operators::Representable> parseNode(XmlNodePtr node);
template <typename L = CXX>
std::string doGenerateCode(ModelPtr m);
template <typename L = CXX>
std::string generateInitConsts();
template <typename L = CXX>
std::string generateComputeRates(
std::shared_ptr<libcellml::operators::Representable> r);
template <typename L = CXX>
std::string generateComputeVariables();
template <typename L = CXX>
std::string generateStateAliases();
template <typename L = CXX>
std::string generateVoiAlias();
std::string doGenerateCode(ModelPtr m);
std::string generateInitConsts();
std::string generateComputeRates(std::shared_ptr<libcellml::operators::Representable> r);
std::string generateComputeVariables();
std::string generateStateAliases();
std::string generateVoiAlias();

std::string returnType(types t);
std::string argType(types t);
std::string argListOp() {return "(";}
std::string argListCl() {return ")";}
std::string funBodyOp() {return "{";}
std::string funBodyCl() {return "}";}
std::string instructionDelimiter() {return ";";}
std::string dereferenceOp() {return "*";}

std::string mVoi;
std::vector<std::string> mStates;
@@ -87,77 +65,73 @@ Generator::~Generator()
delete mPimpl;
}

template<typename L>
std::string Generator::GeneratorImpl::generateStateAliases()
{
std::string s;
std::ostringstream oss(s);
for (size_t i = 0; i < mStates.size(); i++)
{
oss << " "
<< L::argType(L::types::double_rt) << mStates[i] << " = " << L::dereferenceOp() << "(states + " << i
<< ")" << L::instructionDelimiter() << std::endl;
<< argType(types::double_rt) << mStates[i] << " = " << dereferenceOp() << "(states + " << i
<< ")" << instructionDelimiter() << std::endl;
}
oss << std::endl;
return oss.str();
}

template<typename L>
std::string Generator::GeneratorImpl::generateVoiAlias()
{
std::string s;
std::ostringstream oss(s);
oss << " "
<< L::argType(L::types::double_ct) << mVoi << " = voi" << L::instructionDelimiter() << std::endl;
<< argType(types::double_ct) << mVoi << " = voi" << instructionDelimiter() << std::endl;
oss << std::endl;
return oss.str();
}

template<typename L>
std::string Generator::GeneratorImpl::generateInitConsts()
{
std::string s;
std::ostringstream oss(s);
oss << L::returnType(L::types::void_t) << "initConsts"
<< L::argListOp()
<< L::argType(L::types::double_pt)
oss << returnType(types::void_t) << "initConsts"
<< argListOp()
<< argType(types::double_pt)
<< "constants, "
<< L::argType(L::types::double_pt)
<< argType(types::double_pt)
<< "rates, "
<< L::argType(L::types::double_pt)
<< argType(types::double_pt)
<< "states"
<< L::argListCl() << std::endl
<< L::funBodyOp() << std::endl;
<< argListCl() << std::endl
<< funBodyOp() << std::endl;

oss << generateStateAliases() << std::endl;
for (auto s : mInitialValues)
{
oss << " " << s.first << " = "
<< std::setprecision(16) << s.second << L::instructionDelimiter() << std::endl;
<< std::setprecision(16) << s.second << instructionDelimiter() << std::endl;
}
oss << std::endl << L::funBodyCl();
oss << std::endl << funBodyCl();
return oss.str();
}

template<typename L>
std::string Generator::GeneratorImpl::generateComputeRates(std::shared_ptr<Representable> r)
{
std::string s;
std::ostringstream oss(s);
oss << L::returnType(L::types::void_t) << "computeRates"
<< L::argListOp()
<< L::argType(L::types::double_t)
oss << returnType(types::void_t) << "computeRates"
<< argListOp()
<< argType(types::double_t)
<< "voi, "
<< L::argType(L::types::double_pt)
<< argType(types::double_pt)
<< "constants, "
<< L::argType(L::types::double_pt)
<< argType(types::double_pt)
<< "rates, "
<< L::argType(L::types::double_pt)
<< argType(types::double_pt)
<< "states, "
<< L::argType(L::types::double_pt)
<< argType(types::double_pt)
<< "algebraic"
<< L::argListCl() << std::endl
<< L::funBodyOp() << std::endl;
<< argListCl() << std::endl
<< funBodyOp() << std::endl;

oss << generateVoiAlias() << std::endl;
oss << generateStateAliases() << std::endl;
@@ -166,30 +140,29 @@ std::string Generator::GeneratorImpl::generateComputeRates(std::shared_ptr<Repre
<< r->repr() << ";" << std::endl;

oss << std::endl
<< L::funBodyCl();
<< funBodyCl();
return oss.str();
}

template<typename L>
std::string Generator::GeneratorImpl::generateComputeVariables()
{
std::string s;
std::ostringstream oss(s);
oss << L::returnType(L::types::void_t) << "computeVariables"
<< L::argListOp()
<< L::argType(L::types::double_t)
oss << returnType(types::void_t) << "computeVariables"
<< argListOp()
<< argType(types::double_t)
<< "voi, "
<< L::argType(L::types::double_pt)
<< argType(types::double_pt)
<< "constants, "
<< L::argType(L::types::double_pt)
<< argType(types::double_pt)
<< "rates, "
<< L::argType(L::types::double_pt)
<< argType(types::double_pt)
<< "states, "
<< L::argType(L::types::double_pt)
<< argType(types::double_pt)
<< "algebraic"
<< L::argListCl() << std::endl
<< L::funBodyOp() << std::endl
<< L::funBodyCl();
<< argListCl() << std::endl
<< funBodyOp() << std::endl
<< funBodyCl();
return oss.str();
}

@@ -205,29 +178,53 @@ void Generator::GeneratorImpl::findInitialValues(ComponentPtr c)
}
}

template<typename L>
std::string Generator::GeneratorImpl::doGenerateCode(ModelPtr m)
{
ComponentPtr c = m->getComponent(0);

findVOI(c->getMath());
findInitialValues(c);
auto r = parseMathML(c->getMath());

std::string generatedCode;
std::ostringstream oss(generatedCode);
oss << generateInitConsts<L>() << std::endl;
oss << generateComputeRates<L>(r) << std::endl;
oss << generateComputeVariables<L>() << std::endl;
oss << generateInitConsts() << std::endl;
oss << generateComputeRates(r) << std::endl;
oss << generateComputeVariables() << std::endl;

mCode = oss.str();
return mCode;
}

template <typename L>
std::string Generator::GeneratorImpl::returnType(types t)
{
static const std::unordered_map<types, std::string, EnumClassHash> returnTypes = {
{types::void_t,"void "},
{types::double_t, "double "},
{types::double_ct, "const double "},
{types::double_pt, "double *"},
{types::double_rt, "double &"}
};

return returnTypes.at(t);
}

std::string Generator::GeneratorImpl::argType(types t)
{
static const std::unordered_map<types, std::string, EnumClassHash> argTypes = {
{types::void_t,"void "},
{types::double_t, "double "},
{types::double_ct, "const double "},
{types::double_pt, "double *"},
{types::double_rt, "double &"},
};

return argTypes.at(t);
}

std::string Generator::generateCode(ModelPtr m)
{
return mPimpl->doGenerateCode<L>(m);
return mPimpl->doGenerateCode(m);
}

void Generator::writeCodeToFile(std::string filename)
@@ -433,6 +430,4 @@ const char * UnknownNode::what () const throw ()
return "Found node of unknown type";
}

template std::string Generator::generateCode<CXX>(ModelPtr m);

}

0 comments on commit f859acb

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.