Giter Site home page Giter Site logo

nayuta403 / magic_get Goto Github PK

View Code? Open in Web Editor NEW

This project forked from apolukhin/magic_get

0.0 0.0 0.0 1.13 MB

std::tuple like methods for user defined types without any macro or boilerplate code

Home Page: https://boost.org/libs/pfr

License: Boost Software License 1.0

Shell 0.83% C++ 97.03% Python 1.63% HTML 0.31% CMake 0.20%

magic_get's Introduction

Boost.PFR

This is a C++14 library for very basic reflection that gives you access to structure elements by index and provides other std::tuple like methods for user defined types without any macro or boilerplate code.

Boost.PFR is a part of the Boost C++ Libraries. However, Boost.PFR is a header only library that does not depend on Boost. You can just copy the content of the "include" folder from the github into your project, and the library will work fine.

For a version of the library without boost:: namespace see PFR.

Test results

Branches Build Tests coverage More info
Develop: CI Build status Coverage Status details...
Master: CI Build status Coverage Status details...

Latest developer documentation

Motivating Example #0

#include <iostream>
#include <fstream>
#include <string>

#include "boost/pfr.hpp"

struct some_person {
  std::string name;
  unsigned birth_year;
};

int main(int argc, const char* argv[]) {
  some_person val{"Edgar Allan Poe", 1809};

  std::cout << boost::pfr::get<0>(val)                // No macro!
      << " was born in " << boost::pfr::get<1>(val);  // Works with any aggregate initializables!

  if (argc > 1) {
    std::ofstream ofs(argv[1]);
    ofs << boost::pfr::io(val);                       // File now contains: {"Edgar Allan Poe", 1809}
  }
}

Outputs:

Edgar Allan Poe was born in 1809

Motivating Example #1

#include <iostream>
#include "boost/pfr.hpp"

struct my_struct { // no ostream operator defined!
    int i;
    char c;
    double d;
};

int main() {
    my_struct s{100, 'H', 3.141593};
    std::cout << "my_struct has " << boost::pfr::tuple_size<my_struct>::value
        << " fields: " << boost::pfr::io(s) << "\n";
}

Outputs:

my_struct has 3 fields: {100, H, 3.14159}

Motivating Example #2

#include <iostream>
#include "boost/pfr.hpp"

struct my_struct { // no ostream operator defined!
    std::string s;
    int i;
};

int main() {
    my_struct s{{"Das ist fantastisch!"}, 100};
    std::cout << "my_struct has " << boost::pfr::tuple_size<my_struct>::value
        << " fields: " << boost::pfr::io(s) << "\n";
}

Outputs:

my_struct has 2 fields: {"Das ist fantastisch!", 100}

Requirements and Limitations

See docs.

License

Distributed under the Boost Software License, Version 1.0.

magic_get's People

Contributors

abutcher-gh avatar akrzemi1 avatar alexey-romanov avatar apolukhin avatar bryant1410 avatar cbeck88 avatar denzor200 avatar eldiener avatar ericlemanissier avatar glenfe avatar jatinkh25 avatar kojoley avatar ofats avatar pdimov avatar zmij 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.