32 if (std::get<int>(value) < 0) {
33 throw std::invalid_argument(std::format(
"{} must be non-negative", name));
41 const int size = std::get<int>(value);
43 throw std::invalid_argument(std::format(
"{} must be at least 1", name));
44 if (size > std::numeric_limits<unsigned short>::max())
45 throw std::invalid_argument(std::format(
"{} is too large", name));
56 const std::string type = std::string(
typeName());
83 if (allowedValues.empty()) {
84 throw std::invalid_argument(std::format(
85 "String-list property '{}' on component '{}' must define allowed values", key,
89 const bool defaultAllowed = std::ranges::contains(allowedValues, defaultValue);
90 if (!defaultAllowed) {
91 throw std::invalid_argument(std::format(
92 "Default value '{}' is not allowed for property '{}' on component '{}'",
96 const std::string propertyKey = key;
98 defineProperty(std::move(key), std::move(defaultValue), std::move(callback));
99 const auto& propertyValue =
properties.at(propertyKey);
107 if (currentValue.index() != newValue.index()) {
108 throw std::invalid_argument(std::format(
109 "Type mismatch when setting property '{}' on component '{}'", key,
typeName()));
116 const auto* stringValue = std::get_if<std::string>(&newValue);
117 if (!stringValue || !std::ranges::contains(options->second, *stringValue)) {
118 throw std::invalid_argument(std::format(
119 "Invalid value for property '{}' on component '{}'", key,
typeName()));
123 const unsigned int newSize)
125 const auto currentSize =
inputs[index].size();
126 if (currentSize != newSize)
127 throw std::runtime_error(std::format(
128 "Input size mismatch, currently is {} but wants to be {}", currentSize, newSize));
134 if (busCollection.size() > index && busCollection[index] == newBus)
136 if (busCollection.size() <= index)
137 busCollection.resize(index + 1);
138 busCollection[index] = std::move(newBus);
149 throw std::invalid_argument(
150 std::format(
"Property '{}' does not exist on component '{}'", key,
typeName()));
162 it->second = std::move(finalValue);
173std::optional<std::reference_wrapper<const StringPropertyOptions>>
178 return std::cref(it->second);
187 throw std::invalid_argument(
188 std::format(
"Property '{}' does not exist on component '{}'", key,
typeName()));
194 property->second = std::move(finalValue);
199 if (this->
inputs.size() > index && this->inputs[index] == bus)
210 if (this->
outputs.size() > index && this->outputs[index] == bus)
218 if (this->
inputs == newInputs)
220 this->
inputs.resize(newInputs.size());
221 for (
auto [index, bus] : newInputs | SILICON::views::enumerate) {
229 if (this->
outputs == newOutputs)
231 this->
outputs.resize(newOutputs.size());
232 for (
auto [index, bus] : newOutputs | SILICON::views::enumerate) {
240 return std::ranges::contains(
allBuses(), b);
246 std::array{std::span<Bus>(
inputs), std::span<Bus>(
outputs)} | std::views::join)
247 bus =
Bus(std::vector<Wire_ptr>(bus.size()));
std::function< void(Component *)> IOObserver
Callback type for I/O change notifications.
bool isConnectedTo(const Bus &b) const
Checks if this component is connected to a bus.
void removeIOListener(uint64_t id)
Removes a previously added I/O listener.
std::optional< PropertyValue > getProperty(std::string_view key) const
Gets a property value.
virtual std::string_view typeName() const =0
Gets the component type name.
auto allBuses() const
Borrows all buses, inputs first, without copying or deduplicating.
StringPropertyOptionsMap stringPropertyOptions
Allowed values for constrained string properties.
void setOutputs(const std::vector< Bus > &newOutputs)
Replaces all output buses.
void setInput(unsigned int index, const Bus &bus, bool checkSize=false)
Sets an input bus at a specific index.
std::vector< Bus > outputs
Output buses produced by this component.
void setInputs(const std::vector< Bus > &newInputs)
Replaces all input buses.
void setProperty(std::string_view key, const PropertyValue &value)
Sets a property value.
std::vector< Bus > inputs
Input buses connected to this component.
void defineStringListProperty(std::string key, std::string defaultValue, StringPropertyOptions allowedValues, PropertyCallback callback=nullptr)
Defines a string property constrained to a fixed set of values.
PropertyMap properties
Map of configurable properties for this component.
void setOutput(unsigned int index, const Bus &bus)
Sets an output bus at a specific index.
void replaceBus(std::vector< Bus > &busCollection, unsigned int index, Bus newBus)
Replaces a bus in the input or output collection.
void setPropertyCallback(std::string_view key, PropertyCallback callback)
Sets a callback for property validation/transformation.
virtual void clearWires()
Clears all wire connections.
PropertyCallbackMap propertyCallbacks
Map of callbacks for property validation/transformation.
uint64_t addIOListener(IOObserver cb)
Adds a listener for I/O changes.
virtual ComponentMetadata metadata() const
Gets catalog metadata for this component type.
uint64_t nextIoListenerId
Counter for generating unique I/O listener IDs.
std::optional< std::reference_wrapper< const StringPropertyOptions > > getStringPropertyOptions(std::string_view key) const
Gets allowed values for a constrained string property.
void defineProperty(std::string key, T &&defaultValue, PropertyCallback callback=nullptr)
Defines a property with a default value and optional callback.
void notifyIOListeners()
Notifies all registered I/O listeners of a change.
std::unordered_map< uint64_t, IOObserver > ioListeners
Map of I/O change listeners indexed by ID.
void handleInputSizeChange(const unsigned int index, const unsigned int newSize)
Handles input size changes. By default input size shouldn't change automatically.
void validatePropertyValue(std::string_view key, const PropertyValue ¤tValue, const PropertyValue &newValue) const
Validates a property value against the declared type and metadata.
std::function< PropertyValue(const PropertyValue &)> PropertyCallback
Callback function for property validation/transformation.
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
PropertyValue requireValidSize(const std::string_view name, const PropertyValue &value)
Validates that an integer property value is a valid bus size.
PropertyValue requireNonNegative(const std::string_view name, const PropertyValue &value)
Validates that an integer property value is non-negative.
std::vector< std::string > StringPropertyOptions
Allowed values for a constrained string property.