Limit scope of #ifdefs, remove macros

Macros do not fit within the ChaiScript coding standards because
they do not respect namespaces and are more difficult to debug
of something goes wrong.
This commit is contained in:
Jason Turner
2015-01-09 19:30:28 -07:00
parent 1a4dec0df0
commit 0695eec3ca

View File

@@ -31,7 +31,6 @@ namespace chaiscript
arithmetic_error(const std::string& reason) : std::runtime_error("Arithmetic error: " + reason) {} arithmetic_error(const std::string& reason) : std::runtime_error("Arithmetic error: " + reason) {}
virtual ~arithmetic_error() {} virtual ~arithmetic_error() {}
}; };
#define CHAISCRIPT_ARITHMETIC_CHECKDIVIDEBYZERO(T, n) if(std::is_arithmetic<T>::value) if(n==0) throw chaiscript::exception::arithmetic_error("divide by zero");
} }
} }
@@ -50,6 +49,19 @@ namespace chaiscript
class Boxed_Number class Boxed_Number
{ {
private: private:
#ifdef CHAISCRIPT_PROTECT_DIVIDEBYZERO
template<typename T>
static void check_divide_by_zero(T t)
{
if(std::is_arithmetic<T>::value) if(t==0) throw chaiscript::exception::arithmetic_error("divide by zero");
}
#else
template<typename T>
static void check_divide_by_zero(T)
{
}
#endif
struct boolean struct boolean
{ {
@@ -102,10 +114,8 @@ namespace chaiscript
t += u; t += u;
break; break;
case Operators::assign_quotient: case Operators::assign_quotient:
#ifdef CHAISCRIPT_PROTECT_DIVIDEBYZERO check_divide_by_zero(u);
CHAISCRIPT_ARITHMETIC_CHECKDIVIDEBYZERO(U, u) t /= u;
#endif
t /= u;
break; break;
case Operators::assign_difference: case Operators::assign_difference:
t -= u; t -= u;
@@ -138,9 +148,7 @@ namespace chaiscript
t >>= u; t >>= u;
break; break;
case Operators::assign_remainder: case Operators::assign_remainder:
#ifdef CHAISCRIPT_PROTECT_DIVIDEBYZERO check_divide_by_zero(u);
CHAISCRIPT_ARITHMETIC_CHECKDIVIDEBYZERO(U, u)
#endif
t %= u; t %= u;
break; break;
case Operators::assign_bitwise_xor: case Operators::assign_bitwise_xor:
@@ -165,10 +173,8 @@ namespace chaiscript
case Operators::shift_right: case Operators::shift_right:
return const_var(t >> u); return const_var(t >> u);
case Operators::remainder: case Operators::remainder:
#ifdef CHAISCRIPT_PROTECT_DIVIDEBYZERO check_divide_by_zero(u);
CHAISCRIPT_ARITHMETIC_CHECKDIVIDEBYZERO(U, u) return const_var(t % u);
#endif
return const_var(t % u);
case Operators::bitwise_and: case Operators::bitwise_and:
return const_var(t & u); return const_var(t & u);
case Operators::bitwise_or: case Operators::bitwise_or:
@@ -193,10 +199,8 @@ namespace chaiscript
case Operators::sum: case Operators::sum:
return const_var(t + u); return const_var(t + u);
case Operators::quotient: case Operators::quotient:
#ifdef CHAISCRIPT_PROTECT_DIVIDEBYZERO check_divide_by_zero(u);
CHAISCRIPT_ARITHMETIC_CHECKDIVIDEBYZERO(U, u) return const_var(t / u);
#endif
return const_var(t / u);
case Operators::product: case Operators::product:
return const_var(t * u); return const_var(t * u);
case Operators::difference: case Operators::difference:
@@ -352,7 +356,6 @@ namespace chaiscript
return oss.str(); return oss.str();
} }
public: public:
Boxed_Number() Boxed_Number()
: bv(Boxed_Value(0)) : bv(Boxed_Value(0))