Giter Site home page Giter Site logo

apolukhin / pfr_non_boost Goto Github PK

View Code? Open in Web Editor NEW
59.0 5.0 5.0 275 KB

Boost.PFR without the boost namespaces

Home Page: https://apolukhin.github.io/pfr_non_boost/

License: Boost Software License 1.0

CMake 0.12% C++ 96.60% HTML 0.16% Python 3.12%
reflection

pfr_non_boost's Introduction

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 "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 << pfr::get<0>(val)                // No macro!
      << " was born in " << pfr::get<1>(val);  // Works with any aggregate initializables!

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

Outputs:

Edgar Allan Poe was born in 1809

Run the above sample

Motivating Example #1

#include <iostream>
#include "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 " << pfr::tuple_size<my_struct>::value
        << " fields: " << pfr::io(s) << "\n";
}

Outputs:

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

Motivating Example #2

#include <iostream>
#include "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 " << pfr::tuple_size<my_struct>::value
        << " fields: " << pfr::io(s) << "\n";
}

Outputs:

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

Motivating Example #3

#include <iostream>
#include <string>

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_boost_pfr.hpp>

#include "pfr/io.hpp"

namespace x3 = boost::spirit::x3;

struct ast_employee { // No BOOST_FUSION_ADAPT_STRUCT defined
    int age;
    std::string forename;
    std::string surname;
    double salary;
};

auto const quoted_string = x3::lexeme['"' >> +(x3::ascii::char_ - '"') >> '"'];

x3::rule<class employee, ast_employee> const employee = "employee";
auto const employee_def =
    x3::lit("employee")
    >> '{'
    >>  x3::int_ >> ','
    >>  quoted_string >> ','
    >>  quoted_string >> ','
    >>  x3::double_
    >>  '}'
    ;
BOOST_SPIRIT_DEFINE(employee);

int main() {
    std::string str = R"(employee{34, "Chip", "Douglas", 2500.00})";
    ast_employee emp;
    x3::phrase_parse(str.begin(),
                     str.end(),
                     employee,
                     x3::ascii::space,
                     emp);
    std::cout << pfr::io(emp) << std::endl;
}

Outputs:

(34 Chip Douglas 2500)

Requirements and Limitations

See docs.

License

Distributed under the Boost Software License, Version 1.0.

pfr_non_boost's People

Contributors

apolukhin avatar

Stargazers

Michael Galuza avatar  avatar Taehwan Kim avatar Jiawen (Kevin) Chen avatar DD avatar Aaron Appel avatar TaoTao avatar Arthur avatar  avatar Francisco Gallego avatar Ian Prest avatar Cppshizoid avatar Alejandro Exojo avatar  avatar Geoffrey Gunter avatar  avatar whs31 avatar George L. Albany avatar Jonathan R. Madsen avatar Kyle Collins avatar Axel PASCON avatar BLUELOVETH avatar  avatar Mengnan Yang  avatar Zhao Xiaohong avatar Hwanhee "Asher" Kim avatar Alex avatar Milan Svoboda avatar  avatar Guillaume Vincke avatar  avatar Nick Hayashi avatar Tomashu avatar Ali Can Demiralp avatar Konstantinos Chatzilygeroudis avatar Sergey Makeev avatar eri avatar  avatar  avatar Fabio avatar  avatar Dalton Messmer avatar Benjamin Wiberg avatar Krayfaus avatar  avatar Sergei Bastrakov avatar Pavel Misko avatar Nikola Šobajić avatar Vadim Dmitriev avatar Mikhail Mitkevich avatar Celestin de Villa avatar 西风逍遥游 avatar Olivier Le Doeuff avatar M. Galib Uludag avatar  avatar Li Zhang avatar Konstantin Khomyakov avatar  avatar  avatar

Watchers

James Cloos avatar  avatar  avatar  avatar  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.