nk 1.0.0
C++ Numerical Computing Runtime
Loading...
Searching...
No Matches
graphics_handle.hpp
1// include/nk/graphics/graphics_handle.hpp
2//
3// Graphics-handle object payload — the Value-side half of the handle
4// registry (bugs/closed/graphics/plot-family-no-return-value.md).
5//
6// Layering: the registry (id → HandleRecord) lives in FigureManager
7// (figure layer, no Engine). This header carries the NativePayload that
8// a handle Value holds: just the registry id. It depends only on the
9// value layer, so three consumers can share it without layering knots:
10// • the graphics plot bodies (core-free, e.g. close(figureHandle)),
11// • the bundle install hub (graphics_library.cpp — accessor layer),
12// • the builtin layer (general_reg.cpp — unified delete dispatch).
13#pragma once
14
15#include <nk/value/object.hpp>
16#include <nk/value/value.hpp>
17
18#include <memory>
19#include <string>
20
21namespace nk {
22
23// NativePayload carrying the FigureManager handle-registry id.
25{
26 int handleId = 0;
27 std::shared_ptr<NativePayload> clone() const override
28 {
29 auto p = std::make_shared<GraphicsHandlePayload>();
30 p->handleId = handleId;
31 return p;
32 }
33};
34
35// Registry id of a graphics-handle Value, or -1 when `v` is not one.
36// Payload-based (not class-name-based): class(h) reports the MATLAB
37// class (matlab.graphics.chart.primitive.Line, …), so the class name is
38// NOT a discriminator — the payload is.
39inline int graphicsHandleIdOf(const Value &v)
40{
41 if (!v.isObject())
42 return -1;
43 auto *st = v.objectStateConst();
44 if (!st || !st->native)
45 return -1;
46 auto *p = dynamic_cast<const GraphicsHandlePayload *>(st->native.get());
47 return p ? p->handleId : -1;
48}
49
50// Handle object Value wrapping registry id `id` with MATLAB class name
51// `className` (that string is what class()/isa() report).
52inline Value makeGraphicsHandleValue(const std::string &className, int id,
53 std::pmr::memory_resource *mr)
54{
55 auto st = std::make_shared<ObjectState>(mr);
56 auto payload = std::make_shared<GraphicsHandlePayload>();
57 payload->handleId = id;
58 st->native = payload;
59 return Value::object(className, std::move(st), /*isHandle=*/true, mr);
60}
61
62} // namespace nk
Core MATLAB-compatible multidimensional array and scalar value type.
Definition value.hpp:61