ImpactX
Loading...
Searching...
No Matches
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
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
25{
28 struct Named
29 {
30 static constexpr std::nullopt_t DEFAULT_name = std::nullopt;
31
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
60 std::optional<std::string> name
61 )
62 {
63 if (name.has_value()) {
64 set_name(*name);
65 }
66 }
67
70 {
72 if (m_name != nullptr) {
73 delete[] m_name;
74 m_name = nullptr;
75 }
76 ))
77 }
78
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
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
101 Named ([[maybe_unused]] Named && other) noexcept
102 {
104 std::swap(this->m_name, other.m_name);
105 other.m_name = nullptr;
106 ))
107 }
108
110 Named& operator=([[maybe_unused]] Named && other) noexcept
111 {
113 std::swap(this->m_name, other.m_name);
114 other.m_name = nullptr;
115 ))
116 return *this;
117 }
118
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
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
#define AMREX_FORCE_INLINE
#define AMREX_IF_ON_HOST(CODE)
#define AMREX_GPU_HOST_DEVICE
#define AMREX_GPU_HOST
Definition alignment.H:25
static constexpr std::nullopt_t DEFAULT_name
Definition named.H:30
AMREX_FORCE_INLINE bool has_name() const
Definition named.H:137
AMREX_GPU_HOST Named(std::optional< std::string > name)
Definition named.H:59
AMREX_GPU_HOST_DEVICE Named & operator=(Named &&other) noexcept
Definition named.H:110
AMREX_GPU_HOST Named & operator=(Named const &other)
Definition named.H:89
AMREX_GPU_HOST void set_name(std::string const &new_name)
Definition named.H:37
AMREX_GPU_HOST Named(Named const &other)
Definition named.H:80
AMREX_GPU_HOST_DEVICE Named(Named &&other) noexcept
Definition named.H:101
char * m_name
Definition named.H:150
AMREX_GPU_HOST_DEVICE ~Named()
Definition named.H:69
AMREX_FORCE_INLINE std::string name() const
Definition named.H:124