Skip to content

C++ header-only library which enable using aggregates like tuples

License

Notifications You must be signed in to change notification settings

Dwarfobserver/AggregatesToTuples

Repository files navigation

GCC Build Status MSVC Build Status License: MIT

Aggregates to tuples

This is a single-header library in partial C++17 which enable to treat aggregate (or record) types as tuples. It works for MSVC, Clang and GCC. It has been inspired by this nice article : https://playfulprogramming.blogspot.ca/2016/12/serializing-structs-with-c17-structured.html

Using tuples instead of aggregates can allow many functional programming algorithms and automatic properties, such as equality comparaison, serialization or hash.

There is no documentation for now, tests can be used as a replacement.

Overview of some features :

struct info {
    int age;
    int size;
};
struct person {
    std::string name;
    info infos;
};

auto alex = person { "Alex", { 24, 182 } };

size_t hash = att::hash(alex); // combinaison of string hash and two int hash

using namespace att::operators;

std::ostringstream stream;
stream << alex;                // stream.str() == "{ Alex , { 24 , 182 } }"

person alex2;
stream >> alex2;

bool eq = alex == alex2;       // true

alex2.infos.size += 1;

bool lt = alex < alex2;        // true

att::as_tuple_t<person> refs  = att::as_tuple(alex); // std::tuple<std::string&, info&>
att::to_tuple_t<person> tuple = att::to_tuple(alex); // std::tuple<std::string, info>

auto alex3 = att::from_tuple(refs).make<person>();

Tests can be built and launched with :

mkdir build
cd build
cmake ..
cmake --build .
ctest -V

'single_include/aggregates_to_tuples.hpp' and 'include/arity_functions.inl' are created by the programs contained in 'generators'.

The python file 'full_build.py' can be called from your build directory (which must be in the root directory of the project) to build and execute generators, then build and launch tests.

Project limitations :

  • The aggregate max size is limited (it can be increased with the arity_functions generator)
  • It does not support aggregates with native arrays (eg. T[N], use std::array<T, N> instead)
  • It does not support aggregates with base classes (they are detected as aggregates but can't be destructured)

Note : std::is_aggregate is not used because it is not implemented in MSVC (and not Clang from what I saw), but it would avoid some types detection caveats and greatly reduce compile time.

About

C++ header-only library which enable using aggregates like tuples

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages