Git Product home page Git Product logo

serialize's Introduction

Build status

Introduction

serialize is a simple bitpacking serializer for C++.

image

It has the following features:

  • Serialize a bool with only one bit
  • Serialize any integer value from [1,64] bits writing only that number of bits to the buffer
  • Serialize signed integer values with [min,max] writing only the required bits to the buffer
  • Serialize floats, doubles, compressed floats, strings, byte arrays, and integers relative to another integer
  • Alignment support so you can align your bitstream to a byte boundary whenever you want
  • Template based serialization system so you can write a unified serialize function instead of separate read and write functions

Usage

You can use the bitpacker directly:

const int BufferSize = 256;

uint8_t buffer[BufferSize];

serialize::BitWriter writer( buffer, BufferSize );

writer.WriteBits( 0, 1 );
writer.WriteBits( 1, 1 );
writer.WriteBits( 10, 8 );
writer.WriteBits( 255, 8 );
writer.WriteBits( 1000, 10 );
writer.WriteBits( 50000, 16 );
writer.WriteBits( 9999999, 32 );
writer.FlushBits();

const int bytesWritten = writer.GetBytesWritten();

serialize::BitReader reader( buffer, bytesWritten );

uint32_t a = reader.ReadBits( 1 );
uint32_t b = reader.ReadBits( 1 );
uint32_t c = reader.ReadBits( 8 );
uint32_t d = reader.ReadBits( 8 );
uint32_t e = reader.ReadBits( 10 );
uint32_t f = reader.ReadBits( 16 );
uint32_t g = reader.ReadBits( 32 );

Or you can write serialize methods for your types:

struct Vector
{
    float x,y,z;

    template <typename Stream> bool Serialize( Stream & stream )
    {
        serialize_float( stream, x );
        serialize_float( stream, y );
        serialize_float( stream, z );
        return true;
    }
};

struct Quaternion
{
    float x,y,z,w;

    template <typename Stream> bool Serialize( Stream & stream )
    {
        serialize_float( stream, x );
        serialize_float( stream, y );
        serialize_float( stream, z );
        serialize_float( stream, w );
        return true;
    }
};

struct RigidBody
{
    Vector position;
    Quaternion orientation;
    Vector linearVelocity;
    Vector angularVelocity;
    bool atRest;

    template <typename Stream> bool Serialize( Stream & stream )
    {
        serialize_object( stream, position );
        serialize_object( stream, orientation );
        serialize_bool( stream, atRest );
        if ( !atRest )
        {
            serialize_object( stream, linearVelocity );
            serialize_object( stream, angularVelocity );
        }
        else if ( Stream::IsReading )
        {
            linearVelocity.x = linearVelocity.y = linearVelocity.z = 0.0;
            angularVelocity.x = angularVelocity.y = angularVelocity.z = 0.0;
        }
        return true;
    }
};

See example.cpp for more examples.

Author

The author of this library is Glenn Fiedler.

Open source libraries by the same author include: netcode, reliable and yojimbo

If you find this software useful, please consider sponsoring it. Thanks!

License

BSD 3-Clause license.

serialize's People

Contributors

gafferongames avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

serialize's Issues

Trouble reading struct

Hello,

Sorry about the last issue.

I'm having trouble reading a serialized structure back from a file. I've followed to example on how to serialize the structure but unsure how to reconstruct the structure from the serialized buffer.

This is the structure I'm using.

#define kEntityPackedsize 16
struct Vec2{
    float x;
    float y;
    template <typename Stream> bool Serialize( Stream & stream ){
        serialize_float( stream, x );
        serialize_float( stream, y );
                        return true;
    }

};//8
enum EntityIdentifier{
    kEntityIdNPC = 1,
    kEntityIdPlayer,
    kEntityIdMax,
};//4
struct Entity{
    uint32_t ident; //4
    uint32_t ctx;   //4
    Vec2 position;  //8
    template <typename Stream> bool Serialize( Stream & stream ){
        
        serialize_int(stream,ident,kEntityIdNPC,kEntityIdMax -1);
        
        //serialize_uint32 doesn't exist ?
        serialize_bits(stream,ctx,32);
        
        serialize_object( stream, position );
        
        return true;
    }
};

And I'm serializing it like so

                FILE* f = fopen("entity","wb");
                if(f)
                {
                    Entity ent;
                    ent.ident = kEntityIdPlayer;
                    ent.ctx = 0xCAFEF00D;
                    ent.position = {32.f,16.f};
                    
                    
                    uint8_t buffer[kEntityPackedsize];
                    serialize::WriteStream writer(buffer,sizeof(buffer));
                    ent.Serialize(writer);
                    fwrite(buffer,sizeof(buffer),1,f);
                    fclose(f);
                }

And I'm trying to read it back

    FILE* f = fopen("entity","rb");
    if(f)
    {
        uint8_t entitybuffer[kEntityPackedsize];
        fread(entitybuffer,sizeof(entitybuffer),1,f);
        serialize::ReadStream reader(entitybuffer,sizeof(entitybuffer));
        
        //No idea how to read the buffer back into the structure
        
        
        //this doesn't work
        int id;
        reader.SerializeInteger(id,kEntityIdNPC,kEntityIdMax -1);
        
        
        fclose(f);
    }

I don't know if SerializeInteger is the correct function to call. Or how to read back individual bits/floats.

Edit:
Solved!

Sorry! I feel silly. I just call Serialize on the structure passing the serialize::ReadStream object.
But I still don't know why write_uint32 or write_uint64 doesn't exist in Stream_Write. I suppose write_bits makes it redundant?

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.