|
| 1 | +// |
| 2 | +// Base Time Series Input/Output Shim Layer |
| 3 | +// |
| 4 | +// This file provides BaseTimeSeriesInput and BaseTimeSeriesOutput classes that sit |
| 5 | +// between the abstract TimeSeriesInput/TimeSeriesOutput interfaces and the concrete |
| 6 | +// implementations. The Base classes hold all the state and concrete behavior. |
| 7 | +// |
| 8 | +// The goal is to eventually make TimeSeriesInput/TimeSeriesOutput pure virtual interfaces, |
| 9 | +// with all implementation details moved to these Base classes. |
| 10 | +// |
| 11 | + |
| 12 | +#ifndef BASE_TIME_SERIES_H |
| 13 | +#define BASE_TIME_SERIES_H |
| 14 | + |
| 15 | +#include <hgraph/types/time_series_type.h> |
| 16 | +#include <unordered_set> |
| 17 | + |
| 18 | +namespace hgraph { |
| 19 | + struct OutputBuilder; |
| 20 | + |
| 21 | + /** |
| 22 | + * @brief Base class for TimeSeriesOutput implementations |
| 23 | + * |
| 24 | + * This class holds all the state and concrete behavior currently in TimeSeriesOutput. |
| 25 | + * All concrete output types should extend this class rather than TimeSeriesOutput directly. |
| 26 | + * Eventually, TimeSeriesOutput will become a pure virtual interface. |
| 27 | + */ |
| 28 | + struct HGRAPH_EXPORT BaseTimeSeriesOutput : TimeSeriesOutput { |
| 29 | + using ptr = nb::ref<BaseTimeSeriesOutput>; |
| 30 | + |
| 31 | + explicit BaseTimeSeriesOutput(const node_ptr &parent) : _parent_ts_or_node{parent} {} |
| 32 | + explicit BaseTimeSeriesOutput(const TimeSeriesType::ptr &parent) : _parent_ts_or_node{parent} {} |
| 33 | + |
| 34 | + // Implement TimeSeriesType pure virtuals |
| 35 | + [[nodiscard]] node_ptr owning_node() override; |
| 36 | + [[nodiscard]] node_ptr owning_node() const override; |
| 37 | + [[nodiscard]] graph_ptr owning_graph() override; |
| 38 | + [[nodiscard]] graph_ptr owning_graph() const override; |
| 39 | + [[nodiscard]] bool is_reference() const override; |
| 40 | + [[nodiscard]] bool has_reference() const override; |
| 41 | + void reset_parent_or_node() override; |
| 42 | + [[nodiscard]] bool has_parent_or_node() const override; |
| 43 | + [[nodiscard]] bool has_owning_node() const override; |
| 44 | + void re_parent(const node_ptr &parent) override; |
| 45 | + void re_parent(const TimeSeriesType::ptr &parent) override; |
| 46 | + |
| 47 | + // Inherited interface implementations |
| 48 | + [[nodiscard]] bool modified() const override; |
| 49 | + [[nodiscard]] engine_time_t last_modified_time() const override; |
| 50 | + void mark_invalid() override; |
| 51 | + void mark_modified() override; |
| 52 | + void mark_child_modified(TimeSeriesOutput &child, engine_time_t modified_time) override; |
| 53 | + [[nodiscard]] bool valid() const override; |
| 54 | + [[nodiscard]] bool all_valid() const override; |
| 55 | + [[nodiscard]] TimeSeriesOutput::ptr parent_output() const override; |
| 56 | + [[nodiscard]] TimeSeriesOutput::ptr parent_output() override; |
| 57 | + [[nodiscard]] bool has_parent_output() const override; |
| 58 | + |
| 59 | + void subscribe(Notifiable *node) override; |
| 60 | + void un_subscribe(Notifiable *node) override; |
| 61 | + void builder_release_cleanup() override; |
| 62 | + |
| 63 | + bool can_apply_result(nb::object value) override; |
| 64 | + void clear() override; |
| 65 | + void invalidate() override; |
| 66 | + void mark_modified(engine_time_t modified_time) override; |
| 67 | + |
| 68 | + static void register_with_nanobind(nb::module_ &m); |
| 69 | + |
| 70 | + protected: |
| 71 | + // State and helpers moved from TimeSeriesType |
| 72 | + TimeSeriesType::ptr &_parent_time_series() const; |
| 73 | + TimeSeriesType::ptr &_parent_time_series(); |
| 74 | + bool _has_parent_time_series() const; |
| 75 | + void _set_parent_time_series(TimeSeriesType *ts); |
| 76 | + node_ptr _owning_node() const; |
| 77 | + |
| 78 | + void _notify(engine_time_t modified_time); |
| 79 | + void _reset_last_modified_time(); |
| 80 | + |
| 81 | + private: |
| 82 | + friend OutputBuilder; |
| 83 | + using TsOrNode = std::variant<TimeSeriesType::ptr, node_ptr>; |
| 84 | + std::optional<TsOrNode> _parent_ts_or_node{}; |
| 85 | + std::unordered_set<Notifiable *> _subscribers{}; |
| 86 | + engine_time_t _last_modified_time{MIN_DT}; |
| 87 | + }; |
| 88 | + |
| 89 | + /** |
| 90 | + * @brief Base class for TimeSeriesInput implementations |
| 91 | + * |
| 92 | + * This class holds all the state and concrete behavior currently in TimeSeriesInput. |
| 93 | + * All concrete input types should extend this class rather than TimeSeriesInput directly. |
| 94 | + * Eventually, TimeSeriesInput will become a pure virtual interface. |
| 95 | + */ |
| 96 | + struct HGRAPH_EXPORT BaseTimeSeriesInput : TimeSeriesInput { |
| 97 | + using ptr = nb::ref<BaseTimeSeriesInput>; |
| 98 | + |
| 99 | + explicit BaseTimeSeriesInput(const node_ptr &parent) : _parent_ts_or_node{parent} {} |
| 100 | + explicit BaseTimeSeriesInput(const TimeSeriesType::ptr &parent) : _parent_ts_or_node{parent} {} |
| 101 | + |
| 102 | + // Implement TimeSeriesType pure virtuals |
| 103 | + [[nodiscard]] node_ptr owning_node() override; |
| 104 | + [[nodiscard]] node_ptr owning_node() const override; |
| 105 | + [[nodiscard]] graph_ptr owning_graph() override; |
| 106 | + [[nodiscard]] graph_ptr owning_graph() const override; |
| 107 | + [[nodiscard]] bool is_reference() const override; |
| 108 | + [[nodiscard]] bool has_reference() const override; |
| 109 | + void reset_parent_or_node() override; |
| 110 | + [[nodiscard]] bool has_parent_or_node() const override; |
| 111 | + [[nodiscard]] bool has_owning_node() const override; |
| 112 | + void re_parent(const node_ptr &parent) override; |
| 113 | + void re_parent(const TimeSeriesType::ptr &parent) override; |
| 114 | + |
| 115 | + // Inherited interface implementations |
| 116 | + [[nodiscard]] TimeSeriesInput::ptr parent_input() const override; |
| 117 | + [[nodiscard]] bool has_parent_input() const override; |
| 118 | + [[nodiscard]] bool bound() const override; |
| 119 | + [[nodiscard]] bool has_peer() const override; |
| 120 | + [[nodiscard]] time_series_output_ptr output() const override; |
| 121 | + |
| 122 | + bool bind_output(time_series_output_ptr output_) override; |
| 123 | + void un_bind_output(bool unbind_refs) override; |
| 124 | + |
| 125 | + [[nodiscard]] bool active() const override; |
| 126 | + void make_active() override; |
| 127 | + void make_passive() override; |
| 128 | + [[nodiscard]] bool has_output() const override; |
| 129 | + |
| 130 | + void builder_release_cleanup() override; |
| 131 | + |
| 132 | + [[nodiscard]] nb::object py_value() const override; |
| 133 | + [[nodiscard]] nb::object py_delta_value() const override; |
| 134 | + [[nodiscard]] bool modified() const override; |
| 135 | + [[nodiscard]] bool valid() const override; |
| 136 | + [[nodiscard]] bool all_valid() const override; |
| 137 | + [[nodiscard]] engine_time_t last_modified_time() const override; |
| 138 | + [[nodiscard]] time_series_reference_output_ptr reference_output() const override; |
| 139 | + |
| 140 | + [[nodiscard]] const TimeSeriesInput *get_input(size_t index) const override; |
| 141 | + [[nodiscard]] TimeSeriesInput *get_input(size_t index) override; |
| 142 | + |
| 143 | + static void register_with_nanobind(nb::module_ &m); |
| 144 | + |
| 145 | + protected: |
| 146 | + // State and helpers moved from TimeSeriesType |
| 147 | + TimeSeriesType::ptr &_parent_time_series() const; |
| 148 | + TimeSeriesType::ptr &_parent_time_series(); |
| 149 | + bool _has_parent_time_series() const; |
| 150 | + void _set_parent_time_series(TimeSeriesType *ts); |
| 151 | + node_ptr _owning_node() const; |
| 152 | + |
| 153 | + // Protected virtual methods for derived classes to override |
| 154 | + virtual bool do_bind_output(time_series_output_ptr &output_); |
| 155 | + virtual void do_un_bind_output(bool unbind_refs); |
| 156 | + |
| 157 | + void notify(engine_time_t modified_time) override; |
| 158 | + virtual void notify_parent(TimeSeriesInput *child, engine_time_t modified_time); |
| 159 | + |
| 160 | + void set_sample_time(engine_time_t sample_time); |
| 161 | + [[nodiscard]] engine_time_t sample_time() const; |
| 162 | + [[nodiscard]] bool sampled() const; |
| 163 | + |
| 164 | + void reset_output(); |
| 165 | + void set_output(time_series_output_ptr output); |
| 166 | + void set_active(bool active); |
| 167 | + |
| 168 | + private: |
| 169 | + using TsOrNode = std::variant<TimeSeriesType::ptr, node_ptr>; |
| 170 | + std::optional<TsOrNode> _parent_ts_or_node{}; |
| 171 | + time_series_output_ptr _output; |
| 172 | + time_series_reference_output_ptr _reference_output; |
| 173 | + bool _active{false}; |
| 174 | + engine_time_t _sample_time{MIN_DT}; |
| 175 | + engine_time_t _notify_time{MIN_DT}; |
| 176 | + }; |
| 177 | + |
| 178 | +} // namespace hgraph |
| 179 | + |
| 180 | +#endif // BASE_TIME_SERIES_H |
| 181 | + |
0 commit comments