From 26a97259f5a7b066cff2927e88c867fac2aaad87 Mon Sep 17 00:00:00 2001 From: manuel Date: Tue, 26 May 2009 23:10:13 +0200 Subject: template FTW!!!!!11 --- ue4/mycpu/cinstruction.h | 50 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'ue4/mycpu/cinstruction.h') diff --git a/ue4/mycpu/cinstruction.h b/ue4/mycpu/cinstruction.h index 4cc69de..8c35d2c 100644 --- a/ue4/mycpu/cinstruction.h +++ b/ue4/mycpu/cinstruction.h @@ -2,7 +2,7 @@ * @module cinstruction * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) * @brief Abstract class for displays - * @date 13.05.2009 + * @date 26.05.2009 */ #ifndef CINSTRUCTION_H @@ -10,8 +10,14 @@ #include #include +#include +#include +#include +#include +#include "ccpu.h" /* forward declare CCPU */ +template class CCPU; /** @@ -19,6 +25,7 @@ class CCPU; * * Abstract class for displays */ +template class CInstruction { public: @@ -70,7 +77,7 @@ class CInstruction * @exception std::runtime_error * @conditions none */ - virtual CInstruction& operator()(CCPU *cpu) + virtual CInstruction& operator()(CCPU *cpu) { execute(cpu); return *this; @@ -142,7 +149,7 @@ class CInstruction * @exception std::runtime_error * @conditions none */ - virtual void checkRegister(CCPU *cpu, const unsigned regidx); + virtual void checkRegister(CCPU *cpu, const unsigned regidx); /** * @method factory @@ -176,7 +183,7 @@ class CInstruction * @exception std::runtime_error * @conditions none */ - virtual void execute(CCPU *cpu) = 0; + virtual void execute(CCPU *cpu) = 0; protected: /* members */ @@ -184,6 +191,41 @@ class CInstruction std::string m_name; }; +/*----------------------------------------------------------------------------*/ + +template +const unsigned CInstruction::parseRegister(const std::string& str) +{ + unsigned reg; + if (str.length() < 2 || str[0] != 'r') + throw std::runtime_error("Invalid syntax of register"); + + try + { + reg = boost::lexical_cast(str.substr(1)); + } + catch(boost::bad_lexical_cast& ex) + { + throw std::runtime_error("Invalid syntax of register"); + } + + return reg; +} + +/*----------------------------------------------------------------------------*/ + +template +inline void CInstruction::checkRegister(CCPU *cpu, const unsigned regidx) +{ + assert(cpu != NULL); + if (regidx >= cpu->getRegisterCount()) + { + std::stringstream sstr; + sstr << "Register R" << regidx << " doesn't exist (out of bound)"; + throw std::runtime_error(sstr.str()); + } +} + #endif /* vim: set et sw=2 ts=2: */ -- cgit v1.2.3