2#ifndef CEL_RUNTIME_VALUE_HPP
3#define CEL_RUNTIME_VALUE_HPP
5#include <cel/runtime/record_adapter.hpp>
6#include <cel/runtime/value_type.hpp>
34 const value_type& element;
35 const std::byte* data;
40struct concrete_adapter;
42concept nominal_record_adapter =
requires(
const T& input,
const value& encoded) {
43 {record_adapter<T>::type()}->std::same_as<value_type>;
44 {record_adapter<T>::to_value(input)}->std::same_as<value>;
45 {record_adapter<T>::from_value(encoded)}->std::same_as<T>;
48struct boundary_adapter;
84 std::unique_ptr<detail::owned_payload> _payload;
85 friend struct detail::value_access;
87 friend struct detail::concrete_adapter;
89 friend struct detail::boundary_adapter;
94 explicit value(std::unique_ptr<detail::owned_payload> payload);
108 void copy_leaf(
const value_type& expected,
void* destination)
const;
117 static value aggregate(
value_type type, std::span<const value> fields);
126 static value array_owned(
value_type element, std::span<value> children);
130 static const std::byte* raw_data(
const value& owner)
noexcept;
134 static value copy_canonical(
const value_type&
type,
const std::byte* data);
138 static void release_canonical(value& source, std::byte* destination)
noexcept;
143 static detail::array_view array_data(
const std::byte* data)
noexcept;
153 void (*write)(
const void*,
const value_type&, std::byte*));
163 static void write_array(
const value_type& element,
164 std::byte* destination,
167 void (*write)(
const void*, std::size_t,
const value_type&, std::byte*));
173 static void destroy_fields(
const value_type&
type, std::byte* data, std::size_t count)
noexcept;
195 value(const value& other);
199 value(value&& other) noexcept;
208 value& operator=(const value& other);
214 value& operator=(value&& other) noexcept;
229 static value
from(const T& source) {
230 return detail::boundary_adapter<std::remove_cv_t<T>>
::from(source);
244 return detail::boundary_adapter<std::remove_cv_t<T>>
::as(*
this);
266 static value
tuple(std::span<const value> fields);
304 value
at(std::size_t index)
const;
315 void set(std::size_t index,
const value& replacement);
325 value
field(std::string_view name)
const;
335 void set_field(std::string_view name,
const value& replacement);
344struct concrete_adapter {
350 static value from(
const T& source) {
return value::from_leaf(type(), &source); }
355 static T as(
const value& source) {
356 std::array<std::byte,
sizeof(T)> bytes{};
357 source.copy_leaf(type(), bytes.data());
358 return std::bit_cast<T>(bytes);
366 static T read(
const value_type&,
const std::byte* data) {
367 assert(data !=
nullptr);
368 std::array<std::byte,
sizeof(T)> bytes{};
369 std::memcpy(bytes.data(), data, bytes.size());
370 return std::bit_cast<T>(bytes);
377 static void write(
const T& source,
const value_type&, std::byte* data) {
378 static_assert(std::is_trivially_copyable_v<T>);
379 std::memcpy(data, &source,
sizeof(T));
386template <
class T,
class Allocator>
387struct concrete_adapter<std::vector<T, Allocator>> {
392 static value_type type() {
401 static value from(
const std::vector<T, Allocator>& source) {
402 return value::from_concrete(
403 type(), &source, [](
const void*
object,
const value_type& shape, std::byte* data) {
404 write(*
static_cast<const std::vector<T, Allocator>*
>(
object), shape, data);
414 static void write(
const std::vector<T, Allocator>& source,
415 const value_type& shape,
417 value::write_array(shape.element(), data, source.size(), &source,
418 [](
const void*
object, std::size_t index,
const value_type& element,
421 *static_cast<const std::vector<T, Allocator>*>(object);
422 boundary_adapter<T>::write(input[index], element, target);
431 static std::vector<T, Allocator> as(
const value& source) {
432 if (!source.type().same_shape(type()))
throw std::invalid_argument(
"wrong vector type");
433 return read(source.type(), value::raw_data(source));
443 static std::vector<T, Allocator> read(
const value_type&,
const std::byte* data) {
444 std::vector<T, Allocator> result;
445 const auto children = value::array_data(data);
446 result.reserve(children.size);
448 for (std::size_t i = 0; i != children.size; ++i)
449 result.push_back(boundary_adapter<T>::read(
450 children.element, children.data ? children.data + i * children.stride :
nullptr));
457struct concrete_adapter<std::
tuple<T...>> {
462 static value_type type() {
463 static const auto descriptor = [] {
464 const std::array<value_type,
sizeof...(T)> fields{boundary_adapter<T>::type()...};
474 static value from(
const std::tuple<T...>& source) {
475 return value::from_concrete(
476 type(), &source, [](
const void*
object,
const value_type& shape, std::byte* data) {
477 write(*
static_cast<const std::tuple<T...
>*>(
object), shape, data);
487 template <std::size_t... I>
488 static void write_fields(
const std::tuple<T...>& source,
489 const value_type& shape,
491 std::index_sequence<I...>) {
492 std::size_t completed = 0;
494 ((boundary_adapter<T>::write(std::get<I>(source), shape.fields()[I],
495 data ? data + shape.offsets()[I] :
nullptr),
499 value::destroy_fields(shape, data, completed);
509 static void write(
const std::tuple<T...>& source,
const value_type& shape, std::byte* data) {
510 write_fields(source, shape, data, std::index_sequence_for<T...>{});
519 template <std::size_t... I>
520 static std::tuple<T...> extract(
const value_type& shape,
521 const std::byte* data,
522 std::index_sequence<I...>) {
523 return {boundary_adapter<T>::read(shape.fields()[I],
524 data ? data + shape.offsets()[I] :
nullptr)...};
532 static std::tuple<T...> as(
const value& source) {
533 if (!source.type().same_shape(type()))
throw std::invalid_argument(
"wrong tuple type");
534 return read(source.type(), value::raw_data(source));
544 static std::tuple<T...> read(
const value_type& shape,
const std::byte* data) {
545 return extract(shape, data, std::index_sequence_for<T...>{});
550const value_type& adapter_record_type() {
551 static const auto descriptor = [] {
552 auto declared = record_adapter<T>::type();
554 throw std::invalid_argument(
"record adapter type must be a nominal record");
561struct boundary_adapter {
563 static value_type type() {
564 if constexpr (nominal_record_adapter<T>) {
565 return adapter_record_type<T>();
567 return concrete_adapter<T>::type();
571 static value from(
const T& source) {
572 if constexpr (nominal_record_adapter<T>) {
573 const auto& expected = adapter_record_type<T>();
574 auto encoded = record_adapter<T>::to_value(source);
575 if (!encoded.type().compatible_with(expected))
576 throw std::invalid_argument(
577 "record adapter returned the wrong nominal record type");
580 return concrete_adapter<T>::from(source);
584 static T as(
const value& source) {
585 if constexpr (nominal_record_adapter<T>) {
586 const auto& expected = adapter_record_type<T>();
587 if (!source.type().same_shape(expected))
588 throw std::invalid_argument(
"wrong nominal record type");
589 return record_adapter<T>::from_value(source);
591 return concrete_adapter<T>::as(source);
597 static T read(
const value_type& shape,
const std::byte* data) {
598 if constexpr (nominal_record_adapter<T>) {
599 const auto& expected = adapter_record_type<T>();
600 if (!shape.same_shape(expected))
601 throw std::invalid_argument(
"wrong nominal record type");
602 auto encoded = value::copy_canonical(shape, data);
603 return record_adapter<T>::from_value(encoded);
605 return concrete_adapter<T>::read(shape, data);
611 static void write(
const T& source,
const value_type& shape, std::byte* data) {
612 if constexpr (nominal_record_adapter<T>) {
613 const auto& expected = adapter_record_type<T>();
614 if (!shape.compatible_with(expected))
615 throw std::invalid_argument(
"wrong nominal record type");
616 auto encoded = from(source);
617 value::release_canonical(encoded, data);
619 concrete_adapter<T>::write(source, shape, data);
636 return detail::boundary_adapter<std::remove_cvref_t<T>>::type();
An immutable, shared semantic type with a canonical byte layout.
Definition value_type.hpp:65
static value_type array(value_type element)
Creates an array descriptor retaining the element's complete shape and capabilities.
static value_type leaf()
Returns T's descriptor as an opaque, trivially copyable leaf.
Definition value_type.hpp:122
static value_type tuple(std::span< const value_type > fields)
Creates a tuple descriptor with canonical source-order fields; empty fields describe unit.
Holds any type representable by CEL, including client-defined types.
Definition value.hpp:83
std::size_t size() const
Returns array length, fixed aggregate arity, or range endpoint count.
T as() const
Returns a concrete copy of the exact requested adapter, leaf, vector, or tuple type.
Definition value.hpp:243
static value array(value_type element, std::span< const value > elements)
Copies a sequence of elements with the specified complete shape and ownership operations.
void set(std::size_t index, const value &replacement)
Replaces one child with a copy of the exact expected shape and ownership operations.
value() noexcept
Creates the unit value () without allocating.
value field(std::string_view name) const
Copies a named record field under the class's copy semantics.
static value tuple(std::span< const value > fields)
Copies source-order fields into a canonical tuple; empty fields are source unit.
const value_type & type() const
Borrows the complete descriptor until this value is assigned or destroyed.
static value range(value_type type, std::span< const value > endpoints)
Copies endpoints in source order for the given range descriptor.
static value from(const T &source)
Copies a concrete leaf, nominal record adapter, or nested owning vector/tuple value.
Definition value.hpp:229
static value record(value_type type, std::span< const value > fields)
Copies source-order fields into a record of the specified nominal type.
value at(std::size_t index) const
Copies a source-order child under the class's aggregate and callable copy semantics.
void set_field(std::string_view name, const value &replacement)
Replaces a named record field with a copy of the exact expected shape.
value_type native_type()
Returns the runtime's complete descriptor for a trusted native value type.
Definition value.hpp:635
@ tuple
Stores ordered, canonically laid-out fields.
Definition value_type.hpp:37
@ record
Stores named fields and a nominal identity.
Definition value_type.hpp:38
Byte extents for stack reservation and type descriptors.
Definition callable.hpp:6