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 10.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. #include <boost/lexical_cast.hpp>
  16. #ifdef DEBUG
  17. # include <iostream>
  18. # include <iomanip>
  19. #endif
  20. #include "cdat.h"
  21.  
  22. /**
  23.  * @class CVectorMem
  24.  *
  25.  * Extends std::vector template for use as memory for CCPU.
  26.  */
  27. template <class T, class Allocator=std::allocator<T> >
  28. class CVectorMem
  29. : public std::vector<T, Allocator>
  30. {
  31. public:
  32. using std::vector<T, Allocator>::size;
  33. using std::vector<T, Allocator>::at;
  34.  
  35. /**
  36.   * @method initialize
  37.   * @brief initialize the vector with the content of istream. istream is
  38.   * read per line. empty lines will add unitialized elements.
  39.   * @param in inputstream to read from
  40.   * @return void
  41.   * @globalvars none
  42.   * @exception std::runtime_error
  43.   * @conditions none
  44.   */
  45. void initialize(std::istream& in)
  46. {
  47. if (!in.good())
  48. return;
  49.  
  50. std::string line;
  51. unsigned i = 0;
  52. while (!in.eof() && in.good())
  53. {
  54. ++i;
  55. std::getline(in, line);
  56.  
  57. /* skip last line if it's empty */
  58. if (line.empty() && in.eof())
  59. break;
  60.  
  61. T value;
  62. try
  63. {
  64. if (!line.empty())
  65. value = boost::lexical_cast<T>(line);
  66. }
  67. catch(boost::bad_lexical_cast& ex)
  68. {
  69. std::stringstream sstr;
  70. sstr << "Unable to convert input (line " << i << "): " << ex.what();
  71. throw std::runtime_error(sstr.str());
  72. }
  73.  
  74. push_back(value);
  75. }
  76. }
  77.  
  78. #if DEBUG
  79. /**
  80.   * @method dump
  81.   * @brief dumps contents of vector to outputstream
  82.   * @param out outputstream to write to
  83.   * @return void
  84.   * @globalvars none
  85.   * @exception none
  86.   * @conditions none
  87.   */
  88. void dump(std::ostream& out)
  89. {
  90. out << "[MEMORY DUMP]" << std::endl;
  91. for(unsigned i = 0; i < size(); ++i)
  92. {
  93. out << "[" << std::setw(4) << std::setfill('0') << i << "] "
  94. << at(i) << std::endl;
  95. }
  96. }
  97. #endif
  98. };
  99.  
  100. /**
  101.  * @class CMem
  102.  *
  103.  * Memory definition for CCPU
  104.  */
  105. typedef CVectorMem<CDat> CMem;
  106.  
  107. #endif
  108.  
  109. /* vim: set et sw=2 ts=2: */
  110.