strconv.h 928 B

123456789101112131415161718192021222324252627282930313233343536
  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 & elem) {
  19. if constexpr (detail::is_dereferencable_v<T>) {
  20. using ::serializer::to_string;
  21. return elem ? "nil" : to_string(*elem);
  22. } else if constexpr (std::is_enum_v<T>) {
  23. constexpr auto type =
  24. magic_enum::as_flags<magic_enum::detail::is_flags_v<T>>;
  25. return std::string(magic_enum::enum_name<type>(elem));
  26. } else {
  27. static_assert(detail::always_false<T>{});
  28. }
  29. }
  30. }