EN 中文
Back to Overview

LiteRtLmCallback

Callback function prototype. Defines how the engine pushes asynchronous generation results to the host application. Implements context retention using the User Pointer pattern.

01. Context Passthrough Principle

graph TD Caller[Caller: RunInference] -->|1. Pass MyObject Pointer| DLL[Wrapper DLL Async Task] DLL -->|2. Push Result| Callback[User Callback] DLL -->|3. Return MyObject as is| Callback Callback -->|4. Cast Type| Object[Operate MyObject Instance]

Since the C-API cannot recognize C++ class member functions, a static function or Lambda like LiteRtLmCallback, combined with user_ptr, must be used to retrieve the C++ object context.

02. Function Prototype Definition

typedef void (*LiteRtLmCallback)(LiteRtLm_Result result, void* user_ptr);

Parameter: result

Contains the incremental text fragments and status info for this turn. See details in LiteRtLm_Result.

Parameter: user_ptr

Original pointer passed by the caller in RunInference. Usually points to a C++ class instance (e.g., this).

03. Typical C++ Class Binding Implementation

// Static callback adapter
static void OnStaticResult(LiteRtLm_Result Result, void* UserPtr) {
    // Cast to the corresponding C++ class
    UMyAiSubsystem* Subsystem = static_cast<UMyAiSubsystem*>(UserPtr);
    if (Subsystem) {
        Subsystem->HandleAsyncText(Result);
    }
}

// Call site
LiteRtLm_RunInference(Conv, Params, OnStaticResult, this); // Pass this pointer