mycpu/cmem.h

00001 
00008 #ifndef CMEM_H
00009 #define CMEM_H 1
00010 
00011 #include <vector>
00012 #include <istream>
00013 #include <sstream>
00014 #include <stdexcept>
00015 #include <boost/lexical_cast.hpp>
00016 #ifdef DEBUG
00017 # include <iostream>
00018 # include <iomanip>
00019 #endif
00020 #include "cdat.h"
00021 
00027 template <class T, class Allocator=std::allocator<T> >
00028 class CVectorMem
00029   : public std::vector<T, Allocator>
00030 {
00031   public:
00032     using std::vector<T, Allocator>::size;
00033     using std::vector<T, Allocator>::at;
00034 
00045     void initialize(std::istream& in)
00046     {
00047       if (!in.good())
00048         return;
00049 
00050       std::string line;
00051       unsigned i = 0;
00052       while (!in.eof() && in.good())
00053       {
00054         ++i;
00055         std::getline(in, line);
00056 
00057         /* skip last line if it's empty */
00058         if (line.empty() && in.eof())
00059           break;
00060 
00061         T value;
00062         try
00063         {
00064           if (!line.empty())
00065             value = boost::lexical_cast<T>(line);
00066         }
00067         catch(boost::bad_lexical_cast& ex)
00068         {
00069           std::stringstream sstr;
00070           sstr << "Unable to convert input (line " << i << "): " << ex.what();
00071           throw std::runtime_error(sstr.str());
00072         }
00073 
00074         push_back(value);
00075       }
00076     }
00077 
00078 #if DEBUG
00079 
00088     void dump(std::ostream& out)
00089     {
00090       out << "[MEMORY DUMP]" << std::endl;
00091       for(unsigned i = 0; i < size(); ++i)
00092       {
00093         out << "[" << std::setw(4) << std::setfill('0') << i << "]  "
00094             << at(i) << std::endl;
00095       }
00096     }
00097 #endif
00098 };
00099 
00105 typedef CVectorMem<CDat> CMem;
00106 
00107 #endif
00108 
00109 /* vim: set et sw=2 ts=2: */

Generated on Thu May 14 18:19:16 2009 for mycpu by  doxygen 1.5.3