Skip to content

Commit

Permalink
errorchecker refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kosyloa committed Apr 18, 2024
1 parent 4b761e4 commit ff48333
Show file tree
Hide file tree
Showing 19 changed files with 77 additions and 61 deletions.
4 changes: 4 additions & 0 deletions DXFeedFramework.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -668,6 +669,7 @@
6433B1302BCE87D4004EFED7 /* RequestProfile.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = RequestProfile.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
6433B1312BCFC01F004EFED7 /* DXLastEventsSubscribedTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DXLastEventsSubscribedTest.swift; sourceTree = "<group>"; };
6435EE3C2B1F1E9200E8496C /* PrintQuoteEvents.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = PrintQuoteEvents.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
643A329A2BD0137000F6F790 /* Optional+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Optional+Ext.swift"; sourceTree = "<group>"; };
64437A8E2A9DEE6F005929B2 /* InstrumentProfile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstrumentProfile.swift; sourceTree = "<group>"; };
64437A912A9DF1DE005929B2 /* NativeInstrumentProfileReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeInstrumentProfileReader.swift; sourceTree = "<group>"; };
644551C92B973A0D0069E3A2 /* FetchDailyCandles.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = FetchDailyCandles.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
Expand Down Expand Up @@ -1264,6 +1266,7 @@
6464074E2A9F62D4006FF769 /* Date+Ext.swift */,
64A42F3A2B07A7A3001C3ACC /* SymbolParser.swift */,
640885C92B1F7EE700E6CF88 /* QdsUtils.swift */,
643A329A2BD0137000F6F790 /* Optional+Ext.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
2 changes: 1 addition & 1 deletion DXFeedFramework/Native/Endpoint/NativeBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)).value()
return NativeEndpoint(value)
}
}
8 changes: 4 additions & 4 deletions DXFeedFramework/Native/Endpoint/NativeEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)).value()
return NativeFeed(feed: nativeFeed)
} catch {
return nil
Expand All @@ -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)).value()
return NativePublisher(publisher: nativeFeed)
} catch {
return nil
Expand Down Expand Up @@ -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)).value()
defer {
_ = try? ErrorCheck.nativeCall(thread,
dxfg_CList_EventClazz_release(thread, list))
Expand All @@ -121,7 +121,7 @@ class NativeEndpoint {
let listener = try ErrorCheck.nativeCall(thread,
dxfg_PropertyChangeListener_new(thread,
NativeEndpoint.listenerCallback,
voidPtr))
voidPtr)).value()
self.listener = listener
try ErrorCheck.nativeCall(thread, dxfg_Object_finalize(thread,
&(listener.pointee.handler),
Expand Down
13 changes: 4 additions & 9 deletions DXFeedFramework/Native/ErrorHandling/ErrorCheck.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,11 @@ class ErrorCheck {
}
}

static func nativeCall<T>(_ 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<T>(_ thread: OpaquePointer!, _ result: T) throws -> T {
if let exception = fetchException(thread) {
throw exception
}
return result
}

static func graalCall(_ result: Int32) throws {
Expand Down
16 changes: 8 additions & 8 deletions DXFeedFramework/Native/Feed/NativeFeed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class NativeFeed {
thread,
type.eventSymbol,
type.type.nativeCode())
)
).value()
defer {
_ = try? ErrorCheck.nativeCall(thread,
dxfg_EventType_release(
Expand Down Expand Up @@ -156,7 +156,7 @@ class NativeFeed {
dxfg_DXFeed_getLastEventPromise(thread,
feed,
type.type.nativeCode(),
converted))
converted)).value()
return NativePromise(promise: &native.pointee.handler)

}
Expand All @@ -175,7 +175,7 @@ class NativeFeed {
dxfg_DXFeed_getLastEventsPromises(thread,
feed,
type.type.nativeCode(),
listPointer))
listPointer)).value()
var result = [NativePromise]()
for index in 0..<Int(native.pointee.list.size) {
let promise = native.pointee.list.elements[index]
Expand All @@ -201,7 +201,7 @@ class NativeFeed {
feed,
type.type.nativeCode(),
converted,
nativeSource))
nativeSource)).value()

return NativePromise(promise: &native.pointee.base)
}
Expand All @@ -223,7 +223,7 @@ class NativeFeed {
type.type.nativeCode(),
converted,
fromTime,
toTime))
toTime)).value()
return NativePromise(promise: &native.pointee.base)
}
}
Expand Down Expand Up @@ -260,7 +260,7 @@ extension NativeFeed {
dxfg_DXFeed_getLastEventIfSubscribed(thread,
feed,
type.type.nativeCode(),
converted))
converted)).value()
return try mapper.fromNative(native: result)?.lastingEvent
} catch GraalException.nullException {
return nil
Expand All @@ -283,7 +283,7 @@ extension NativeFeed {
feed,
type.type.nativeCode(),
converted,
source.name.toCStringRef()))
source.name.toCStringRef())).value()
if result.pointee.size == 0 {
return nil
} else {
Expand Down Expand Up @@ -319,7 +319,7 @@ extension NativeFeed {
type.type.nativeCode(),
converted,
fromTime,
toTime))
toTime)).value()
if result.pointee.size == 0 {
return nil
} else {
Expand Down
2 changes: 1 addition & 1 deletion DXFeedFramework/Native/Graal/Isolate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ class Isolate {

func throwException() throws {
let thread = currentThread()
_ = try ErrorCheck.nativeCall(thread, dxfg_throw_exception(thread))
_ = try ErrorCheck.nativeCall(thread, dxfg_throw_exception(thread))
}
}
6 changes: 3 additions & 3 deletions DXFeedFramework/Native/Ipf/Live/NativeExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class NativeExecutor {
static func createOnConcurrentLinkedQueue() throws -> NativeExecutor {
let thread = currentThread()
let executor = try ErrorCheck.nativeCall(thread,
dxfg_ExecutorBaseOnConcurrentLinkedQueue_new(thread))
dxfg_ExecutorBaseOnConcurrentLinkedQueue_new(thread)).value()
return NativeExecutor(executor: executor)
}

Expand All @@ -35,7 +35,7 @@ class NativeExecutor {
let executor = try ErrorCheck.nativeCall(thread,
dxfg_Executors_newScheduledThreadPool(thread,
numberOfThreads,
nameOfthread.toCStringRef()))
nameOfthread.toCStringRef())).value()
return NativeExecutor(executor: executor)
}

Expand All @@ -44,7 +44,7 @@ class NativeExecutor {
let executor = try ErrorCheck.nativeCall(thread,
dxfg_Executors_newFixedThreadPool(thread,
numberOfThreads,
nameOfthread.toCStringRef()))
nameOfthread.toCStringRef())).value()
return NativeExecutor(executor: executor)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)).value()
return NativeProfileIterator(native, isDeallocated: true)
}

Expand All @@ -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)).value()
return NativeExecutor(executor: native)
}

Expand All @@ -132,7 +132,7 @@ public class NativeInstrumentProfileCollector {
let nativeListener = try ErrorCheck.nativeCall(thread,
dxfg_InstrumentProfileUpdateListener_new(thread,
callback,
voidPtr))
voidPtr)).value()
try ErrorCheck.nativeCall(thread, dxfg_Object_finalize(thread,
&(nativeListener.pointee.handler),
NativeInstrumentProfileCollector.finalizeCallback,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class NativeInstrumentProfileConnection {
dxfg_IpfPropertyChangeListener_new(
thread,
NativeInstrumentProfileConnection.listenerCallback,
voidPtr))
voidPtr)).value()

try ErrorCheck.nativeCall(thread, dxfg_Object_finalize(thread,
&(listener.pointee.handler),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)).value()

let profile = mapper.fromNative(native: result)
_ = try ErrorCheck.nativeCall(thread, dxfg_InstrumentProfile_release(thread, result))
Expand Down
Loading

0 comments on commit ff48333

Please sign in to comment.