vir-reflect-light

lightweight macro based struct reflection

https://github.com/mattkretz/vir-reflect-light

Science Score: 67.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 1 DOI reference(s) in README
  • Academic publication links
    Links to: zenodo.org
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.1%) to scientific vocabulary

Keywords

cpp20 reflection
Last synced: 4 months ago · JSON representation ·

Repository

lightweight macro based struct reflection

Basic Info
  • Host: GitHub
  • Owner: mattkretz
  • License: lgpl-3.0
  • Language: C++
  • Default Branch: main
  • Homepage:
  • Size: 185 KB
Statistics
  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 1
Topics
cpp20 reflection
Created over 1 year ago · Last pushed about 1 year ago
Metadata Files
Readme License Citation

README.md

vir-reflect-light

CI DOI OpenSSF Best Practices REUSE status fair-software.eu

This package tries to be as light on compile-time as possible while enabling the following features:

  1. List the number of data members.

  2. Provide the variable names (identifiers) of the data members as constexpr_string (which can be used as std::string_view via the .view() member on constexpr_string.

  3. Access lvalue references to the data members of a given object using either an index or the variable name.

  4. Supports any inheritance hierarchy.

  5. Allow exposing only a subset of the data members.

Installation

sh make install prefix=/usr

Usage

The macro VIR_MAKE_REFLECTABLE

Add this macro in the public section of a class definition to make all listed class data members reflectable. The members that are not listed will be ignored and not be reflectable.

VIR_MAKE_REFLECTABLE(<class name>, [<data member name>, [<data member name>, [...]]])

Example:

```c++ template struct Base { int id; VIRMAKEREFLECTABLE(Base, id); };

struct Point : Base { float x, y, z; VIRMAKEREFLECTABLE(Point, x, y, z); };

namespace yours { template class Person { std::string firstname, lastname; T height; T weight;

public: VIRMAKEREFLECTABLE(Person, height, weight); }; } ```

In the above yours::Person<T> example, note that:

  1. VIRMAKEREFLECTABLE needs to be public.

  2. The class name Person in the macro can also be spelled as yours::Person<T>, or Person<T> but not as yours::Person.

  3. first_name and last_name will not be seen by the vir::refl:: API. It is no error to omit them here if you want to hide them from the reflection API.

vir::refl::reflectable<T>

Concept that is satisfied if the class T definition contains a valid (public) VIR_MAKE_REFLECTABLE expansion.

vir::refl::nttp_name<X>

A vir::constexpr_string object identifying the value/name of X. For integers this will be a string representation of the value. For enums it is equivalent to the following facility.

vir::refl::enum_name<X>

A vir::constexpr_string object identifying the name of X. This name includes namespaces. For the name of the enum type use the following facility:

vir::refl::type_name<T>

A vir::constexpr_string object identifying the name of T. This name includes namespaces and template arguments.

vir::refl::class_name<T>

A vir::constexpr_string object identifying the class name of T. This name includes namespaces but no template arguments.

vir::refl::base_type<T>

Alias for the reflectable base type of the type T. If no base type exists (is known) an alias for void.

There is no support for inheriting from multiple reflectable base classes. But multiple inheritance is otherwise not a problem. The derived class can also make members of the non-reflectable base type reflectable.

Given the above definition of Point and Base:

  • vir::refl::base_type<Point> is an alias for Base<Point>.

  • vir::refl::base_type<Base> is an alias for void.

vir::refl::data_member_count<T>

A std::size_t value identifying the number of reflectable data members.

  • vir::refl::data_member_count<Point> is 4.

  • vir::refl::data_member_count<Base<Point>> is 1.

vir::refl::data_member_name<T, Idx>

  • vir::refl::data_member_name<Base<Point>, 0> is vir::constexpr_string("id").

  • vir::refl::data_member_name<Point, 0> is vir::constexpr_string("id").

  • vir::refl::data_member_name<Point, 1> is vir::constexpr_string("x").

  • ...

vir::refl::data_member_index<T, Name>

The index value of the data member with the name Name of class T. The name must match the name returned by data_member_name. It can be given as a string literal or vir::fixed_string.

vir::refl::data_member_type<T, Idx> / data_member_type<T, Name>

Alias for the type of the data member with index Idx or name Name of class T. data_member_index<T, Name> is used for mapping a name to an index.

vir::refl::data_member<Idx>(obj) / data_member<Name>(obj)

Returns a (const) lvalue-reference to the member of obj at the given index Idx / with the given name Name. data_member_index<T, Name> is used for mapping a name to an index.

vir::refl::all_data_members(obj)

Returns a vir::simple_tuple<...> of references to all the reflectable data members of obj.

vir::refl::find_data_members<T, Predicate>

A constexpr std::array<size_t, N> identifying all data member indices that match Predicate. Predicate needs to be a template with two template arguments:

  1. A typename that simply passes the T argument from find_data_members on.
  2. A size_t index identifying the data member to consider.

Example:

```c++ struct A { int foo; double x, y, z; float angle; };

template using sizeof4 = std::boolconstant<sizeof(vir::refl::datamember_type) == 4>;

constexpr std::array idxs = vir::refl::finddatamembers; // => idxs == {0, 4}

template using intorangle = std::disjunction == "angle">, std::sameas<vir::refl::datamember_type, int>>;

constexpr std::array idx2 = vir::refl::finddatamembers; // => idx2 == {0, 4} ```

vir::refl::find_data_members_by_type<T, Predicate>

Shorthand for the above so that standard type traits can be used as Predicate. Predicate<data_member_type<T, Idx>> determines whether the index Idx is returned from the array. The above sizeof4 predicate can thus be written as:

```c++ template using sizeof4 = std::bool_constant;

constexpr std::array idxs = vir::refl::finddatamembersbytype; // => idxs == {0, 4} ```

vir::refl::data_member_types<T, IndexArray = /*all*/>

Alias for a vir::simple_tuple<...> type where the tuple types are equal to the data member types of T at the indexes given by IndexArray. If IndexArray is omitted, all data members will be listed.

vir::refl::for_each_data_member_index<T>(callable)

Calls the function callable with one argument of type std::integral_constant<std::size_t, i>. The function is called for each i in the half-open range 0 to data_member_count<T>.

Owner

  • Name: Matthias Kretz
  • Login: mattkretz
  • Kind: user
  • Location: Darmstadt, Germany
  • Company: GSI Helmholtzzentrum für Schwerionenforschung

C++ Committee Numerics Chair, SIMD specialist, CS PhD, Dipl.-Phys, High Energy Physics Software, former KDE core developer, ORCID: 0000-0002-0867-243X

Citation (CITATION.cff)

# This CITATION.cff file was initially generated with cffinit.
# Visit https://bit.ly/cffinit to generate yours today!
#
# SPDX-FileCopyrightText: 2024 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
# SPDX-License-Identifier: CC0-1.0

cff-version: 1.2.0
title: vir-reflect-light
message: >-
  If you use this software, please cite it using the metadata from
  this file.
type: software
authors:
  - given-names: Matthias
    family-names: Kretz
    email: m.kretz@gsi.de
    affiliation: GSI Helmholtz Centre for Heavy Ion Research
    orcid: 'https://orcid.org/0000-0002-0867-243X'
doi: 10.5281/zenodo.13762632
version: 0.0.190
date-released: 2024-09-14
identifiers:
  - type: doi
    value: 10.5281/zenodo.13762632
    description: Version 0.0.190
repository-code: 'https://github.com/mattkretz/vir-simd'
keywords:
  - C++
  - reflection
  - library
license: LGPL-3.0-or-later

GitHub Events

Total
Last Year

Committers

Last synced: 11 months ago

All Time
  • Total Commits: 65
  • Total Committers: 1
  • Avg Commits per committer: 65.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 65
  • Committers: 1
  • Avg Commits per committer: 65.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Matthias Kretz m****z@g****e 65
Committer Domains (Top 20 + Academic)
gsi.de: 1

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 0
  • Total pull requests: 1
  • Average time to close issues: N/A
  • Average time to close pull requests: about 22 hours
  • Total issue authors: 0
  • Total pull request authors: 1
  • Average comments per issue: 0
  • Average comments per pull request: 0.0
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 1
  • Average time to close issues: N/A
  • Average time to close pull requests: about 22 hours
  • Issue authors: 0
  • Pull request authors: 1
  • Average comments per issue: 0
  • Average comments per pull request: 0.0
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
  • mattkretz (2)
Top Labels
Issue Labels
Pull Request Labels
enhancement (2)

Dependencies

.github/workflows/CI.yml actions
  • actions/checkout v3 composite
.github/workflows/fair-software.yml actions
  • fair-software/howfairis-github-action 0.2.1 composite
.github/workflows/reuse.yml actions
  • actions/checkout v4 composite
  • fsfe/reuse-action v4 composite