Implementation:Ggml org Llama cpp Jinja Value Header
| Knowledge Sources | |
|---|---|
| Domains | Template_Engine |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Declares the complete Jinja value type hierarchy, helper templates, and the JSON conversion interface that forms the type system backbone of the Jinja engine.
Description
This header defines value as a shared_ptr<value_t> universal value handle, with template helpers (is_val, mk_val, cast_val) for type checking and creation. It declares the value_t base class with virtual methods for type operations (comparison, hashing, member access, iteration, arithmetic) and concrete types including value_int_t, value_float_t, value_string_t, value_bool_t, value_array_t, value_tuple_t, value_object_t, value_none_t, value_undefined_t, value_func_t, and value_kwarg_t. It also includes a stats_t struct for capability detection and the global_from_json() template for JSON import with input marking.
Usage
Include this header when working with Jinja template values. All runtime values in the Jinja engine are instances of these types, and the shared_ptr semantics enable reference sharing between AST nodes and containers.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: common/jinja/value.h
- Lines: 1-754
Signature
namespace jinja {
using value = std::shared_ptr<value_t>;
template<typename T> bool is_val(const value & ptr);
template<typename T, typename... Args> std::shared_ptr<typename extract_pointee<T>::type> mk_val(Args&&... args);
template<typename T> const typename extract_pointee<T>::type * cast_val(const value & ptr);
struct value_t {
virtual std::string type() const;
virtual int64_t as_int() const;
virtual double as_float() const;
virtual string as_string() const;
virtual bool as_bool() const;
virtual const std::vector<value> & as_array() const;
virtual value invoke(const func_args &) const;
virtual bool is_none() const;
virtual bool is_undefined() const;
virtual hasher unique_hash() const noexcept = 0;
};
template<typename T_JSON>
void global_from_json(context & ctx, const T_JSON & json_obj, bool mark_input);
std::string value_to_json(const value & val, int indent = -1, ...);
}
Import
#pragma once
#include "string.h"
#include "utils.h"
#include <memory>
#include <functional>
#include <map>
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| ptr | value (shared_ptr<value_t>) | Yes | Value handle to check type, cast, or operate on |
| args | Args&&... | No | Variadic constructor arguments forwarded by mk_val |
| json_obj | T_JSON | Yes | JSON object for conversion to Jinja values |
| mark_input | bool | Yes | Whether to tag strings as user input for provenance |
Outputs
| Name | Type | Description |
|---|---|---|
| value | std::shared_ptr<value_t> | Universal value handle wrapping any Jinja type |
| bool | bool | Type check result from is_val |
| pointer | const T* | Raw pointer to concrete type from cast_val |
Usage Examples
#include "value.h"
using namespace jinja;
// Create a string value
value str = mk_val<value_string>("hello");
// Type check
if (is_val<value_string>(str)) {
auto * s = cast_val<value_string>(str);
std::string text = s->as_string().str();
}
// Create an array value
value arr = mk_val<value_array>(std::vector<value>{
mk_val<value_int>(1),
mk_val<value_int>(2),
mk_val<value_int>(3)
});