Skip to content

Design for serialization API #29

@jchv

Description

@jchv

Hey. I'm working on trying to add basic write functionality to the runtime, in preparation for hopefully eventually contributing to the serialization issue.

Unfortunately, it seems like this is actually pretty tough to do without breaking changes.

Right now the C++ API has a constructor like this:

    /**
     * Constructs new Kaitai Stream object, wrapping a given std::istream.
     * \param io istream object to use for this Kaitai Stream
     */
    kstream(std::istream* io);

It uses the actual std::istream* passed in, which means that we kind of need to keep an std::istream* to remain fully semantically compatible.

I think there's basically four approaches:

  1. Take the streambuf from the incoming std::istream* and pass it to an std::iostream. This would enable you to do the same for an incoming std::ostream*. In theory I think this does most of what you want, but it almost certainly will behave differently since the passed in stream is not touched directly. In addition, there are several const member functions that call non-const methods on m_io, so if you make m_io an std::iostream (not a pointer) it must be marked mutable to maintain the same API (or const-casted everywhere, etc.)

  2. Store the incoming std::istream* as both std::ios* and std::istream*, and have an additional pointer for std::ostream*. On input streams, the std::ostream* would be NULL and on output streams, the std::istream* would be NULL. This sucks because we need additional checks in order to prevent segfaults, since now any read/write call may hit a null pointer if kstream was created on the wrong type of iostream.

  3. Make a new class for output streams, perhaps kaitai::kostream. Perhaps have a base class for some of the shared functionality. Although this solution is not terrible, because kaitai::kstream exists today, it would likely always have to exist or at least be aliased to something like kaitai::kistream.

  4. Break the API and only take in a streambuf* or something similarly more generic. Is this even an option?

I don't think input and output streams have a ton of overlap, so maybe this is not a huge issue; but it does seem like this requires some thought...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions