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:
@@ -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
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -74,7 +86,7 @@ namespace chaiscript
|
|||||||
case Operators::not_equal:
|
case Operators::not_equal:
|
||||||
return const_var(t != u);
|
return const_var(t != u);
|
||||||
default:
|
default:
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -102,16 +114,14 @@ 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;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
}
|
}
|
||||||
|
|
||||||
return t_lhs;
|
return t_lhs;
|
||||||
@@ -138,16 +148,14 @@ 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:
|
||||||
t ^= u;
|
t ^= u;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
}
|
}
|
||||||
return t_lhs;
|
return t_lhs;
|
||||||
}
|
}
|
||||||
@@ -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:
|
||||||
@@ -178,7 +184,7 @@ namespace chaiscript
|
|||||||
case Operators::bitwise_complement:
|
case Operators::bitwise_complement:
|
||||||
return const_var(~t);
|
return const_var(~t);
|
||||||
default:
|
default:
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -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:
|
||||||
@@ -206,7 +210,7 @@ namespace chaiscript
|
|||||||
case Operators::unary_plus:
|
case Operators::unary_plus:
|
||||||
return const_var(+t);
|
return const_var(+t);
|
||||||
default:
|
default:
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -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))
|
||||||
@@ -868,12 +871,12 @@ namespace chaiscript
|
|||||||
struct Cast_Helper<const Boxed_Number &> : Cast_Helper<Boxed_Number>
|
struct Cast_Helper<const Boxed_Number &> : Cast_Helper<Boxed_Number>
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Cast_Helper for converting from Boxed_Value to Boxed_Number
|
/// Cast_Helper for converting from Boxed_Value to Boxed_Number
|
||||||
template<>
|
template<>
|
||||||
struct Cast_Helper<const Boxed_Number> : Cast_Helper<Boxed_Number>
|
struct Cast_Helper<const Boxed_Number> : Cast_Helper<Boxed_Number>
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CHAISCRIPT_MSVC
|
#ifdef CHAISCRIPT_MSVC
|
||||||
|
Reference in New Issue
Block a user