strconv.h 864 B

123456789101112131415161718192021222324252627282930313233
  1. //
  2. // stringizer.h
  3. // serializer
  4. //
  5. // Created by Sam Jaffe on 3/15/23.
  6. //
  7. #pragma once
  8. #include <string>
  9. #include <string_view>
  10. #include <type_traits>
  11. #include <magic_enum.hpp>
  12. #include <serializer/traits.h>
  13. namespace serializer {
  14. using ::std::to_string;
  15. inline std::string to_string(char const *str) { return str; }
  16. inline std::string to_string(std::string const &str) { return str; }
  17. inline std::string to_string(std::string_view str) { return std::string(str); }
  18. template <typename T> std::string to_string(T const * t) { return to_string(*t); }
  19. template <typename T> std::string to_string(T const &elem) {
  20. if constexpr (std::is_enum_v<T>) {
  21. constexpr auto type = magic_enum::as_flags<magic_enum::detail::is_flags_v<T>>;
  22. return std::string(magic_enum::enum_name<type>(elem));
  23. } else {
  24. static_assert(detail::always_false<T>{});
  25. }
  26. }
  27. }