LotPropertyAccessor.vala 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Invercargill.DataStructures;
  2. namespace Invercargill.Expressions {
  3. /**
  4. * Property accessor for types implementing the Lot<T> interface.
  5. *
  6. * Provides access to Lot properties:
  7. * - `length`: The number of items in the Lot (uint)
  8. *
  9. * Example usage in expressions:
  10. * {{{
  11. * items.length // Returns the number of items in the Lot
  12. * }}}
  13. */
  14. public class LotPropertyAccessor : Object, PropertyAccessor {
  15. /**
  16. * The Lot value being accessed.
  17. */
  18. private Lot<Object>? _lot;
  19. /**
  20. * Creates a new LotPropertyAccessor.
  21. *
  22. * @param lot The Lot value to access properties on
  23. */
  24. public LotPropertyAccessor(Lot<Object>? lot) {
  25. _lot = lot;
  26. }
  27. /**
  28. * {@inheritDoc}
  29. */
  30. public bool has_property(string property) {
  31. return property == "length";
  32. }
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public Enumerable<string> get_property_names() {
  37. return new Wrappers.Array<string>(new string[]{"length"});
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public Element read_property(string property) throws ExpressionError {
  43. switch (property) {
  44. case "length":
  45. return new NativeElement<uint>(_lot != null ? _lot.length : 0);
  46. default:
  47. throw new ExpressionError.NON_EXISTANT_PROPERTY(
  48. @"Unknown Lot property: $property"
  49. );
  50. }
  51. }
  52. /**
  53. * {@inheritDoc}
  54. */
  55. public Type get_property_type(string property) throws ExpressionError {
  56. switch (property) {
  57. case "length":
  58. return typeof(uint);
  59. default:
  60. throw new ExpressionError.NON_EXISTANT_PROPERTY(
  61. @"Unknown Lot property: $property"
  62. );
  63. }
  64. }
  65. }
  66. /**
  67. * Factory for creating LotPropertyAccessor instances.
  68. *
  69. * This factory is registered with the TypeAccessorRegistry to provide
  70. * property access for Lot<T> values.
  71. */
  72. public class LotPropertyAccessorFactory : Object, PropertyAccessorFactory {
  73. /**
  74. * {@inheritDoc}
  75. */
  76. public PropertyAccessor? create(Element element) {
  77. // Check if the element's value implements Lot
  78. // We need to check for Lot<Object> since we can't check for Lot<T> generically
  79. Lot<Object>? lot;
  80. if (element.try_get_as(out lot)) {
  81. return new LotPropertyAccessor(lot);
  82. }
  83. return null;
  84. }
  85. }
  86. }