LogicEnum<T extends Enum>.withMapping constructor

LogicEnum<T extends Enum>.withMapping(
  1. Map<T, dynamic> mapping, {
  2. int? width,
  3. String? name,
  4. Naming? naming,
  5. String? definitionName,
  6. bool reserveDefinitionName = false,
})

Creates a signal using the explicit hardware encodings in mapping.

The width is inferred from the member count and encoding values unless width is provided. If reserveDefinitionName is true, generated type and member names cannot be uniquified around collisions.

Implementation

LogicEnum.withMapping(
  Map<T, dynamic> mapping, {
  int? width,
  super.name,
  super.naming,
  String? definitionName,
  super.reserveDefinitionName,
}) : super(
          width: _computeWidth(requestedWidth: width, mapping: mapping),
          definitionName: definitionName ?? T.toString()) {
  this.mapping =
      Map.unmodifiable(_computeMapping(mapping: mapping, width: this.width));

  _wire._constrainValue((value) {
    if (value.isFloating) {
      return LogicValue.filled(this.width, LogicValue.z);
    }
    if (!value.isValid) {
      return LogicValue.filled(this.width, LogicValue.x);
    }
    if (!this.mapping.containsValue(value)) {
      return LogicValue.filled(this.width, LogicValue.x);
    }
    return value;
  });
}