This is the header only library to work with aggregate types and tuple-like objects (std::tuple
, std::pair
and std::array
).
Features:
-
Get struct from tuple-like object
struct Person { std::string name; int age; double weight; }; ... std::tuple<std::string, int, double> tuple {"Peter", 10, 20}; Person person = tutils::from_tuple<Person>(tuple);
person
will contain valuesPeter
,10
,20
-
Write tuple-like objects to ostream
using tutils::operator <<; std::tuple<std::string, int, double> tuple {"Peter", 10, 20}; std::cout << tuple;
Output
{"Peter", 10, 20}
-
Put values from istream to tuple-like object
using tutils::operator <<; using tutils::operator >>; std::tuple<std::string, int> tuple; std::cin >> tuple; std::cout << tuple;
Input
Hello 5
Output
{"Hello", 5}
-
Get aggregate type fields count (since C++20)
std::cout << tutils::aggregate_size_v<Person>; or std::cout << tutils::aggregate_size<Person>::value;
Output
3
-
For each cycle for tuple-like object elements
using tutils::operator <<; void triple(int& n) { n *= 3; } ... std::tuple<int, int, int> tuple {1, 2, 3}; tutils::for_each(tuple, triple); std::cout << tuple;
Output
{3, 6, 9}
Put the headers utilities.hpp
and aggregate_size.hpp
to your project. Include the header utilities.hpp
. Nothing to compile
For run tests you need to install googletest
(for example from here) and put
googletest
folder to the tests
directory.
To build tests go to the tests
directory and run cmake CMakeLists.txt
and then make
.
Run Google_Tests_run
file to start the tests
C++17 (C++20 for tutils::aggregate_size
)
googletest
for run tests