diff options
Diffstat (limited to 'ue4/mycpu/cmem.h')
| -rw-r--r-- | ue4/mycpu/cmem.h | 65 |
1 files changed, 44 insertions, 21 deletions
diff --git a/ue4/mycpu/cmem.h b/ue4/mycpu/cmem.h index c6b8735..3f35a70 100644 --- a/ue4/mycpu/cmem.h +++ b/ue4/mycpu/cmem.h | |||
| @@ -12,21 +12,43 @@ | |||
| 12 | #include <istream> | 12 | #include <istream> |
| 13 | #include <sstream> | 13 | #include <sstream> |
| 14 | #include <stdexcept> | 14 | #include <stdexcept> |
| 15 | #include <boost/lexical_cast.hpp> | ||
| 16 | #include <boost/algorithm/string.hpp> | ||
| 17 | #ifdef DEBUG | 15 | #ifdef DEBUG |
| 18 | # include <iostream> | 16 | # include <iostream> |
| 19 | # include <iomanip> | 17 | # include <iomanip> |
| 20 | #endif | 18 | #endif |
| 21 | 19 | ||
| 22 | /** | 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 | /** | ||
| 23 | * @class CMem | 45 | * @class CMem |
| 24 | * | 46 | * |
| 25 | * Extends std::vector template for use as memory for CCPU. | 47 | * Extends std::vector template for use as memory for CCPU. |
| 26 | */ | 48 | */ |
| 27 | template<class T=CDat<int>, int width=0> | 49 | template <class T> |
| 28 | class CMem | 50 | class CMem |
| 29 | : public std::vector<T> | 51 | : public std::vector<T> |
| 30 | { | 52 | { |
| 31 | typedef std::vector<T> super; | 53 | typedef std::vector<T> super; |
| 32 | typedef typename super::iterator iterator; | 54 | typedef typename super::iterator iterator; |
| @@ -39,13 +61,15 @@ class CMem | |||
| 39 | * @method initialize | 61 | * @method initialize |
| 40 | * @brief initialize the vector with the content of istream. istream is | 62 | * @brief initialize the vector with the content of istream. istream is |
| 41 | * read per line. empty lines will add unitialized elements. | 63 | * read per line. empty lines will add unitialized elements. |
| 42 | * @param in inputstream to read from | 64 | * @param in inputstream to read from |
| 65 | * @param datatype reference instance of datatype to copy from | ||
| 43 | * @return void | 66 | * @return void |
| 44 | * @globalvars none | 67 | * @globalvars none |
| 45 | * @exception std::runtime_error | 68 | * @exception CMemError |
| 46 | * @conditions none | 69 | * @pre none |
| 70 | * @post none | ||
| 47 | */ | 71 | */ |
| 48 | void initialize(std::istream& in) | 72 | void initialize(std::istream& in, T& datatype) |
| 49 | { | 73 | { |
| 50 | if (!in.good()) | 74 | if (!in.good()) |
| 51 | return; | 75 | return; |
| @@ -56,24 +80,22 @@ class CMem | |||
| 56 | { | 80 | { |
| 57 | ++i; | 81 | ++i; |
| 58 | std::getline(in, line); | 82 | std::getline(in, line); |
| 59 | boost::algorithm::trim(line); | 83 | |
| 60 | |||
| 61 | /* skip last line if it's empty */ | 84 | /* skip last line if it's empty */ |
| 62 | if (line.empty() && in.eof()) | 85 | if (line.empty() && in.eof()) |
| 63 | break; | 86 | break; |
| 64 | 87 | ||
| 65 | T value; | 88 | T value(datatype); |
| 66 | try | ||
| 67 | { | ||
| 68 | if (!line.empty()) | 89 | if (!line.empty()) |
| 69 | { | 90 | { |
| 70 | value = boost::lexical_cast<T>(line); | 91 | /* simple boost::lexical_cast replacement */ |
| 71 | } | 92 | std::stringstream interpreter; |
| 72 | catch(boost::bad_lexical_cast& ex) | 93 | if(!(interpreter << line && interpreter >> value && interpreter.get() == std::char_traits<char>::eof())) |
| 73 | { | 94 | { |
| 74 | std::stringstream sstr; | 95 | std::stringstream sstr; |
| 75 | sstr << "Unable to convert input (line " << i << "): " << ex.what(); | 96 | sstr << "Unable to convert input (line " << i << ") to datatype"; |
| 76 | throw std::runtime_error(sstr.str()); | 97 | throw CMemError(sstr.str()); |
| 98 | } | ||
| 77 | } | 99 | } |
| 78 | 100 | ||
| 79 | push_back(value); | 101 | push_back(value); |
| @@ -88,7 +110,8 @@ class CMem | |||
| 88 | * @return void | 110 | * @return void |
| 89 | * @globalvars none | 111 | * @globalvars none |
| 90 | * @exception none | 112 | * @exception none |
| 91 | * @conditions none | 113 | * @pre none |
| 114 | * @post none | ||
| 92 | */ | 115 | */ |
| 93 | void dump(std::ostream& out) | 116 | void dump(std::ostream& out) |
| 94 | { | 117 | { |
