Commit 390fab55 authored by GILLES Sebastien's avatar GILLES Sebastien

#1470 Extend associative container print policy to deal as well with the format expected by Lua.

parent 532ccd6a
......@@ -35,6 +35,7 @@ namespace MoReFEM
InputDataNS::Mesh<5>,
InputDataNS::ScalarTransientSource<1>,
InputDataNS::ScalarTransientSource<72>,
InputDataNS::VectorialTransientSource<4>
>;
......
......@@ -100,6 +100,16 @@ TransientSource4 = {
} -- TransientSource4
TransientSource1 = {
nature = "piecewise_constant_by_domain",
value = { [1] = 0., [3] = 14.5, [4] = -31.231 }
} -- TransientSource1
TransientSource72 = {
nature = "lua_function",
......
......@@ -42,9 +42,9 @@ namespace MoReFEM
std::ostringstream oconv;
oconv << "Domains do not contain the expected number of elements: prevision was ";
Utilities::PrintContainer<Utilities::PrintPolicyNS::Associative>::Do(expected, oconv);
Utilities::PrintContainer<Utilities::PrintPolicyNS::Associative<>>::Do(expected, oconv);
oconv << " and what was obtained was: ";
Utilities::PrintContainer<Utilities::PrintPolicyNS::Associative>::Do(obtained, oconv);
Utilities::PrintContainer<Utilities::PrintPolicyNS::Associative<>>::Do(obtained, oconv);
oconv << " (key is domain id, value the number of GeometricElt inside).";
throw Exception(oconv.str(), __FILE__, __LINE__);
......
......@@ -108,7 +108,7 @@ BOOST_AUTO_TEST_CASE(print_associative_containers)
std::map<std::string, unsigned int> map { { "triangle", 3 }, { "segment", 2 }, { "quadrangle", 4 } };
std::ostringstream oconv;
Utilities::PrintContainer<Utilities::PrintPolicyNS::Associative>::Do(map, oconv, ", ", "{ ", " }");
Utilities::PrintContainer<Utilities::PrintPolicyNS::Associative<>>::Do(map, oconv, ", ", "{ ", " }");
BOOST_CHECK_EQUAL(oconv.str(), "{ (quadrangle, 4), (segment, 2), (triangle, 3) }");
}
......
......@@ -18,17 +18,25 @@
namespace MoReFEM::Utilities::PrintPolicyNS
{
/*!
* \brief Enum class to decide the format to adopt.
*
* Two are proposed so far:
* - Default: the format is (key, value), i.e.:
* * Opening by '('
* * Closing by ')'
* * Separation by ", ".
* - Lua: the format is [key] = value
*/
enum class associative_format { Default, Lua };
/*!
* \brief Policy to handle the an associative container.
*
* Currently it is not customizable: the format is (key, value), i.e.:
* - Opening by '('
* - Closing by ')'
* - Separation by ", ".
*
* Of course if need be it is easy to extend this class, but no need currently.
* \tparam FormatT Format to use for the output. It is rather rigid now; it is easy to make it more customizable
* if need be (for instance default one could use different opening, closing or separation characters).
*/
template<associative_format FormatT = associative_format::Default>
struct Associative
{
......
......@@ -16,10 +16,20 @@ namespace MoReFEM::Utilities::PrintPolicyNS
{
template<associative_format FormatT>
template<class ElementTypeT>
void Associative::Do(std::ostream& stream, ElementTypeT&& element)
void Associative<FormatT>::Do(std::ostream& stream, ElementTypeT&& element)
{
stream << '(' << element.first << ", " << element.second << ')';
if constexpr (FormatT == associative_format::Default)
stream << '(' << element.first << ", " << element.second << ')';
else if constexpr (FormatT == associative_format::Lua)
stream << '[' << element.first << "] = " << element.second;
else
{
// Unfortunately static_assert(false) is not supported, so we'll have to do with runtime error.
assert(false);
exit(EXIT_FAILURE);
}
}
......
......@@ -27,17 +27,17 @@ namespace MoReFEM::Internal::PrintPolicyNS
type
>())
{
stream << "\n[[\n";
std::visit([&stream](auto&& arg)
{
LuaFormat::Do(stream, arg);
},
entry);
stream << "\n]]";
}
else if constexpr(Utilities::IsSpecializationOf<Utilities::InputDataNS::LuaFunction, type>())
{
stream << "\n[[\n";
stream << entry.GetString();
stream << "\n]]";
}
else if constexpr (std::is_same<type, std::string>())
{
......@@ -50,7 +50,9 @@ namespace MoReFEM::Internal::PrintPolicyNS
else if constexpr (Utilities::IsSpecializationOf<std::map, type>())
{
// Map can only called on the end of the recursion, contrary to vector.
Utilities::PrintContainer<::MoReFEM::Utilities::PrintPolicyNS::Associative>::Do(entry, stream);
using policy =
::MoReFEM::Utilities::PrintPolicyNS::Associative<Utilities::PrintPolicyNS::associative_format::Lua>;
Utilities::PrintContainer<policy>::Do(entry, stream, ", ", '{', '}');
}
else if constexpr (Utilities::IsSpecializationOf<std::vector, type>())
{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment