runtime 1.1.4
Checked CEL programs, owned values, and reusable execution stacks
Loading...
Searching...
No Matches
value.hpp
1// SPDX-License-Identifier: BSL-1.0
2#ifndef CEL_RUNTIME_VALUE_HPP
3#define CEL_RUNTIME_VALUE_HPP
4
5#include <cel/runtime/record_adapter.hpp>
6#include <cel/runtime/value_type.hpp>
7
8#include <array>
9#include <bit>
10#include <cassert>
11#include <cstring>
12#include <memory>
13#include <span>
14#include <stdexcept>
15#include <tuple>
16#include <type_traits>
17#include <utility>
18#include <vector>
19
20namespace cel {
23class value;
24
26namespace detail {
27struct owned_payload;
28struct value_access;
33struct array_view {
34 const value_type& element;
35 const std::byte* data;
36 std::size_t size;
37 std::size_t stride;
38};
39template <class T>
40struct concrete_adapter;
41template <class T>
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>;
46};
47template <class T>
48struct boundary_adapter;
49} // namespace detail
50
82// [knowledge] Never byte-relocate public wrappers; only private canonical carriers transfer owners.
83class value {
84 std::unique_ptr<detail::owned_payload> _payload;
85 friend struct detail::value_access;
86 template <class T>
87 friend struct detail::concrete_adapter;
88 template <class T>
89 friend struct detail::boundary_adapter;
90
94 explicit value(std::unique_ptr<detail::owned_payload> payload);
100 static value from_leaf(value_type type, const void* source);
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;
151 static value from_concrete(value_type type,
152 const void* source,
153 void (*write)(const void*, const value_type&, std::byte*));
163 static void write_array(const value_type& element,
164 std::byte* destination,
165 std::size_t count,
166 const void* source,
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;
174
175public:
179 value() noexcept;
187 ~value();
195 value(const value& other);
199 value(value&& other) noexcept;
208 value& operator=(const value& other);
214 value& operator=(value&& other) noexcept;
220 const value_type& type() const;
228 template <class T>
229 static value from(const T& source) {
230 return detail::boundary_adapter<std::remove_cv_t<T>>::from(source);
231 }
232
242 template <class T>
243 T as() const {
244 return detail::boundary_adapter<std::remove_cv_t<T>>::as(*this);
245 }
246
256 static value array(value_type element, std::span<const value> elements);
266 static value tuple(std::span<const value> fields);
278 static value record(value_type type, std::span<const value> fields);
288 static value range(value_type type, std::span<const value> endpoints);
294 std::size_t size() const;
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);
336};
337
338namespace detail {
339
343template <class T>
344struct concrete_adapter {
346 static value_type type() { return value_type::leaf<T>(); }
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);
359 }
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);
371 }
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));
380 }
381};
382
386template <class T, class Allocator>
387struct concrete_adapter<std::vector<T, Allocator>> {
392 static value_type type() {
393 static const auto descriptor = value_type::array(boundary_adapter<T>::type());
394 return descriptor;
395 }
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);
405 });
406 }
414 static void write(const std::vector<T, Allocator>& source,
415 const value_type& shape,
416 std::byte* data) {
417 value::write_array(shape.element(), data, source.size(), &source,
418 [](const void* object, std::size_t index, const value_type& element,
419 std::byte* target) {
420 const auto& input =
421 *static_cast<const std::vector<T, Allocator>*>(object);
422 boundary_adapter<T>::write(input[index], element, target);
423 });
424 }
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));
434 }
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);
447 // Extract each element through its trusted concrete adapter.
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));
451 return result;
452 }
453};
454
456template <class... T>
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()...};
465 return value_type::tuple(fields);
466 }();
467 return descriptor;
468 }
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);
478 });
479 }
487 template <std::size_t... I>
488 static void write_fields(const std::tuple<T...>& source,
489 const value_type& shape,
490 std::byte* data,
491 std::index_sequence<I...>) {
492 std::size_t completed = 0;
493 try {
494 ((boundary_adapter<T>::write(std::get<I>(source), shape.fields()[I],
495 data ? data + shape.offsets()[I] : nullptr),
496 ++completed),
497 ...);
498 } catch (...) {
499 value::destroy_fields(shape, data, completed);
500 throw;
501 }
502 }
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...>{});
511 }
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)...};
525 }
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));
535 }
544 static std::tuple<T...> read(const value_type& shape, const std::byte* data) {
545 return extract(shape, data, std::index_sequence_for<T...>{});
546 }
547};
548
549template <class T>
550const value_type& adapter_record_type() {
551 static const auto descriptor = [] {
552 auto declared = record_adapter<T>::type();
553 if (declared.kind() != type_kind::record)
554 throw std::invalid_argument("record adapter type must be a nominal record");
555 return declared;
556 }();
557 return descriptor;
558}
559
560template <class T>
561struct boundary_adapter {
563 static value_type type() {
564 if constexpr (nominal_record_adapter<T>) {
565 return adapter_record_type<T>();
566 } else {
567 return concrete_adapter<T>::type();
568 }
569 }
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");
578 return encoded;
579 } else {
580 return concrete_adapter<T>::from(source);
581 }
582 }
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);
590 } else {
591 return concrete_adapter<T>::as(source);
592 }
593 }
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);
604 } else {
605 return concrete_adapter<T>::read(shape, data);
606 }
607 }
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);
618 } else {
619 concrete_adapter<T>::write(source, shape, data);
620 }
621 }
622};
623
624} // namespace detail
634template <class T>
636 return detail::boundary_adapter<std::remove_cvref_t<T>>::type();
637}
638
640} // namespace cel
641#endif
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