-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
|
||
#if __cpp_lib_start_lifetime_as >= 202207L | ||
|
||
namespace alpaqa::util { | ||
using std::start_lifetime_as; | ||
using std::start_lifetime_as_array; | ||
} // namespace alpaqa::util | ||
|
||
#else | ||
|
||
#include <cstring> | ||
#include <new> | ||
#include <type_traits> | ||
|
||
namespace alpaqa::util { | ||
template <class T> | ||
requires std::is_trivially_copyable_v<T> | ||
T *start_lifetime_as_array(void *p, size_t n) noexcept { | ||
#if __cpp_lib_is_implicit_lifetime >= 202302L | ||
static_assert(std::is_implicit_lifetime_v<T>); | ||
#endif | ||
return std::launder(static_cast<T *>(std::memmove(p, p, n * sizeof(T)))); | ||
} | ||
template <class T> | ||
requires std::is_trivially_copyable_v<T> | ||
const T *start_lifetime_as_array(const void *p, size_t n) noexcept { | ||
#if __cpp_lib_is_implicit_lifetime >= 202302L | ||
static_assert(std::is_implicit_lifetime_v<T>); | ||
#endif | ||
static_cast<void>(n); // TODO | ||
// best we can do without compiler support | ||
return std::launder(static_cast<const T *>(p)); | ||
} | ||
template <class T> | ||
requires std::is_trivially_copyable_v<T> | ||
T *start_lifetime_as(void *p) noexcept { | ||
return start_lifetime_as_array<T>(p, 1); | ||
} | ||
template <class T> | ||
requires std::is_trivially_copyable_v<T> | ||
const T *start_lifetime_as(const void *p) noexcept { | ||
return start_lifetime_as_array<T>(p, 1); | ||
} | ||
} // namespace alpaqa::util | ||
|
||
#endif |