ImpactX
Toggle main menu visibility
Loading...
Searching...
No Matches
src
elements
mixin
named.H
Go to the documentation of this file.
1
/* Copyright 2022-2026 The Regents of the University of California, through Lawrence
2
* Berkeley National Laboratory (subject to receipt of any required
3
* approvals from the U.S. Dept. of Energy). All rights reserved.
4
*
5
* This file is part of ImpactX.
6
*
7
* Authors: Axel Huebl
8
* License: BSD-3-Clause-LBNL
9
*/
10
#ifndef IMPACTX_ELEMENTS_MIXIN_NAMED_H
11
#define IMPACTX_ELEMENTS_MIXIN_NAMED_H
12
13
#include "
particles/ImpactXParticleContainer.H
"
14
15
#include <
AMReX_Extension.H
>
16
#include <
AMReX_GpuQualifiers.H
>
17
18
#include <cstring>
19
#include <optional>
20
#include <stdexcept>
21
#include <string>
22
23
24
namespace
impactx::elements::mixin
25
{
28
struct
Named
29
{
30
static
constexpr
std::nullopt_t
DEFAULT_name
= std::nullopt;
31
36
AMREX_GPU_HOST
37
void
set_name
(
38
std::string
const
& new_name
39
)
40
{
41
// free old name
42
if
(
m_name
!=
nullptr
) {
43
delete
[]
m_name
;
44
m_name
=
nullptr
;
45
}
46
47
// set new name
48
if
(new_name.size() > 0) {
49
m_name
=
new
char
[new_name.size() + 1];
50
std::strcpy(
m_name
, new_name.c_str());
51
}
52
}
53
58
AMREX_GPU_HOST
59
Named
(
60
std::optional<std::string>
name
61
)
62
{
63
if
(
name
.has_value()) {
64
set_name
(*
name
);
65
}
66
}
67
68
AMREX_GPU_HOST_DEVICE
69
~Named
()
70
{
71
AMREX_IF_ON_HOST
((
72
if
(
m_name
!=
nullptr
) {
73
delete
[]
m_name
;
74
m_name
=
nullptr
;
75
}
76
))
77
}
78
79
AMREX_GPU_HOST
80
Named
(
Named
const
& other)
81
{
82
if
(other.
has_name
()) {
83
m_name
=
new
char
[std::strlen(other.
m_name
) + 1];
84
std::strcpy(
m_name
, other.
m_name
);
85
}
86
}
87
88
AMREX_GPU_HOST
89
Named
&
operator=
(
Named
const
& other)
90
{
91
if
(&other !=
this
) {
92
if
(other.
has_name
()) {
93
m_name
=
new
char
[std::strlen(other.
m_name
) + 1];
94
std::strcpy(
m_name
, other.
m_name
);
95
}
96
}
97
return
*
this
;
98
}
99
100
AMREX_GPU_HOST_DEVICE
101
Named
([[maybe_unused]]
Named
&& other)
noexcept
102
{
103
AMREX_IF_ON_HOST
((
104
std::swap(this->
m_name
, other.m_name);
105
other.m_name =
nullptr
;
106
))
107
}
108
109
AMREX_GPU_HOST_DEVICE
110
Named
&
operator=
([[maybe_unused]]
Named
&& other)
noexcept
111
{
112
AMREX_IF_ON_HOST
((
113
std::swap(this->
m_name
, other.m_name);
114
other.m_name =
nullptr
;
115
))
116
return
*
this
;
117
}
118
123
AMREX_FORCE_INLINE
124
std::string
name
()
const
125
{
126
if
(!
has_name
()) {
127
throw
std::runtime_error(
"Name not set on element!"
);
128
}
129
return
std::string(
m_name
);
130
}
131
136
AMREX_FORCE_INLINE
137
bool
has_name
()
const
138
{
139
return
m_name
!=
nullptr
;
140
}
141
142
private
:
143
// Implementation note:
144
// This is used as a mixin class in elements that are copied to GPU. GPU compilers copy
145
// a whole element by its sizeof(T).
146
// To save on this copy operation at kernel start, this is implemented
147
// as a simple C pointer (8 bytes), contrary to a std::string (32 bytes) or
148
// a std::optional<std::string> (40 bytes). m_name points to host-side memory and
149
// must not be dereferenced (used) on GPU.
150
char
*
m_name
=
nullptr
;
151
};
152
153
}
// namespace impactx::elements::mixin
154
155
#endif
// IMPACTX_ELEMENTS_MIXIN_NAMED_H
AMReX_Extension.H
AMREX_FORCE_INLINE
#define AMREX_FORCE_INLINE
AMReX_GpuQualifiers.H
AMREX_IF_ON_HOST
#define AMREX_IF_ON_HOST(CODE)
AMREX_GPU_HOST_DEVICE
#define AMREX_GPU_HOST_DEVICE
AMREX_GPU_HOST
#define AMREX_GPU_HOST
ImpactXParticleContainer.H
impactx::elements::mixin
Definition
alignment.H:25
impactx::elements::mixin::Named::DEFAULT_name
static constexpr std::nullopt_t DEFAULT_name
Definition
named.H:30
impactx::elements::mixin::Named::has_name
AMREX_FORCE_INLINE bool has_name() const
Definition
named.H:137
impactx::elements::mixin::Named::Named
AMREX_GPU_HOST Named(std::optional< std::string > name)
Definition
named.H:59
impactx::elements::mixin::Named::operator=
AMREX_GPU_HOST_DEVICE Named & operator=(Named &&other) noexcept
Definition
named.H:110
impactx::elements::mixin::Named::operator=
AMREX_GPU_HOST Named & operator=(Named const &other)
Definition
named.H:89
impactx::elements::mixin::Named::set_name
AMREX_GPU_HOST void set_name(std::string const &new_name)
Definition
named.H:37
impactx::elements::mixin::Named::Named
AMREX_GPU_HOST Named(Named const &other)
Definition
named.H:80
impactx::elements::mixin::Named::Named
AMREX_GPU_HOST_DEVICE Named(Named &&other) noexcept
Definition
named.H:101
impactx::elements::mixin::Named::m_name
char * m_name
Definition
named.H:150
impactx::elements::mixin::Named::~Named
AMREX_GPU_HOST_DEVICE ~Named()
Definition
named.H:69
impactx::elements::mixin::Named::name
AMREX_FORCE_INLINE std::string name() const
Definition
named.H:124
Generated by
1.17.0