parser 1.1.3
UTF-8 CEL expression compilation and checked library extensions
Loading...
Searching...
No Matches
cached_decoded.cpp

Compiles decoded expressions once and repeatedly evaluates their cached programs on a reserved stack while preserving caller-owned prefix storage. See Compile and reuse expressions for the walkthrough.

The example's helper definitions are:

// SPDX-License-Identifier: BSL-1.0
#ifndef CEL_EXAMPLE_CACHED_DECODED_HPP
#define CEL_EXAMPLE_CACHED_DECODED_HPP
namespace client {
inline std::vector<cel::prepared_segment<double>> compile_decoded(
std::span<const std::string_view> expressions) {
cel::parser compiler(std::move(lookup));
std::vector<cel::prepared_segment<double>> result;
result.reserve(expressions.size());
// Prepare each expression before the compiler and decoded strings cease to exist.
for (const auto source : expressions)
result.push_back(compiler.parse_str(source).prepare<double>());
return result;
}
inline void reserve(std::span<const cel::prepared_segment<double>> programs,
cel::raw_stack& stack) {
// Each reservation retains existing byte alignment and capacity.
for (const auto& program : programs) {
const auto needed = program.requirements().for_prefix(stack.cursor(), stack.depth());
stack.reserve(needed.bytes.size, needed.bytes.alignment);
stack.reserve_depth(needed.slots);
}
}
} // namespace client
#endif
Includes the expression compiler and checked library registration interfaces. Prefer <cel/parser....
Owns exact registrations, nominal schemas, and LIFO scope callbacks. Owns copied names,...
Definition lookup.hpp:81
Owns lookup state and compiles CEL expressions into reusable checked programs. Each successful call r...
Definition compiler.hpp:37
compiled_segment parse_str(std::string_view source)
Compiles one complete decoded UTF-8 CEL source through an owned source copy. Selects grammar alternat...
// SPDX-License-Identifier: BSL-1.0
#include "cached_decoded.hpp"
#include <iostream>
int main() try {
const auto programs = client::compile_decoded(
std::array<std::string_view, 3>{"2.0f64+3.0f64*4.0f64", "3.0*3.0", "(|| 7.0f64)()"});
cel::raw_stack stack;
stack.push(true);
client::reserve(programs, stack);
double sum = 0;
for (int repeat = 0; repeat != 1000; ++repeat)
for (const auto& program : programs)
sum += program.call(stack);
std::cout << "cached sum=" << sum << '\n';
return sum == 30000 && stack.pop<bool>() ? 0 : 1;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}