aboutsummaryrefslogtreecommitdiff
path: root/julia/CrystFEL
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2023-12-20 12:57:50 +0100
committerThomas White <taw@physics.org>2024-02-06 16:59:34 +0100
commitdfa3ecf8c416470609f97b3b2eeafc5914fc4ba8 (patch)
treebe4f6065489e54855dc4795977c4cb3b2f61d367 /julia/CrystFEL
parenta54740542de1610b63027e1224b89546b42278e3 (diff)
Julia: Stream semantics
Diffstat (limited to 'julia/CrystFEL')
-rw-r--r--julia/CrystFEL/src/streams.jl38
1 files changed, 35 insertions, 3 deletions
diff --git a/julia/CrystFEL/src/streams.jl b/julia/CrystFEL/src/streams.jl
index afc23f4c..94a1de17 100644
--- a/julia/CrystFEL/src/streams.jl
+++ b/julia/CrystFEL/src/streams.jl
@@ -13,36 +13,68 @@ mutable struct Stream
end
+"""
+ Stream(filename, "w", dtempl)
+
+Opens a CrystFEL stream for writing. Note that you must provide a `DataTemplate`,
+which is needed to translate "panel coordinates" to "file coordinates".
+
+Corresponds to CrystFEL C API routine `stream_open_for_write`.
+"""
function Stream(filename, mode::AbstractString, dtempl::DataTemplate)
+
if mode == "w"
out = @ccall libcrystfel.stream_open_for_write(filename::Cstring,
- dtempl.internalptr::Ptr{InternalDataTemplate})::Ptr{InternalStream}
+ dtempl.internalptr::Ptr{InternalDataTemplate})::Ptr{InternalStream}
if out == C_NULL
throw(ErrorException("Failed to open stream for reading"))
end
- return Stream(out)
+ finalizer(close, Stream(out))
+
elseif mode =="r"
throw(ArgumentError("To open a stream for reading, don't provide the DataTemplate"))
+
else
throw(ArgumentError("Unrecognised CrystFEL stream mode"*mode))
end
end
+"""
+ Stream(filename, "r")
+
+Opens a CrystFEL stream for reading.
+
+Close the stream with `close` when you've finished (this will happen
+automatically when the `Stream` object is finalized).
+
+Corresponds to CrystFEL C API routine `stream_open_for_read`.
+"""
function Stream(filename, mode::AbstractString)
+
if mode == "r"
out = @ccall libcrystfel.stream_open_for_read(filename::Cstring)::Ptr{InternalStream}
if out == C_NULL
throw(ErrorException("Failed to open stream for reading"))
end
- return Stream(out)
+ finalizer(close, Stream(out))
+
elseif mode == "w"
throw(ArgumentError("To open a stream for writing, you must provide "
*"a DataTemplate: use Stream(filename, \"w\", dtempl)"))
+
else
throw(ArgumentError("Unrecognised CrystFEL stream mode"*mode))
end
end
+function Base.close(st::Stream)
+ if st.internalptr != C_NULL
+ @ccall libcrystfel.stream_close(st.internalptr::Ptr{InternalStream})::Cvoid
+ st.internalptr = C_NULL
+ end
+end
+
+
end # of module