From 1afa4b89fdb0e14d90bfc087118cfff0700bc5c4 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Mon, 25 Nov 2019 03:25:57 +0200 Subject: [PATCH] Add boost::source_location --- include/boost/assert/source_location.hpp | 69 ++++++++++++++++++++++++ test/Jamfile.v2 | 3 ++ test/source_location_test.cpp | 30 +++++++++++ test/source_location_test2.cpp | 32 +++++++++++ 4 files changed, 134 insertions(+) create mode 100644 include/boost/assert/source_location.hpp create mode 100644 test/source_location_test.cpp create mode 100644 test/source_location_test2.cpp diff --git a/include/boost/assert/source_location.hpp b/include/boost/assert/source_location.hpp new file mode 100644 index 0000000..8c3ef49 --- /dev/null +++ b/include/boost/assert/source_location.hpp @@ -0,0 +1,69 @@ +#ifndef BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED +#define BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED + +// http://www.boost.org/libs/assert +// +// Copyright 2019 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include + +namespace boost +{ + +struct source_location +{ +private: + + char const * file_; + char const * function_; + boost::uint_least32_t line_; + boost::uint_least32_t column_; + +public: + + BOOST_CONSTEXPR source_location() BOOST_NOEXCEPT: file_( "(unknown)" ), function_( file_ ), line_( 0 ), column_( 0 ) + { + } + + BOOST_CONSTEXPR source_location( char const * file, boost::uint_least32_t line, char const * function, boost::uint_least32_t column = 0 ) BOOST_NOEXCEPT: file_( file ), function_( function ), line_( line ), column_( column ) + { + } + + BOOST_CONSTEXPR char const * file_name() const BOOST_NOEXCEPT + { + return file_; + } + + BOOST_CONSTEXPR char const * function_name() const BOOST_NOEXCEPT + { + return function_; + } + + BOOST_CONSTEXPR boost::uint_least32_t line() const BOOST_NOEXCEPT + { + return line_; + } + + BOOST_CONSTEXPR boost::uint_least32_t column() const BOOST_NOEXCEPT + { + return column_; + } +}; + +} // namespace boost + +#if defined( BOOST_DISABLE_CURRENT_LOCATION ) + +# define BOOST_CURRENT_LOCATION ::boost::source_location() + +#else + +# define BOOST_CURRENT_LOCATION ::boost::source_location(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION) + +#endif + +#endif // #ifndef BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index d1595c8..5d8f0c7 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -27,3 +27,6 @@ run assert_msg_test2.cpp ; run quick.cpp ; run current_function_test2.cpp ; + +run source_location_test.cpp ; +run source_location_test2.cpp ; diff --git a/test/source_location_test.cpp b/test/source_location_test.cpp new file mode 100644 index 0000000..f94e230 --- /dev/null +++ b/test/source_location_test.cpp @@ -0,0 +1,30 @@ +// Copyright 2019 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +#include +#include + +int main() +{ + { + boost::source_location loc; + + BOOST_TEST_CSTR_EQ( loc.file_name(), "(unknown)" ); + BOOST_TEST_CSTR_EQ( loc.function_name(), "(unknown)" ); + BOOST_TEST_EQ( loc.line(), 0 ); + BOOST_TEST_EQ( loc.column(), 0 ); + } + + { + boost::source_location loc = BOOST_CURRENT_LOCATION; + + + BOOST_TEST_CSTR_EQ( loc.file_name(), __FILE__ ); + BOOST_TEST_CSTR_EQ( loc.function_name(), BOOST_CURRENT_FUNCTION ); + BOOST_TEST_EQ( loc.line(), 20 ); + BOOST_TEST_EQ( loc.column(), 0 ); + } + + return boost::report_errors(); +} diff --git a/test/source_location_test2.cpp b/test/source_location_test2.cpp new file mode 100644 index 0000000..e21e3f6 --- /dev/null +++ b/test/source_location_test2.cpp @@ -0,0 +1,32 @@ +// Copyright 2019 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +#define BOOST_DISABLE_CURRENT_LOCATION + +#include +#include + +int main() +{ + { + boost::source_location loc; + + BOOST_TEST_CSTR_EQ( loc.file_name(), "(unknown)" ); + BOOST_TEST_CSTR_EQ( loc.function_name(), "(unknown)" ); + BOOST_TEST_EQ( loc.line(), 0 ); + BOOST_TEST_EQ( loc.column(), 0 ); + } + + { + boost::source_location loc = BOOST_CURRENT_LOCATION; + + + BOOST_TEST_CSTR_EQ( loc.file_name(), "(unknown)" ); + BOOST_TEST_CSTR_EQ( loc.function_name(), "(unknown)" ); + BOOST_TEST_EQ( loc.line(), 0 ); + BOOST_TEST_EQ( loc.column(), 0 ); + } + + return boost::report_errors(); +}