Skip to content
Snippets Groups Projects
Verified Commit a1b6862c authored by Tamas Gal's avatar Tamas Gal :speech_balloon:
Browse files

Add I/O helper functions

parent 910db20a
No related branches found
No related tags found
1 merge request!51Draft: Add more I/O functionalities
......@@ -150,6 +150,8 @@ getevent
categorize
nthbitset
most_frequent
packedsize
writestruct
```
### DAQ
......
......@@ -52,3 +52,9 @@ function Base.read(s::IO, ::Type{T}; legacy=false) where T<:DAQEvent
T(header, snapshot_hits, triggered_hits)
end
function Base.write(io::IO, s::Summaryslice)
# write(io, Int32(size?))
# write(io, Int32(DAQDATATYPES.DAQSUMMARYSLICE))
writestruct(io, s.header)
end
......@@ -130,6 +130,10 @@ MCEventMatcher,
SummarysliceIntervalIterator,
getevent,
# I/O
packedsize,
writestruct,
# Utils
categorize,
is3dmuon,
......
......@@ -109,3 +109,42 @@ function tonumifpossible(v::AbstractString)
end
v
end
"""
Calculates the packed size in bytes of an immutable struct containing only
primitives and other isbitstypes.
"""
function packedsize(dt::DataType)
isbitstype(dt) || error("Only isbits types are supported")
n = 0
for ftype in fieldtypes(dt)
if isprimitivetype(ftype)
n += sizeof(ftype)
else
n += packedsize(ftype)
end
end
n
end
packedsize(::T) where T = packedsize(T)
"""
Serialises an immutable struct containing only primitives and
other isbitstypes. Returns the number of bytes written.
"""
function writestruct(io::IO, s::T) where T
isbitstype(T) || error("Only isbits types are supported")
n = 0
for fname in fieldnames(T)
v = getfield(s, fname)
if isprimitivetype(fieldtype(T, fname))
write(io, v)
n += sizeof(v)
else
n += writestruct(io, v)
end
end
n
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment