|
55 | 55 | #include <stdexcept> |
56 | 56 | #include <string> |
57 | 57 | #include <type_traits> |
| 58 | +#include <typeinfo> |
58 | 59 | #include <variant> |
59 | 60 |
|
60 | 61 | namespace openPMD |
@@ -100,56 +101,66 @@ namespace |
100 | 101 | } |
101 | 102 |
|
102 | 103 | 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 * |
115 | 105 | { |
116 | | - return std::to_string(value); |
| 106 | + return typeid(T).name(); |
117 | 107 | } |
118 | 108 |
|
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) |
133 | 111 | { |
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 | + } |
135 | 134 | } |
136 | 135 |
|
137 | | - // Generic fallback (will use operator<<) |
138 | 136 | template <typename T> |
139 | | - inline std::string formatValue(T const &) |
| 137 | + inline std::string formatValue(T const *it, size_t size) |
140 | 138 | { |
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(); |
142 | 153 | } |
143 | 154 | } // namespace |
144 | 155 |
|
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 | +} |
153 | 164 |
|
154 | 165 | std::optional<size_t> joinedDimension(adios2::Dims const &dims) |
155 | 166 | { |
@@ -802,8 +813,11 @@ void ADIOS2IOHandlerImpl::createFile( |
802 | 813 | adios_defaults::str_groupBasedWarning, |
803 | 814 | std::string(warningADIOS2NoGroupbasedEncoding)); |
804 | 815 | 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 | + ");"); |
807 | 821 | } |
808 | 822 | } |
809 | 823 | } |
@@ -2442,7 +2456,14 @@ namespace detail |
2442 | 2456 | /* separator = */ "/", |
2443 | 2457 | /* allowModification = */ modifiable); |
2444 | 2458 | 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 | + ");"); |
2446 | 2467 |
|
2447 | 2468 | if (!attr) |
2448 | 2469 | { |
@@ -2558,8 +2579,11 @@ namespace detail |
2558 | 2579 | { |
2559 | 2580 | var = IO.DefineVariable<T>(name, shape, start, count, constantDims); |
2560 | 2581 | 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);"); |
2563 | 2587 | } |
2564 | 2588 | else |
2565 | 2589 | { |
|
0 commit comments