Skip to content

Commit

Permalink
Fix #88 - Support display for std::optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jfalcou committed Nov 8, 2023
1 parent f6633a2 commit b0558ee
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/tts/tools/as_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <iomanip>
#include <sstream>
#include <optional>
#include <type_traits>
#include <tts/tools/concepts.hpp>
#include <tts/tools/typename.hpp>
Expand Down Expand Up @@ -125,4 +126,11 @@ namespace tts
inline std::string as_string(std::string const& e) { return e; }
inline std::string as_string(std::string_view const& e) { return std::string(e); }
inline std::string as_string(std::nullptr_t) { return std::string("nullptr"); }

template<typename T>
std::string as_string(std::optional<T> const& o)
{
if(o) return std::string("optional<") + typename_<T> +">{" + as_string(*o) + "}";
else return std::string("optional<") + typename_<T> + ">{}";
}
}
7 changes: 7 additions & 0 deletions standalone/tts/tts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@ namespace tts::detail
}
#include <iomanip>
#include <sstream>
#include <optional>
#include <type_traits>
namespace tts
{
Expand Down Expand Up @@ -1052,6 +1053,12 @@ namespace tts
inline std::string as_string(std::string const& e) { return e; }
inline std::string as_string(std::string_view const& e) { return std::string(e); }
inline std::string as_string(std::nullptr_t) { return std::string("nullptr"); }
template<typename T>
std::string as_string(std::optional<T> const& o)
{
if(o) return std::string("optional<") + typename_<T> +">{" + as_string(*o) + "}";
else return std::string("optional<") + typename_<T> + ">{}";
}
}
#define TTS_RELATION_BASE(A, B, OP, T, F, FAILURE) \
if( ::tts::detail::OP(a,b) ) \
Expand Down
7 changes: 7 additions & 0 deletions test/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,10 @@ TTS_CASE( "Check display of sequence type" )
, "{ some_type[13] some_type[34] }"s
);
};


TTS_CASE( "Check display of std::optional" )
{
TTS_EQUAL(tts::as_string( std::optional{42} ) , "optional<int>{42}"s );
TTS_EQUAL(tts::as_string( std::optional<int>{} ), "optional<int>{}"s );
};

0 comments on commit b0558ee

Please sign in to comment.