#ifndef __JEEP__JSTATUS__ #define __JEEP__JSTATUS__ #include #include #include #include #include #include "JLang/JEquals.hh" #include "JLang/JType.hh" #include "JLang/JVectorize.hh" #include "JIO/JSerialisable.hh" /** * \author mdejong */ namespace JEEP {} namespace JPP { using namespace JEEP; } namespace JEEP { using JLANG::JEquals; using JIO::JReader; using JIO::JWriter; /** * Auxiliary class for handling status. * * The various status are controlled using a bitwise data field.\n * The corresponding bits can externally be defined (e.g.\ via an enumeration). */ struct JStatus : public JEquals { /** * Default constructor. */ JStatus() : status(0) {} /** * Constructor. * * \param status status */ JStatus(const int status) : status(status) {} /** * Get status. * * \return status */ int getStatus() const { return this->status; } /** * Get status. * * \return status */ JStatus& getStatus() { return *this; } /** * Get status. * * \param mask mask * \return status */ int getStatus(const int mask) const { return (this->status & mask); } /** * Set status. * * \param status status */ void setStatus(const JStatus& status) { this->status = status.status; } /** * Equal method. * * \param status status * \result true if this status equal to given status; else false */ inline bool equals(const JStatus& status) const { return this->status == status.status; } /** * Test PMT status. * * \param bit bit */ bool has(const int bit) const { return (this->status & (1<status |= (1<status &= ~(1<>(std::istream& in, JStatus& status) { return in >> status.status; } /** * Write status to output. * * \param out output stream * \param status status * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JStatus& status) { return out << status.status; } /** * Read status from input. * * \param in reader * \param status status * \return reader */ friend inline JReader& operator>>(JReader& in, JStatus& status) { return in >> status.status; } /** * Write status to output. * * \param out writer * \param status status * \return writer */ friend inline JWriter& operator<<(JWriter& out, const JStatus& status) { return out << status.status; } protected: int status; }; /** * Auxiliary class to map key to status bit. */ struct JGetStatusBit : public std::map { /** * Get status bit. * * \param key key * \return bit */ int operator()(const std::string& key) const { return this->at(key); } }; /** * Auxiliary class to map status bit to key. */ struct JPutStatusBit : public std::map { /** * Constructor. * * \param input status bits */ JPutStatusBit(const JGetStatusBit& input) { using namespace std; for (JGetStatusBit::const_iterator i = input.begin(); i != input.end(); ++i) { this->insert(make_pair(i->second, i->first)); } } /** * Put status bit. * * \param bit bit * \return key */ const std::string& operator()(const int bit) const { return this->at(bit); } }; /** * Print status. * * \param out output stream * \param status status * \param helper helper */ inline void print(std::ostream& out, const JStatus& status, const JPutStatusBit& helper) { using namespace std; for (const auto& i : helper) { out << setw(24) << left << i.second << right << status.has(i.first) << endl; } } /** * Print status. * * \param out output stream * \param status status * \param helper helper */ inline void print(std::ostream& out, const JStatus& status, const JGetStatusBit& helper) { using namespace std; for (const auto& i : helper) { out << setw(24) << left << i.first << right << status.has(i.second) << endl; } } } #endif