Skip to content

Commit 1b3d51f

Browse files
authored
Ref: Add concepts and restructure io directories (#911)
Restructure the io folder to make it easier to understand which classes are part of the backend and which belong to the frontend: merge the json reader and writer to the json_converter. This way only the backend types are called "reader" and "writer" rename the "common" folder to "backend" move the detector related concepts to detray/core Also factors out a function read_components_from_file from the detector reader, so that readers can be added to a detector construction workflow without calling the read_detector function (e.g. if some components should be read from file, while others are built/converted directly). I tried to fix a nullptr dereferencing warning in the surface_factory_interface that appeared with gcc 14, but it seems to be a false positive
1 parent 5732f6b commit 1b3d51f

35 files changed

+581
-467
lines changed

core/include/detray/builders/surface_factory_interface.hpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77

88
#pragma once
99

10+
// TODO: Remove this when gcc fixes their false positives.
11+
#if defined(__GNUC__) && !defined(__clang__)
12+
#pragma GCC diagnostic warning "-Wnull-dereference"
13+
#endif
14+
1015
// Project include(s).
1116
#include "detray/definitions/detail/indexing.hpp"
1217
#include "detray/definitions/detail/qualifiers.hpp"
1318
#include "detray/definitions/geometry.hpp"
1419

1520
// System include(s)
1621
#include <memory>
22+
#include <stdexcept>
1723
#include <tuple>
1824
#include <vector>
1925

@@ -160,7 +166,11 @@ class factory_decorator : public surface_factory_interface<detector_t> {
160166
explicit factory_decorator(
161167
std::unique_ptr<surface_factory_interface<detector_t>> factory)
162168
: m_factory(std::move(factory)) {
163-
assert(m_factory != nullptr);
169+
if (m_factory == nullptr || m_factory.get() == nullptr) {
170+
throw std::runtime_error(
171+
"Surface factory decorator constructed with invalid base "
172+
"factory");
173+
}
164174
}
165175

166176
/// @returns access to the underlying factory - const

core/include/detray/core/concepts.hpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/** Detray library, part of the ACTS project (R&D line)
2+
*
3+
* (c) 2025 CERN for the benefit of the ACTS project
4+
*
5+
* Mozilla Public License Version 2.0
6+
*/
7+
8+
#pragma once
9+
10+
// Project include(s)
11+
#include "detray/core/detail/type_traits.hpp"
12+
13+
namespace detray::concepts {
14+
15+
/// Check for the the presence of any type of grids in a detector definition
16+
template <class detector_t>
17+
concept has_grids = detail::contains_grids_v<typename detector_t::accel> ||
18+
detail::contains_grids_v<typename detector_t::materials>;
19+
20+
/// Check for the the presence of surface grids in a detector definition
21+
template <class detector_t>
22+
concept has_surface_grids =
23+
detail::contains_surface_grids_v<typename detector_t::accel>;
24+
25+
/// Check for the the presence of material slabs in a detector definition
26+
template <class detector_t>
27+
concept has_material_slabs =
28+
detail::contains_material_slabs_v<typename detector_t::materials>;
29+
30+
/// Check for the the presence of material rods in a detector definition
31+
template <class detector_t>
32+
concept has_material_rods =
33+
detail::contains_material_rods_v<typename detector_t::materials>;
34+
35+
/// Check for the the presence of homogeneous material types in a detector
36+
/// definition
37+
template <class detector_t>
38+
concept has_homogeneous_material =
39+
detail::contains_homogeneous_material_v<typename detector_t::materials>;
40+
41+
/// Check for the the presence of material maps in a detector definition
42+
template <class detector_t>
43+
concept has_material_maps =
44+
detail::contains_material_maps_v<typename detector_t::materials>;
45+
46+
} // namespace detray::concepts

io/include/detray/io/frontend/detail/type_traits.hpp core/include/detray/core/detail/type_traits.hpp

+3-43
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** Detray library, part of the ACTS project (R&D line)
22
*
3-
* (c) 2023-2024 CERN for the benefit of the ACTS project
3+
* (c) 2022-2025 CERN for the benefit of the ACTS project
44
*
55
* Mozilla Public License Version 2.0
66
*/
@@ -14,14 +14,11 @@
1414
#include "detray/navigation/accelerators/concepts.hpp"
1515
#include "detray/utils/grid/detail/concepts.hpp"
1616
#include "detray/utils/type_registry.hpp"
17-
#include "detray/utils/type_traits.hpp"
1817

1918
// System include(s)
2019
#include <type_traits>
2120

22-
namespace detray {
23-
24-
namespace detail {
21+
namespace detray::detail {
2522

2623
/// Check for grid types in a data store
2724
/// @{
@@ -115,41 +112,4 @@ inline constexpr bool contains_material_maps_v =
115112
contains_material_maps<T>::value;
116113
/// @}
117114

118-
} // namespace detail
119-
120-
namespace concepts {
121-
122-
/// Check for the the presence of any type of grids in a detector definition
123-
template <class detector_t>
124-
concept has_grids = detail::contains_grids_v<typename detector_t::accel> ||
125-
detail::contains_grids_v<typename detector_t::materials>;
126-
127-
/// Check for the the presence of surface grids in a detector definition
128-
template <class detector_t>
129-
concept has_surface_grids =
130-
detail::contains_surface_grids_v<typename detector_t::accel>;
131-
132-
/// Check for the the presence of material slabs in a detector definition
133-
template <class detector_t>
134-
concept has_material_slabs =
135-
detail::contains_material_slabs_v<typename detector_t::materials>;
136-
137-
/// Check for the the presence of material rods in a detector definition
138-
template <class detector_t>
139-
concept has_material_rods =
140-
detail::contains_material_rods_v<typename detector_t::materials>;
141-
142-
/// Check for the the presence of homogeneous material types in a detector
143-
/// definition
144-
template <class detector_t>
145-
concept has_homogeneous_material =
146-
detail::contains_homogeneous_material_v<typename detector_t::materials>;
147-
148-
/// Check for the the presence of material maps in a detector definition
149-
template <class detector_t>
150-
concept has_material_maps =
151-
detail::contains_material_maps_v<typename detector_t::materials>;
152-
153-
} // namespace concepts
154-
155-
} // namespace detray
115+
} // namespace detray::detail

io/CMakeLists.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ detray_add_library( detray_io_utils io_utils
2121
file(
2222
GLOB _detray_io_public_headers
2323
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
24-
"include/detray/io/common/*.hpp"
24+
"include/detray/io/backend/*.hpp"
25+
"include/detray/io/backend/detail/*.hpp"
2526
"include/detray/io/covfie/*.hpp"
2627
"include/detray/io/frontend/*.hpp"
2728
"include/detray/io/frontend/detail/*.hpp"
28-
"include/detray/io/frontend/implementation/*.hpp"
29+
"include/detray/io/frontend/impl/*.hpp"
2930
"include/detray/io/json/*.hpp"
31+
"include/detray/io/json/detail/*.hpp"
3032
)
3133
detray_add_library( detray_io io
3234
${_detray_io_public_headers}
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/** Detray library, part of the ACTS project (R&D line)
2+
*
3+
* (c) 2025 CERN for the benefit of the ACTS project
4+
*
5+
* Mozilla Public License Version 2.0
6+
*/
7+
8+
#pragma once
9+
10+
// Project include(s)
11+
#include "detray/builders/detector_builder.hpp"
12+
13+
// System include(s)
14+
#include <concepts>
15+
#include <string_view>
16+
17+
namespace detray::io::concepts {
18+
19+
/// Concept for detray io reader backends
20+
template <typename D, typename R>
21+
concept reader_backend =
22+
requires(const R rb, detector_builder<typename D::metadata> det_builder,
23+
typename D::name_map names) {
24+
25+
typename R::payload_type;
26+
27+
{ R::tag }
28+
->std::same_as<const std::string_view&>;
29+
30+
{
31+
R::template from_payload<D>(det_builder, names,
32+
typename R::payload_type())
33+
}
34+
->std::same_as<void>;
35+
};
36+
37+
/// Concept for detray io writer backends
38+
template <typename D, typename W>
39+
concept writer_backend = requires(const W wb, D det,
40+
typename D::name_map names) {
41+
42+
typename W::payload_type;
43+
44+
{ W::tag }
45+
->std::same_as<const std::string_view&>;
46+
47+
{ W::to_payload(det, names) }
48+
->std::same_as<typename W::payload_type>;
49+
};
50+
51+
} // namespace detray::io::concepts

io/include/detray/io/common/detail/basic_converter.hpp io/include/detray/io/backend/detail/basic_converter.hpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
/** Detray library, part of the ACTS project (R&D line)
22
*
3-
* (c) 2023-2024 CERN for the benefit of the ACTS project
3+
* (c) 2023-2025 CERN for the benefit of the ACTS project
44
*
55
* Mozilla Public License Version 2.0
66
*/
77

88
#pragma once
99

1010
// Project include(s)
11-
#include "detray/io/frontend/detail/io_metadata.hpp"
1211
#include "detray/io/frontend/payloads.hpp"
12+
#include "detray/io/utils/io_metadata.hpp"
1313

1414
// System include(s)
1515
#include <string_view>
1616

17-
// Convert basic information like links and header data
17+
// Convert basic information like links and header data to and from payloads
1818
namespace detray::io::detail::basic_converter {
1919

2020
/// @returns a link from its io payload @param link_data
21-
inline dindex convert(const single_link_payload& link_data) {
21+
inline dindex from_payload(const single_link_payload& link_data) {
2222
return static_cast<dindex>(link_data.link);
2323
}
2424

2525
/// Convert a link @param idx into its io payload
26-
inline single_link_payload convert(const std::size_t idx) {
26+
inline single_link_payload to_payload(const std::size_t idx) {
2727
single_link_payload link_data;
2828
link_data.link = idx;
2929

@@ -33,8 +33,8 @@ inline single_link_payload convert(const std::size_t idx) {
3333
/// Convert a typed link with a type id @param id and index @param idx into its
3434
/// io payload
3535
template <typename type_id>
36-
inline typed_link_payload<type_id> convert(const type_id id,
37-
const std::size_t idx) {
36+
inline typed_link_payload<type_id> to_payload(const type_id id,
37+
const std::size_t idx) {
3838
typed_link_payload<type_id> link_data;
3939

4040
link_data.type = id;
@@ -46,8 +46,8 @@ inline typed_link_payload<type_id> convert(const type_id id,
4646
/// Convert the common header information using the detector name
4747
/// @param det_name and the file tag @param tag that describes the data file
4848
/// content
49-
inline common_header_payload convert(const std::string_view det_name,
50-
const std::string_view tag) {
49+
inline common_header_payload to_payload(const std::string_view det_name,
50+
const std::string_view tag) {
5151
common_header_payload header_data;
5252

5353
header_data.version = io::detail::get_detray_version();

0 commit comments

Comments
 (0)