Itasca C++ Interface
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
to.h
Go to the documentation of this file.
1 #pragma once
2 
8 #include "limit.h"
9 #include <cassert>
10 #include <stdexcept>
11 
12 #ifdef _WIN32
13 # ifdef __INTEL_COMPILER
14 # pragma warning(disable:2557)
15 # else
16 # pragma warning(disable:4018)
17 # endif
18 #endif
19 
24 template <class D,class T> D to(const T &t) {
25 #ifdef _WIN32
26 # ifdef _DEBUG
27 # ifndef __INTEL_COMPILER // NEED TO FIND SOMETHING THAT WORKS FOR INTEL!
28  if (t<0 && t<limits<D>::lowest())
29  throw std::runtime_error("Underflow error in numeric conversion.");
30  if (t>limits<D>::max())
31  throw std::runtime_error("Overflow error in numeric conversion.");
32 # endif
33 # endif
34 #endif
35  return static_cast<D>(t);
36 }
37 #ifdef _WIN32
38 # pragma warning(default:4018)
39 #endif
40 
45 template <class D,class T> D *check_cast(T *t) {
46  D *d = (D *)t;
47 #ifdef _DEBUG
48  D *d2 = dynamic_cast<D *>(t);
49  assert(d2==d);
50 #endif
51  return d;
52 }
53 
58 template <class D,class T> const D *check_cast(const T *t) {
59  const D *d = (const D *)t;
60  assert(dynamic_cast<const D *>(t)==d);
61  return d;
62 }
63 
69 template <class T> const T safeDiv(const T num,const T denom) {
70  if (!denom) return num;
71  return num/denom;
72 }
73 
75 // EoF
const T safeDiv(const T num, const T denom)
This function provids "safe" division operation, checks explicitly for zero.
Definition: to.h:69
D to(const T &t)
This template function serves as an alternative to static_cast<T>().
Definition: to.h:24
debug checked shorthand for std::numeric_limits<T>::
Definition: limit.h:25
D * check_cast(T *t)
This template function serves as a fast alternative to dynamic_cast, when you know the base offset is...
Definition: to.h:45
std::numeric_limits shorthand, with debug overflow checking