From cd5c329babc69b8ea30dfe09c3b7617079f36f2a Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Fri, 10 Nov 2017 17:15:27 -0500 Subject: [PATCH] IEnumerable.Cast does not run implicit conversion operators --- .../GlobalExtensions/GenericCollectionExtensions.cs | 1 + src/client/spark/mspark/Shell/MarketModule.cs | 2 +- .../mspark/Shell/TimeAndSales/TimeAndSalesWindow.cs | 2 +- src/client/spark/mspark/Shell/TradesModule.cs | 2 +- .../spark/view/Controls/PriceEntryControl.xaml.cs | 10 +++++----- .../spark/view/Converters/TickDisplayConverter.cs | 6 +++--- .../api/service_compiler/templates/c#/immutable.pb.cs | 2 +- 7 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs b/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs index ff184043b..a9b506ddb 100644 --- a/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs @@ -128,6 +128,7 @@ namespace System.Collections.ObjectModel public static class ObservableCollectionExtensions { public static int RemoveAll(this ObservableCollection collection, Func match) { + if (collection == null) return 0; // In terms of allocations we could do this more efficiently by copying items from the end of the // collection in (see https://referencesource.microsoft.com/#q=RemoveAll) but that would allow // transient duplication in the list which could be observed from the outside diff --git a/src/client/spark/mspark/Shell/MarketModule.cs b/src/client/spark/mspark/Shell/MarketModule.cs index cd75859ba..3b88bd3f4 100644 --- a/src/client/spark/mspark/Shell/MarketModule.cs +++ b/src/client/spark/mspark/Shell/MarketModule.cs @@ -68,7 +68,7 @@ namespace mbts.spark if (_securities != null && _securities.TryGetValue((uint)header.version, out SecurityMD md)) return create_ladder_window(md); else break; case MarketViewVM.SAVE_NAME: - if (try_deserialize(load_stream, out MarketViewVM.StartupData data)) return create_market_view(data.securities.Select(deserialize).Cast().ToList(), data.sorting_criteria); + if (try_deserialize(load_stream, out MarketViewVM.StartupData data)) return create_market_view(data.securities.Select(x => (SecurityImmutable)deserialize(x)).ToList(), data.sorting_criteria); else break; } } diff --git a/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesWindow.cs b/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesWindow.cs index 103d59f7f..aba5d04e3 100644 --- a/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesWindow.cs +++ b/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesWindow.cs @@ -13,7 +13,7 @@ namespace mbts.spark internal class TimeAndSalesWindow : IShellWindow { public TimeAndSalesWindow(IShellModule owner, IBus bus, IReadOnlyCollection securities) : this (owner, bus, securities, null) { } - public TimeAndSalesWindow(IShellModule owner, IBus bus, TimeAndSalesVM.StartupData startup_data) : this(owner, bus, startup_data.securities.Select(deserialize).Cast().ToList(), startup_data.sorting_criteria) { } + public TimeAndSalesWindow(IShellModule owner, IBus bus, TimeAndSalesVM.StartupData startup_data) : this(owner, bus, startup_data.securities.Select(x => (SecurityImmutable)deserialize(x)).ToList(), startup_data.sorting_criteria) { } private TimeAndSalesWindow(IShellModule owner, IBus bus, IReadOnlyCollection securities, IReadOnlyCollection sorting_criteria) { _bus = bus; diff --git a/src/client/spark/mspark/Shell/TradesModule.cs b/src/client/spark/mspark/Shell/TradesModule.cs index 41aa7152d..54b03548e 100644 --- a/src/client/spark/mspark/Shell/TradesModule.cs +++ b/src/client/spark/mspark/Shell/TradesModule.cs @@ -68,7 +68,7 @@ namespace mbts.spark return ShellWindow.create(this, "Order Book", view, vm.get_state); } - private IShellWindow create_fills_window(FillsVM.StartupData startup_data) { return create_fills_window(startup_data.securities?.Select(deserialize).Cast().ToList(), startup_data.sorting_criteria); } + private IShellWindow create_fills_window(FillsVM.StartupData startup_data) { return create_fills_window(startup_data.securities?.Select(x => (SecurityImmutable)deserialize(x)).ToList(), startup_data.sorting_criteria); } private IShellWindow create_fills_window(IReadOnlyCollection securities) { return create_fills_window(securities, null); } private IShellWindow create_fills_window(IReadOnlyCollection securities, IReadOnlyCollection sorting_criteria) { var vm = new FillsVM(_bus, new OrdersModel(_bus), securities, sorting_criteria); diff --git a/src/client/spark/view/Controls/PriceEntryControl.xaml.cs b/src/client/spark/view/Controls/PriceEntryControl.xaml.cs index 29a5ca5f9..cf7ae26c4 100644 --- a/src/client/spark/view/Controls/PriceEntryControl.xaml.cs +++ b/src/client/spark/view/Controls/PriceEntryControl.xaml.cs @@ -19,7 +19,7 @@ namespace mbts.ui.spark #region Value public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(PriceEntryControl),new FrameworkPropertyMetadata(double.NaN, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, CoerceValue)); - private static object CoerceValue(DependencyObject d, object baseValue) { return TickUtils.get_regional_ticks((ContractSpecification)d.GetValue(ContractSpecProperty)).round((double)baseValue); } + private static object CoerceValue(DependencyObject d, object baseValue) { return TickUtils.get_regional_ticks((ContractSpecificationImmutable)d.GetValue(ContractSpecProperty)).round((double)baseValue); } public double Value { get => (double)GetValue(ValueProperty); set => SetValue(ValueProperty, value); } #endregion @@ -36,9 +36,9 @@ namespace mbts.ui.spark #endregion #region ContractSpec - public static readonly DependencyProperty ContractSpecProperty = DependencyProperty.Register("ContractSpec", typeof(ContractSpecification), typeof(PriceEntryControl), new PropertyMetadata(TickUtils.DefaultSpec, OnContractSpecChanged)); + public static readonly DependencyProperty ContractSpecProperty = DependencyProperty.Register("ContractSpec", typeof(ContractSpecificationImmutable), typeof(PriceEntryControl), new PropertyMetadata(TickUtils.DefaultSpec, OnContractSpecChanged)); private static void OnContractSpecChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((PriceEntryControl)d)._update_text_binding(); } - public ContractSpecificationImmutable ContractSpec { get => GetValue(ContractSpecProperty) as ContractSpecification; set => SetValue(ContractSpecProperty, value); } + public ContractSpecificationImmutable? ContractSpec { get => GetValue(ContractSpecProperty) as ContractSpecificationImmutable?; set => SetValue(ContractSpecProperty, value); } #endregion public PriceEntryControl() { @@ -48,8 +48,8 @@ namespace mbts.ui.spark private TextBox _text; - private void _tick_up() { SetCurrentValue(ValueProperty, ContractSpec.tickup(Value)); } - private void _tick_down() { SetCurrentValue(ValueProperty, ContractSpec.tickdown(Value)); } + private void _tick_up() { SetCurrentValue(ValueProperty, ContractSpec.HasValue ? ContractSpec.Value.tickup(Value) : Value); } + private void _tick_down() { SetCurrentValue(ValueProperty, ContractSpec.HasValue ? ContractSpec.Value.tickdown(Value) : Value); } private void _update_text_binding() { if (_text != null) { diff --git a/src/client/spark/view/Converters/TickDisplayConverter.cs b/src/client/spark/view/Converters/TickDisplayConverter.cs index 1a3deb4b1..309499636 100644 --- a/src/client/spark/view/Converters/TickDisplayConverter.cs +++ b/src/client/spark/view/Converters/TickDisplayConverter.cs @@ -25,16 +25,16 @@ namespace mbts.ui.spark.converters internal class TickDisplayTwoWayConverter : IValueConverter { public bool HideNaN { get; set; } - public ContractSpecificationImmutable ContractSpec { get; set; } = new ContractSpecification { CashPerPoint = 1.0, LowTick = 0.01, HighTick = 0.01, PriceBreak = double.MaxValue, PriceMultiplier = 1.0 }; + public ContractSpecificationImmutable? ContractSpec { get; set; } = new ContractSpecification { CashPerPoint = 1.0, LowTick = 0.01, HighTick = 0.01, PriceBreak = double.MaxValue, PriceMultiplier = 1.0 }; public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var price = (double)value; if (HideNaN && double.IsNaN(price)) return string.Empty; - return ContractSpec.format(price); + return ContractSpec.Value.format(price); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { - return ContractSpec.parse(value as string); + return ContractSpec.Value.parse(value as string); } } } \ No newline at end of file diff --git a/src/lib/api/service_compiler/templates/c#/immutable.pb.cs b/src/lib/api/service_compiler/templates/c#/immutable.pb.cs index 0fdc36732..1541c11ed 100644 --- a/src/lib/api/service_compiler/templates/c#/immutable.pb.cs +++ b/src/lib/api/service_compiler/templates/c#/immutable.pb.cs @@ -65,7 +65,7 @@ namespace {{package}} { {{else}} {{#if type.islist}} // Can remove the cast if immutable==cstype - public IEnumerable<{{type.immutable}}> {{Name}} => _impl.{{Name}}.Cast<{{type.immutable}}>(); + public IEnumerable<{{type.immutable}}> {{Name}} => _impl.{{Name}}.Select(t => ({{type.immutable}})t); {{else}} public {{type.immutable}} {{Name}} => ({{type.immutable}})_impl.{{Name}}; {{/if}}