]>
Commit | Line | Data |
---|---|---|
1e59de90 | 1 | # [Boost.PFR](https://boost.org/libs/pfr) |
20effc67 TL |
2 | |
3 | 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. | |
4 | ||
1e59de90 | 5 | [Boost.PFR](https://boost.org/libs/pfr) is a part of the [Boost C++ Libraries](https://github.com/boostorg). 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. |
20effc67 | 6 | |
1e59de90 | 7 | For a version of the library without `boost::` namespace see [PFR](https://github.com/apolukhin/pfr_non_boost). |
20effc67 TL |
8 | |
9 | ### Test results | |
10 | ||
11 | Branches | Build | Tests coverage | More info | |
12 | ----------------|-------------- | -------------- |----------- | |
1e59de90 TL |
13 | Develop: | [![CI](https://github.com/boostorg/pfr/actions/workflows/ci.yml/badge.svg?branch=develop)](https://github.com/boostorg/pfr/actions/workflows/ci.yml) [![Build status](https://ci.appveyor.com/api/projects/status/0mavmnkdmltcdmqa/branch/develop?svg=true)](https://ci.appveyor.com/project/apolukhin/pfr/branch/develop) | [![Coverage Status](https://coveralls.io/repos/github/apolukhin/magic_get/badge.png?branch=develop)](https://coveralls.io/github/apolukhin/magic_get?branch=develop) | [details...](https://www.boost.org/development/tests/develop/developer/pfr.html) |
14 | Master: | [![CI](https://github.com/boostorg/pfr/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/boostorg/pfr/actions/workflows/ci.yml) [![Build status](https://ci.appveyor.com/api/projects/status/0mavmnkdmltcdmqa/branch/master?svg=true)](https://ci.appveyor.com/project/apolukhin/pfr/branch/master) | [![Coverage Status](https://coveralls.io/repos/github/apolukhin/magic_get/badge.png?branch=master)](https://coveralls.io/github/apolukhin/magic_get?branch=master) | [details...](https://www.boost.org/development/tests/master/developer/pfr.html) | |
15 | ||
16 | [Latest developer documentation](https://www.boost.org/doc/libs/develop/doc/html/boost_pfr.html) | |
20effc67 TL |
17 | |
18 | ### Motivating Example #0 | |
19 | ```c++ | |
20 | #include <iostream> | |
21 | #include <fstream> | |
22 | #include <string> | |
23 | ||
24 | #include "boost/pfr.hpp" | |
25 | ||
26 | struct some_person { | |
27 | std::string name; | |
28 | unsigned birth_year; | |
29 | }; | |
30 | ||
31 | int main(int argc, const char* argv[]) { | |
32 | some_person val{"Edgar Allan Poe", 1809}; | |
33 | ||
34 | std::cout << boost::pfr::get<0>(val) // No macro! | |
35 | << " was born in " << boost::pfr::get<1>(val); // Works with any aggregate initializables! | |
36 | ||
37 | if (argc > 1) { | |
38 | std::ofstream ofs(argv[1]); | |
39 | ofs << boost::pfr::io(val); // File now contains: {"Edgar Allan Poe", 1809} | |
40 | } | |
41 | } | |
42 | ``` | |
43 | Outputs: | |
44 | ``` | |
45 | Edgar Allan Poe was born in 1809 | |
46 | ``` | |
47 | ||
48 | ||
49 | ### Motivating Example #1 | |
50 | ```c++ | |
51 | #include <iostream> | |
1e59de90 | 52 | #include "boost/pfr.hpp" |
20effc67 TL |
53 | |
54 | struct my_struct { // no ostream operator defined! | |
55 | int i; | |
56 | char c; | |
57 | double d; | |
58 | }; | |
59 | ||
60 | int main() { | |
61 | my_struct s{100, 'H', 3.141593}; | |
62 | std::cout << "my_struct has " << boost::pfr::tuple_size<my_struct>::value | |
63 | << " fields: " << boost::pfr::io(s) << "\n"; | |
64 | } | |
65 | ||
66 | ``` | |
67 | ||
68 | Outputs: | |
69 | ``` | |
70 | my_struct has 3 fields: {100, H, 3.14159} | |
71 | ``` | |
72 | ||
73 | ### Motivating Example #2 | |
74 | ||
75 | ```c++ | |
76 | #include <iostream> | |
1e59de90 | 77 | #include "boost/pfr.hpp" |
20effc67 TL |
78 | |
79 | struct my_struct { // no ostream operator defined! | |
80 | std::string s; | |
81 | int i; | |
82 | }; | |
83 | ||
84 | int main() { | |
85 | my_struct s{{"Das ist fantastisch!"}, 100}; | |
86 | std::cout << "my_struct has " << boost::pfr::tuple_size<my_struct>::value | |
87 | << " fields: " << boost::pfr::io(s) << "\n"; | |
88 | } | |
89 | ||
90 | ``` | |
91 | ||
92 | Outputs: | |
93 | ``` | |
94 | my_struct has 2 fields: {"Das ist fantastisch!", 100} | |
95 | ``` | |
96 | ||
97 | ||
98 | ### Requirements and Limitations | |
99 | ||
1e59de90 | 100 | [See docs](https://www.boost.org/doc/libs/develop/doc/html/boost_pfr.html). |
20effc67 TL |
101 | |
102 | ### License | |
103 | ||
1e59de90 | 104 | Distributed under the [Boost Software License, Version 1.0](https://boost.org/LICENSE_1_0.txt). |