Skip to content

Commit

Permalink
Utility: support C++17 std::string_view in Debug.
Browse files Browse the repository at this point in the history
  • Loading branch information
mosra committed Feb 19, 2022
1 parent 1ce936c commit e7ec9c5
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/corrade-changelog.dox
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ namespace Corrade {
- @cpp !Debug{} @ce or @ref Utility-Debug-source-location "source location output in Debug"
is now enabled on MSVC 2019 16.6+ as well, making it available across all
compilers
- @ref Utility::Debug now accepts also a C++17 @ref std::string_view if
you include @ref Corrade/Utility/DebugStlStringView.h
- @ref Utility::Directory::isDirectory() now follows symlinks on Unix
platforms
- @ref Utility::Directory::Flag::SkipFiles and
Expand Down
1 change: 1 addition & 0 deletions src/Corrade/Utility/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ if(WITH_UTILITY)
ConfigurationValue.h
Debug.h
DebugStl.h
DebugStlStringView.h
Directory.h
Endianness.h
EndiannessBatch.h
Expand Down
11 changes: 10 additions & 1 deletion src/Corrade/Utility/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,22 @@ Support for printing more types can be added by implementing an overload of
To optimize compile times, the @ref Corrade/Utility/Debug.h header provides
only support for printing builtin types, generic iterable containers and
@ref std::pair. Printing of @ref std::string and @ref std::tuple is possible if
you @cpp #include @ce a separate @ref Corrade/Utility/DebugStl.h header. This
you include @ref Corrade/Utility/DebugStl.h. The support is provided in a
separate header to avoid unconditional @cpp #include <string> @ce or
@cpp #include <tuple> @ce, which significantly affect compile times. This
header also provides a fallback to @ref std::ostream @cpp operator<<() @ce
overloads, if there's no @cpp operator<<() @ce implemented for printing given
type using @ref Debug. Note that printing @ref std::vector or @ref std::map
containers is already possible with the generic iterable container support in
@ref Corrade/Utility/Debug.h.
On compilers that support C++17 and @ref std::string_view, support for printing
it is provided @ref Corrade/Utility/DebugStlStringView.h. For
similar reasons, it's a dedicated header to avoid unconditional
@cpp #include <string_view> @ce, but this one is even significantly heavier
than the @ref string "<string>" etc. includes on certain implementations, so
it's separate from the others as well.
@section Utility-Debug-scoped-output Scoped output redirection
Output specified in class constructor is used for all instances created during
Expand Down
6 changes: 4 additions & 2 deletions src/Corrade/Utility/DebugStl.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
@m_since{2019,10}
Including this header allows you to use STL types such as @ref std::string or
@ref std::tuple with @ref Corrade::Utility::Debug. See @ref Utility-Debug-stl
for more information.
@ref std::tuple with @ref Corrade::Utility::Debug. A separate
@ref Corrade/Utility/DebugStlStringView.h header provides compatibility with
@ref std::string_view from C++17. See @ref Utility-Debug-stl for more
information.
*/

#include <iosfwd>
Expand Down
67 changes: 67 additions & 0 deletions src/Corrade/Utility/DebugStlStringView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef Corrade_Utility_DebugStlStringView_h
#define Corrade_Utility_DebugStlStringView_h
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019, 2020, 2021, 2022
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

/** @file
@brief STL @ref std::string_view compatibility for @ref Corrade::Utility::Debug
@m_since_latest
Including this header allows you to use a C++17 @ref std::string_view with
@ref Corrade::Utility::Debug. A separate @ref Corrade/Utility/DebugStl.h header
provides compatibility with @ref std::string or @ref std::tuple. See
@ref Utility-Debug-stl for more information.
*/

#include <string_view>

#include "Corrade/Containers/ArrayView.h"
#include "Corrade/Containers/StringView.h"
#include "Corrade/Utility/Debug.h"

namespace Corrade { namespace Utility {

/** @relatesalso Debug
@brief Print a @ref std::string_view to debug output
@m_since_latest
*/
inline Debug& operator<<(Debug& debug, std::string_view value) {
return debug << Containers::StringView{value.data(), value.size()};
}

/** @relatesalso Debug
@brief Print a @ref std::basic_string_view to debug output
@m_since_latest
All other types than exactly @ref std::string_view are printed as containers.
*/
template<class T> Debug& operator<<(Debug& debug, std::basic_string_view<T> value) {
return debug << Containers::ArrayView<const T>{value.data(), value.size()};
}

}}

#endif
2 changes: 2 additions & 0 deletions src/Corrade/Utility/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,11 @@ if(NOT CMAKE_CXX_FLAGS MATCHES "-std=")
(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.0") OR
(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.3") OR
(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "19.10"))
corrade_add_test(UtilityDebugStlStringViewTest DebugStlStringViewTest.cpp)
corrade_add_test(UtilityFormatStlStringViewTest FormatStlStringViewTest.cpp)
corrade_add_test(UtilityMacrosCpp17Test MacrosCpp17Test.cpp)
set_target_properties(
UtilityDebugStlStringViewTest
UtilityFormatStlStringViewTest
UtilityMacrosCpp17Test
PROPERTIES
Expand Down
64 changes: 64 additions & 0 deletions src/Corrade/Utility/Test/DebugStlStringViewTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019, 2020, 2021, 2022
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#include <sstream>

#include "Corrade/TestSuite/Tester.h"
#include "Corrade/Utility/DebugStl.h"
#include "Corrade/Utility/DebugStlStringView.h"

namespace Corrade { namespace Utility { namespace Test { namespace {

struct DebugStlStringViewTest: TestSuite::Tester {
explicit DebugStlStringViewTest();

void test();
void testEmpty();
};

DebugStlStringViewTest::DebugStlStringViewTest() {
addTests({&DebugStlStringViewTest::test,
&DebugStlStringViewTest::testEmpty});
}

using namespace std::string_view_literals;

void DebugStlStringViewTest::test() {
std::ostringstream out;
Debug{&out} << "hello\0world!"sv;
CORRADE_COMPARE(out.str(), (std::string{"hello\0world!\n", 13}));
}

void DebugStlStringViewTest::testEmpty() {
std::ostringstream out;
/* Empty string view should not cause any issues with data access */
Debug{&out} << "hello" << std::string_view{} << "!";
CORRADE_COMPARE(out.str(), "hello !\n");
}

}}}}

CORRADE_TEST_MAIN(Corrade::Utility::Test::DebugStlStringViewTest)

0 comments on commit e7ec9c5

Please sign in to comment.