samcrow / rust-xplm

Rust interfaces to the X-Plane plugin SDK
Apache License 2.0
39 stars 15 forks source link

OwnedData custom read/write callbacks? #14

Open JDeeth opened 8 months ago

JDeeth commented 8 months ago

A feature I used a lot in my C++ days was to register new datarefs that basically acted as a filter on some other sources of data:

class FilteredBus {
public:
    FilteredBus() {
        this._source_volts = XPLMFindDataRef("sim/cockpit2/electrical/something_volts");
        this._bus_switch = XPLMFindDataRef("sim/somewhere/something_switch");
        this._bus_volts = XPLMRegisterDataAccessor("com/jdeeth/something/bus_volts, xplm_float, false, nullptr, nullptr,
            // read float accessor
            [this]{XPLMGetDatai(this._bus_switch) > 0 ? XPLMGetDataf(this._source_volts) : 0.f;},
            nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
    }
private:
    XPLMDataRef _source_volts;
    XPLMDataRef _bus_switch;
    XPLMDataRef _bus_volts;
}

i.e. my bus_volts dataref doesn't store a value of its own, it's a float dataref that when you read it, it gives you something_volts if the switch is on otherwise zero.

Is there a way to do this kind of thing currently which I'm missing?

Was thinking maybe OwnedData should be split into a trait and a standard implementation. The trait would specify int_read, float_write etc functions which default to just returning None. The standard implementation would create a Box<T> and register read and optionally write functions for it. Whereas I'd just be impl OwnedData for FilteredBus and providing my own float_read function and not storing anything.

samcrow commented 8 months ago

That makes sense. This library doesn't have a way to do that right now. I don't have time to implement it myself right now, but the idea of a trait that OwnedData implements sounds good.