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
18
22#include <array>
23#include <memory>
24#include <optional>
25#include <utility>
26#include <vector>
27
28namespace shambase {
29
42 template<class T, class AccU8>
43 inline void store_u8(AccU8 &acc, u64 idx_write, T a) {
44 constexpr u64 szT = sizeof(T);
45 u8 *bytes = (u8 *) &a;
46#pragma unroll
47 for (u64 i = 0; i < szT; i++) {
48 acc[idx_write + i] = bytes[i];
49 }
50 }
51
61 template<class T, class AccU8>
62 inline T load_u8(AccU8 &acc, u64 ptr_load) {
63 constexpr u64 szT = sizeof(T);
64 T ret;
65 u8 *bytes = (u8 *) &ret;
66#pragma unroll
67 for (u64 i = 0; i < szT; i++) {
68 bytes[i] = acc[ptr_load + i];
69 }
70 return ret;
71 }
72
81 template<class T, class TAcc>
82 inline void store_conv(TAcc *acc, T a) {
83 T *ptr = (T *) acc;
84 *ptr = a;
85 }
86
95 template<class T, class TAcc>
96 inline T load_conv(TAcc *acc) {
97 T *ptr = (T *) acc;
98 return *ptr;
99 }
100
111 template<class T>
112 inline T &get_check_ref(const std::unique_ptr<T> &ptr, SourceLocation loc = SourceLocation()) {
113 // Check if the std::unique_ptr holds anything
114 if (!bool(ptr)) {
115 // Throw a std::runtime_error with a descriptive message
116 throw make_except_with_loc<std::runtime_error>("the ptr does not hold anything", loc);
117 }
118 // Return a reference to the object held by the std::unique_ptr
119 return *ptr;
120 }
121
132 template<class T>
133 inline T &get_check_ref(const std::shared_ptr<T> &ptr, SourceLocation loc = SourceLocation()) {
134 // Check if the std::shared_ptr holds anything
135 if (!bool(ptr)) {
136 // Throw a std::runtime_error with a descriptive message
137 throw make_except_with_loc<std::runtime_error>("the ptr does not hold anything", loc);
138 }
139 // Return a reference to the object held by the std::shared_ptr
140 return *ptr;
141 }
142
150 template<class T>
151 inline const T &get_check_ref(
152 const std::optional<T> &ptr, SourceLocation loc = SourceLocation()) {
153 if (!bool(ptr)) {
154 throw make_except_with_loc<std::runtime_error>("the ptr does not hold anything", loc);
155 }
156 return *ptr;
157 }
158
160 template<class T>
161 inline T &get_check_ref(std::optional<T> &ptr, SourceLocation loc = SourceLocation()) {
162 if (!bool(ptr)) {
163 throw make_except_with_loc<std::runtime_error>("the ptr does not hold anything", loc);
164 }
165 return *ptr;
166 }
167
178 template<class T>
179 inline T &get_check_ref(T *ptr, SourceLocation loc = SourceLocation()) {
180 if (!ptr) {
182 "Can not get reference to null pointer", loc);
183 }
184 return *ptr;
185 }
186
188 template<class T>
189 inline const T &get_check_ref(const T *ptr, SourceLocation loc = SourceLocation()) {
190 if (!ptr) {
192 "Can not get reference to null pointer", loc);
193 }
194 return *ptr;
195 }
196
212 template<typename T>
213 auto extract_value(std::optional<T> &o, SourceLocation loc = SourceLocation()) -> T {
214 if (!bool(o)) {
216 "the value cannot be extracted, as the optional is empty", loc);
217 }
218 return std::exchange(o, std::nullopt).value();
219 }
220
228 template<typename T>
229 auto extract_pointer(std::unique_ptr<T> &o, SourceLocation loc = SourceLocation()) -> T {
230 if (!bool(o)) {
232 "the value cannot be extracted, as the unique_ptr is empty", loc);
233 }
234 std::unique_ptr<T> tmp = std::exchange(o, {});
235 return T(std::move(*tmp));
236 }
237
247 template<int n, class T>
248 inline std::array<T, n> convert_to_array(std::vector<T> &in) {
249 if (in.size() != n) {
251 "you've input values with the wrong size, input size = {}, wanted = {}",
252 in.size(),
253 n));
254 }
255
256 std::array<T, n> tmp;
257
258 for (u32 i = 0; i < n; i++) {
259 tmp[i] = in[i];
260 }
261
262 return tmp;
263 }
264
265} // 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.
Core formatting functions: format, vformat, and format_printf.
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:43
auto extract_value(std::optional< T > &o, SourceLocation loc=SourceLocation()) -> T
Extracts the content out of an optional.
Definition memory.hpp:213
std::array< T, n > convert_to_array(std::vector< T > &in)
Convert a vector to an array of size n.
Definition memory.hpp:248
T load_conv(TAcc *acc)
pointer cast load from a pointer
Definition memory.hpp:96
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:112
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
auto extract_pointer(std::unique_ptr< T > &o, SourceLocation loc=SourceLocation()) -> T
extract content out of unique_ptr
Definition memory.hpp:229
T load_u8(AccU8 &acc, u64 ptr_load)
load a value of type T from a byte buffer
Definition memory.hpp:62
void store_conv(TAcc *acc, T a)
pointer cast store the value
Definition memory.hpp:82
provide information about the source location