Skip to content

Commit

Permalink
Restored support for using h5pp without fmt, spdlog or eigen
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAce committed Feb 23, 2025
1 parent 2e66302 commit 89dec1a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
10 changes: 7 additions & 3 deletions include/h5pp/details/h5ppFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,16 @@ namespace h5pp {
result.emplace_back(sstr.str());
} else if constexpr(is_iterable_v<T>) {
std::stringstream sstr;
sstr << std::boolalpha << "{";
for(const auto &elem : first) sstr << elem << ",";
sstr << std::boolalpha << "[";
for(const auto &elem : first) {
if constexpr(formatting::is_streamable_v<decltype(elem)>) sstr << elem << ",";
else
for(const auto &tmp : convert_to_string_list(elem)) sstr << tmp << ",";
}
// Laborious casting here to avoid MSVC warnings and errors in std::min()
long rewind = -1 * std::min(1l, static_cast<long>(first.size()));
sstr.seekp(rewind, std::ios_base::end);
sstr << "}";
sstr << "]";
result.emplace_back(sstr.str());
}
if constexpr(sizeof...(rest) > 0)
Expand Down
14 changes: 14 additions & 0 deletions include/h5pp/details/h5ppFstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,29 +132,37 @@ namespace h5pp::type::flen {
if(v.ptr == nullptr) return;
strncpy(ptr, v.ptr, N);
ptr[N - 1] = '\0';
#if defined(H5PP_USE_FMT)
if constexpr(internal::debug_fstr_t) h5pp::logger::log->info("fstr_t copied into {}: {}", fmt::ptr(ptr), ptr);
#endif
}
template<size_t N>
inline fstr_t<N>::fstr_t(const char *v) {
if(v == nullptr) return;
strncpy(ptr, v, N);
ptr[N - 1] = '\0';
#if defined(H5PP_USE_FMT)
if constexpr(internal::debug_fstr_t) h5pp::logger::log->info("fstr_t copied into {}: {}", fmt::ptr(ptr), ptr);
#endif
}
template<size_t N>
inline fstr_t<N>::fstr_t(std::string_view v) {
if(v.empty()) return;
strncpy(ptr, v.data(), N);
ptr[N - 1] = '\0';
#if defined(H5PP_USE_FMT)
if constexpr(internal::debug_fstr_t) h5pp::logger::log->info("fstr_t copied into {}: {}", fmt::ptr(ptr), ptr);
#endif
}

template<size_t N>
inline fstr_t<N>::fstr_t(fstr_t &&v) noexcept {
if(v.ptr == nullptr) return;
strncpy(ptr, v.ptr, N);
ptr[N - 1] = '\0';
#if defined(H5PP_USE_FMT)
if constexpr(internal::debug_fstr_t) h5pp::logger::log->info("fstr_t copied into {}: {}", fmt::ptr(ptr), ptr);
#endif
}
template<size_t N>
template<typename T, typename>
Expand All @@ -167,7 +175,9 @@ namespace h5pp::type::flen {
clear();
strncpy(ptr, v.ptr, N);
ptr[N - 1] = '\0';
#if defined(H5PP_USE_FMT)
if constexpr(internal::debug_fstr_t) h5pp::logger::log->info("fstr_t assigned into {}: {}", fmt::ptr(ptr), ptr);
#endif
}
return *this;
}
Expand All @@ -176,7 +186,9 @@ namespace h5pp::type::flen {
clear();
strncpy(ptr, v.data(), N);
ptr[N - 1] = '\0';
#if defined(H5PP_USE_FMT)
if constexpr(internal::debug_fstr_t) h5pp::logger::log->info("fstr_t assigned into {}: {}", fmt::ptr(ptr), ptr);
#endif
return *this;
}
template<size_t N>
Expand Down Expand Up @@ -275,7 +287,9 @@ namespace h5pp::type::flen {
size_t oldlen = size();
strncpy(ptr + oldlen, v, N - oldlen);
ptr[N - 1] = '\0';
#if defined(H5PP_USE_FMT)
if constexpr(internal::debug_fstr_t) h5pp::logger::log->info("fstr_t appended to {} | {} -> {}", fmt::ptr(ptr), v, ptr);
#endif
}

