Giter Club home page Giter Club logo

pod_reflection's Introduction

pod_reflection

This is a simple C++ one-header STL based library implementing reflection for POD data types.

Limitations:

C++ itself does not provide adequate means for reflection, so this library's implementation is based around various template metaprogramming tools and hacks. Though this code is written according to the standard, there are some C++ features that make it impossible to use this library. Here is the list of known limitations:

  • Structs with default values for elements: in C++ 11 structure is not considered as a POD in case it has default initializations for any of its elements. In C++ 14 a structure is allowed to have default values for elements and to remain a POD.
  • Structs with bitfields: It is impossible to get an address of a bitfield member of class in C++. Accessing a first bitfield element of an addressable location is possible, but is still considered UB. So it is restricted to use (no valid results will be provided) eld::get and eld::for_each on PODS that contain bitfields.
  • Structs with fixed size arrays: it seems to be impossible to deduce that an element is a fixed size array because arrays can only be initialized only with brace initialization. Thus an attempt to deduce an element will yield a type of an array element. Structs with fixed size array elements are prohibited from usage with eld::get, eld::for_each

Public API:

The following public functions are implemented within the library: include/pod_reflection/pod_reflection.h - new header to be used.

  • eld::pod_size<POD> - get size of a POD structure at compile time
  • eld::pod_element_t<I, POD, TupleFeed> - get a type of an Ith element of a POD structure. Tuple with feed types TupleFeed is required to deduce the Ith element type.
  • deduced& get<I, TupleFeed>(POD& pod) - extract the Ith element of POD using TupleFeed. This function requires testing for a particular compiler since it utilizes UB (reinterpret_cast from an offset).-
  • int eld::for_each<TupleFeed>(POD& pod, Callable &&callable) - one of the most useful functions in practice. For each element in POD object calls a callable object. The most obvious use case as can be seen from example - is to log (print) the contents of a POD structure. The most useful case which this library can be used - automatic endian conversion for each of the POD's element.

Example usage:

See example/main.cpp. In your CMakeLists.txt declare: target_link_libraries(YourTarget PRIVATE eld::pod_reflection)

Contributing:

This library requires automated testing with CI integration. Any help is appreciated.

pod_reflection's People

Contributors

sergio-eld 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.