SDDSColumn.cpp 2.8 KB
Newer Older
1 2 3 4
#include "Structure/SDDSColumn.h"
#include "Utilities/OpalException.h"

#include <iomanip>
kraus's avatar
kraus committed
5
#include <list>
6 7 8 9

SDDSColumn::SDDSColumn(const std::string& name,
                       const std::string& type,
                       const std::string& unit,
10 11 12
                       const std::string& desc,
                       std::ios_base::fmtflags flags,
                       unsigned short prec):
13 14
    name_m(name),
    description_m(std::make_tuple(type, unit, desc)),
15 16
    writeFlags_m(flags),
    writePrecision_m(prec),
17
    set_m(false)
kraus's avatar
kraus committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
    std::list<std::ios_base::fmtflags> numericalBase({std::ios_base::dec,
                                                      std::ios_base::hex,
                                                      std::ios_base::oct});
    std::list<std::ios_base::fmtflags> floatFormat({std::ios_base::fixed,
                                                    std::ios_base::scientific});
    std::list<std::ios_base::fmtflags> adjustmentFlags({std::ios_base::internal,
                                                        std::ios_base::left,
                                                        std::ios_base::right});

    // This code ensures that for each group of flags only one flag is given
    for (std::ios_base::fmtflags flag: numericalBase) {
        if (writeFlags_m & flag) {
            writeFlags_m = (flag | (writeFlags_m & ~std::ios_base::basefield));
            break;
        }
    }
    for (std::ios_base::fmtflags flag: floatFormat) {
        if (writeFlags_m & flag) {
            writeFlags_m = (flag | (writeFlags_m & ~std::ios_base::floatfield));
            break;
        }
    }
    for (std::ios_base::fmtflags flag: adjustmentFlags) {
        if (writeFlags_m & flag) {
            writeFlags_m = (flag | (writeFlags_m & ~std::ios_base::adjustfield));
            break;
        }
    }

}
49 50


51 52 53
void SDDSColumn::writeHeader(std::ostream& os,
                             unsigned int colNr,
                             const std::string& indent) const {
54
    os << "&column\n"
55 56 57 58 59 60 61 62
       << indent << "name=" << name_m << ",\n"
       << indent << "type=" << std::get<0>(description_m) << ",\n";

    if (std::get<1>(description_m) != "")
        os << indent << "units=" << std::get<1>(description_m) << ",\n";

    os << indent << "description=\"" << colNr << " " << std::get<2>(description_m) << "\"\n"
       << "&end\n";
63 64 65 66 67
}


void SDDSColumn::writeValue(std::ostream& os) const {
    if (!set_m) {
frey_m's avatar
frey_m committed
68
        throw OpalException("SDDSColumn::writeValue",
69 70 71
                            "value for column '" + name_m + "' isn't set");
    }

kraus's avatar
kraus committed
72
    os.flags(writeFlags_m);
73
    os.precision(writePrecision_m);
74
    os << value_m << std::setw(10) << "\t";
kraus's avatar
kraus committed
75
    set_m = false;
76 77 78 79 80 81 82 83
}

std::ostream& operator<<(std::ostream& os,
                         const SDDSColumn& col) {
    col.writeValue(os);

    return os;
}