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