struct/strpack future

35 views
Skip to first unread message

Simon Kornblith

unread,
Dec 16, 2012, 12:28:02 PM12/16/12
to juli...@googlegroups.com
Looking at Patrick's StrPack.jl module, I noticed that he has pulled the s_str macro that's in the version of strpack.jl in extras. I can't say I really miss it, but for a project I'm working on I need to be able to specify structs containing arrays of fixed lengths, and I didn't see an obvious way in the new StrPack module. I ended up writing a short macro that takes code like:

@struct PL_EventHeader begin
    Name::ASCIIString(32)
    Channel::Int32
    Comment::ASCIIString(128)
    Padding::Int32(33)
end

and creates a type that looks like:

    type PL_EventHeader
        begin
            Name::ASCIIString
            Channel::Int32
            Comment::ASCIIString
            Padding::Array{Int32, 1}
        end
    end

and a corresponding function read(ios::IOStream, ::Type{PL_EventHeader}) to read the struct out of a file as a PL_EventHeader.

For my use case, I didn't have to deal with alignment or serializing structs to bytes, so the macro is quite simple, but I wonder whether it's worth trying to integrate something similar into strpack. Or maybe someone has a better idea that doesn't abuse the AST.

Simon

Patrick O'Leary

unread,
Dec 16, 2012, 12:34:07 PM12/16/12
to juli...@googlegroups.com
It's funny you mention this. I'm working on the infrastructure to allow fixed-size arrays and strings, and plan to have a macro kind of like that. I beg patience, it's a major refactoring :D
Reply all
Reply to author
Forward
0 new messages