ftz.Lyberta.net

Modern C++ goodness

ftz Serialization

Library for binary input and output

This library provides a simple API for reading and writing binary data. It takes care about endianness and allows you to write straightforward and portable code. operator>> does input and operator<< does output. In order for your classes to be [de]serializable, you need to provide Deserialize and Serialize member functions. Everything else is handled automatically.

Example code

// File: Foo.h
#include <ftz/Serialization/BinaryInputStream.h>
#include <ftz/Serialization/BinaryOutputStream.h>

struct Foo
{
	std::array<char, 4> fourcc;
	int a;
	float b;

	void Deserialize(ftz::Serialization::BinaryInputStream& stream)
	{
		stream >> fourcc;
		stream >> a;
		stream >> b;
	}
	void Serialize(ftz::Serialization::BinaryOutputStream& stream) const
	{
		stream << fourcc;
		stream << a;
		stream << b;
	}
};

// File: Main.cpp
#include <iostream>
#include <ftz/Serialization/FileInputStream.h>
#include "Foo.h"

int main(int argc, char* argv[])
{
	ftz::Serialization::FileInputStream stream{argv[1]};
	Foo foo;
	stream >> foo;
	std::cout << foo.a << ' ' << foo.b << '\n';
}

Key features

  • Cross platform - written in ISO C++17 + Concepts TS (+ #pragma once).
  • Endianness aware - no need to manually swap bytes.
  • Extensible - Read and write to memory, files or your own custom streams using a single interface.
  • Free software - released under the terms of GNU GPLv3 or any later version.

Compiler support

  • G++ 8 or newer with libstdc++

How to get

You need to use Conan to install and use this library. To install Conan on APT-based distros you would typically do:

# apt install python-pip
$ pip install conan

Then add official ftz repository:

$ conan remote add ftz https://conan.ftz.lyberta.net

Then you need to follow a Conan tutorial to declare that your project depends on this library, for example, using conanfile.txt:

[requires]
ftzSerialization/Latest@Lyberta/Latest

Or using conanfile.py:

build_requires = "ftzSerialization/Latest@Lyberta/Latest"

The rest depends on your build system. In CMake you would do:

# Importing Conan settings.
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

# Adding example executable target.
add_executable(Example ...)

# Specifying libraries to link executable to.
target_link_libraries(Example PRIVATE CONAN_PKG::ftzSerialization)

After that in your source code you include files like this:

#include <ftz/Serialization/SomeHeader.h>

To build your code, remember to execute conan install before calling conan build or cmake so your dependencies will be set correctly.

Source code

API reference

Manually building the library

Build dependencies:

  • Git
  • G++ 8 or newer
  • CMake 3.8 or newer
  • Conan 1.2 or newer

To install dependencies on Debian Testing you would invoke:

# apt install git g++-8 cmake python-pip
$ pip install conan

If you didn’t have Conan installed before, you probably need to configure the default profile:

$ conan profile new --detect default
$ conan profile update settings.compiler.version=8 default
$ conan profile update settings.compiler.libcxx=libstdc++11 default
$ conan profile update settings.cppstd=17 default

Clone the repository and switch into it:

$ git clone https://gitlab.com/ftz/serialization.git
$ cd serialization

Build the Conan package with your specified user and channel:

$ conan create . User/Channel