nk 1.0.0
C++ Numerical Computing Runtime
Loading...
Searching...
No Matches
graphics_context.hpp
Go to the documentation of this file.
1
3#pragma once
4// nk/graphics/graphics_context.hpp
5//
6// The core-free execution context handed to every graphics plotting body.
7//
8// Graphics is a plotting *service* (figure / plot / surf / imshow / …): each
9// function's job is a side effect — build a wire-JSON dataset and push it onto
10// the active figure's axes via the FigureManager, then emit a "modified"
11// notification. None of that needs the Engine; it needs (1) the figure session
12// state, (2) a scratch allocator, and (3) two escape hatches that DO reach back
13// into the runtime: forwarding to another registered plot builtin by name
14// (surfc → surf, fcontour → contour, geoplot → plot, …) and evaluating a user
15// function-handle (fplot/fsurf sampling f(x)).
16//
17// `GraphicsContext` bundles exactly those four things. The two runtime escape
18// hatches are std::function callbacks bound by the bundle-side adapter (see
19// library.cpp) to the live Engine — so the plotting bodies (plots.cpp) stay
20// core-free and the only Engine-coupled glue is the one generic adapter.
21//
22// This mirrors the FsContext decoupling: stateful/runtime surfaces are pulled
23// behind a context object so the L2 compute layer never includes <nk/core>.
24
25#include <nk/figure/figure_manager.hpp> // FigureManager, DatasetInfo, AxesState
26#include <nk/value/value.hpp> // Value, ValueType
27#include <nk/value/span.hpp> // Span
28
29#include <cstddef>
30#include <functional>
31#include <memory_resource>
32#include <string_view>
33#include <vector>
34
35namespace nk {
36
39
40
41// Context passed to every graphics plotting body. Core-free: it knows the
42// figure session (fm), a scratch arena (mr), and how to re-enter the runtime
43// for the two operations that genuinely require it (callBuiltin / callHandle).
45 FigureManager &fm; // active figure / axes session state
46 std::pmr::memory_resource *mr; // scratch allocator for Value building
47
48 // Forward to another *registered* plot builtin by name (e.g. surfc → surf).
49 // Resolves the builtin at call time, invokes it with (args, nargout, outs),
50 // and returns true; returns false if no such builtin is registered (callers
51 // treat that as a no-op and return an empty value). Bound by the adapter to
52 // Engine::findExternal + invoke.
53 std::function<bool(std::string_view name,
55 std::size_t nargout,
56 Span<Value> outs)>
57 callBuiltin;
58
59 // Evaluate a user function-handle Value (the @(x)… callbacks fed to
60 // fplot / fcontour / fsurf / fplot3). Bound by the adapter to
61 // Engine::callFunctionHandle; works on both the TreeWalker and VM backends.
62 std::function<Value(const Value &handle, Span<const Value> args)> callHandle;
63};
64
65// A graphics plotting body. Same shape as ExternalFunc but takes the core-free
66// GraphicsContext instead of the Engine-coupled CallContext.
67using GraphicsFn = std::function<void(Span<const Value> args,
68 std::size_t nargout,
69 Span<Value> outs,
70 GraphicsContext &gc)>;
71
72// One registration record. `sub` is the graphics sub-namespace (layout / line /
73// bar / surface / polar / contour / image); `core` requests the extra bare-name
74// registration into core (figure / close / hold etc., namespace_design.md §7).
75struct PlotEntry {
76 const char *sub;
77 const char *name;
78 bool core;
79 GraphicsFn fn;
80};
81
82// Builds the full table of graphics plotting bodies (core-free; defined in
83// plots.cpp). The bundle-side installer (library.cpp GraphicsLibrary::install)
84// wraps each entry's GraphicsFn in a CallContext→GraphicsContext adapter and
85// registers it under graphics.<sub>.<name> (+ the bare core name when set;
86// non-core names resolve bare through the bare-name resolver).
87void buildPlotTable(std::vector<PlotEntry> &table);
88
89
91} // namespace nk
Core MATLAB-compatible multidimensional array and scalar value type.
Definition value.hpp:61