1 #ifndef SYMENGINE_CASTS_H
2 #define SYMENGINE_CASTS_H
6 #include <symengine/symengine_config.h>
7 #include <symengine/symengine_assert.h>
36 template <
typename To,
typename From>
37 inline To implicit_cast(
const From &f)
60 template <
typename To,
typename From>
61 inline To down_cast(From *f)
68 implicit_cast<From *, To>(0);
71 SYMENGINE_ASSERT(f == NULL ||
dynamic_cast<To
>(f) != NULL);
73 return static_cast<To
>(f);
76 template <
typename To,
typename From>
77 inline To down_cast(From &f)
79 typedef typename remove_reference<To>::type *ToAsPointer;
85 implicit_cast<From *, ToAsPointer>(0);
88 SYMENGINE_ASSERT(
dynamic_cast<ToAsPointer
>(&f) != NULL);
90 return *
static_cast<ToAsPointer
>(&f);
93 template <
typename To,
typename From>
94 inline To numeric_cast(
96 typename std::enable_if<
97 (std::is_signed<From>::value && std::is_signed<To>::value)
98 || (std::is_unsigned<From>::value && std::is_unsigned<To>::value)>::type
101 SYMENGINE_ASSERT(f <= std::numeric_limits<To>::max());
102 SYMENGINE_ASSERT(f >= std::numeric_limits<To>::min());
103 return static_cast<To
>(f);
106 template <
typename To,
typename From>
107 inline To numeric_cast(
109 typename std::enable_if<(std::is_signed<From>::value
110 && std::is_unsigned<To>::value)>::type * =
nullptr)
112 #ifdef WITH_SYMENGINE_ASSERT
114 typedef typename std::make_unsigned<From>::type unsigned_from_type;
115 SYMENGINE_ASSERT(f >= 0);
116 SYMENGINE_ASSERT(
static_cast<unsigned_from_type
>(f)
117 <= std::numeric_limits<To>::max());
119 return static_cast<To
>(f);
122 template <
typename To,
typename From>
123 inline To numeric_cast(
125 typename std::enable_if<(std::is_unsigned<From>::value
126 && std::is_signed<To>::value)>::type * =
nullptr)
128 #ifdef WITH_SYMENGINE_ASSERT
129 typedef typename std::make_unsigned<To>::type unsigned_to_type;
131 f <=
static_cast<unsigned_to_type
>(std::numeric_limits<To>::max()));
134 return static_cast<To
>(f);
Main namespace for SymEngine package.