Procházet zdrojové kódy

* Improved string conversion (write to stream if cppcore supports the type)

* Added tests to group 'cppcore'
master
bergmann před 6 roky
rodič
revize
14883b894a
4 změnil soubory, kde provedl 34 přidání a 28 odebrání
  1. +4
    -13
      include/cppcore/conversion/string.h
  2. +15
    -14
      include/cppcore/conversion/string.inl
  3. +1
    -1
      test/CMakeLists.txt
  4. +14
    -0
      test/cppcore/conversion/string_tests.cpp

+ 4
- 13
include/cppcore/conversion/string.h Zobrazit soubor

@@ -210,22 +210,13 @@ namespace std
{

/**
* @brief Operator overload to write value to stream that supports the to_string method, with stream parameter.
* @brief Operator overload to write value to stream.
*/
template<typename T_char, typename T_traits, typename X>
inline auto operator<<(basic_ostream<T_char, T_traits>& os, X&& x)
-> decltype(
std::forward<X>(x).to_string(std::declval<basic_ostream<T_char, T_traits>&>()),
std::declval<basic_ostream<T_char, T_traits>&>());

/**
* @brief Operator overload to write value to stream that supports the to_string method, with stream parameter.
*/
template<typename T_char, typename T_traits, typename X>
inline auto operator<<(basic_ostream<T_char, T_traits>& os, X&& x)
-> decltype(
std::forward<X>(x).to_string(),
std::declval<basic_ostream<T_char, T_traits>&>());
-> std::enable_if_t<
cppcore::__impl::op_to_stream<std::decay_t<X>>::enable_streaming,
basic_ostream<T_char, T_traits>&>;

}



+ 15
- 14
include/cppcore/conversion/string.inl Zobrazit soubor

@@ -224,6 +224,8 @@ namespace cppcore
template<typename T>
struct op_to_stream<T, std::enable_if_t<std::is_enum_v<T>>>
{
static constexpr decltype(auto) enable_streaming = true;

inline void operator()(std::ostream& os, const T& v) const
{ os << enum_conversion<T>::to_string(v); }
};
@@ -231,6 +233,8 @@ namespace cppcore
template<typename T>
struct op_to_stream<T, decltype(std::declval<T>().to_string(std::declval<std::ostream&>()), void())>
{
static constexpr decltype(auto) enable_streaming = true;

inline void operator()(std::ostream& os, const T& v) const
{ v.to_string(os); }
};
@@ -238,6 +242,8 @@ namespace cppcore
template<typename T>
struct op_to_stream<T, decltype(std::declval<T>().to_string(), void())>
{
static constexpr decltype(auto) enable_streaming = true;

inline void operator()(std::ostream& os, const T& v) const
{ os << v.to_string(); }
};
@@ -246,6 +252,8 @@ namespace cppcore
template<typename T>
struct op_to_stream<std::vector<T>, void>
{
static constexpr decltype(auto) enable_streaming = true;

inline void operator()(std::ostream& os, const std::vector<T>& v) const
{
bool first = true;
@@ -263,6 +271,8 @@ namespace cppcore
template<typename T>
struct op_to_stream<std::list<T>, void>
{
static constexpr decltype(auto) enable_streaming = true;

inline void operator()(std::ostream& os, const std::list<T>& v) const
{
bool first = true;
@@ -420,21 +430,12 @@ namespace std

template<typename T_char, typename T_traits, typename X>
inline auto operator<<(basic_ostream<T_char, T_traits>& os, X&& x)
-> decltype(
std::forward<X>(x).to_string(std::declval<basic_ostream<T_char, T_traits>&>()),
std::declval<basic_ostream<T_char, T_traits>&>())
{
std::forward<X>(x).to_string(os);
return os;
}

template<typename T_char, typename T_traits, typename X>
inline auto operator<<(basic_ostream<T_char, T_traits>& os, X&& x)
-> decltype(
std::forward<X>(x).to_string(),
std::declval<basic_ostream<T_char, T_traits>&>())
-> std::enable_if_t<
cppcore::__impl::op_to_stream<std::decay_t<X>>::enable_streaming,
basic_ostream<T_char, T_traits>&>
{
os << std::forward<X>(x).to_string();
using op_type = cppcore::__impl::op_to_stream<std::decay_t<X>>;
op_type()(os, std::forward<X>(x));
return os;
}



+ 1
- 1
test/CMakeLists.txt Zobrazit soubor

@@ -45,7 +45,7 @@ ForEach ( FILE IN LISTS CPPCORE_TEST_SOURCE_FILES )

# test
If ( HAS_CMAKE_TESTS )
Add_CMake_Test ( NAME ${TEST_NAME} TARGET ${TEST_NAME} )
Add_CMake_Test ( NAME ${TEST_NAME} TARGET ${TEST_NAME} GROUP cppcore )
Else ( )
Add_Test ( NAME ${TEST_NAME} COMMAND ${TEST_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )
EndIf ( )


+ 14
- 0
test/cppcore/conversion/string_tests.cpp Zobrazit soubor

@@ -73,6 +73,13 @@ TEST(string_tests, vector_to_string)
::cppcore::to_string(std::vector<int> { 1, 2, 3, 4, 5 }));
}

TEST(string_tests, vector_to_stream)
{
std::ostringstream ss;
ss << std::vector<int> { 1, 2, 3, 4, 5 };
EXPECT_EQ("1,2,3,4,5", ss.str());
}

TEST(string_tests, list_to_string)
{
EXPECT_EQ(
@@ -80,6 +87,13 @@ TEST(string_tests, list_to_string)
::cppcore::to_string(std::list<int> { 1, 2, 3, 4, 5 }));
}

TEST(string_tests, list_to_stream)
{
std::ostringstream ss;
ss << std::list<int> { 1, 2, 3, 4, 5 };
EXPECT_EQ("1,2,3,4,5", ss.str());
}

TEST(string_tests, string_to_vector)
{
EXPECT_EQ(


Načítá se…
Zrušit
Uložit