ImpactX
Loading...
Searching...
No Matches
named.H
Go to the documentation of this file.
1/* Copyright 2022-2024 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 {
35 void set_name (
36 std::string const & new_name
37 )
38 {
39 // free old name
40 if (m_name != nullptr) {
41 delete[] m_name;
42 m_name = nullptr;
43 }
44
45 // set new name
46 if (new_name.size() > 0) {
47 m_name = new char[new_name.size() + 1];
48 std::strcpy(m_name, new_name.c_str());
49 }
50 }
51
58 std::optional<std::string> name
59 )
60 {
61 if (name.has_value()) {
62 set_name(*name);
63 }
64 }
65
68 {
70 if (m_name != nullptr) {
71 delete[] m_name;
72 m_name = nullptr;
73 }
74 ))
75 }
76
78 Named (Named const & other)
79 {
80 if (other.has_name()) {
81 m_name = new char[std::strlen(other.m_name) + 1];
82 std::strcpy(m_name, other.m_name);
83 }
84 }
85
87 Named& operator=(Named const & other)
88 {
89 if (&other != this) {
90 if (other.has_name()) {
91 m_name = new char[std::strlen(other.m_name) + 1];
92 std::strcpy(m_name, other.m_name);
93 }
94 }
95 return *this;
96 }
97
99 Named ([[maybe_unused]] Named && other) noexcept
100 {
102 std::swap(this->m_name, other.m_name);
103 other.m_name = nullptr;
104 ))
105 }
106
108 Named& operator=([[maybe_unused]] Named && other) noexcept
109 {
111 std::swap(this->m_name, other.m_name);
112 other.m_name = nullptr;
113 ))
114 return *this;
115 }
116
122 std::string name () const
123 {
124 if (!has_name()) {
125 throw std::runtime_error("Name not set on element!");
126 }
127 return std::string(m_name);
128 }
129
135 bool has_name () const
136 {
137 return m_name != nullptr;
138 }
139
140 private:
141 // Implementation note:
142 // This is used as a mixin class in elements that are copied to GPU. GPU compilers copy
143 // a whole element by its sizeof(T).
144 // To save on this copy operation at kernel start, this is implemented
145 // as a simple C pointer (8 bytes), contrary to a std::string (32 bytes) or
146 // a std::optional<std::string> (40 bytes). m_name points to host-side memory and
147 // must not be dereferenced (used) on GPU.
148 char * m_name = nullptr;
149 };
150
151} // namespace impactx::elements::mixin
152
153#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:23
AMREX_FORCE_INLINE bool has_name() const
Definition named.H:135
AMREX_GPU_HOST Named(std::optional< std::string > name)
Definition named.H:57
AMREX_GPU_HOST_DEVICE Named & operator=(Named &&other) noexcept
Definition named.H:108
AMREX_GPU_HOST Named & operator=(Named const &other)
Definition named.H:87
AMREX_GPU_HOST void set_name(std::string const &new_name)
Definition named.H:35
AMREX_GPU_HOST Named(Named const &other)
Definition named.H:78
AMREX_GPU_HOST_DEVICE Named(Named &&other) noexcept
Definition named.H:99
char * m_name
Definition named.H:148
AMREX_GPU_HOST_DEVICE ~Named()
Definition named.H:67
AMREX_FORCE_INLINE std::string name() const
Definition named.H:122