Skip to content

Commit c2dc8a1

Browse files
committed
Initial support for the C++20 modules
1 parent f3d4d55 commit c2dc8a1

File tree

6 files changed

+144
-0
lines changed

6 files changed

+144
-0
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ target_link_libraries(boost_throw_exception
1616
Boost::config
1717
)
1818

19+
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.28.0" AND BUILD_MODULE)
20+
add_subdirectory(module)
21+
endif()
22+
1923
if(BUILD_TESTING)
2024

2125
add_subdirectory(test)

include/boost/exception/exception.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,28 @@
66
#ifndef BOOST_EXCEPTION_274DA366004E11DCB1DDFE2E56D89593
77
#define BOOST_EXCEPTION_274DA366004E11DCB1DDFE2E56D89593
88

9+
#ifndef BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT
10+
#define BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT
11+
#endif
12+
13+
#ifndef BOOST_THROW_EXCEPTION_END_MODULE_EXPORT
14+
#define BOOST_THROW_EXCEPTION_END_MODULE_EXPORT
15+
#endif
16+
917
#include <boost/assert/source_location.hpp>
1018
#include <boost/config.hpp>
1119
#include <exception>
1220

1321
#ifdef BOOST_EXCEPTION_MINI_BOOST
1422
#include <memory>
23+
BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT
1524
namespace boost { namespace exception_detail { using std::shared_ptr; } }
25+
BOOST_THROW_EXCEPTION_END_MODULE_EXPORT
1626
#else
27+
BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT
1728
namespace boost { template <class T> class shared_ptr; }
1829
namespace boost { namespace exception_detail { using boost::shared_ptr; } }
30+
BOOST_THROW_EXCEPTION_END_MODULE_EXPORT
1931
#endif
2032

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

46+
BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT
3447
namespace
3548
boost
3649
{
@@ -566,4 +579,6 @@ boost
566579
#pragma warning(pop)
567580
#endif
568581

582+
BOOST_THROW_EXCEPTION_END_MODULE_EXPORT
583+
569584
#endif // #ifndef BOOST_EXCEPTION_274DA366004E11DCB1DDFE2E56D89593

include/boost/throw_exception.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// http://www.boost.org/libs/throw_exception
2020

2121
#include <boost/exception/exception.hpp>
22+
2223
#include <boost/assert/source_location.hpp>
2324
#include <boost/config.hpp>
2425
#include <boost/config/workaround.hpp>
@@ -38,8 +39,10 @@ namespace boost
3839

3940
#if defined( BOOST_NO_EXCEPTIONS )
4041

42+
BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT
4143
BOOST_NORETURN void throw_exception( std::exception const & e ); // user defined
4244
BOOST_NORETURN void throw_exception( std::exception const & e, boost::source_location const & loc ); // user defined
45+
BOOST_THROW_EXCEPTION_END_MODULE_EXPORT
4346

4447
#endif
4548

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

6972
} // namespace detail
7073

74+
75+
BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT
76+
7177
template<class E> struct BOOST_SYMBOL_VISIBLE wrapexcept:
7278
public detail::wrapexcept_add_base<E, boost::exception_detail::clone_base>::type,
7379
public E,
@@ -175,6 +181,8 @@ template<class E> BOOST_NORETURN void throw_exception( E const & e, boost::sourc
175181

176182
#endif // !defined( BOOST_NO_EXCEPTIONS )
177183

184+
BOOST_THROW_EXCEPTION_END_MODULE_EXPORT
185+
178186
} // namespace boost
179187

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

218226
} // namespace detail
219227

228+
BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT
229+
220230
#if !defined(BOOST_NO_EXCEPTIONS)
221231

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

286+
BOOST_THROW_EXCEPTION_END_MODULE_EXPORT
287+
276288
} // namespace boost
277289

278290
#endif // #ifndef BOOST_THROW_EXCEPTION_HPP_INCLUDED

