This tutorial extends a checked runtime program with a C++ operation and a client value type. Build the runtime and link cel::runtime as described in Build and link the runtime with CPM.
Add a typed operation
An operation consumes the newest operands in source order and returns one value. The template arguments name the result first:
builder.
op<double,
double>([](
double x) {
return x * 2.0; });
auto program = std::move(builder).finish().prepare<double, double>();
double result = program.call(stack, 3.0);
A move-only builder of checked, source-ordered operations.
Definition dyn_segment.hpp:249
void push_argument(std::size_t index, value_type type)
Emits a read that clones a borrowed positional input when the program executes.
void op(F &&callback)
Emits an arbitrary-arity const callback with exact declared operands and result.
Definition dyn_segment.hpp:563
An exclusive owner of reusable canonical operand storage.
Definition raw_stack.hpp:47
value_type native_type()
Returns the runtime's complete descriptor for a trusted native value type.
Definition value.hpp:635
Provides values, program builders, and reusable execution for C++20 runtime clients.
The builder checks operand types before publishing the program. The callback must accept the declared argument types and return the declared result type; implicit conversions do not substitute for matching types. A callback runs during evaluation, not during construction.
Store these builder operations in your own installer functions to reuse them across programs. The runtime does not assign language names to operations. Use the parser's registry when your application needs named CEL functions.
Introduce a client type
A trivially copyable C++ type can cross a checked boundary as an opaque leaf:
struct distance {
double metres;
};
builder.
op<double, distance>([](distance x) {
return x.metres; });
auto program = std::move(builder).finish().prepare<double, distance>();
double result = program.call(stack, distance{12.0});
Type identity is the C++ type, not its size or display name. A leaf owns its stored bytes, not resources reached through pointers inside those bytes. Use the provided std::vector<T> and std::tuple<T...> adapters for owning arrays and tuples instead of wrapping nontrivial containers as opaque leaves.
For a nontrivial nominal record, specialize cel::record_adapter<T> with a record descriptor and field-wise encode/decode callbacks:
struct point {
double x;
double y;
};
template <>
"sample.point",
return descriptor;
}
static cel::value to_value(const point& source) {
}
static point from_value(const cel::value& source) {
throw std::invalid_argument("wrong point type");
return {.x = source.
field(
"x").
as<
double>(),
.y = source.
field(
"y").
as<
double>()};
}
};
An immutable, shared semantic type with a canonical byte layout.
Definition value_type.hpp:65
static value_type leaf()
Returns T's descriptor as an opaque, trivially copyable leaf.
Definition value_type.hpp:122
static value_type record(std::string_view identity, std::span< const record_field > fields)
Creates named fields with a nonempty nominal identity.
bool same_shape(const value_type &other) const
Returns true exactly when identities, shapes, source ordering, and canonical layouts match.
T as() const
Returns a concrete copy of the exact requested adapter, leaf, vector, or tuple type.
Definition value.hpp:243
value field(std::string_view name) const
Copies a named record field under the class's copy semantics.
const value_type & type() const
Borrows the complete descriptor until this value is assigned or destroyed.
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.
Adapts one client nominal record type to and from canonical CEL record values.
Definition record_adapter.hpp:38
A source-order name/type pair validated by value_type::record.
Definition value_type.hpp:272
The runtime requires type() to be a nominal record. value::from<T>() verifies that to_value() returns exactly that schema. value::as<T>() checks complete nominal identity and schema before from_value().
Choose the result boundary
Use prepare<R, Args...>() when the application knows the signature. Use compiled_segment::execute for an owning cel::value result when the shape is determined at runtime. Extract a concrete result with as<T>(), or inspect an aggregate with at() and field(). Aggregate copies own independent mutable payloads; callable values share frozen executable code.
Callback exceptions propagate and evaluation retires its temporary suffix. Caller-owned stack prefixes survive, but callback side effects are not rolled back. Captures stored by value remain with the program; referenced objects must outlive every call that uses them.
Use these snippets in the main.cpp from the CPM build tutorial. Return zero when the result matches the expected value, then rebuild and run your client:
cmake --build build
ctest --test-dir build --output-on-failure
Confirm that the typed-operation example returns 6.0 and the client-type example returns 12.0. See Choose execution and ownership boundaries for reservation, callable results, and structural emission.