opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
object_representation.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <cstddef>
7#include <ranges>
8#include <span>
9
10namespace osc
11{
12 template<ObjectRepresentationByte Byte = std::byte, BitCastable T>
13 constexpr std::span<const Byte, sizeof(T)> view_object_representation(const T& v)
14 {
15 // this is one of the few cases where `reinterpret_cast` is guaranteed to be safe
16 // for _examination_ (i.e. reading)
17 //
18 // > from: https://en.cppreference.com/w/cpp/language/reinterpret_cast
19 // >
20 // > Whenever an attempt is made to read or modify the stored value of an object of
21 // > type DynamicType through a glvalue of type AliasedType, the behavior is undefined
22 // > unless one of the following is true:
23 // >
24 // > - AliasedType is std::byte,(since C++17) char, or unsigned char: this permits
25 // > examination of the object representation of any object as an array of bytes.
26 return std::span<const Byte, sizeof(T)>{reinterpret_cast<const Byte*>(&v), sizeof(T)}; // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
27 }
28
29 template<ObjectRepresentationByte Byte = std::byte, std::ranges::contiguous_range Range>
31 constexpr std::span<const Byte> view_object_representations(const Range& range)
32 {
33 // this is one of the few cases where `reinterpret_cast` is guaranteed to be safe
34 // for _examination_ (i.e. reading)
35 //
36 // > from: https://en.cppreference.com/w/cpp/language/reinterpret_cast
37 // >
38 // > Whenever an attempt is made to read or modify the stored value of an object of
39 // > type DynamicType through a glvalue of type AliasedType, the behavior is undefined
40 // > unless one of the following is true:
41 // >
42 // > - AliasedType is std::byte,(since C++17) char, or unsigned char: this permits
43 // > examination of the object representation of any object as an array of bytes.
44 return {reinterpret_cast<const Byte*>(std::ranges::data(range)), sizeof(typename Range::value_type) * std::ranges::size(range)}; // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
45 }
46}
Definition custom_decoration_generator.h:5
constexpr std::span< const Byte, sizeof(T)> view_object_representation(const T &v)
Definition object_representation.h:13
constexpr std::span< const Byte > view_object_representations(const Range &range)
Definition object_representation.h:31
constexpr U to(T &&value)
Definition conversion.h:56