tschoonj / xraylib

A library for X-ray matter interaction cross sections for X-ray fluorescence applications
https://github.com/tschoonj/xraylib/wiki
Other
120 stars 54 forks source link

Update c++ example to use c++11 features, std lib, and error handling #134

Closed mschollmeier closed 3 years ago

mschollmeier commented 3 years ago

I've updated the c++ example 6 to include std library features (e.g., unique_ptr, auto, cout, ...) and added rudimentary error handling. For example:

/*parser test for Ca(HCO3)2 (calcium bicarbonate)*/
auto cdtest = unique_ptr<compoundData>(CompoundParser("Ca(HCO3)2", &err));
if (cdtest) {
    cout << "Ca(HCO3)2 contains " << cdtest->nAtomsAll << " atoms, " << cdtest->nElements 
         << " elements and has a molar mass of " << cdtest->molarMass << " g/mol" << endl;
    for (int i = 0; i < cdtest->nElements; i++)
        cout << "  Element " << cdtest->Elements[i] << ": " 
             << cdtest->massFractions[i] * 100.0 << "% and " 
             << cdtest->nAtoms[i] << " atoms" << endl;
}
else
    throw errorstring(__LINE__, *err);

If you're interested I can upload and create a pull request.

tschoonj commented 3 years ago

Yep that looks cool. Feel free to open a PR.

Thanks!

tschoonj commented 3 years ago

Closed in #139 and #144