module/CMakeLists.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (c) 2025 Antony Polukhin
2+
#
3+
# Distributed under the Boost Software License, Version 1.0.
4+
# https://www.boost.org/LICENSE_1_0.txt
5+
6+
cmake_minimum_required(VERSION 3.28)
7+
8+
function (_add_boost_throw_exception_module_impl NAME)
9+
add_library(${NAME})
10+
target_compile_features(${NAME} PUBLIC cxx_std_20)
11+
target_sources(${NAME} PUBLIC
12+
FILE_SET modules_public TYPE CXX_MODULES FILES
13+
${CMAKE_CURRENT_LIST_DIR}/throw_exception.cppm
14+
)
15+
endfunction()
16+
17+
function (add_boost_throw_exception_module NAME)
18+
_add_boost_throw_exception_module_impl(${NAME})
19+
target_include_directories(${NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/../include)
20+
21+
_add_boost_throw_exception_module_impl(${NAME}_migration)
22+
target_include_directories(${NAME}_migration PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../include)
23+
target_compile_definitions(${NAME}_migration PRIVATE BOOST_THROW_EXCEPTION_ATTACH_TO_GLOBAL_MODULE)
24+
endfunction()
25+
26+
add_boost_throw_exception_module(boost_throw_exception_module)
27+
add_library(Boost::throw_exception_module ALIAS boost_throw_exception_module)
28+
add_library(Boost::throw_exception_module_migration ALIAS boost_throw_exception_module_migration)
29+
30+
if (BUILD_TESTING)
31+
add_executable(boost_throw_exception_module_usage usage_sample.cpp)
32+
target_link_libraries(boost_throw_exception_module_usage PRIVATE Boost::throw_exception_module)
33+
34+
# Make sure that mixing includes and imports is fine for different TU
35+
add_executable(boost_throw_exception_module_usage_mu usage_test_mu1.cpp usage_test_mu2.cpp)
36+
target_link_libraries(boost_throw_exception_module_usage_mu PRIVATE Boost::throw_exception_module_migration)
37+
endif()

module/throw_exception.cppm

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2016-2024 Antony Polukhin
2+
//
3+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5+
6+
// To compile manually use a command like the folowing:
7+
// clang++ -I ../include -std=c++20 --precompile -x c++-module throw_exception.cppm
8+
9+
#define BOOST_THROW_EXCEPTION_BEGIN_MODULE_EXPORT export {
10+
#define BOOST_THROW_EXCEPTION_END_MODULE_EXPORT }
11+
12+
module;
13+
14+
#include <boost/assert/source_location.hpp> // TODO: modularize
15+
#include <boost/config.hpp>
16+
#include <boost/config/workaround.hpp>
17+
18+
#ifndef BOOST_THROW_EXCEPTION_HAS_STD_MODULE
19+
#include <exception>
20+
#include <utility>
21+
#include <cstddef>
22+
#include <memory>
23+
#include <exception>
24+
#include <type_traits>
25+
#endif
26+
27+
export module Boost.ThrowException;
28+
29+
#ifdef BOOST_THROW_EXCEPTION_HAS_STD_MODULE
30+
import std;
31+
#endif
32+
33+
#ifdef __clang__
34+
# pragma clang diagnostic ignored "-Winclude-angled-in-module-purview"
35+
#endif
36+
37+
#ifdef BOOST_THROW_EXCEPTION_ATTACH_TO_GLOBAL_MODULE
38+
extern "C++" {
39+
#include <boost/throw_exception.hpp>
40+
}
41+
#else
42+
#include <boost/throw_exception.hpp>
43+
#endif

module/usage_sample.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
2+
//Copyright (c) 2025 Antony Polukhin.
3+
4+
//Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
7+
// To compile manually use a command like the folowing:
8+
// clang++ -std=c++20 -fmodule-file=throw_exception.pcm throw_exception.pcm usage_sample.cpp
9+
10+
#include <exception>
11+
12+
import Boost.ThrowException;
13+
14+
class my_exception: public std::exception { };
15+
16+
int
17+
main()
18+
{
19+
try
20+
{
21+
boost::throw_exception(my_exception());
22+
return 1;
23+
}
24+
catch(
25+
my_exception & )
26+
{
27+
}
28+
catch(
29+
... )
30+
{
31+
return 2;
32+
}
33+
}

0 commit comments

Comments
 (0)