is a c++20 compile and runtime Struct Reflections header only library.

ruby-reflexpr

is a c++20 compile and runtime Struct Reflections header only library. It allows you to iterate over aggregate type's member variables.

Example Usage

void operator()() const { std::cout << "Type: " << typeid(T).name() << '\n'; }; }; struct functor_reflected { template void operator()(const std::string& name) const { std::cout << "Name: " << name << " Type: " << typeid(T).name() << '\n'; }; }; int main() { // DEMO: reflexpr::for_each_member_variable { std::cout << "For each member variable:\n"; auto func = [](T & v) { std::cout << "Type: " << typeid(T).name() << " Value: " << v << '\n'; }; aggregate_type at{ 1 , 3.5f, "Foxes are great!" }; reflexpr::for_each_member_variable(at, func); std::cout << '\n'; } // DEMO: reflexpr::for_each_member_type { std::cout << "For each member type:\n"; reflexpr::for_each_member_type(functor{}); std::cout << '\n'; } // DEMO: reflexpr::for_each_reflected_member_variable { std::cout << "For each member variable reflected:\n"; auto func = [](T & v, const std::string& name) { std::cout << "Name: " << name << " Type: " << typeid(T).name() << " Value: " << v << '\n'; }; aggregate_type_reflected at{ 1 , 3.5f, "Foxes are great!" }; reflexpr::for_each_reflected_member_variable(at, func); std::cout << '\n'; } // DEMO: reflexpr::for_each_reflected_member_type { std::cout << "For each member type reflected:\n"; reflexpr::for_each_reflected_member_type(functor_reflected{}); std::cout << '\n'; } return 0; } ">
// demo.cpp
#include <iostream>
#include <string>

#include "ruby_reflexpr.hpp"

struct aggregate_type
{
	int a;
	float b;
	std::string str;
};

REFLECT(
struct aggregate_type_reflected
{
	int a;
	float b;
	std::string str;
}
);

struct functor
{
	template<class T>
	void operator()() const
	{
		std::cout << "Type: " << typeid(T).name() << '\n';
	};
};

struct functor_reflected
{
	template<class T>
	void operator()(const std::string& name) const
	{
		std::cout << "Name: " << name << " Type: " << typeid(T).name() << '\n';
	};
};

int main()
{
	// DEMO: reflexpr::for_each_member_variable
	{
		std::cout << "For each member variable:\n";
		auto func = []<class T>(T & v)
		{
			std::cout << "Type: " << typeid(T).name() << " Value: " << v << '\n';
		};

		aggregate_type at{ 1 , 3.5f, "Foxes are great!" };
		
		reflexpr::for_each_member_variable(at, func);
		std::cout << '\n';
	}

	// DEMO: reflexpr::for_each_member_type
	{
		std::cout << "For each member type:\n";

		reflexpr::for_each_member_type(functor{});
		std::cout << '\n';
	}

	// DEMO: reflexpr::for_each_reflected_member_variable
	{
		std::cout << "For each member variable reflected:\n";
		auto func = []<class T>(T & v, const std::string& name)
		{
			std::cout << "Name: " << name << " Type: " << typeid(T).name() << " Value: " << v << '\n';
		};

		aggregate_type_reflected at{ 1 , 3.5f, "Foxes are great!" };

		reflexpr::for_each_reflected_member_variable(at, func);
		std::cout << '\n';
	}

	// DEMO: reflexpr::for_each_reflected_member_type
	{
		std::cout << "For each member type reflected:\n";

		reflexpr::for_each_reflected_member_type(functor_reflected{});
		std::cout << '\n';
	}
	
	return 0;
}

Planned Improvements

  • Increasing member variable limit to 30
  • Introducting noreflect specifier.
  • Improving member variable names parsing.

Limitation

Right now it supports only up to 10 member variables and introduces small runtime overhead when registering member variable names.

Extra Notice

This library is part of Ruby ecosystem created and managed by RedSkittleFox. Not all headers are avialable. This header is standalone.

Owner
RedSkittleFox
Highschool Student, C++ and CS enthusiast.
RedSkittleFox
Similar Resources

distributed builds for C, C++ and Objective C

distcc -- a free distributed C/C++ compiler system by Martin Pool Current Documents: https://distcc.github.io/ Formally http://distcc.org/ "pump" func

Sep 18, 2022

Roaring bitmaps in C (and C++)

CRoaring Portable Roaring bitmaps in C (and C++) with full support for your favorite compiler (GNU GCC, LLVM's clang, Visual Studio). Included in the

Sep 19, 2022

New generation entropy codecs : Finite State Entropy and Huff0

New Generation Entropy coders This library proposes two high speed entropy coders : Huff0, a Huffman codec designed for modern CPU, featuring OoO (Out

Sep 11, 2022

Easing the task of comparing code generated by cc65, vbcc, and 6502-gcc

6502 C compilers benchmark Easing the way to compare code generated by cc65, 6502-gcc, vbcc, and KickC. This repository contains scripts to: Compile t

Sep 4, 2022

Secure ECC-based DID intersection in Go, Java and C.

SecureUnionID Secure ECC-based DID intersection. ABSTRACT This project is used to protect device ID using Elliptic Curve Cryptography algorithm. The d

Aug 22, 2022

nanoc is a tiny subset of C and a tiny compiler that targets 32-bit x86 machines.

nanoc is a tiny subset of C and a tiny compiler that targets 32-bit x86 machines. Tiny? The only types are: int (32-bit signed integer) char (8-

Feb 13, 2022

Smaller C is a simple and small single-pass C compiler

Smaller C is a simple and small single-pass C compiler, currently supporting most of the C language common between C89/ANSI C and C99 (minus some C89 and plus some C99 features).

Sep 21, 2022

Microvm is a virtual machine and compiler

Microvm is a virtual machine and compiler

The aim of this project is to create a stack based language and virtual machine for microcontrollers. A mix of approaches is used. Separate memory is used for program and variable space (Harvard architecture). An interpreter, virtual machine and compiler are available. A demostration of the interpreter in action is presented below.

Aug 14, 2022

Pre-configured LLVM and ANTLR4 for C++

LLVM + ANTLR4 Starter Project Starter project for ANTLR4 and LLVM C++ project. Prerequisite LLVM 12 Java (for ANTLR4) git Install prerequisite librari

Aug 9, 2022
Compile and execute C "scripts" in one go!
Compile and execute C

c "There isn't much that's special about C. That's one of the reasons why it's fast." I love C for its raw speed (although it does have its drawbacks)

Sep 24, 2022
Compile-Time Reflection in C++ for use with Scripting Languages

Introspective Introspective is a header file that brings reflection to any class that wants it, regardless of whether the reflected member is a consta

Mar 10, 2022
Compression abstraction library and utilities

Squash - Compresion Abstraction Library

Sep 14, 2022
Multi-format archive and compression library

Welcome to libarchive! The libarchive project develops a portable, efficient C library that can read and write streaming archives in a variety of form

Sep 20, 2022
Sep 8, 2022
Superfast compression library

DENSITY Superfast compression library DENSITY is a free C99, open-source, BSD licensed compression library. It is focused on high-speed compression, a

Sep 13, 2022
data compression library for embedded/real-time systems

heatshrink A data compression/decompression library for embedded/real-time systems. Key Features: Low memory usage (as low as 50 bytes) It is useful f

Sep 23, 2022
A simple C library for compressing lists of integers using binary packing

The SIMDComp library A simple C library for compressing lists of integers using binary packing and SIMD instructions. The assumption is either that yo

Sep 21, 2022
Small strings compression library

SMAZ - compression for very small strings ----------------------------------------- Smaz is a simple compression library suitable for compressing ver

Sep 16, 2022
A portable, simple zip library written in C
A portable, simple zip library written in C

A portable (OSX/Linux/Windows), simple zip library written in C This is done by hacking awesome miniz library and layering functions on top of the min

Sep 22, 2022