Download | Plain Text | No Line Numbers


  1. /**
  2.  * @module cdatn
  3.  * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
  4.  * @brief Datatype template and datatype definition for CCPU and CMem
  5.  * @date 26.05.2009
  6.  */
  7.  
  8. #ifndef CDATN_H
  9. #define CDATN_H 1
  10.  
  11. #include <boost/operators.hpp>
  12. #include <iostream>
  13.  
  14. /**
  15.  * @class CDatN
  16.  *
  17.  * Datatype template for CCPU and CMem.
  18.  */
  19. class CDatN
  20. : public boost::operators<CDatN>
  21. {
  22. private:
  23. /**
  24.   * @method CDatN
  25.   * @brief Default ctor
  26.   * @param -
  27.   * @return -
  28.   * @globalvars none
  29.   * @exception bad_alloc
  30.   * @pre none
  31.   * @post none
  32.   */
  33. CDatN()
  34. {}
  35.  
  36. public:
  37. /**
  38.   * @method ~CDatN
  39.   * @brief Default dtor
  40.   * @param -
  41.   * @return -
  42.   * @globalvars none
  43.   * @exception none
  44.   * @pre none
  45.   * @post none
  46.   */
  47. virtual ~CDatN()
  48. {}
  49.  
  50. /**
  51.   * @method CDatN
  52.   * @brief Copy constructor for CDatN
  53.   * @param other reference to CDatN which will be copied
  54.   * @return -
  55.   * @globalvars none
  56.   * @exception none
  57.   * @pre none
  58.   * @post none
  59.   */
  60. CDatN(const CDatN& other)
  61. : m_value(other.m_value), m_width(other.m_width)
  62. {}
  63.  
  64. /**
  65.   * @method CDatN
  66.   * @brief Copy constructor for int
  67.   * @param newval new value for CDatN
  68.   * @param width maximum width
  69.   * @return -
  70.   * @globalvars none
  71.   * @exception std::runtime_error
  72.   * @pre none
  73.   * @post none
  74.   */
  75. CDatN(const int newval, unsigned width = 31)
  76. : m_value(((1 << width) - 1) & newval), m_width(width)
  77. {
  78. if (width < 2 || width > 32)
  79. throw std::runtime_error("width must be between 2 and 32");
  80. }
  81.  
  82. /**
  83.   * @method getValue
  84.   * @brief returns value of CDatN
  85.   * @param -
  86.   * @return value of CDatN
  87.   * @globalvars none
  88.   * @exception none
  89.   * @pre none
  90.   * @post none
  91.   */
  92. int getValue() const
  93. {
  94. return m_value;
  95. }
  96.  
  97. /**
  98.   * @method operator int
  99.   * @brief convert to int
  100.   * @param -
  101.   * @return int
  102.   * @globalvars none
  103.   * @exception none
  104.   * @pre none
  105.   * @post none
  106.   */
  107. operator int()
  108. {
  109. return m_value;
  110. }
  111.  
  112. /**
  113.   * @method operator<
  114.   * @brief implementation of operator <
  115.   * @param x reference to CDatN
  116.   * @return true if cdat is less than object x
  117.   * @globalvars none
  118.   * @exception none
  119.   * @pre none
  120.   * @post none
  121.   */
  122. bool operator<(const CDatN& x) const
  123. {
  124. return m_value < x.m_value;
  125. }
  126.  
  127. /**
  128.   * @method operator==
  129.   * @brief implementation of operator ==
  130.   * @param x reference to CDatN
  131.   * @return true if cdat equals object x
  132.   * @globalvars none
  133.   * @exception none
  134.   * @pre none
  135.   * @post none
  136.   */
  137. bool operator==(const CDatN& x) const
  138. {
  139. return m_value == x.m_value;
  140. }
  141.  
  142. /**
  143.   * @method operator=
  144.   * @brief implementation of operator =
  145.   * @param newval reference to int
  146.   * @return refecence to int
  147.   * @globalvars none
  148.   * @exception none
  149.   * @pre none
  150.   * @post none
  151.   */
  152. CDatN &operator=(const int& newval)
  153. {
  154. m_value = ((1 << m_width) - 1) & newval;
  155. return *this;
  156. }
  157.  
  158. /**
  159.   * @method operator+=
  160.   * @brief implementation of operator +=
  161.   * @param x reference to CDatN
  162.   * @return refecence to CDatN
  163.   * @globalvars none
  164.   * @exception none
  165.   * @pre none
  166.   * @post none
  167.   */
  168. CDatN& operator+=(const CDatN& x)
  169. {
  170. m_value = ((1 << m_width) - 1) & (m_value + x.m_value);
  171. return *this;
  172. }
  173.  
  174. /**
  175.   * @method operator-=
  176.   * @brief implementation of operator -=
  177.   * @param x reference to CDatN
  178.   * @return refecence to CDatN
  179.   * @globalvars none
  180.   * @exception none
  181.   * @pre none
  182.   * @post none
  183.   */
  184. CDatN& operator-=(const CDatN& x)
  185. {
  186. m_value = ((1 << m_width) - 1) & (m_value - x.m_value);
  187. return *this;
  188. }
  189.  
  190. /**
  191.   * @method operator*=
  192.   * @brief implementation of operator *=
  193.   * @param x reference to CDatN
  194.   * @return refecence to CDatN
  195.   * @globalvars none
  196.   * @exception none
  197.   * @pre none
  198.   * @post none
  199.   */
  200. CDatN& operator*=(const CDatN& x)
  201. {
  202. m_value = ((1 << m_width) - 1) & (m_value * x.m_value);
  203. return *this;
  204. }
  205.  
  206. /**
  207.   * @method operator/=
  208.   * @brief implementation of operator /=
  209.   * @param x reference to CDatN
  210.   * @return refecence to CDatN
  211.   * @globalvars none
  212.   * @exception none
  213.   * @pre none
  214.   * @post none
  215.   */
  216. CDatN& operator/=(const CDatN& x)
  217. {
  218. m_value = ((1 << m_width) - 1) & (m_value / x.m_value);
  219. return *this;
  220. }
  221.  
  222. /**
  223.   * @method operator%=
  224.   * @brief implementation of operator %=
  225.   * @param x reference to CDatN
  226.   * @return refecence to CDatN
  227.   * @globalvars none
  228.   * @exception none
  229.   * @pre none
  230.   * @post none
  231.   */
  232. CDatN& operator%=(const CDatN& x)
  233. {
  234. m_value = ((1 << m_width) - 1) & (m_value % x.m_value);
  235. return *this;
  236. }
  237.  
  238. /**
  239.   * @method operator|=
  240.   * @brief implementation of operator |=
  241.   * @param x reference to CDatN
  242.   * @return refecence to CDatN
  243.   * @globalvars none
  244.   * @exception none
  245.   * @pre none
  246.   * @post none
  247.   */
  248. CDatN& operator|=(const CDatN& x)
  249. {
  250. m_value = ((1 << m_width) - 1) & (m_value | x.m_value);
  251. return *this;
  252. }
  253.  
  254. /**
  255.   * @method operator&=
  256.   * @brief implementation of operator &=
  257.   * @param x reference to CDatN
  258.   * @return refecence to CDatN
  259.   * @globalvars none
  260.   * @exception none
  261.   * @pre none
  262.   * @post none
  263.   */
  264. CDatN& operator&=(const CDatN& x)
  265. {
  266. m_value = ((1 << m_width) - 1) & (m_value & x.m_value);
  267. return *this;
  268. }
  269.  
  270. /**
  271.   * @method operator^=
  272.   * @brief implementation of operator ^=
  273.   * @param x reference to CDatN
  274.   * @return refecence to CDatN
  275.   * @globalvars none
  276.   * @exception none
  277.   * @pre none
  278.   * @post none
  279.   */
  280. CDatN& operator^=(const CDatN& x)
  281. {
  282. m_value = ((1 << m_width) - 1) & (m_value ^ x.m_value);
  283. return *this;
  284. }
  285.  
  286. /**
  287.   * @method operator++
  288.   * @brief implementation of operator ++
  289.   * @param -
  290.   * @return refecence to CDatN
  291.   * @globalvars none
  292.   * @exception none
  293.   * @pre none
  294.   * @post none
  295.   */
  296. CDatN& operator++()
  297. {
  298. m_value = ((1 << m_width) - 1) & (m_value + 1);
  299. return *this;
  300. }
  301.  
  302. /**
  303.   * @method operator--
  304.   * @brief implementation of operator --
  305.   * @param -
  306.   * @return refecence to CDatN
  307.   * @globalvars none
  308.   * @exception none
  309.   * @pre none
  310.   * @post none
  311.   */
  312. CDatN& operator--()
  313. {
  314. m_value--;
  315. return *this;
  316. }
  317.  
  318. /**
  319.   * @method operator<<
  320.   * @brief Shift/output operator for outputstream
  321.   * @param stream reference to outputstream
  322.   * @param cdat object which will be printed to stream
  323.   * @return reference to outputstream
  324.   * @globalvars none
  325.   * @exception none
  326.   * @pre none
  327.   * @post none
  328.   */
  329. friend std::ostream& operator<<(std::ostream& stream, CDatN cdat)
  330. {
  331. stream << cdat.m_value;
  332. return stream;
  333. }
  334.  
  335. /**
  336.   * @method operator>>
  337.   * @brief Shift/read operator for inputstream
  338.   * @param stream reference to inputstream
  339.   * @param cdat reference to object which will be read from stream
  340.   * @return reference to inputstream
  341.   * @globalvars none
  342.   * @exception none
  343.   * @pre none
  344.   * @post none
  345.   */
  346. friend std::istream& operator>>(std::istream & stream, CDatN& cdat)
  347. {
  348. stream >> cdat.m_value;
  349. cdat.m_value = ((1 << cdat.m_width) - 1) & cdat.m_value;
  350. return stream;
  351. }
  352.  
  353. protected:
  354. /* members */
  355. /** internal value of datatype */
  356. int m_value;
  357. /** width of datatype */
  358. unsigned m_width;
  359. };
  360.  
  361. #endif
  362.  
  363. /* vim: set et sw=2 ts=2: */
  364.