template<size_t N>
Expand Down
8 changes: 4 additions & 4 deletions include/h5pp/details/h5ppInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,9 @@ namespace h5pp {
size_t nwidth = 4;
for(const auto &name : fieldNames.value()) nwidth = std::max(nwidth, 1ul + name.size());
/* clang-format off */
std::string msg = fmt::format("{1:4}: {2:{0}} | {3:6} | {4:6} | {5:16} | {6:24}\n", nwidth, "idx", "name", "size", "offset", "class", "type");
std::string msg = h5pp::format("{1:4}: {2:{0}} | {3:6} | {4:6} | {5:16} | {6:24}\n", nwidth, "idx", "name", "size", "offset", "class", "type");
for(size_t m = 0; m < type::safe_cast<size_t>(numFields.value()); ++m) {
msg += fmt::format("{1:4}: {2:{0}} | {3:6} | {4:6} | {5:16} | {6:24}\n", nwidth,
msg += h5pp::format("{1:4}: {2:{0}} | {3:6} | {4:6} | {5:16} | {6:24}\n", nwidth,
m,
fieldNames and fieldNames->size() > m ? fieldNames->at(m) : "",
fieldSizes and fieldSizes->size() > m ? fieldSizes->at(m) : -1ul,
Expand Down Expand Up @@ -873,9 +873,9 @@ namespace h5pp {
size_t nwidth = 4;
for(const auto &name : memberNames.value()) nwidth = std::max(nwidth, 1ul + name.size());
/* clang-format off */
std::string msg = fmt::format("{1:4}: {2:{0}} | {3:6} | {4:6} | {5:16} | {6:24}\n", nwidth, "idx", "name", "size", "offset", "class", "type");
std::string msg = h5pp::format("{1:4}: {2:{0}} | {3:6} | {4:6} | {5:16} | {6:24}\n", nwidth, "idx", "name", "size", "offset", "class", "type");
for(size_t m = 0; m < type::safe_cast<size_t>(numMembers.value()); ++m) {
msg += fmt::format("{1:4}: {2:{0}} | {3:6} | {4:6} | {5:16} | {6:24}\n", nwidth,
msg += h5pp::format("{1:4}: {2:{0}} | {3:6} | {4:6} | {5:16} | {6:24}\n", nwidth,
m,
memberNames and memberNames->size() > m ? memberNames->at(m) : "",
memberSizes and memberSizes->size() > m ? memberSizes->at(m) : -1ul,
Expand Down
4 changes: 2 additions & 2 deletions include/h5pp/details/h5ppType.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ namespace h5pp::type {
if(type::custom::H5T_FLOAT128::equal(h5type)) return type::custom::H5T_FLOAT128::h5name();
#endif
if(H5Tget_class(h5type) == H5T_class_t::H5T_ENUM) return h5pp::format("H5T_ENUM{}",H5Tget_size(h5type));
if(H5Tget_class(h5type) == H5T_class_t::H5T_ARRAY) return fmt::format("H5T_ARRAY<{}>", getH5TypeName(H5Tget_super(h5type)));
if(H5Tget_class(h5type) == H5T_class_t::H5T_ARRAY) return h5pp::format("H5T_ARRAY<{}>", getH5TypeName(H5Tget_super(h5type)));
if(H5Tis_variable_str(h5type)) return "H5T_VARIABLE";
if(H5Tcommitted(h5type) == 0){
hid::h5t h5native = H5Tget_native_type(h5type, H5T_direction_t::H5T_DIR_DEFAULT);
if(H5Tequal(h5native, h5type) == 0)
return fmt::format("{}<{}>",getH5ClassName(h5type), getH5TypeName(h5native));
return h5pp::format("{}<{}>",getH5ClassName(h5type), getH5TypeName(h5native));
// Just get the class and size...
return h5pp::format("{}:{}",getH5ClassName(h5type), H5Tget_size(h5type));
}
Expand Down
18 changes: 18 additions & 0 deletions include/h5pp/details/h5ppVstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,30 +135,38 @@ namespace h5pp::type::vlen {
ptr = static_cast<char *>(malloc(v.size() + 1 * sizeof(char)));
strcpy(ptr, v.ptr);
ptr[v.size()] = '\0';
#if defined(H5PP_USE_FMT)
if constexpr(internal::debug_vstr_t) h5pp::logger::log->info("vstr_t allocated {}: {}", fmt::ptr(ptr), ptr);
#endif
}
inline vstr_t::vstr_t(const char *v) {
if(v == nullptr) return;
size_t len = strlen(v);
ptr = static_cast<char *>(malloc(len + 1 * sizeof(char)));
strcpy(ptr, v);
ptr[len] = '\0';
#if defined(H5PP_USE_FMT)
if constexpr(internal::debug_vstr_t) h5pp::logger::log->info("vstr_t allocated {}: {}", fmt::ptr(ptr), ptr);
#endif
}
inline vstr_t::vstr_t(std::string_view v) {
if(v.empty()) return;
ptr = static_cast<char *>(malloc(v.size() + 1 * sizeof(char)));
strcpy(ptr, v.data());
ptr[v.size()] = '\0';
#if defined(H5PP_USE_FMT)
if constexpr(internal::debug_vstr_t) h5pp::logger::log->info("vstr_t allocated {}: {}", fmt::ptr(ptr), ptr);
#endif
}

inline vstr_t::vstr_t(vstr_t &&v) noexcept {
if(v.ptr == nullptr) return;
ptr = static_cast<char *>(malloc(v.size() + 1 * sizeof(char)));
strcpy(ptr, v.ptr);
ptr[v.size()] = '\0';
#if defined(H5PP_USE_FMT)
if constexpr(internal::debug_vstr_t) h5pp::logger::log->info("vstr_t allocated {}: {}", fmt::ptr(ptr), ptr);
#endif
}

template<typename T, typename>
Expand All @@ -172,7 +180,9 @@ namespace h5pp::type::vlen {
ptr = static_cast<char *>(malloc(v.size() + 1 * sizeof(char)));
strcpy(ptr, v.ptr);
ptr[v.size()] = '\0';
#if defined(H5PP_USE_FMT)
if constexpr(internal::debug_vstr_t) h5pp::logger::log->info("vstr_t allocated {}: {}", fmt::ptr(ptr), ptr);
#endif
}
return *this;
}
Expand All @@ -182,7 +192,9 @@ namespace h5pp::type::vlen {
ptr = static_cast<char *>(malloc(v.size() + 1 * sizeof(char)));
strcpy(ptr, v.data());
ptr[v.size()] = '\0';
#if defined(H5PP_USE_FMT)
if constexpr(internal::debug_vstr_t) h5pp::logger::log->info("vstr_t allocated {}: {}", fmt::ptr(ptr), ptr);
#endif
return *this;
}
inline vstr_t &vstr_t::operator=(const char *v) { return this->operator=(std::string_view(v)); }
Expand Down Expand Up @@ -226,7 +238,9 @@ namespace h5pp::type::vlen {

inline void vstr_t::clear() noexcept {
if constexpr(internal::debug_vstr_t) {
#if defined(H5PP_USE_FMT)
if(ptr != nullptr) h5pp::logger::log->info("vstr_t clearing {} | {}", fmt::ptr(ptr), ptr);
#endif
}
free(ptr);
ptr = nullptr;
Expand All @@ -238,7 +252,9 @@ namespace h5pp::type::vlen {
throw;
}
ptr = newptr;
#if defined(H5PP_USE_FMT)
if constexpr(internal::debug_vstr_t) h5pp::logger::log->info("vstr_t realloc {} | {}", fmt::ptr(ptr), ptr);
#endif
}
inline void vstr_t::erase(std::string::size_type pos, std::string::size_type n) { *this = std::string(*this).erase(pos, n); }
inline void vstr_t::erase(const char *b, const char *e) {
Expand All @@ -252,7 +268,9 @@ namespace h5pp::type::vlen {
resize(oldlen + strlen(v));
strcpy(ptr + oldlen, v);
ptr[size()] = '\0';
#if defined(H5PP_USE_FMT)
if constexpr(internal::debug_vstr_t) h5pp::logger::log->info("vstr_t appended to {} | {} -> {}", fmt::ptr(ptr), v, ptr);
#endif
}
inline void vstr_t::append(const std::string &v) { append(v.c_str()); }
inline void vstr_t::append(std::string_view v) { append(v.data()); }
Expand Down

0 comments on commit 89dec1a

Please sign in to comment.