Skip to content

Commit d7a8b77

Browse files
committed
Constrain InputParameterContainer supported types
1 parent f0180ff commit d7a8b77

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/core/io/src/4C_io_input_parameter_container.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ namespace Core::IO
5656
/**
5757
* \brief Add @data to the container at the given key @name.
5858
*
59-
* If an entry with given @p name already exists, it will be overwritten.
59+
* If an entry with given @p name already exists, it will be overwritten. The type must be
60+
* one of the SupportedType of the input framework.
6061
*/
6162
template <typename T>
6263
void add(const std::string& name, const T& data);
@@ -77,7 +78,7 @@ namespace Core::IO
7778
[[nodiscard]] auto groups() const;
7879

7980
/**
80-
* Check if a group with the name @p name exists.
81+
* Check if a group with the given @p name exists.
8182
*/
8283
[[nodiscard]] bool has_group(const std::string& name) const;
8384

src/core/io/src/4C_io_input_parameter_container.templates.hpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,34 @@ FOUR_C_NAMESPACE_OPEN
2222
// into this special templates file.
2323
namespace Core::IO::Internal::InputParameterContainerImplementation
2424
{
25-
// Default printer if not printable.
2625
template <typename T>
27-
struct PrintHelper
28-
{
29-
void operator()(std::ostream& os, const std::any& data) { os << "<not printable> "; }
26+
concept StreamInsertable = requires(std::ostream& os, const std::any& data) {
27+
{ os << std::any_cast<T>(data) };
3028
};
3129

30+
// Helper struct to print the data in the container.
3231
template <typename T>
33-
concept StreamInsertable = requires(std::ostream& os, const T& t) { os << t; };
34-
35-
// Specialization for stream insert.
36-
template <StreamInsertable T>
37-
struct PrintHelper<T>
32+
struct PrintHelper
3833
{
39-
void operator()(std::ostream& os, const std::any& data) { os << std::any_cast<T>(data) << " "; }
34+
void operator()(std::ostream& os, const std::any& data) const
35+
{
36+
if constexpr (StreamInsertable<std::remove_reference_t<T>> || std::is_enum_v<T>)
37+
{
38+
using EnumTools::operator<<;
39+
os << std::any_cast<T>(data) << " ";
40+
}
41+
else
42+
{
43+
os << "<not printable> ";
44+
}
45+
}
4046
};
4147

42-
template <StreamInsertable T>
48+
// Specialization for std::optional.
49+
template <SupportedType T>
4350
struct PrintHelper<std::optional<T>>
4451
{
45-
void operator()(std::ostream& os, const std::any& data)
52+
void operator()(std::ostream& os, const std::any& data) const
4653
{
4754
auto val = std::any_cast<std::optional<T>>(data);
4855
if (val.has_value())
@@ -53,10 +60,10 @@ namespace Core::IO::Internal::InputParameterContainerImplementation
5360
};
5461

5562
// Specialization for vectors.
56-
template <typename T>
63+
template <SupportedType T>
5764
struct PrintHelper<std::vector<T>>
5865
{
59-
void operator()(std::ostream& os, const std::any& data)
66+
void operator()(std::ostream& os, const std::any& data) const
6067
{
6168
FOUR_C_ASSERT(typeid(std::vector<T>) == data.type(), "Implementation error.");
6269
const auto& vec = std::any_cast<std::vector<T>>(data);
@@ -68,10 +75,10 @@ namespace Core::IO::Internal::InputParameterContainerImplementation
6875
};
6976

7077
// Specialization for maps.
71-
template <typename Key, typename Value>
78+
template <typename Key, SupportedType Value>
7279
struct PrintHelper<std::map<Key, Value>>
7380
{
74-
void operator()(std::ostream& os, const std::any& data)
81+
void operator()(std::ostream& os, const std::any& data) const
7582
{
7683
FOUR_C_ASSERT(typeid(std::map<Key, Value>) == data.type(), "Implementation error.");
7784
const auto& map = std::any_cast<std::map<Key, Value>>(data);

0 commit comments

Comments
 (0)