Skip to content

Commit

Permalink
Merge pull request #515 from thelfer/514-material-properties-add-tryc…
Browse files Browse the repository at this point in the history
…atch-blocks-in-interfaces-for-the-c-family-fortran-c-cast3m

Fix Issue #514
  • Loading branch information
thelfer authored Feb 20, 2024
2 parents de924b7 + 39420b4 commit 369c892
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 23 deletions.
1 change: 1 addition & 0 deletions docs/web/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ pandoc_html(release-notes-4.0.2 "--toc" "--toc-depth=2" "--number-sections")
pandoc_html(release-notes-4.0.3 "--toc" "--toc-depth=2" "--number-sections")
pandoc_html(release-notes-4.1 "--toc" "--toc-depth=2" "--number-sections")
pandoc_html(release-notes-4.1.1 "--toc" "--toc-depth=2" "--number-sections")
pandoc_html(release-notes-4.1.2 "--toc" "--toc-depth=2" "--number-sections")
pandoc_html(release-notes-4.2 "--toc" "--toc-depth=2" "--number-sections")
pandoc_html(release-notes-4.2.1 "--toc" "--toc-depth=2" "--number-sections")
pandoc_html(theory "--toc" "--toc-depth=2")
Expand Down
1 change: 1 addition & 0 deletions docs/web/mfront-template.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
<ul>
<li><a href="release-notes-4.1.html">Version 4.1.0</a></li>
<li><a href="release-notes-4.1.1.html">Version 4.1.1</a></li>
<li><a href="release-notes-4.1.2.html">Version 4.1.2</a></li>
</ul>
</li>
</ul>
Expand Down
24 changes: 24 additions & 0 deletions docs/web/release-notes-4.1.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: Release notes of the 4.1.2 version of `TFEL`, `MFront` and `MTest`
author: Thomas Helfer
date: 2024
lang: en-EN
numbersections: true
documentclass: article
from: markdown+tex_math_single_backslash
geometry:
- margin=2cm
papersize: a4
link-citations: true
colorlinks: true
figPrefixTemplate: "$$i$$"
tblPrefixTemplate: "$$i$$"
secPrefixTemplate: "$$i$$"
eqnPrefixTemplate: "($$i$$)"
---

# Issues fixed

## Issue 514: [material-properties] add try/catch blocks in interfaces for the C family (fortran, C, Cast3M)

For more details, see <https://github.com/thelfer/tfel/issues/514.
33 changes: 31 additions & 2 deletions mfront/include/MFront/CMaterialPropertyInterfaceBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,36 @@ namespace mfront {
const MaterialPropertyDescription&,
const std::string_view,
const bool) const;

/*!
* \brief write the `try` statement before the material property body
* \param[out] os: output file stream
*/
virtual void writeCxxTryBlock(std::ostream&) const;
/*!
* \brief write catch blocks before the material property body
* \param[out] os: output file stream
* \param[in] mpd: material property description
* \param[in] floating_point_type: floating-point type
* \param[in] use_qt: use quantities
*/
virtual void writeCxxCatchBlock(std::ostream&,
const MaterialPropertyDescription&,
const std::string_view,
const bool) const;
/*!
* \brief write catch blocks before the material property body
* \param[out] os: output file stream
* \param[in] msh: code returning the error message (a string must be escaped)
* \param[in] mpd: material property description
* \param[in] floating_point_type: floating-point type
* \param[in] use_qt: use quantities
*/
virtual void returnInvalidValue(std::ostream&,
std::string_view,
const MaterialPropertyDescription&,
const std::string_view,
const bool) const;
//
virtual std::string getCallingConvention() const;
/*!
* \return the name used to generate the symbols associated with the
Expand All @@ -270,7 +299,7 @@ namespace mfront {
virtual void writeSrcFile(const MaterialPropertyDescription&,
const FileDescription&) const;

}; // end of MfrontCMaterialPropertyInterfaceBase
}; // end of MfrontCMaterialPropertyInterfaceBase

} // end of namespace mfront

Expand Down
72 changes: 51 additions & 21 deletions mfront/src/CMaterialPropertyInterfaceBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -566,24 +566,27 @@ namespace mfront {
<< "errno=0;\n"
<< "#endif /* MFRONT_NOERRNO_HANDLING */\n";
}
os << mpd.output.type << " " << mpd.output.name << ";\n"
<< mpd.f.body << "\n";
if (!mpd.inputs.empty()) {
os << "#ifndef MFRONT_NOERRNO_HANDLING\n";
// can't use std::swap here as errno might be a macro
os << "const auto mfront_errno = errno;\n"
<< "errno = mfront_errno_old;\n";
if (use_qt) {
os << "if((mfront_errno != 0)||"
<< "(!tfel::math::ieee754::isfinite(" << mpd.output.name
<< ".getValue()))){\n";
} else {
os << "if((mfront_errno != 0)||"
<< "(!tfel::math::ieee754::isfinite(" << mpd.output.name << "))){\n";
}
this->writeCErrorTreatment(os, mpd, floating_point_type, use_qt);
os << "}\n"
<< "#endif /* MFRONT_NOERRNO_HANDLING */\n";

os << "auto " << mpd.output.name << " = " << mpd.output.type << "{};\n";
this->writeCxxTryBlock(os);
os << mpd.f.body << "\n";
this->writeCxxCatchBlock(os, mpd, floating_point_type, use_qt);
if (!mpd.inputs.empty()) {
os << "#ifndef MFRONT_NOERRNO_HANDLING\n";
// can't use std::swap here as errno might be a macro
os << "const auto mfront_errno = errno;\n"
<< "errno = mfront_errno_old;\n";
if (use_qt) {
os << "if((mfront_errno != 0)||"
<< "(!tfel::math::ieee754::isfinite(" << mpd.output.name
<< ".getValue()))){\n";
} else {
os << "if((mfront_errno != 0)||"
<< "(!tfel::math::ieee754::isfinite(" << mpd.output.name << "))){\n";
}
this->writeCErrorTreatment(os, mpd, floating_point_type, use_qt);
os << "}\n"
<< "#endif /* MFRONT_NOERRNO_HANDLING */\n";
}
if ((useQuantities(mpd)) && (!use_qt)) {
os << "return " << mpd.output.name << ".getValue();\n";
Expand Down Expand Up @@ -612,22 +615,49 @@ namespace mfront {
os << "return 0;\n";
} // end of writeMaterialPropertyCheckBoundsBody

void CMaterialPropertyInterfaceBase::writeCxxTryBlock(
std::ostream& os) const {
os << "try{\n";
} // end of writeCxxCatchBlock

void CMaterialPropertyInterfaceBase::writeCxxCatchBlock(
std::ostream& os,
const MaterialPropertyDescription& mpd,
const std::string_view floating_point_type,
const bool use_qt) const {
os << "} catch(std::exception&){\n";
this->returnInvalidValue(os, "e.what()", mpd, floating_point_type, use_qt);
os << "} catch(...){\n";
this->returnInvalidValue(os, "\"unsupported C++ exception\"", mpd,
floating_point_type, use_qt);
os << "}\n";
} // end of writeCxxCatchBlock

void CMaterialPropertyInterfaceBase::writeCErrorTreatment(
std::ostream& os,
const MaterialPropertyDescription& mpd,
const std::string_view floating_point_type,
const bool use_qt) const {
const auto* msg = "\"invalid call to a C function (errno is not null)\"";
this->returnInvalidValue(os, msg, mpd, floating_point_type, use_qt);
} // end of writeCErrorTreatment

void CMaterialPropertyInterfaceBase::returnInvalidValue(
std::ostream& os,
std::string_view,
const MaterialPropertyDescription& mpd,
const std::string_view floating_point_type,
const bool use_qt) const {
os << "return ";
if (use_qt) {
os << getOutputType(mpd, std::string{floating_point_type}, true) << "{";
}
os << "std::nan(\"" << this->getFunctionName(mpd)
<< ": invalid call to a C function (errno is not null)\")";
os << "std::nan(\"\")";
if (use_qt) {
os << "}";
}
os << ";\n";
} // end of writeCErrorTreatment
} // end of returnInvalidValue

CMaterialPropertyInterfaceBase::~CMaterialPropertyInterfaceBase() = default;

Expand Down

0 comments on commit 369c892

Please sign in to comment.