Code Monkey home page Code Monkey logo

refl-cpp's Introduction

refl-cpp v0.12.1

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

refl-cpp's People

Contributors

veselink1 avatar khmiller-globus avatar siapran avatar james-conrad avatar otreblan avatar ticelo 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.