EleFits  5.3.0
A modern C++ API on top of CFITSIO
Loading...
Searching...
No Matches

◆ parse_struct_or() [1/2]

TOut parse_struct_or ( const Record< Ts > &...  fallbacks) const

Parse a sequence of records if they exist, return fallbacks for those which don't.

Parse a sequence of records.

Template Parameters
TOutThe return type
Ts...The desired record value types (automatically deduced)
TSeqThe record sequence type (automatically deduced)

Several methods are provided to return records or record values as a user-defined structure, instead of a std::vector or std::tuple. These methods differe from parse_n-prefixed methods in that the returned sequence is interpretted as a user-defined structure, provided that it can be constructed from a brace-enclosed list of Records or from a brace-enclosed list of record values. For example, the return type can be a mere structure like:

struct TOut {
T0 p0;
T1 p1;
T2 p2;
};

or a class with such a constructor:

TOut::TOut(T0 p0, T1 p1, T2 p2)

where T0, T1, T2 are record value types or Records.

The output structure can be used to mimic a named tuple, which is generally more convenient than a std::tuple, because the records or values are accessed as named parameters – e.g. tout.p1 – instead of being accessed by their indices – e.g. std::get<1>(tout).

Example usage:

struct Body {
int age;
float height;
float mass;
float bmi() const {
return mass / (height * height);
}
};
// Body can be constructed from a brace-enclosed list:
// Body body {name, age, height, mass};
auto body = hdu.parse_struct<Body>(
as<std::string>("NAME"),
as<int>("AGE"),
as<float>("HEIGHT"),
as<float>("MASS"));
std::cout << "Hello, " << body.name << "!" << std::endl;
std::cout << "Your BMI is: " << body.bmi() << std::endl;
T endl(T... args)