2
0
mirror of https://github.com/boostorg/pfr.git synced 2026-01-19 04:22:13 +00:00

More functions, more assertions, enums support, more tests and better docs

This commit is contained in:
Antony Polukhin
2016-04-07 21:02:21 +03:00
parent 3520c3e84e
commit 4005ef56f8
3 changed files with 384 additions and 48 deletions

View File

@@ -129,6 +129,81 @@ auto flat_tie(T& val, typename std::enable_if< std::is_trivially_assignable<T, T
* Static variables are ignored
### Cool usecase examples
* Auto generate comparisons:
```c++
namespace pod_ops {
template <class T1, class T2>
inline bool operator==(const T1& lhs, const T2& rhs) {
return flat_make_tuple(lhs) == flat_make_tuple(rhs);
}
template <class T1, class T2>
inline bool operator!=(const T1& lhs, const T2& rhs) {
return flat_make_tuple(lhs) != flat_make_tuple(rhs);
}
template <class T1, class T2>
inline bool operator<(const T1& lhs, const T2& rhs) {
return flat_make_tuple(lhs) < flat_make_tuple(rhs);
}
template <class T1, class T2>
inline bool operator>(const T1& lhs, const T2& rhs) {
return flat_make_tuple(lhs) > flat_make_tuple(rhs);
}
} // pod_ops
// ...
struct comparable_struct {
int i; short s; char data[7]; bool bl; int a,b,c,d,e,f;
};
using namespace pod_ops;
comparable_struct s1 {0, 1, "Hello", false, 6,7,8,9,10,11};
comparable_struct s2 {0, 1, "Hello", false, 6,7,8,9,10,11111};
assert(s1 == s1);
assert(s1 < sq);
```
* Universal ostream operator<<:
```c++
template <std::size_t I, std::size_t N>
struct print_impl {
template <class T>
static void print (std::ostream& out, const T& value) {
if (!!I) out << ", ";
out << flat_get<I>(value);
print_impl<I + 1, N>::print(out, value);
}
};
template <std::size_t I>
struct print_impl<I, I> {
template <class T> static void print (std::ostream&, const T&) {}
};
template <class Ostreamable, class T>
typename std::enable_if<std::is_pod<T>::value, Ostreamable& >::type
operator<<(Ostreamable& out, const T& value)
{
out << boost::typeindex::type_id<T>().pretty_name() << " { ";
print_impl<0, flat_tuple_size_v<T> >::print(out, value);
out << " };";
return out;
}
// ...
comparable_struct s1 {0, 1, "Hello", false, 6,7,8,9,10,11};
std::cout << s1 << std::endl; // Outputs: { 0, 1, H, e, l, l, o, , , 0, 6, 7, 8, 9, 10, 11 }
```
### License
Distributed under the [Boost Software License, Version 1.0](http://boost.org/LICENSE_1_0.txt).