Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ target_link_libraries(boost_throw_exception
Boost::config
)

if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.28.0" AND BUILD_MODULE)
add_subdirectory(module)
endif()

if(BUILD_TESTING)

add_subdirectory(test)
Expand Down
15 changes: 15 additions & 0 deletions include/boost/exception/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@
#ifndef BOOST_EXCEPTION_274DA366004E11DCB1DDFE2E56D89593
#define BOOST_EXCEPTION_274DA366004E11DCB1DDFE2E56D89593

#ifndef BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT
#define BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT
#endif

#ifndef BOOST_THROW_EXCEPTION_END_MODULE_EXPORT
#define BOOST_THROW_EXCEPTION_END_MODULE_EXPORT
#endif

#include <boost/assert/source_location.hpp>
#include <boost/config.hpp>
#include <exception>

#ifdef BOOST_EXCEPTION_MINI_BOOST
#include <memory>
BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT
namespace boost { namespace exception_detail { using std::shared_ptr; } }
BOOST_THROW_EXCEPTION_END_MODULE_EXPORT
#else
BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT
namespace boost { template <class T> class shared_ptr; }
namespace boost { namespace exception_detail { using boost::shared_ptr; } }
BOOST_THROW_EXCEPTION_END_MODULE_EXPORT
#endif

#if !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
Expand All @@ -31,6 +43,7 @@ namespace boost { namespace exception_detail { using boost::shared_ptr; } }
#endif
#endif

BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT
namespace
boost
{
Expand Down Expand Up @@ -566,4 +579,6 @@ boost
#pragma warning(pop)
#endif

BOOST_THROW_EXCEPTION_END_MODULE_EXPORT

#endif // #ifndef BOOST_EXCEPTION_274DA366004E11DCB1DDFE2E56D89593
12 changes: 12 additions & 0 deletions include/boost/throw_exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// http://www.boost.org/libs/throw_exception

#include <boost/exception/exception.hpp>

#include <boost/assert/source_location.hpp>
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
Expand All @@ -38,8 +39,10 @@ namespace boost

#if defined( BOOST_NO_EXCEPTIONS )

BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT
BOOST_NORETURN void throw_exception( std::exception const & e ); // user defined
BOOST_NORETURN void throw_exception( std::exception const & e, boost::source_location const & loc ); // user defined
BOOST_THROW_EXCEPTION_END_MODULE_EXPORT

#endif

Expand Down Expand Up @@ -68,6 +71,9 @@ template<class E, class B> struct wrapexcept_add_base<E, B, 2>

} // namespace detail


BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT

template<class E> struct BOOST_SYMBOL_VISIBLE wrapexcept:
public detail::wrapexcept_add_base<E, boost::exception_detail::clone_base>::type,
public E,
Expand Down Expand Up @@ -175,6 +181,8 @@ template<class E> BOOST_NORETURN void throw_exception( E const & e, boost::sourc

#endif // !defined( BOOST_NO_EXCEPTIONS )

BOOST_THROW_EXCEPTION_END_MODULE_EXPORT

} // namespace boost

// BOOST_THROW_EXCEPTION
Expand Down Expand Up @@ -217,6 +225,8 @@ template<class E> class BOOST_SYMBOL_VISIBLE with_throw_location: public E, publ

} // namespace detail

BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT

#if !defined(BOOST_NO_EXCEPTIONS)

#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
Expand Down Expand Up @@ -273,6 +283,8 @@ template<class E> boost::source_location get_throw_location( E const & e )
#endif
}

BOOST_THROW_EXCEPTION_END_MODULE_EXPORT

} // namespace boost

#endif // #ifndef BOOST_THROW_EXCEPTION_HPP_INCLUDED
37 changes: 37 additions & 0 deletions module/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (c) 2025 Antony Polukhin
#
# Distributed under the Boost Software License, Version 1.0.
# https://www.boost.org/LICENSE_1_0.txt

cmake_minimum_required(VERSION 3.28)

function (_add_boost_throw_exception_module_impl NAME)
add_library(${NAME})
target_compile_features(${NAME} PUBLIC cxx_std_20)
target_sources(${NAME} PUBLIC
FILE_SET modules_public TYPE CXX_MODULES FILES
${CMAKE_CURRENT_LIST_DIR}/throw_exception.cppm
)
endfunction()

function (add_boost_throw_exception_module NAME)
_add_boost_throw_exception_module_impl(${NAME})
target_include_directories(${NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/../include)

_add_boost_throw_exception_module_impl(${NAME}_migration)
target_include_directories(${NAME}_migration PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../include)
target_compile_definitions(${NAME}_migration PRIVATE BOOST_THROW_EXCEPTION_ATTACH_TO_GLOBAL_MODULE)
endfunction()

add_boost_throw_exception_module(boost_throw_exception_module)
add_library(Boost::throw_exception_module ALIAS boost_throw_exception_module)
add_library(Boost::throw_exception_module_migration ALIAS boost_throw_exception_module_migration)

if (BUILD_TESTING)
add_executable(boost_throw_exception_module_usage usage_sample.cpp)
target_link_libraries(boost_throw_exception_module_usage PRIVATE Boost::throw_exception_module)

# Make sure that mixing includes and imports is fine for different TU
add_executable(boost_throw_exception_module_usage_mu usage_test_mu1.cpp usage_test_mu2.cpp)
target_link_libraries(boost_throw_exception_module_usage_mu PRIVATE Boost::throw_exception_module_migration)
endif()
43 changes: 43 additions & 0 deletions module/throw_exception.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2016-2024 Antony Polukhin
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// To compile manually use a command like the folowing:
// clang++ -I ../include -std=c++20 --precompile -x c++-module throw_exception.cppm

#define BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT export {
#define BOOST_THROW_EXCEPTION_END_MODULE_EXPORT }

module;

#include <boost/assert/source_location.hpp> // TODO: modularize
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>

#ifndef BOOST_THROW_EXCEPTION_HAS_STD_MODULE
#include <exception>
#include <utility>
#include <cstddef>
#include <memory>
#include <exception>
#include <type_traits>
#endif

export module Boost.ThrowException;

#ifdef BOOST_THROW_EXCEPTION_HAS_STD_MODULE
import std;
#endif

#ifdef __clang__
# pragma clang diagnostic ignored "-Winclude-angled-in-module-purview"
#endif

#ifdef BOOST_THROW_EXCEPTION_ATTACH_TO_GLOBAL_MODULE
extern "C++" {
#include <boost/throw_exception.hpp>
}
#else
#include <boost/throw_exception.hpp>
#endif
33 changes: 33 additions & 0 deletions module/usage_sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
//Copyright (c) 2025 Antony Polukhin.

//Distributed under the Boost Software License, Version 1.0. (See accompanying
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// To compile manually use a command like the folowing:
// clang++ -std=c++20 -fmodule-file=throw_exception.pcm throw_exception.pcm usage_sample.cpp

#include <exception>

import Boost.ThrowException;

class my_exception: public std::exception { };

int
main()
{
try
{
boost::throw_exception(my_exception());
return 1;
}
catch(
my_exception & )
{
}
catch(
... )
{
return 2;
}
}
Loading