Loading...
Searching...
No Matches
symengine_casts.h
1#ifndef SYMENGINE_CASTS_H
2#define SYMENGINE_CASTS_H
3
4#include <iostream>
5#include <limits>
6#include <symengine/symengine_config.h>
7#include <symengine/symengine_assert.h>
8
9namespace SymEngine
10{
11
12// Reference modifications.
13template <typename T>
15 typedef T type;
16};
17template <typename T>
18struct remove_reference<T &> {
19 typedef T type;
20};
21
22// Use implicit_cast as a safe version of static_cast or const_cast
23// for upcasting in the type hierarchy (i.e. casting a pointer to Foo
24// to a pointer to SuperclassOfFoo or casting a pointer to Foo to
25// a const pointer to Foo).
26// When you use implicit_cast, the compiler checks that the cast is safe.
27// Such explicit implicit_casts are necessary in surprisingly many
28// situations where C++ demands an exact type match instead of an
29// argument type convertable to a target type.
30//
31// The From type can be inferred, so the preferred syntax for using
32// implicit_cast is the same as for static_cast etc.:
33//
34// implicit_cast<ToType>(expr)
35
36template <typename To, typename From>
37inline To implicit_cast(const From &f)
38{
39 return f;
40}
41
42// When you upcast (that is, cast a pointer from type Foo to type
43// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
44// always succeed. When you downcast (that is, cast a pointer from
45// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
46// how do you know the pointer is really of type SubclassOfFoo? It
47// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
48// when you downcast, you should use this macro. In debug mode, we
49// use dynamic_cast<> to double-check the downcast is legal (we die
50// if it's not). In normal mode, we do the efficient static_cast<>
51// instead. Thus, it's important to test in debug mode to make sure
52// the cast is legal!
53// This is the only place in the code we should use dynamic_cast<>.
54// In particular, you SHOULDN'T be using dynamic_cast<> in order to
55// do RTTI (eg code like this:
56// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
57// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
58// You should design the code some other way not to need this.
59
60template <typename To, typename From> // use like this: down_cast<T*>(foo).
61inline To down_cast(From *f) // Only accept pointers.
62{
63 // Ensures that To is a sub-type of From *. This test is here only
64 // for compile-time type checking, and has no overhead in an
65 // optimized build at run-time, as it will be optimized away
66 // completely.
67 if (false) {
68 implicit_cast<From *, To>(0);
69 }
70
71 SYMENGINE_ASSERT(f == NULL || dynamic_cast<To>(f) != NULL);
72
73 return static_cast<To>(f);
74}
75
76template <typename To, typename From> // use like this: down_cast<T&>(foo);
77inline To down_cast(From &f)
78{
79 typedef typename remove_reference<To>::type *ToAsPointer;
80 // Ensures that To is a sub-type of From *. This test is here only
81 // for compile-time type checking, and has no overhead in an
82 // optimized build at run-time, as it will be optimized away
83 // completely.
84 if (false) {
85 implicit_cast<From *, ToAsPointer>(0);
86 }
87
88 SYMENGINE_ASSERT(dynamic_cast<ToAsPointer>(&f) != NULL);
89
90 return *static_cast<ToAsPointer>(&f);
91}
92
93template <typename To, typename From>
94inline To numeric_cast(
95 From f,
96 typename std::enable_if<
99 * = nullptr)
100{
101 SYMENGINE_ASSERT(f <= std::numeric_limits<To>::max());
102 SYMENGINE_ASSERT(f >= std::numeric_limits<To>::min());
103 return static_cast<To>(f);
104}
105
106template <typename To, typename From>
107inline To numeric_cast(
108 From f,
110 && std::is_unsigned<To>::value)>::type * = nullptr)
111{
112#ifdef WITH_SYMENGINE_ASSERT
113 // Above ifdef is needed to avoid a warning about unused typedefs
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)
118#endif
119 return static_cast<To>(f);
120}
121
122template <typename To, typename From>
123inline To numeric_cast(
124 From f,
126 && std::is_signed<To>::value)>::type * = nullptr)
127{
128#ifdef WITH_SYMENGINE_ASSERT
129 typedef typename std::make_unsigned<To>::type unsigned_to_type;
130 SYMENGINE_ASSERT(
131 f <= static_cast<unsigned_to_type>(std::numeric_limits<To>::max()));
132
133#endif
134 return static_cast<To>(f);
135}
136
137} // namespace SymEngine
138
139#endif // SYMENGINE_CASTS_H
Main namespace for SymEngine package.
Definition: add.cpp:19