Skip to content

Commit

Permalink
Initial support for C++20 modules (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
apolukhin authored Aug 15, 2024
1 parent dec9341 commit 6039165
Show file tree
Hide file tree
Showing 39 changed files with 359 additions and 4 deletions.
9 changes: 6 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by `boostdep --cmake pfr`
# Copyright 2020 Peter Dimov
# Copyright (c) 2016-2024 Antony Polukhin
#
# Distributed under the Boost Software License, Version 1.0.
# https://www.boost.org/LICENSE_1_0.txt

Expand All @@ -12,9 +13,11 @@ add_library(Boost::pfr ALIAS boost_pfr)

target_include_directories(boost_pfr INTERFACE include)

if(BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt")
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.28.0" AND BUILD_MODULE)
add_subdirectory(module)
endif()

if (BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt")
add_subdirectory(test)

endif()

34 changes: 34 additions & 0 deletions doc/pfr.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,40 @@ parameters provided to `BOOST_PFR_CORE_NAME_PARSING` macro [*and] the initial ou

[endsect]

[section PFR as a C++20 module]

[caution C++20 PFR module support is on early stage, targets and flags may change in the future]

If using CMake of version 3.28.0 or higher define CMake option `-DBUILD_MODULE=1`
to make the `Boost::pfr_module` and `Boost::pfr_module_migration` libraries
available. With `Boost::pfr_module` C++20 module Boost.PFR could be used:

[import ../module/usage_sample.cpp]
[pfr_module_example]

The `Boost::pfr_module_migration` CMake target gives an ability to
mix includes and imports of the PFR library in different translation units.

If not using CMake, then the module could be build manually from the
`module/pfr.cppm` file. If mixing of includes in imports is desired, additionally
define `BOOST_PFR_ATTACH_TO_GLOBAL_MODULE` preprocessor macro to attach all the
module entities to a global module and avoid ODR issues.

For manual module build the following commands could be used for clang compiler:

```
cd pfr/module
clang++ -I ../include -std=c++20 --precompile -x c++-module pfr.cppm
```

After that, the module could be used in the following way:

```
clang++ -std=c++20 -fmodule-file=pfr.pcm pfr.pcm usage_sample.cpp
```

[endsect]

[section How it works]

[h2 Fields count detection and getting references to members]
Expand Down
8 changes: 8 additions & 0 deletions include/boost/pfr/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,12 @@

#undef BOOST_PFR_NOT_SUPPORTED

#ifndef BOOST_PFR_BEGIN_MODULE_EXPORT
# define BOOST_PFR_BEGIN_MODULE_EXPORT
#endif

#ifndef BOOST_PFR_END_MODULE_EXPORT
# define BOOST_PFR_END_MODULE_EXPORT
#endif

#endif // BOOST_PFR_CONFIG_HPP
4 changes: 4 additions & 0 deletions include/boost/pfr/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

namespace boost { namespace pfr {

BOOST_PFR_BEGIN_MODULE_EXPORT

/// \brief Returns reference or const reference to a field with index `I` in \aggregate `val`.
/// Overload taking the type `U` returns reference or const reference to a field
/// with provided type `U` in \aggregate `val` if there's only one field of such type in `val`.
Expand Down Expand Up @@ -260,6 +262,8 @@ constexpr detail::tie_from_structure_tuple<Elements...> tie_from_structure(Eleme
return detail::tie_from_structure_tuple<Elements...>(args...);
}

BOOST_PFR_END_MODULE_EXPORT

}} // namespace boost::pfr

#endif // BOOST_PFR_CORE_HPP
4 changes: 4 additions & 0 deletions include/boost/pfr/core_name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

namespace boost { namespace pfr {

BOOST_PFR_BEGIN_MODULE_EXPORT

/// \brief Returns name of a field with index `I` in \aggregate `T`.
///
/// \b Example:
Expand Down Expand Up @@ -82,6 +84,8 @@ names_as_array() noexcept {
);
}

BOOST_PFR_END_MODULE_EXPORT

}} // namespace boost::pfr

#endif // BOOST_PFR_CORE_NAME_HPP
Expand Down
4 changes: 4 additions & 0 deletions include/boost/pfr/detail/core14_classic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

#include <boost/pfr/detail/config.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <type_traits>
#include <utility> // metaprogramming stuff
#endif

#include <boost/pfr/detail/sequence_tuple.hpp>
#include <boost/pfr/detail/offset_based_getter.hpp>
Expand Down
4 changes: 4 additions & 0 deletions include/boost/pfr/detail/core14_loophole.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@

#include <boost/pfr/detail/config.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <type_traits>
#include <utility>
#endif

#include <boost/pfr/detail/offset_based_getter.hpp>
#include <boost/pfr/detail/fields_count.hpp>
Expand Down
5 changes: 5 additions & 0 deletions include/boost/pfr/detail/core17_generated.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@

#include <boost/pfr/detail/sequence_tuple.hpp>
#include <boost/pfr/detail/size_t_.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <type_traits> // for std::conditional_t, std::is_reference
#endif

namespace boost { namespace pfr { namespace detail {

Expand Down
5 changes: 5 additions & 0 deletions include/boost/pfr/detail/core_name20_static.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
#include <boost/pfr/detail/fields_count.hpp>
#include <boost/pfr/detail/stdarray.hpp>
#include <boost/pfr/detail/fake_object.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <type_traits>
#include <string_view>
#include <array>
#include <memory> // for std::addressof
#endif

namespace boost { namespace pfr { namespace detail {

Expand Down
4 changes: 4 additions & 0 deletions include/boost/pfr/detail/detectors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

#include <boost/pfr/detail/config.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <functional>
#include <type_traits>
#endif

namespace boost { namespace pfr { namespace detail {
///////////////////// `value` is true if Detector<Tleft, Tright> does not compile (SFINAE)
Expand Down
4 changes: 4 additions & 0 deletions include/boost/pfr/detail/fields_count.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
#include <boost/pfr/detail/size_t_.hpp>
#include <boost/pfr/detail/unsafe_declval.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <climits> // CHAR_BIT
#include <type_traits>
#include <utility> // metaprogramming stuff
#endif

#ifdef __clang__
# pragma clang diagnostic push
Expand Down
4 changes: 4 additions & 0 deletions include/boost/pfr/detail/for_each_field_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

#include <boost/pfr/detail/config.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <utility> // metaprogramming stuff
#endif

#include <boost/pfr/detail/sequence_tuple.hpp>
#include <boost/pfr/detail/rvalue_t.hpp>
Expand Down
4 changes: 4 additions & 0 deletions include/boost/pfr/detail/functional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

#include <boost/pfr/detail/config.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <functional>
#include <cstdint>
#endif

#include <boost/pfr/detail/sequence_tuple.hpp>

Expand Down
6 changes: 6 additions & 0 deletions include/boost/pfr/detail/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include <boost/pfr/detail/config.hpp>

#include <boost/pfr/detail/sequence_tuple.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <iosfwd> // stream operators
#include <iomanip>

Expand All @@ -19,6 +23,8 @@
# endif
#endif

#endif

namespace boost { namespace pfr { namespace detail {

inline auto quoted_helper(const std::string& s) noexcept {
Expand Down
5 changes: 5 additions & 0 deletions include/boost/pfr/detail/make_flat_tuple_of_references.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@

#include <boost/pfr/detail/config.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <utility> // metaprogramming stuff
#endif

#include <boost/pfr/detail/sequence_tuple.hpp>
#include <boost/pfr/detail/rvalue_t.hpp>
#include <boost/pfr/detail/make_integer_sequence.hpp>
Expand Down
4 changes: 4 additions & 0 deletions include/boost/pfr/detail/make_integer_sequence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@

#include <boost/pfr/detail/config.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <type_traits>
#include <utility>
#include <cstddef>
#endif

namespace boost { namespace pfr { namespace detail {

Expand Down
5 changes: 5 additions & 0 deletions include/boost/pfr/detail/offset_based_getter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@

#include <boost/pfr/detail/config.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <type_traits>
#include <utility>
#include <memory> // std::addressof
#endif

#include <boost/pfr/detail/sequence_tuple.hpp>
#include <boost/pfr/detail/rvalue_t.hpp>
#include <boost/pfr/detail/size_t_.hpp>
Expand Down
4 changes: 4 additions & 0 deletions include/boost/pfr/detail/possible_reflectable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
#include <boost/pfr/detail/config.hpp>
#include <boost/pfr/traits_fwd.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <type_traits> // for std::is_aggregate
#endif

namespace boost { namespace pfr { namespace detail {

Expand Down
4 changes: 4 additions & 0 deletions include/boost/pfr/detail/rvalue_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
#define BOOST_PFR_DETAIL_RVALUE_T_HPP
#pragma once

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <type_traits>
#include <utility> // std::enable_if_t
#endif

// This header provides aliases rvalue_t and lvalue_t.
//
Expand Down
4 changes: 4 additions & 0 deletions include/boost/pfr/detail/sequence_tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
#include <boost/pfr/detail/config.hpp>
#include <boost/pfr/detail/make_integer_sequence.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <utility> // metaprogramming stuff
#include <cstddef> // std::size_t
#endif

///////////////////// Tuple that holds its values in the supplied order
namespace boost { namespace pfr { namespace detail { namespace sequence_tuple {
Expand Down
6 changes: 5 additions & 1 deletion include/boost/pfr/detail/size_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

#include <boost/pfr/detail/config.hpp>

#include <cstddef> // metaprogramming stuff
#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <cstddef>
#endif

namespace boost { namespace pfr { namespace detail {

Expand Down
4 changes: 4 additions & 0 deletions include/boost/pfr/detail/stdarray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@

#include <boost/pfr/detail/config.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <utility> // metaprogramming stuff
#include <array>
#include <type_traits> // for std::common_type_t
#include <cstddef>
#endif

#include <boost/pfr/detail/sequence_tuple.hpp>

Expand Down
4 changes: 4 additions & 0 deletions include/boost/pfr/detail/stdtuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

#include <boost/pfr/detail/config.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <utility> // metaprogramming stuff
#include <tuple>
#endif

#include <boost/pfr/detail/sequence_tuple.hpp>

Expand Down
4 changes: 4 additions & 0 deletions include/boost/pfr/detail/tie_from_structure_tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
#include <boost/pfr/tuple_size.hpp>
#include <boost/pfr/detail/make_integer_sequence.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <tuple>
#endif

namespace boost { namespace pfr { namespace detail {

Expand Down
4 changes: 4 additions & 0 deletions include/boost/pfr/detail/unsafe_declval.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

#include <boost/pfr/detail/config.hpp>

#ifdef BOOST_PFR_HAS_STD_MODULE
import std;
#else
#include <type_traits>
#endif

namespace boost { namespace pfr { namespace detail {

Expand Down
Loading

0 comments on commit 6039165

Please sign in to comment.