Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
memory.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
21#include "shambase/string.hpp"
22#include <memory>
23#include <optional>
24#include <utility>
25
26namespace shambase {
27
40 template<class T, class AccU8>
41 inline void store_u8(AccU8 &acc, u64 idx_write, T a) {
42 constexpr u64 szT = sizeof(T);
43 u8 *bytes = (u8 *) &a;
44#pragma unroll
45 for (u64 i = 0; i < szT; i++) {
46 acc[idx_write + i] = bytes[i];
47 }
48 }
49
59 template<class T, class AccU8>
60 inline T load_u8(AccU8 &acc, u64 ptr_load) {
61 constexpr u64 szT = sizeof(T);
62 T ret;
63 u8 *bytes = (u8 *) &ret;
64#pragma unroll
65 for (u64 i = 0; i < szT; i++) {
66 bytes[i] = acc[ptr_load + i];
67 }
68 return ret;
69 }
70
79 template<class T, class TAcc>
80 inline void store_conv(TAcc *acc, T a) {
81 T *ptr = (T *) acc;
82 *ptr = a;
83 }
84
93 template<class T, class TAcc>
94 inline T load_conv(TAcc *acc) {
95 T *ptr = (T *) acc;
96 return *ptr;
97 }
98
109 template<class T>
110 inline T &get_check_ref(const std::unique_ptr<T> &ptr, SourceLocation loc = SourceLocation()) {
111 // Check if the std::unique_ptr holds anything
112 if (!bool(ptr)) {
113 // Throw a std::runtime_error with a descriptive message
114 throw make_except_with_loc<std::runtime_error>("the ptr does not hold anything", loc);
115 }
116 // Return a reference to the object held by the std::unique_ptr
117 return *ptr;
118 }
119
130 template<class T>
131 inline T &get_check_ref(const std::shared_ptr<T> &ptr, SourceLocation loc = SourceLocation()) {
132 // Check if the std::shared_ptr holds anything
133 if (!bool(ptr)) {
134 // Throw a std::runtime_error with a descriptive message
135 throw make_except_with_loc<std::runtime_error>("the ptr does not hold anything", loc);
136 }
137 // Return a reference to the object held by the std::shared_ptr
138 return *ptr;
139 }
140
148 template<class T>
149 inline const T &get_check_ref(
150 const std::optional<T> &ptr, SourceLocation loc = SourceLocation()) {
151 if (!bool(ptr)) {
152 throw make_except_with_loc<std::runtime_error>("the ptr does not hold anything", loc);
153 }
154 return *ptr;
155 }
156
158 template<class T>
159 inline T &get_check_ref(std::optional<T> &ptr, SourceLocation loc = SourceLocation()) {
160 if (!bool(ptr)) {
161 throw make_except_with_loc<std::runtime_error>("the ptr does not hold anything", loc);
162 }
163 return *ptr;
164 }
165
176 template<class T>
177 inline T &get_check_ref(T *ptr, SourceLocation loc = SourceLocation()) {
178 if (!ptr) {
180 "Can not get reference to null pointer", loc);
181 }
182 return *ptr;
183 }
184
186 template<class T>
187 inline const T &get_check_ref(const T *ptr, SourceLocation loc = SourceLocation()) {
188 if (!ptr) {
190 "Can not get reference to null pointer", loc);
191 }
192 return *ptr;
193 }
194
210 template<typename T>
211 auto extract_value(std::optional<T> &o, SourceLocation loc = SourceLocation()) -> T {
212 if (!bool(o)) {
214 "the value cannot be extracted, as the optional is empty", loc);
215 }
216 return std::exchange(o, std::nullopt).value();
217 }
218
226 template<typename T>
227 auto extract_pointer(std::unique_ptr<T> &o, SourceLocation loc = SourceLocation()) -> T {
228 if (!bool(o)) {
230 "the value cannot be extracted, as the unique_ptr is empty", loc);
231 }
232 std::unique_ptr<T> tmp = std::exchange(o, {});
233 return T(std::move(*tmp));
234 }
235
245 template<int n, class T>
246 inline std::array<T, n> convert_to_array(std::vector<T> &in) {
247 if (in.size() != n) {
249 "you've input values with the wrong size, input size = {}, wanted = {}",
250 in.size(),
251 n));
252 }
253
254 std::array<T, n> tmp;
255
256 for (u32 i = 0; i < n; i++) {
257 tmp[i] = in[i];
258 }
259
260 return tmp;
261 }
262
263} // namespace shambase
std::uint8_t u8
8 bit unsigned integer
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
This header file contains utility functions related to exception handling in the code.
namespace for basic c++ utilities
void store_u8(AccU8 &acc, u64 idx_write, T a)
Store a value of type T in a byte buffer.
Definition memory.hpp:41
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
auto extract_value(std::optional< T > &o, SourceLocation loc=SourceLocation()) -> T
Extracts the content out of an optional.
Definition memory.hpp:211
std::array< T, n > convert_to_array(std::vector< T > &in)
Convert a vector to an array of size n.
Definition memory.hpp:246
T load_conv(TAcc *acc)
pointer cast load from a pointer
Definition memory.hpp:94
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
Definition memory.hpp:110
auto extract_pointer(std::unique_ptr< T > &o, SourceLocation loc=SourceLocation()) -> T
extract content out of unique_ptr
Definition memory.hpp:227
T load_u8(AccU8 &acc, u64 ptr_load)
load a value of type T from a byte buffer
Definition memory.hpp:60
void store_conv(TAcc *acc, T a)
pointer cast store the value
Definition memory.hpp:80
provide information about the source location