All Projects → veselink1 → Refl Cpp

veselink1 / Refl Cpp

Licence: mit
A modern compile-time reflection library for C++ with support for overloads, templates, attributes and proxies

Programming Languages

cpp
1120 projects
metaprogramming
66 projects

Labels

Projects that are alternatives of or similar to Refl Cpp

proxy-pants
Secured and reliable Proxy based utilities for more or less common tasks.
Stars: ✭ 36 (-91.13%)
Mutual labels:  utilities
React Router Util
Useful components and utilities for working with React Router
Stars: ✭ 320 (-21.18%)
Mutual labels:  utilities
Sharpkeys
SharpKeys is a utility that manages a Registry key that allows Windows to remap one key to any other key.
Stars: ✭ 4,402 (+984.24%)
Mutual labels:  utilities
js-utils
A collection of dependency-free JavaScript utilities 🔧
Stars: ✭ 22 (-94.58%)
Mutual labels:  utilities
Go Model
Robust & Easy to use struct mapper and utility methods for Go
Stars: ✭ 295 (-27.34%)
Mutual labels:  utilities
Tqdm
A Fast, Extensible Progress Bar for Python and CLI
Stars: ✭ 20,632 (+4981.77%)
Mutual labels:  utilities
nimblenote
Simple keyboard-driven note taking application for macOS, Linux and Windows.
Stars: ✭ 31 (-92.36%)
Mutual labels:  utilities
Craig S Utility Library
A giant set of utility classes originally start back in the .Net 2.0 days and updated until .Net Core and .Net Standard became a thing. At which point I took the library and broke it up into a ton of smaller libraries. View my profile for more up to date versions of everything.
Stars: ✭ 397 (-2.22%)
Mutual labels:  utilities
Wipe Modules
🗑️ Easily remove the node_modules folder of non-active projects
Stars: ✭ 304 (-25.12%)
Mutual labels:  utilities
Gulp Check Unused Css
A build tool for checking your HTML templates for unused CSS classes
Stars: ✭ 366 (-9.85%)
Mutual labels:  utilities
Jcolor
An easy syntax to format your strings with colored fonts and backgrounds.
Stars: ✭ 255 (-37.19%)
Mutual labels:  utilities
Pycycle
Tool for pinpointing circular imports in Python. Find cyclic imports in any project
Stars: ✭ 278 (-31.53%)
Mutual labels:  utilities
Bigquery Utils
Useful scripts, udfs, views, and other utilities for migration and data warehouse operations in BigQuery.
Stars: ✭ 338 (-16.75%)
Mutual labels:  utilities
gumble
Collection of high-performance, thread-safe, lock-free data structures for go
Stars: ✭ 12 (-97.04%)
Mutual labels:  utilities
Date Fns Tz
Complementary library for date-fns v2 adding IANA time zone support
Stars: ✭ 385 (-5.17%)
Mutual labels:  utilities
PXDToolkit
A collection of Swift utility extensions and functions
Stars: ✭ 14 (-96.55%)
Mutual labels:  utilities
Haxor News
Browse Hacker News like a haxor: A Hacker News command line interface (CLI).
Stars: ✭ 3,342 (+723.15%)
Mutual labels:  utilities
Nest Router
Router Module For Nestjs Framework 🚦 🚀
Stars: ✭ 403 (-0.74%)
Mutual labels:  utilities
Dynamic Dark Mode
The smart, automatic Dark Mode toggle for macOS Mojave+
Stars: ✭ 397 (-2.22%)
Mutual labels:  utilities
Go Keyring
Cross-platform keyring interface for Go
Stars: ✭ 351 (-13.55%)
Mutual labels:  utilities

refl-cpp v0.12.1

Documentation

refl-cpp Logo

Contributors Activity CircleCI Gitter Patrons AUR version

refl-cpp encodes type metadata in the type system to allow compile-time reflection via constexpr and template metaprogramming in C++17

Introduction

refl-cpp aims to provide a generic static reflection system that can be extended to suit your needs. Runtime reflection can also be implemented on top of the existing system for when needed (see the examples).

Some nice things refl-cpp supports out-of-the-box:

  • custom attributes - encodes type-level and member-level attributes as a constexpr std::tuple associated with that item
  • proxy types - builds configurable generic types with identical member functions that can be hooked into to wrap or extend functionality
  • overloaded functions - wraps member functions in generic variadic templates
  • template functions - template function for which the template parameters can be inferred from the arguments are also supported
  • template types - uses template specialization to encode metadata for types; template types are perfectly-well supported

Code Example (see on Compiler Explorer)

A basic example showcasing how refl-cpp can be use to convert an arbitrary type to a tuple at compile-time

// Custom attribute to mark fields for validation
struct NonNegative : refl::attr::usage::member {};

struct Point {
    double x, y;
};

REFL_AUTO(
    type(Point),
    field(x, NonNegative()),
    field(y, NonNegative())
)

struct Circle {
    Point origin;
    double radius;
};

REFL_AUTO(
    type(Circle),
    field(origin),
    field(radius, NonNegative())
)

template <typename T>
constexpr bool checkNonNegaive(const T& obj) {
    // Get the type descriptor for T
    constexpr auto type = refl::reflect<T>();
    // Get the compile-time refl::type_list<...> of member descriptors
    constexpr auto members = get_members(type);
    // Filter out the non-readable members (not field or getters marked with the property() attribute)
    constexpr auto readableMembers = filter(members, [](auto member) { return is_readable(member); });

    auto invalidMemberCount = count_if(readableMembers, [&](auto member) {
        // Function-call syntax is a uniform way to get the value of a member (whether a field or a getter)
        auto&& value = member(obj);
        // Check if the NonNegative attribute is present
        if constexpr (refl::descriptor::has_attribute<NonNegative>(member)) {
            // And if so, make the necessary checks
            return value < 0;
        }
        // Recursively check the value of the member
        else if (!checkNonNegaive(value)) {
            return true;
        }
        return false;
    });

    return invalidMemberCount == 0;
}

// These all run at compile-time
constexpr Circle c1{ {0., 5.}, 100. };
static_assert(checkNonNegaive(c1));

constexpr Circle c2{ {0., 5.}, -100. };
static_assert(!checkNonNegaive(c2));

constexpr Circle c3{ {0., -5.}, 100. };
static_assert(!checkNonNegaive(c3));

Requirements

  • Minimum language standard: C++17

Usage

  • refl-cpp is packaged as a single-header library. #include "refl.hpp" like any other header.

Contributing

License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].