/** * @module cdatn * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) * @brief Datatype template and datatype definition for CCPU and CMem * @date 26.05.2009 */ #ifndef CDATN_H #define CDATN_H 1 #include #include /** * @class CDatN * * Datatype template for CCPU and CMem. */ class CDatN : public boost::operators { private: /** * @method CDatN * @brief Default ctor * @param - * @return - * @globalvars none * @exception bad_alloc * @pre none * @post none */ CDatN() {} public: /** * @method ~CDatN * @brief Default dtor * @param - * @return - * @globalvars none * @exception none * @pre none * @post none */ virtual ~CDatN() {} /** * @method CDatN * @brief Copy constructor for CDatN * @param other reference to CDatN which will be copied * @return - * @globalvars none * @exception none * @pre none * @post none */ CDatN(const CDatN& other) : m_value(other.m_value), m_width(other.m_width) {} /** * @method CDatN * @brief Copy constructor for int * @param newval new value for CDatN * @param width maximum width * @return - * @globalvars none * @exception std::runtime_error * @pre none * @post none */ CDatN(const int newval, unsigned width = 31) : m_value(((1 << width) - 1) & newval), m_width(width) { if (width < 2 || width > 32) throw std::runtime_error("width must be between 2 and 32"); } /** * @method getValue * @brief returns value of CDatN * @param - * @return value of CDatN * @globalvars none * @exception none * @pre none * @post none */ int getValue() const { return m_value; } /** * @method operator int * @brief convert to int * @param - * @return int * @globalvars none * @exception none * @pre none * @post none */ operator int() { return m_value; } /** * @method operator< * @brief implementation of operator < * @param x reference to CDatN * @return true if cdat is less than object x * @globalvars none * @exception none * @pre none * @post none */ bool operator<(const CDatN& x) const { return m_value < x.m_value; } /** * @method operator== * @brief implementation of operator == * @param x reference to CDatN * @return true if cdat equals object x * @globalvars none * @exception none * @pre none * @post none */ bool operator==(const CDatN& x) const { return m_value == x.m_value; } /** * @method operator= * @brief implementation of operator = * @param newval reference to int * @return refecence to int * @globalvars none * @exception none * @pre none * @post none */ CDatN &operator=(const int& newval) { m_value = ((1 << m_width) - 1) & newval; return *this; } /** * @method operator+= * @brief implementation of operator += * @param x reference to CDatN * @return refecence to CDatN * @globalvars none * @exception none * @pre none * @post none */ CDatN& operator+=(const CDatN& x) { m_value = ((1 << m_width) - 1) & (m_value + x.m_value); return *this; } /** * @method operator-= * @brief implementation of operator -= * @param x reference to CDatN * @return refecence to CDatN * @globalvars none * @exception none * @pre none * @post none */ CDatN& operator-=(const CDatN& x) { m_value = ((1 << m_width) - 1) & (m_value - x.m_value); return *this; } /** * @method operator*= * @brief implementation of operator *= * @param x reference to CDatN * @return refecence to CDatN * @globalvars none * @exception none * @pre none * @post none */ CDatN& operator*=(const CDatN& x) { m_value = ((1 << m_width) - 1) & (m_value * x.m_value); return *this; } /** * @method operator/= * @brief implementation of operator /= * @param x reference to CDatN * @return refecence to CDatN * @globalvars none * @exception none * @pre none * @post none */ CDatN& operator/=(const CDatN& x) { m_value = ((1 << m_width) - 1) & (m_value / x.m_value); return *this; } /** * @method operator%= * @brief implementation of operator %= * @param x reference to CDatN * @return refecence to CDatN * @globalvars none * @exception none * @pre none * @post none */ CDatN& operator%=(const CDatN& x) { m_value = ((1 << m_width) - 1) & (m_value % x.m_value); return *this; } /** * @method operator|= * @brief implementation of operator |= * @param x reference to CDatN * @return refecence to CDatN * @globalvars none * @exception none * @pre none * @post none */ CDatN& operator|=(const CDatN& x) { m_value = ((1 << m_width) - 1) & (m_value | x.m_value); return *this; } /** * @method operator&= * @brief implementation of operator &= * @param x reference to CDatN * @return refecence to CDatN * @globalvars none * @exception none * @pre none * @post none */ CDatN& operator&=(const CDatN& x) { m_value = ((1 << m_width) - 1) & (m_value & x.m_value); return *this; } /** * @method operator^= * @brief implementation of operator ^= * @param x reference to CDatN * @return refecence to CDatN * @globalvars none * @exception none * @pre none * @post none */ CDatN& operator^=(const CDatN& x) { m_value = ((1 << m_width) - 1) & (m_value ^ x.m_value); return *this; } /** * @method operator++ * @brief implementation of operator ++ * @param - * @return refecence to CDatN * @globalvars none * @exception none * @pre none * @post none */ CDatN& operator++() { m_value = ((1 << m_width) - 1) & (m_value + 1); return *this; } /** * @method operator-- * @brief implementation of operator -- * @param - * @return refecence to CDatN * @globalvars none * @exception none * @pre none * @post none */ CDatN& operator--() { m_value--; return *this; } /** * @method operator<< * @brief Shift/output operator for outputstream * @param stream reference to outputstream * @param cdat object which will be printed to stream * @return reference to outputstream * @globalvars none * @exception none * @pre none * @post none */ friend std::ostream& operator<<(std::ostream& stream, CDatN cdat) { stream << cdat.m_value; return stream; } /** * @method operator>> * @brief Shift/read operator for inputstream * @param stream reference to inputstream * @param cdat reference to object which will be read from stream * @return reference to inputstream * @globalvars none * @exception none * @pre none * @post none */ friend std::istream& operator>>(std::istream & stream, CDatN& cdat) { stream >> cdat.m_value; cdat.m_value = ((1 << cdat.m_width) - 1) & cdat.m_value; return stream; } protected: /* members */ /** internal value of datatype */ int m_value; /** width of datatype */ unsigned m_width; }; #endif /* vim: set et sw=2 ts=2: */