Git Product home page Git Product logo

Comments (8)

 avatar commented on April 28, 2024

So you want the types to store their union enum as part of their definition? The problem with that is that a type can be part of multiple unions. Also, the advantage over using the current enums doesn't seem that great to me.

from flatbuffers.

lbensaad avatar lbensaad commented on April 28, 2024

What i meant by type is not a union enum but sort of an ID assigned by the user or the compiler to tables and structs like the field id. Because if two peers are exchanging messages, then there is no way to know what type of message is contained in the data if there is no ID in the data (header).

from flatbuffers.

 avatar commented on April 28, 2024

To distinguish multiple types of messages is what the union feature was designed for.. why not use it?

Alternatively, if you want a more free-form solution, use the file_identifier feature.

from flatbuffers.

lbensaad avatar lbensaad commented on April 28, 2024

Does the union accept structs now? or will it does?
May be i will use the file_identifier or use my own IDs.

from flatbuffers.

 avatar commented on April 28, 2024

It does not support structs (structs are inline, so they are not size-compatible with tables).

from flatbuffers.

lbensaad avatar lbensaad commented on April 28, 2024

I think the file_id works with only one table, so how to identify other tables?
I end up using my own IDs class and removed the wrapping Message table defined above.
So my code now looks as follows

public class MessageType{
    public static final int CHALLENGE=1;
    public static final int POINT=2;
}

and use the builder as follows:

        FlatBufferBuilder fbb = new FlatBufferBuilder(100);
        Challenge.startChallenge(fbb);
        Challenge.addX(fbb, x);
        Challenge.addY(fbb, y);
        Challenge.addToken(fbb, t);
        int c = Challenge.endChallenge(fbb);
        fbb.finish(c);
        fbb.putInt(MessageTyte.CHALLENGE)

so after I call fbb.finish() I then call fbb.putInt(type) for example to add the message type;
For parsing, now i use something like:

        int t=bb.getInt(0);
        int offset=4;
        switch(t){
           case MessageType.POINT:
                Point p=Point.getRootAsPoint(bb, offset);
                processPoint(p);
                break;
           case MessageType.CHALLENGE:
                Challenge ch = Challenge.getRootAsChallenge(bb, offset)
                processChallenge(ch);
                break;
        }

The reverse writing in the ByteBuffer is a very good idea as you can see, wee can add header information without the need to copy in new buffer, and use only one write operation later to write the whole message to the socket/file. I am also planning to add the "length" of the message the same way as adding the type of message, like that i will know how many bytes to read before start parsing them, especially if the stream contains many messages.

from flatbuffers.

 avatar commented on April 28, 2024

With file_identifier, you can simply call any number of XBufferHasIdentifier() calls to determine which type it is?

since you cannot use structs as the root of a FlatBuffer either, why not use unions as intended? that is much more compatible than your putInt solution (at least use addInt, as putInt may go out of bounds).

from flatbuffers.

lbensaad avatar lbensaad commented on April 28, 2024

Ok thanks, I will try that.

from flatbuffers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.