Download | Plain Text | No Line Numbers


  1. /**
  2.  * @module cmem
  3.  * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
  4.  * @brief Memory template and memory definition for CCPU
  5.  * @date 26.05.2009
  6.  */
  7.  
  8. #ifndef CMEM_H
  9. #define CMEM_H 1
  10.  
  11. #include <vector>
  12. #include <istream>
  13. #include <sstream>
  14. #include <stdexcept>
  15. #ifdef DEBUG
  16. # include <iostream>
  17. # include <iomanip>
  18. #endif
  19.  
  20. /**
  21.  * @class CMemError
  22.  *
  23.  * Exception thrown by implemententations of CMem
  24.  */
  25. class CMemError
  26. : public std::invalid_argument
  27. {
  28. public:
  29. /**
  30.   * @method CMemError
  31.   * @brief Default exception ctor
  32.   * @param what message to pass along
  33.   * @return -
  34.   * @globalvars none
  35.   * @exception none
  36.   * @pre none
  37.   * @post none
  38.   */
  39. CMemError(const std::string& what)
  40. : std::invalid_argument(what)
  41. {}
  42. };
  43.  
  44. /**
  45.  * @class CMem
  46.  *
  47.  * Extends std::vector template for use as memory for CCPU.
  48.  */
  49. template <class T>
  50. class CMem
  51. : public std::vector<T>
  52. {
  53. typedef std::vector<T> super;
  54. typedef typename super::iterator iterator;
  55. using super::size;
  56. using super::begin;
  57. using super::end;
  58.  
  59. public:
  60. /**
  61.   * @method initialize
  62.   * @brief initialize the vector with the content of istream. istream is
  63.   * read per line. empty lines will add unitialized elements.
  64.   * @param in inputstream to read from
  65.   * @param datatype reference instance of datatype to copy from
  66.   * @return void
  67.   * @globalvars none
  68.   * @exception CMemError
  69.   * @pre none
  70.   * @post none
  71.   */
  72. void initialize(std::istream& in, T& datatype)
  73. {
  74. if (!in.good())
  75. return;
  76.  
  77. std::string line;
  78. unsigned i = 0;
  79. while (!in.eof() && in.good())
  80. {
  81. ++i;
  82. std::getline(in, line);
  83.  
  84. /* skip last line if it's empty */
  85. if (line.empty() && in.eof())
  86. break;
  87.  
  88. T value(datatype);
  89. if (!line.empty())
  90. {
  91. /* simple boost::lexical_cast replacement */
  92. std::stringstream interpreter;
  93. if(!(interpreter << line && interpreter >> value && interpreter.get() == std::char_traits<char>::eof()))
  94. {
  95. std::stringstream sstr;
  96. sstr << "Unable to convert input (line " << i << ") to datatype";
  97. throw CMemError(sstr.str());
  98. }
  99. }
  100.  
  101. push_back(value);
  102. }
  103. }
  104.  
  105. #if DEBUG
  106. /**
  107.   * @method dump
  108.   * @brief dumps contents of vector to outputstream
  109.   * @param out outputstream to write to
  110.   * @return void
  111.   * @globalvars none
  112.   * @exception none
  113.   * @pre none
  114.   * @post none
  115.   */
  116. void dump(std::ostream& out)
  117. {
  118. out << "[MEMORY DUMP]" << std::endl;
  119. unsigned i = 0;
  120. for(iterator it = begin(); it != end(); ++it)
  121. {
  122. out << "[" << std::setw(4) << std::setfill('0') << i << "] "
  123. << *it << std::endl;
  124. ++i;
  125. }
  126. }
  127. #endif
  128. };
  129.  
  130. #endif
  131.  
  132. /* vim: set et sw=2 ts=2: */
  133.