From 9d31e3e84f1803f3e56842acf0735a13884c77da Mon Sep 17 00:00:00 2001 From: kosyloa Date: Wed, 17 Apr 2024 16:57:19 +0200 Subject: [PATCH] errorchecker refactoring --- DXFeedFramework.xcodeproj/project.pbxproj | 4 ++++ .../Native/Endpoint/NativeBuilder.swift | 2 +- .../Native/Endpoint/NativeEndpoint.swift | 8 ++++---- .../Native/ErrorHandling/ErrorCheck.swift | 13 ++++-------- DXFeedFramework/Native/Feed/NativeFeed.swift | 16 +++++++-------- DXFeedFramework/Native/Graal/Isolate.swift | 2 +- .../Native/Ipf/Live/NativeExecutor.swift | 6 +++--- .../NativeInstrumentProfileCollector.swift | 6 +++--- .../NativeInstrumentProfileConnection.swift | 2 +- .../Ipf/Live/NativeProfileIterator.swift | 2 +- .../Ipf/NativeInstrumentProfileReader.swift | 16 +++++++-------- .../Native/Promise/NativePromise.swift | 4 ++-- .../Native/QDS/NativeSymbolParser.swift | 2 +- .../Native/Schedule/NativeSchedule.swift | 20 +++++++++---------- .../Subscription/NativeSubscription.swift | 4 ++-- .../Native/Utils/NativeTimeFormat.swift | 8 ++++---- .../Native/Utils/NativeTimePeriod.swift | 4 ++-- .../Native/Utils/NativeTimeZone.swift | 2 +- DXFeedFramework/Utils/Optional+Ext.swift | 17 ++++++++++++++++ 19 files changed, 77 insertions(+), 61 deletions(-) create mode 100644 DXFeedFramework/Utils/Optional+Ext.swift diff --git a/DXFeedFramework.xcodeproj/project.pbxproj b/DXFeedFramework.xcodeproj/project.pbxproj index 8a8a2b243..214b79e5e 100644 --- a/DXFeedFramework.xcodeproj/project.pbxproj +++ b/DXFeedFramework.xcodeproj/project.pbxproj @@ -110,6 +110,7 @@ 642DC9442AAA29EA00974F5C /* IpfCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 642DC9402AAA290300974F5C /* IpfCell.swift */; }; 6433B12D2BCE7ADD004EFED7 /* DXFeed+LastEvents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6433B12C2BCE7ADD004EFED7 /* DXFeed+LastEvents.swift */; }; 6433B1322BCFC01F004EFED7 /* DXLastEventsSubscribedTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6433B1312BCFC01F004EFED7 /* DXLastEventsSubscribedTest.swift */; }; + 643A329B2BD0137000F6F790 /* Optional+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 643A329A2BD0137000F6F790 /* Optional+Ext.swift */; }; 64437A8F2A9DEE6F005929B2 /* InstrumentProfile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64437A8E2A9DEE6F005929B2 /* InstrumentProfile.swift */; }; 64437A922A9DF1DE005929B2 /* NativeInstrumentProfileReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64437A912A9DF1DE005929B2 /* NativeInstrumentProfileReader.swift */; }; 6447A5DB2A8E559000739CCF /* ILastingEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6447A5DA2A8E559000739CCF /* ILastingEvent.swift */; }; @@ -668,6 +669,7 @@ 6433B1302BCE87D4004EFED7 /* RequestProfile.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = RequestProfile.playground; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; 6433B1312BCFC01F004EFED7 /* DXLastEventsSubscribedTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DXLastEventsSubscribedTest.swift; sourceTree = ""; }; 6435EE3C2B1F1E9200E8496C /* PrintQuoteEvents.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = PrintQuoteEvents.playground; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; + 643A329A2BD0137000F6F790 /* Optional+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Optional+Ext.swift"; sourceTree = ""; }; 64437A8E2A9DEE6F005929B2 /* InstrumentProfile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstrumentProfile.swift; sourceTree = ""; }; 64437A912A9DF1DE005929B2 /* NativeInstrumentProfileReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeInstrumentProfileReader.swift; sourceTree = ""; }; 644551C92B973A0D0069E3A2 /* FetchDailyCandles.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = FetchDailyCandles.playground; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; @@ -1264,6 +1266,7 @@ 6464074E2A9F62D4006FF769 /* Date+Ext.swift */, 64A42F3A2B07A7A3001C3ACC /* SymbolParser.swift */, 640885C92B1F7EE700E6CF88 /* QdsUtils.swift */, + 643A329A2BD0137000F6F790 /* Optional+Ext.swift */, ); path = Utils; sourceTree = ""; @@ -2396,6 +2399,7 @@ 64BA92652A306E0200BE26A0 /* Trade.swift in Sources */, 6486B9752AD0493F00D8D5FA /* TheoPrice+Ext.swift in Sources */, 6464074B2A9DFAF7006FF769 /* InstrumentProfile+Ext.swift in Sources */, + 643A329B2BD0137000F6F790 /* Optional+Ext.swift in Sources */, 64C771F22A94A224009868C2 /* Character+Ext.swift in Sources */, 8088D77129C3A25D00F240CB /* SystemProperty.swift in Sources */, 640C3FD42A6179AB00555161 /* CandlePeriod.swift in Sources */, diff --git a/DXFeedFramework/Native/Endpoint/NativeBuilder.swift b/DXFeedFramework/Native/Endpoint/NativeBuilder.swift index 4ae938b1c..efecc536d 100644 --- a/DXFeedFramework/Native/Endpoint/NativeBuilder.swift +++ b/DXFeedFramework/Native/Endpoint/NativeBuilder.swift @@ -55,7 +55,7 @@ class NativeBuilder { func build() throws -> NativeEndpoint { let thread = currentThread() - let value = try ErrorCheck.nativeCall(thread, dxfg_DXEndpoint_Builder_build(thread, builder)) + let value = try ErrorCheck.nativeCall(thread, dxfg_DXEndpoint_Builder_build(thread, builder)).toValue() return NativeEndpoint(value) } } diff --git a/DXFeedFramework/Native/Endpoint/NativeEndpoint.swift b/DXFeedFramework/Native/Endpoint/NativeEndpoint.swift index 0a44645a1..2b75720fd 100644 --- a/DXFeedFramework/Native/Endpoint/NativeEndpoint.swift +++ b/DXFeedFramework/Native/Endpoint/NativeEndpoint.swift @@ -42,7 +42,7 @@ class NativeEndpoint { private lazy var feed: NativeFeed? = { let thread = currentThread() do { - let nativeFeed = try ErrorCheck.nativeCall(thread, dxfg_DXEndpoint_getFeed(thread, self.endpoint)) + let nativeFeed = try ErrorCheck.nativeCall(thread, dxfg_DXEndpoint_getFeed(thread, self.endpoint)).toValue() return NativeFeed(feed: nativeFeed) } catch { return nil @@ -52,7 +52,7 @@ class NativeEndpoint { private lazy var publisher: NativePublisher? = { let thread = currentThread() do { - let nativeFeed = try ErrorCheck.nativeCall(thread, dxfg_DXEndpoint_getPublisher(thread, self.endpoint)) + let nativeFeed = try ErrorCheck.nativeCall(thread, dxfg_DXEndpoint_getPublisher(thread, self.endpoint)).toValue() return NativePublisher(publisher: nativeFeed) } catch { return nil @@ -96,7 +96,7 @@ class NativeEndpoint { func getEventTypes() throws -> [EventCode] { let thread = currentThread() let list = try ErrorCheck.nativeCall(thread, dxfg_DXEndpoint_getEventTypes( - thread, endpoint)) + thread, endpoint)).toValue() defer { _ = try? ErrorCheck.nativeCall(thread, dxfg_CList_EventClazz_release(thread, list)) @@ -121,7 +121,7 @@ class NativeEndpoint { let listener = try ErrorCheck.nativeCall(thread, dxfg_PropertyChangeListener_new(thread, NativeEndpoint.listenerCallback, - voidPtr)) + voidPtr)).toValue() self.listener = listener try ErrorCheck.nativeCall(thread, dxfg_Object_finalize(thread, &(listener.pointee.handler), diff --git a/DXFeedFramework/Native/ErrorHandling/ErrorCheck.swift b/DXFeedFramework/Native/ErrorHandling/ErrorCheck.swift index 93be1856e..16f7f0cda 100644 --- a/DXFeedFramework/Native/ErrorHandling/ErrorCheck.swift +++ b/DXFeedFramework/Native/ErrorHandling/ErrorCheck.swift @@ -44,16 +44,11 @@ class ErrorCheck { } } - static func nativeCall(_ thread: OpaquePointer!, _ result: T?) throws -> T { - if let result = result { - return result - } else { - if let exception = fetchException(thread) { - throw exception - } else { - throw GraalException.nullException - } + static func nativeCall(_ thread: OpaquePointer!, _ result: T) throws -> T { + if let exception = fetchException(thread) { + throw exception } + return result } static func graalCall(_ result: Int32) throws { diff --git a/DXFeedFramework/Native/Feed/NativeFeed.swift b/DXFeedFramework/Native/Feed/NativeFeed.swift index 3e177c326..f132bcebf 100644 --- a/DXFeedFramework/Native/Feed/NativeFeed.swift +++ b/DXFeedFramework/Native/Feed/NativeFeed.swift @@ -84,7 +84,7 @@ class NativeFeed { thread, type.eventSymbol, type.type.nativeCode()) - ) + ).toValue() defer { _ = try? ErrorCheck.nativeCall(thread, dxfg_EventType_release( @@ -156,7 +156,7 @@ class NativeFeed { dxfg_DXFeed_getLastEventPromise(thread, feed, type.type.nativeCode(), - converted)) + converted)).toValue() return NativePromise(promise: &native.pointee.handler) } @@ -175,7 +175,7 @@ class NativeFeed { dxfg_DXFeed_getLastEventsPromises(thread, feed, type.type.nativeCode(), - listPointer)) + listPointer)).toValue() var result = [NativePromise]() for index in 0.. NativeExecutor { let thread = currentThread() let executor = try ErrorCheck.nativeCall(thread, - dxfg_ExecutorBaseOnConcurrentLinkedQueue_new(thread)) + dxfg_ExecutorBaseOnConcurrentLinkedQueue_new(thread)).toValue() return NativeExecutor(executor: executor) } @@ -35,7 +35,7 @@ class NativeExecutor { let executor = try ErrorCheck.nativeCall(thread, dxfg_Executors_newScheduledThreadPool(thread, numberOfThreads, - nameOfthread.toCStringRef())) + nameOfthread.toCStringRef())).toValue() return NativeExecutor(executor: executor) } @@ -44,7 +44,7 @@ class NativeExecutor { let executor = try ErrorCheck.nativeCall(thread, dxfg_Executors_newFixedThreadPool(thread, numberOfThreads, - nameOfthread.toCStringRef())) + nameOfthread.toCStringRef())).toValue() return NativeExecutor(executor: executor) } } diff --git a/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileCollector.swift b/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileCollector.swift index 53dfe29a7..e6876f944 100644 --- a/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileCollector.swift +++ b/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileCollector.swift @@ -100,7 +100,7 @@ public class NativeInstrumentProfileCollector { func view() throws -> NativeProfileIterator { let thread = currentThread() - let native = try ErrorCheck.nativeCall(thread, dxfg_InstrumentProfileCollector_view(thread, collector)) + let native = try ErrorCheck.nativeCall(thread, dxfg_InstrumentProfileCollector_view(thread, collector)).toValue() return NativeProfileIterator(native, isDeallocated: true) } @@ -114,7 +114,7 @@ public class NativeInstrumentProfileCollector { func getExecutor() throws -> NativeExecutor { let thread = currentThread() - let native = try ErrorCheck.nativeCall(thread, dxfg_InstrumentProfileCollector_getExecutor(thread, collector)) + let native = try ErrorCheck.nativeCall(thread, dxfg_InstrumentProfileCollector_getExecutor(thread, collector)).toValue() return NativeExecutor(executor: native) } @@ -132,7 +132,7 @@ public class NativeInstrumentProfileCollector { let nativeListener = try ErrorCheck.nativeCall(thread, dxfg_InstrumentProfileUpdateListener_new(thread, callback, - voidPtr)) + voidPtr)).toValue() try ErrorCheck.nativeCall(thread, dxfg_Object_finalize(thread, &(nativeListener.pointee.handler), NativeInstrumentProfileCollector.finalizeCallback, diff --git a/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnection.swift b/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnection.swift index f6843ad06..cc15e0f35 100644 --- a/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnection.swift +++ b/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnection.swift @@ -153,7 +153,7 @@ class NativeInstrumentProfileConnection { dxfg_IpfPropertyChangeListener_new( thread, NativeInstrumentProfileConnection.listenerCallback, - voidPtr)) + voidPtr)).toValue() try ErrorCheck.nativeCall(thread, dxfg_Object_finalize(thread, &(listener.pointee.handler), diff --git a/DXFeedFramework/Native/Ipf/Live/NativeProfileIterator.swift b/DXFeedFramework/Native/Ipf/Live/NativeProfileIterator.swift index 261d97ee0..0cd1aaa68 100644 --- a/DXFeedFramework/Native/Ipf/Live/NativeProfileIterator.swift +++ b/DXFeedFramework/Native/Ipf/Live/NativeProfileIterator.swift @@ -34,7 +34,7 @@ class NativeProfileIterator { func next() throws -> InstrumentProfile { let thread = currentThread() - let result = try ErrorCheck.nativeCall(thread, dxfg_Iterable_InstrumentProfile_next(thread, iterator)) + let result = try ErrorCheck.nativeCall(thread, dxfg_Iterable_InstrumentProfile_next(thread, iterator)).toValue() let profile = mapper.fromNative(native: result) _ = try ErrorCheck.nativeCall(thread, dxfg_InstrumentProfile_release(thread, result)) diff --git a/DXFeedFramework/Native/Ipf/NativeInstrumentProfileReader.swift b/DXFeedFramework/Native/Ipf/NativeInstrumentProfileReader.swift index 4dfe2b7ee..8542337e1 100644 --- a/DXFeedFramework/Native/Ipf/NativeInstrumentProfileReader.swift +++ b/DXFeedFramework/Native/Ipf/NativeInstrumentProfileReader.swift @@ -44,7 +44,7 @@ class NativeInstrumentProfileReader { let result = try ErrorCheck.nativeCall(thread, dxfg_InstrumentProfileReader_readFromFile(thread, reader, - address.toCStringRef())) + address.toCStringRef())).toValue() let instruments = convertFromNativeList(result) _ = try ErrorCheck.nativeCall(thread, dxfg_CList_InstrumentProfile_release(thread, result)) return instruments @@ -57,7 +57,7 @@ class NativeInstrumentProfileReader { reader, address.toCStringRef(), user.toCStringRef(), - password.toCStringRef())) + password.toCStringRef())).toValue() let instruments = convertFromNativeList(result) _ = try ErrorCheck.nativeCall(thread, dxfg_CList_InstrumentProfile_release(thread, result)) return instruments @@ -96,14 +96,14 @@ class NativeInstrumentProfileReader { let inputStream = try ErrorCheck.nativeCall(thread, dxfg_ByteArrayInputStream_new(thread, pointer.baseAddress, - Int32(data.count))) + Int32(data.count))).toValue() return inputStream }) let result = try ErrorCheck.nativeCall(thread, dxfg_InstrumentProfileReader_read2(thread, reader, inputStream, - address.toCStringRef())) + address.toCStringRef())).toValue() let instruments = convertFromNativeList(result) _ = try ErrorCheck.nativeCall(thread, dxfg_JavaObjectHandler_release(thread, &(inputStream.pointee.handler))) @@ -117,7 +117,7 @@ class NativeInstrumentProfileReader { let inputStream = try ErrorCheck.nativeCall(thread, dxfg_ByteArrayInputStream_new(thread, pointer.baseAddress, - Int32(data.count))) + Int32(data.count))).toValue() return inputStream }) @@ -125,7 +125,7 @@ class NativeInstrumentProfileReader { let result = try ErrorCheck.nativeCall(thread, dxfg_InstrumentProfileReader_readCompressed(thread, reader, - inputStream)) + inputStream)).toValue() let instruments = convertFromNativeList(result) _ = try ErrorCheck.nativeCall(thread, dxfg_JavaObjectHandler_release(thread, &(inputStream.pointee.handler))) @@ -140,14 +140,14 @@ class NativeInstrumentProfileReader { let inputStream = try ErrorCheck.nativeCall(thread, dxfg_ByteArrayInputStream_new(thread, pointer.baseAddress, - Int32(data.count))) + Int32(data.count))).toValue() return inputStream }) let result = try ErrorCheck.nativeCall(thread, dxfg_InstrumentProfileReader_read(thread, reader, - inputStream)) + inputStream)).toValue() let instruments = convertFromNativeList(result) _ = try ErrorCheck.nativeCall(thread, dxfg_JavaObjectHandler_release(thread, &(inputStream.pointee.handler))) diff --git a/DXFeedFramework/Native/Promise/NativePromise.swift b/DXFeedFramework/Native/Promise/NativePromise.swift index 7faf8d039..fd4b94535 100644 --- a/DXFeedFramework/Native/Promise/NativePromise.swift +++ b/DXFeedFramework/Native/Promise/NativePromise.swift @@ -72,7 +72,7 @@ class NativePromise { let res = try promise?.withMemoryRebound(to: dxfg_promise_events_t.self, capacity: 1, { promiseEvents in let listPointer = try ErrorCheck.nativeCall(thread, dxfg_Promise_List_EventType_getResult(thread, - promiseEvents)) + promiseEvents)).toValue() defer { _ = try? ErrorCheck.nativeCall(thread, dxfg_CList_EventType_release(thread, listPointer)) } @@ -97,7 +97,7 @@ class NativePromise { } let thread = currentThread() let res = try promise?.withMemoryRebound(to: dxfg_promise_event_t.self, capacity: 1, { promiseEvent in - let result = try ErrorCheck.nativeCall(thread, dxfg_Promise_EventType_getResult(thread, promiseEvent)) + let result = try ErrorCheck.nativeCall(thread, dxfg_Promise_EventType_getResult(thread, promiseEvent)).toValue() let marketEvent = try EventMapper().fromNative(native: result) defer { _ = try? ErrorCheck.nativeCall(thread, dxfg_EventType_release(thread, result)) diff --git a/DXFeedFramework/Native/QDS/NativeSymbolParser.swift b/DXFeedFramework/Native/QDS/NativeSymbolParser.swift index 9fd8237c1..59b40b9ae 100644 --- a/DXFeedFramework/Native/QDS/NativeSymbolParser.swift +++ b/DXFeedFramework/Native/QDS/NativeSymbolParser.swift @@ -10,7 +10,7 @@ import Foundation class NativeSymbolParser { func parse(_ symbols: String) throws -> [String] { let thread = currentThread() - let symbols = try ErrorCheck.nativeCall(thread, dxfg_Tools_parseSymbols(thread, symbols.toCStringRef())) + let symbols = try ErrorCheck.nativeCall(thread, dxfg_Tools_parseSymbols(thread, symbols.toCStringRef())).toValue() var result = [String]() for index in 0.. ScheduleDay { let thread = currentThread() - let day = try ErrorCheck.nativeCall(thread, dxfg_Schedule_getDayByTime(thread, schedule, time)) + let day = try ErrorCheck.nativeCall(thread, dxfg_Schedule_getDayByTime(thread, schedule, time)).toValue() return try createDay(thread, day) } public func getDayById(dayId: Int32) throws -> ScheduleDay { let thread = currentThread() - let day = try ErrorCheck.nativeCall(thread, dxfg_Schedule_getDayById(thread, schedule, dayId)) + let day = try ErrorCheck.nativeCall(thread, dxfg_Schedule_getDayById(thread, schedule, dayId)).toValue() return try createDay(thread, day) } public func getDayByYearMonthDay(yearMonthDay: Int32) throws -> ScheduleDay { let thread = currentThread() - let day = try ErrorCheck.nativeCall(thread, dxfg_Schedule_getDayByYearMonthDay(thread, schedule, yearMonthDay)) + let day = try ErrorCheck.nativeCall(thread, dxfg_Schedule_getDayByYearMonthDay(thread, schedule, yearMonthDay)).toValue() return try createDay(thread, day) } @@ -124,7 +124,7 @@ class NativeSchedule { scheduleDay.trading = try ErrorCheck.nativeCall(thread, dxfg_Day_isTrading(thread, day)) scheduleDay.startTime = try ErrorCheck.nativeCall(thread, dxfg_Day_getStartTime(thread, day)) scheduleDay.endTime = try ErrorCheck.nativeCall(thread, dxfg_Day_getEndTime(thread, day)) - let sessions = try ErrorCheck.nativeCall(thread, dxfg_Day_getSessions(thread, day)) + let sessions = try ErrorCheck.nativeCall(thread, dxfg_Day_getSessions(thread, day)).toValue() let count = sessions.pointee.size for index in 0.. ScheduleDay? { let qdValue = filter.toQDValue() let thread = currentThread() - let filter = try ErrorCheck.nativeCall(thread, dxfg_DayFilter_getInstance(thread, qdValue)) + let filter = try ErrorCheck.nativeCall(thread, dxfg_DayFilter_getInstance(thread, qdValue)).toValue() defer { _ = try? ErrorCheck.nativeCall(thread, dxfg_JavaObjectHandler_release(thread, &(filter.pointee.handler))) } @@ -212,7 +212,7 @@ class NativeSchedule { executor: GetSessionyExecutor) throws -> ScheduleSession? { let qdValue = filter.toQDValue() let thread = currentThread() - let filter = try ErrorCheck.nativeCall(thread, dxfg_SessionFilter_getInstance(thread, qdValue)) + let filter = try ErrorCheck.nativeCall(thread, dxfg_SessionFilter_getInstance(thread, qdValue)).toValue() defer { _ = try? ErrorCheck.nativeCall(thread, dxfg_JavaObjectHandler_release(thread, &(filter.pointee.handler))) } @@ -223,7 +223,7 @@ class NativeSchedule { public func getSessionByTime(time: Long) throws -> ScheduleSession { let thread = currentThread() - let nextSession = try ErrorCheck.nativeCall(thread, dxfg_Schedule_getSessionByTime(thread, schedule, time)) + let nextSession = try ErrorCheck.nativeCall(thread, dxfg_Schedule_getSessionByTime(thread, schedule, time)).toValue() let session = try createSession(thread, session: nextSession) return session } @@ -231,7 +231,7 @@ class NativeSchedule { public func getNearestSessionByTime(time: Long, filter: SessionFilter) throws -> ScheduleSession { let qdValue = filter.toQDValue() let thread = currentThread() - let filter = try ErrorCheck.nativeCall(thread, dxfg_SessionFilter_getInstance(thread, qdValue)) + let filter = try ErrorCheck.nativeCall(thread, dxfg_SessionFilter_getInstance(thread, qdValue)).toValue() defer { _ = try? ErrorCheck.nativeCall(thread, dxfg_JavaObjectHandler_release(thread, &(filter.pointee.handler))) } @@ -239,7 +239,7 @@ class NativeSchedule { dxfg_Schedule_getNearestSessionByTime(thread, schedule, time, - filter)) + filter)).toValue() let session = try createSession(thread, session: nextSession) return session } diff --git a/DXFeedFramework/Native/Subscription/NativeSubscription.swift b/DXFeedFramework/Native/Subscription/NativeSubscription.swift index 9c0e005bf..4c9bd6ede 100644 --- a/DXFeedFramework/Native/Subscription/NativeSubscription.swift +++ b/DXFeedFramework/Native/Subscription/NativeSubscription.swift @@ -149,7 +149,7 @@ class NativeSubscription { let listener = try ErrorCheck.nativeCall(thread, dxfg_DXFeedEventListener_new(thread, NativeSubscription.listenerCallback, - voidPtr)) + voidPtr)).toValue() try ErrorCheck.nativeCall(thread, dxfg_Object_finalize(thread, &(listener.pointee.handler), @@ -285,7 +285,7 @@ extension NativeSubscription { let thread = currentThread() let nativeResult = try ErrorCheck.nativeCall(thread, dxfg_DXFeedSubscription_getSymbols(thread, - self.subscription)) + self.subscription)).toValue() defer { _ = try? ErrorCheck.nativeCall(thread, dxfg_CList_symbol_release(thread, nativeResult)) } diff --git a/DXFeedFramework/Native/Utils/NativeTimeFormat.swift b/DXFeedFramework/Native/Utils/NativeTimeFormat.swift index 2b998a3b0..522c0f1ba 100644 --- a/DXFeedFramework/Native/Utils/NativeTimeFormat.swift +++ b/DXFeedFramework/Native/Utils/NativeTimeFormat.swift @@ -33,25 +33,25 @@ class NativeTimeFormat: NativeBox { convenience init(timeZone: NativeTimeZone) throws { let thread = currentThread() - let timeFormat = try ErrorCheck.nativeCall(thread, dxfg_TimeFormat_getInstance(thread, timeZone.native)) + let timeFormat = try ErrorCheck.nativeCall(thread, dxfg_TimeFormat_getInstance(thread, timeZone.native)).toValue() self.init(native: timeFormat) } convenience init(withTimeZone timeFormat: NativeTimeFormat) throws { let thread = currentThread() - let timeFormat = try ErrorCheck.nativeCall(thread, dxfg_TimeFormat_withTimeZone(thread, timeFormat.native)) + let timeFormat = try ErrorCheck.nativeCall(thread, dxfg_TimeFormat_withTimeZone(thread, timeFormat.native)).toValue() self.init(native: timeFormat) } convenience init(withMillis timeFormat: NativeTimeFormat) throws { let thread = currentThread() - let timeFormat = try ErrorCheck.nativeCall(thread, dxfg_TimeFormat_withMillis(thread, timeFormat.native)) + let timeFormat = try ErrorCheck.nativeCall(thread, dxfg_TimeFormat_withMillis(thread, timeFormat.native)).toValue() self.init(native: timeFormat) } convenience init(fullIso timeFormat: NativeTimeFormat) throws { let thread = currentThread() - let timeFormat = try ErrorCheck.nativeCall(thread, dxfg_TimeFormat_asFullIso(thread, timeFormat.native)) + let timeFormat = try ErrorCheck.nativeCall(thread, dxfg_TimeFormat_asFullIso(thread, timeFormat.native)).toValue() self.init(native: timeFormat) } diff --git a/DXFeedFramework/Native/Utils/NativeTimePeriod.swift b/DXFeedFramework/Native/Utils/NativeTimePeriod.swift index d3c34eb14..e821ac94c 100644 --- a/DXFeedFramework/Native/Utils/NativeTimePeriod.swift +++ b/DXFeedFramework/Native/Utils/NativeTimePeriod.swift @@ -33,13 +33,13 @@ class NativeTimePeriod: NativeBox { convenience init(value: Int64) throws { let thread = currentThread() - let timePeriod = try ErrorCheck.nativeCall(thread, dxfg_TimePeriod_valueOf(thread, value)) + let timePeriod = try ErrorCheck.nativeCall(thread, dxfg_TimePeriod_valueOf(thread, value)).toValue() self.init(native: timePeriod) } convenience init(value: String) throws { let thread = currentThread() - let timePeriod = try ErrorCheck.nativeCall(thread, dxfg_TimePeriod_valueOf2(thread, value.toCStringRef())) + let timePeriod = try ErrorCheck.nativeCall(thread, dxfg_TimePeriod_valueOf2(thread, value.toCStringRef())).toValue() self.init(native: timePeriod) } diff --git a/DXFeedFramework/Native/Utils/NativeTimeZone.swift b/DXFeedFramework/Native/Utils/NativeTimeZone.swift index 71aeff17c..8c85e7249 100644 --- a/DXFeedFramework/Native/Utils/NativeTimeZone.swift +++ b/DXFeedFramework/Native/Utils/NativeTimeZone.swift @@ -24,7 +24,7 @@ class NativeTimeZone: NativeBox { convenience init(timeZoneID: String) throws { let thread = currentThread() - let native = try ErrorCheck.nativeCall(thread, dxfg_TimeZone_getTimeZone(thread, timeZoneID.toCStringRef())) + let native = try ErrorCheck.nativeCall(thread, dxfg_TimeZone_getTimeZone(thread, timeZoneID.toCStringRef())).toValue() self.init(native: native) } diff --git a/DXFeedFramework/Utils/Optional+Ext.swift b/DXFeedFramework/Utils/Optional+Ext.swift new file mode 100644 index 000000000..b10caf295 --- /dev/null +++ b/DXFeedFramework/Utils/Optional+Ext.swift @@ -0,0 +1,17 @@ +// +// +// Copyright (C) 2024 Devexperts LLC. All rights reserved. +// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. +// + +import Foundation + +extension Optional { + func toValue() throws -> Wrapped { + guard let value = self else { + throw GraalException.nullException + } + return value + } +}