Skip to content

Commit ca9fceb

Browse files
committed
Some improvements
1 parent 07bbcca commit ca9fceb

1 file changed

Lines changed: 68 additions & 44 deletions

File tree

src/IO/ADIOS/ADIOS2IOHandler.cpp

Lines changed: 68 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include <stdexcept>
5656
#include <string>
5757
#include <type_traits>
58+
#include <typeinfo>
5859
#include <variant>
5960

6061
namespace openPMD
@@ -100,56 +101,66 @@ namespace
100101
}
101102

102103
template <typename T>
103-
inline std::string formatValue(T const &value);
104-
105-
// Specialization for strings
106-
template <>
107-
inline std::string formatValue(std::string const &value)
108-
{
109-
return "\"" + value + "\"";
110-
}
111-
112-
// Specialization for basic types
113-
template <>
114-
inline std::string formatValue(int const &value)
104+
auto formatType() -> char const *
115105
{
116-
return std::to_string(value);
106+
return typeid(T).name();
117107
}
118108

119-
template <>
120-
inline std::string formatValue(size_t const &value)
121-
{
122-
return std::to_string(value) + "UL";
123-
}
124-
125-
// template <>
126-
// inline std::string formatValue(uint64_t const &value)
127-
// {
128-
// return std::to_string(value) + "ULL";
129-
// }
130-
131-
template <>
132-
inline std::string formatValue(bool const &value)
109+
template <typename T>
110+
inline std::string formatValue(T const &value)
133111
{
134-
return value ? "true" : "false";
112+
if constexpr (std::is_same_v<T, bool>)
113+
{
114+
return value ? "true" : "false";
115+
}
116+
else if constexpr (
117+
std::is_same_v<T, char const *> || std::is_same_v<T, char *> ||
118+
std::is_same_v<T, std::string>)
119+
{
120+
std::stringstream res;
121+
res << "\"" << value << "\"";
122+
return res.str();
123+
}
124+
else if constexpr (std::is_arithmetic_v<T>)
125+
{
126+
std::stringstream res;
127+
res << "(" << formatType<T>() << ")" << value;
128+
return res.str();
129+
}
130+
else
131+
{
132+
return "/*value*/";
133+
}
135134
}
136135

137-
// Generic fallback (will use operator<<)
138136
template <typename T>
139-
inline std::string formatValue(T const &)
137+
inline std::string formatValue(T const *it, size_t size)
140138
{
141-
return "/*value*/";
139+
std::stringstream out;
140+
out << "std::vector<" << formatType<T>() << ">{";
141+
if (size == 0)
142+
{
143+
out << "}";
144+
return out.str();
145+
}
146+
out << *it;
147+
for (size_t i = 1; i < size; ++i)
148+
{
149+
out << ", " << it[i];
150+
}
151+
out << "}";
152+
return out.str();
142153
}
143154
} // namespace
144155

145-
#define ADIOS2_LOG_API_CALL(...) \
146-
do \
147-
{ \
148-
if (::openPMD::shouldLogADIOS2ApiCalls()) \
149-
{ \
150-
std::cerr << "[ADIOS2 API] " << __VA_ARGS__ << std::endl; \
151-
} \
152-
} while (false)
156+
template <typename... Args>
157+
void ADIOS2_LOG_API_CALL(Args &&...args)
158+
{
159+
if (::openPMD::shouldLogADIOS2ApiCalls())
160+
{
161+
((std::cerr << "[ADIOS2 API] ") << ... << args) << std::endl;
162+
}
163+
}
153164

154165
std::optional<size_t> joinedDimension(adios2::Dims const &dims)
155166
{
@@ -802,8 +813,11 @@ void ADIOS2IOHandlerImpl::createFile(
802813
adios_defaults::str_groupBasedWarning,
803814
std::string(warningADIOS2NoGroupbasedEncoding));
804815
ADIOS2_LOG_API_CALL(
805-
"IO.DefineAttribute(\"" << adios_defaults::str_groupBasedWarning
806-
<< "\", ...);");
816+
"IO.DefineAttribute(",
817+
formatValue(adios_defaults::str_groupBasedWarning),
818+
",",
819+
formatValue(warningADIOS2NoGroupbasedEncoding),
820+
");");
807821
}
808822
}
809823
}
@@ -2442,7 +2456,14 @@ namespace detail
24422456
/* separator = */ "/",
24432457
/* allowModification = */ modifiable);
24442458
ADIOS2_LOG_API_CALL(
2445-
"IO.DefineAttribute(\"" << fullName << "\", ...);");
2459+
"IO.DefineAttribute(",
2460+
formatValue(fullName),
2461+
", ",
2462+
formatValue(args...),
2463+
", ",
2464+
R"("", "/", )",
2465+
formatValue(modifiable),
2466+
");");
24462467

24472468
if (!attr)
24482469
{
@@ -2558,8 +2579,11 @@ namespace detail
25582579
{
25592580
var = IO.DefineVariable<T>(name, shape, start, count, constantDims);
25602581
ADIOS2_LOG_API_CALL(
2561-
"IO.DefineVariable<T>(\""
2562-
<< name << "\", shape, start, count, constantDims);");
2582+
"IO.DefineVariable<",
2583+
formatType<T>(),
2584+
">(\"",
2585+
name,
2586+
"\", shape, start, count, constantDims);");
25632587
}
25642588
else
25652589
{

0 commit comments

Comments
 (0)