Skip to content

Commit

Permalink
[FEATURE] Make strong type debug streamable.
Browse files Browse the repository at this point in the history
  • Loading branch information
rrahn committed Jun 11, 2020
1 parent 0de0667 commit f33e648
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
33 changes: 33 additions & 0 deletions include/seqan3/core/detail/strong_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <type_traits>

#include <seqan3/core/add_enum_bitwise_operators.hpp>
#include <seqan3/core/detail/debug_stream_type.hpp>
#include <seqan3/core/type_traits/basic.hpp>

namespace seqan3::detail
Expand Down Expand Up @@ -496,3 +497,35 @@ class strong_type
//!\brief The underlying value, which is wrapped as a strong type.
value_t value;
};

//------------------------------------------------------------------------------
// related functions
//------------------------------------------------------------------------------

/*!\name Formatted output
* \relates seqan3::detail::strong_type
* \{
*/

/*!\brief Formatted output to a seqan3::detail::debug_stream_type.
* \tparam char_t The char type of the seqan3::detail::debug_stream_type.
* \tparam strong_type_t The strong type to print; must model seqan3::detail::strong_type_specialisation.
*
* \param[in,out] stream The output stream.
* \param[in] value The strong typed value to print.
*
* \details
*
* Prints the stored value of the given strong type.
*
* \returns `stream_t &` A reference to the given stream.
*/
template <typename char_t, strong_type_specialisation strong_type_t>
debug_stream_type<char_t> & operator<<(debug_stream_type<char_t> & stream, strong_type_t && value)
{
stream << value.get();
return stream;
}
//!\}

} // namespace seqan3::detail
1 change: 1 addition & 0 deletions test/unit/core/detail/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
seqan3_test(int_types_test.cpp)
seqan3_test(pack_algorithm_test.cpp)
seqan3_test(strong_type_debug_stream_test.cpp)
seqan3_test(strong_type_test.cpp)
seqan3_test(type_inspection_test.cpp)
25 changes: 25 additions & 0 deletions test/unit/core/detail/strong_type_debug_stream_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// -----------------------------------------------------------------------------------------------------
// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
// -----------------------------------------------------------------------------------------------------

#include <gtest/gtest.h>

#include <seqan3/core/detail/strong_type.hpp>

struct my_type : seqan3::detail::strong_type<int, my_type>
{
using seqan3::detail::strong_type<int, my_type>::strong_type;
};

TEST(strong_type, debug_stremable)
{
my_type obj{10};

std::ostringstream buffer{};
seqan3::debug_stream_type stream{buffer};
stream << obj;
EXPECT_EQ(buffer.str(), "10");
}

0 comments on commit f33e648

Please sign in to comment.