Implementation:AGenUI AGenUI IMeasurementManager
| Knowledge Sources | |
|---|---|
| Domains | UI_Layout, Cross_Platform |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
Component measurement registry and unified layout measurement entry point, bridging the Yoga layout engine to host-registered `IMeasurement` implementations per component type.
Description
The `IMeasurementManager` interface and its concrete implementation `MeasurementManagerImpl` maintain a type-string → `shared_ptr<IMeasurement>` registry. It is held by `AGenUIEngine` as an engine-level singleton via `unique_ptr<MeasurementManagerImpl>`.
Key methods:
- `registerMeasurement(type, impl)`: registers a measurement implementation for a component type string. Thread-safe via internal mutex. Duplicate registration triggers a warning log and replaces the previous entry (last-write-wins).
- `unregisterMeasurement(type)`: removes the entry. In-flight `measure()` calls are unaffected because the `shared_ptr` ref count keeps the impl alive until the call completes.
- `getMeasurement(type)`: looks up the `shared_ptr` by type, returns `nullptr` if not registered.
- `measure(type, paramJson, modes)`: the unified entry point. Takes a `shared_ptr` copy under lock (ref count +1), releases the lock, then calls `impl->measure()` outside the lock to avoid deadlock. Returns `MeasureResult{Sync, 0, 0, 0}` if not registered.
The implementation also contains `shouldUseMeasureFunc()` — a decision policy that determines whether Yoga should use a measure function for a node, evaluated in priority order (no impl, platform-locked size, children present, explicit px, aspect-ratio).
Usage
- Register custom components: implement `IMeasurement`, register via `engine->getMeasurementManager()->registerMeasurement("MyType", shared_ptr)`.
- Built-in components: platform layer registers measurements for Text, Image, etc. (via JNI on Android).
- Unregister: call before surface destruction to release the shared_ptr.
Code Reference
Source Location
- Repository: AGenUI
- Interface: `core/include/agenui_measurement.h:L77-115`
- Implementation: `core/src/surface/yoga_node/agenui_measurement_manager.cpp:L7-122`
- Header: `core/src/surface/yoga_node/agenui_measurement_manager.h`
Signature
// core/include/agenui_measurement.h
namespace agenui {
class IMeasurementManager {
public:
virtual ~IMeasurementManager() = default;
// Register a measurement implementation for a component type
virtual void registerMeasurement(const std::string& type,
std::shared_ptr<IMeasurement> impl) = 0;
// Unregister a measurement implementation
virtual void unregisterMeasurement(const std::string& type) = 0;
// Look up measurement implementation by type
virtual std::shared_ptr<IMeasurement> getMeasurement(const std::string& type) = 0;
// Unified measurement entry point (thread-safe)
// Returns {Sync, 0, 0, 0} if type not registered
virtual MeasureResult measure(const std::string& type,
const std::string& paramJson,
const MeasureModes& modes) = 0;
};
// Access via engine:
// engine->getMeasurementManager()
Import
#include "agenui_measurement.h"
#include "agenui_engine.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| type | `const std::string&` | Yes | Component type string (e.g. "Text", "Image", "MyCustom") |
| impl | `shared_ptr<IMeasurement>` | Yes (register) | Measurement implementation (shared ownership) |
| paramJson | `const std::string&` | Yes (measure) | Component attribute JSON |
| modes | `const MeasureModes&` | Yes (measure) | Width/height constraints from Yoga |
Outputs
| Name | Type | Description |
|---|---|---|
| measure returns | {calcType, width, height, countOfLines} | |
| getMeasurement returns | Implementation or nullptr if not registered |
Usage Examples
Registering a Custom Component Measurement
#include "agenui_measurement.h"
#include "agenui_engine.h"
class ChartMeasurement : public agenui::IMeasurement {
public:
agenui::MeasureResult measure(const std::string& paramJson,
const agenui::MeasureModes& modes) override {
// Parse component attributes
auto attrs = nlohmann::json::parse(paramJson);
float aspectRatio = attrs.value("aspectRatio", 1.5f);
// Calculate intrinsic size based on Yoga constraints
float width = modes.width.mode == 1 ? modes.width.maxValue // Exactly
: modes.width.maxValue > 0 ? modes.width.maxValue
: 300.0f;
float height = width / aspectRatio;
return {agenui::CalcType::Sync, width, height, 0};
}
};
// Register with the engine
auto* mm = engine->getMeasurementManager();
mm->registerMeasurement("Chart", std::make_shared<ChartMeasurement>());
// Now A2UI protocol components with "component": "Chart" use ChartMeasurement
Async Measurement (Image)
class ImageMeasurement : public agenui::IMeasurement {
public:
agenui::MeasureResult measure(const std::string& paramJson,
const agenui::MeasureModes& modes) override {
// Image size unknown until loaded; return {0,0} first
// Platform calls markDirty() after image loads, triggering recalculation
return {agenui::CalcType::Async, 0.0f, 0.0f, 0};
}
};