Icarus
Vehicle Simulation as a Transformable Computational Graph, built on Vulcan and Janus
Loading...
Searching...
No Matches
InputHandle.hpp
Go to the documentation of this file.
1#pragma once
2
10
12#include <icarus/core/Error.hpp>
13#include <string>
14#include <type_traits>
15
16namespace icarus {
17
18// Forward declarations
19template <typename Scalar> class Backplane;
20template <typename Scalar> class SignalRegistry;
21
35template <typename T> class InputHandle {
36 public:
37 InputHandle() : default_value_{} {}
38
48 [[nodiscard]] T get() const {
49 if (!source_) {
50 // Unwired: return the pokeable default value
51 return default_value_;
52 }
53 // Wired: read from source (with gain for scalars)
54 if constexpr (std::is_arithmetic_v<T>) {
55 return static_cast<T>(*source_ * gain_);
56 } else {
57 return *source_;
58 }
59 }
60
69 void set(const T &value) { default_value_ = value; }
70
74 [[nodiscard]] T operator*() const { return get(); }
75
81 [[nodiscard]] T *data_ptr() { return &default_value_; }
82
86 [[nodiscard]] bool is_wired() const { return source_ != nullptr; }
87
91 [[nodiscard]] const std::string &name() const { return name_; }
92
96 [[nodiscard]] const std::string &full_name() const { return full_name_; }
97
101 [[nodiscard]] const std::string &wired_to() const { return wired_to_; }
102
106 [[nodiscard]] const std::string &units() const { return units_; }
107
111 [[nodiscard]] const std::string &description() const { return description_; }
112
113 private:
114 // Backplane needs access to wire inputs
115 template <typename S> friend class Backplane;
116 template <typename S> friend class SignalRegistry;
117
118 // Internal setters for Backplane/Registry
119 void set_name(const std::string &name) { name_ = name; }
120 void set_full_name(const std::string &full_name) { full_name_ = full_name; }
121 void set_units(const std::string &units) { units_ = units; }
122 void set_description(const std::string &desc) { description_ = desc; }
123
124 void wire(const T *source, const std::string &source_name) {
125 source_ = source;
126 wired_to_ = source_name;
127 gain_ = 1.0; // Default gain
128 }
129
135 void wire_with_gain(const T *source, const std::string &source_name, double gain) {
136 source_ = source;
137 wired_to_ = source_name;
138 gain_ = gain;
139 }
140
141 std::string name_; // Local name (e.g., "throttle")
142 std::string full_name_; // Full name (e.g., "X15.Engine.throttle")
143 std::string wired_to_; // Source signal name
144 const T *source_ = nullptr; // Pointer to source value
145 double gain_ = 1.0; // Scale factor (Phase 4.0)
146 T default_value_{}; // Default/poke buffer (used when unwired)
147
148 // Metadata
149 std::string units_;
150 std::string description_;
151};
152
153// =============================================================================
154// Convenience Aliases
155// =============================================================================
156
160template <typename Scalar> using ScalarInput = InputHandle<Scalar>;
161
165template <typename Scalar> using Vec3Input = InputHandle<Vec3<Scalar>>;
166
170template <typename Scalar> using QuatInput = InputHandle<Vec4<Scalar>>;
171
172} // namespace icarus
Core type definitions, concepts, and configuration for Icarus.
Consolidated error handling for Icarus.
Component-facing facade for signal registration and resolution.
Definition Backplane.hpp:32
Handle to an input signal port.
Definition InputHandle.hpp:35
const std::string & description() const
Get description.
Definition InputHandle.hpp:111
T get() const
Get the current value (from wired source or default buffer).
Definition InputHandle.hpp:48
InputHandle()
Definition InputHandle.hpp:37
bool is_wired() const
Check if this input has been wired.
Definition InputHandle.hpp:86
const std::string & name() const
Get the port name (local name).
Definition InputHandle.hpp:91
T operator*() const
Dereference operator (same as get()).
Definition InputHandle.hpp:74
friend class SignalRegistry
Definition InputHandle.hpp:116
friend class Backplane
Definition InputHandle.hpp:115
T * data_ptr()
Get pointer to the default value buffer (for registry).
Definition InputHandle.hpp:81
const std::string & wired_to() const
Get the wired source signal name (empty if unwired).
Definition InputHandle.hpp:101
const std::string & full_name() const
Get the full qualified name (entity.component.signal).
Definition InputHandle.hpp:96
void set(const T &value)
Set the default value (for external injection when unwired).
Definition InputHandle.hpp:69
const std::string & units() const
Get units.
Definition InputHandle.hpp:106
Central registry for all simulation signals.
Definition Registry.hpp:37
Definition AggregationTypes.hpp:13
InputHandle< Vec3< Scalar > > Vec3Input
Vec3 input handle.
Definition InputHandle.hpp:165
InputHandle< Vec4< Scalar > > QuatInput
Vec4/Quaternion input handle.
Definition InputHandle.hpp:170
InputHandle< Scalar > ScalarInput
Scalar input handle.
Definition InputHandle.hpp:160