Giter Site home page Giter Site logo

mirror's Introduction

DESCRIPTION

Mirror is a minimalistic C++20 reflection library. It contains utilities for counting the non-static data members in types, and for viewing objects as tuples.

Implementation uses the same basic idea as Antony Polukhin's magic_get, and has the same limitations.

Hopefully, this library will become obsolete by the time C++26 comes out.

INSTALLATION

The library consists of one module file in the src directory. By default only types with up to 32 non-static data members can be viewed as tuples. To increase this number the generate.sh script can be used (requires python3 and clang-format).

For example, to increase the limit to 255, the following command can be executed in a terminal:

./generate.sh 255

USAGE

To obtain the number of non-static data members in a type, use the data_member_count variable template:

struct custom_type {
    int x, y, z;
    float w;
};

static_assert(mirror::data_member_count<custom_type> == 4);

To view an object of some type as a tuple of references to its non-static data members, use the reflect function:

auto x = custom_type{1, 2, 3, 4.0f};
auto t = mirror::reflect(x);

// The following expression evaluates to true.
// (std::get<0>(t) == x.x) && (&(std::get<0>(t)) == &(x.x))

LICENSE

Copyright Nezametdinov E. Ildus 2023. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)

mirror's People

Contributors

everard avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.