C++QEDCore  v2 Milestone 10
a framework for simulating open quantum dynamics – core
Conversions.h
Go to the documentation of this file.
1 // Copyright András Vukics 2006–2014. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.txt)
3 #ifndef CPPQEDCORE_UTILS_CONVERSIONS_H_INCLUDED
4 #define CPPQEDCORE_UTILS_CONVERSIONS_H_INCLUDED
5 
6 #include "ConversionsFwd.h"
7 
8 #include <boost/mpl/identity.hpp>
9 
10 #include <boost/numeric/conversion/converter.hpp>
11 
12 
13 
14 namespace cpputils {
15 
16 
17 namespace details {
18 
19 
20 template<class T>
21 struct DummyRangeCheckerPolicy
22 {
23  typedef typename T::argument_type argument_type;
24  static boost::numeric::range_check_result out_of_range(argument_type) {return boost::numeric::cInRange;}
25  static void validate_range(argument_type) {}
26 };
27 
28 
29 typedef
30 #ifndef NDEBUG
31 boost::numeric:: def_overflow_handler
32 #else
33 boost::numeric::silent_overflow_handler
34 #endif // NDEBUG
35 OverflowHandler;
36 
37 
38 template<class T> struct RangeCheckerMF : boost::mpl::identity<
39 #ifndef NDEBUG
40  boost::numeric::UseInternalRangeChecker
41 #else
42  DummyRangeCheckerPolicy<T>
43 #endif // NDEBUG
44 >
45 {};
46 
47 
48 } // details
49 
50 
51 
52 template<typename T, typename S>
53 struct Converter : boost::numeric::converter<T,S,
54  boost::numeric::conversion_traits<T,S>,
55  cpputils::details::OverflowHandler,
56  boost::numeric::Trunc<typename boost::numeric::conversion_traits<T,S>::source_type>,
57  boost::numeric::raw_converter<boost::numeric::conversion_traits<T,S> >,
58  typename cpputils::details::RangeCheckerMF<boost::numeric::conversion_traits<T,S> >::type
59  >
60 {};
61 
62 
63 } // cpputils
64 
65 
66 // Double2Int
67 
68 typedef boost::numeric::converter<int,
69  double,
70  boost::numeric::conversion_traits<int,double>,
71  cpputils::details::OverflowHandler,
72  boost::numeric::Trunc<boost::numeric::conversion_traits<int,double>::source_type>,
73  boost::numeric::raw_converter<boost::numeric::conversion_traits<int,double> >,
74  cpputils::details::RangeCheckerMF<boost::numeric::conversion_traits<int,double> >::type
75  > Double2Int;
76 
77 const Double2Int double2Int=Double2Int();
78 
79 
80 // Long2Int
81 
82 typedef boost::numeric::converter<int,
83  long,
84  boost::numeric::conversion_traits<int,long>,
85  cpputils::details::OverflowHandler,
86  boost::numeric::Trunc<boost::numeric::conversion_traits<int,long>::source_type>,
87  boost::numeric::raw_converter<boost::numeric::conversion_traits<int,long> >,
88  cpputils::details::RangeCheckerMF<boost::numeric::conversion_traits<int,long> >::type
89  > Long2Int;
90 
91 const Long2Int long2Int=Long2Int();
92 
93 
94 // Size2Int
95 
96 typedef boost::numeric::converter<int,
97  size_t,
98  boost::numeric::conversion_traits<int,size_t>,
99  cpputils::details::OverflowHandler,
100  boost::numeric::Trunc<boost::numeric::conversion_traits<int,size_t>::source_type>,
101  boost::numeric::raw_converter<boost::numeric::conversion_traits<int,size_t> >,
102  cpputils::details::RangeCheckerMF<boost::numeric::conversion_traits<int,size_t> >::type
103  > Size2Int;
104 
105 const Size2Int size2Int=Size2Int();
106 
107 
108 // Idx2Int
109 
110 typedef boost::numeric::converter<int,
111  ptrdiff_t,
112  boost::numeric::conversion_traits<int,ptrdiff_t>,
113  cpputils::details::OverflowHandler,
114  boost::numeric::Trunc<boost::numeric::conversion_traits<int,ptrdiff_t>::source_type>,
115  boost::numeric::raw_converter<boost::numeric::conversion_traits<int,ptrdiff_t> >,
116  cpputils::details::RangeCheckerMF<boost::numeric::conversion_traits<int,ptrdiff_t> >::type
117  > Idx2Int;
118 
119 const Idx2Int idx2Int=Idx2Int();
120 
121 
122 // Int2Size
123 
124 typedef boost::numeric::converter<size_t,
125  int,
126  boost::numeric::conversion_traits<size_t,int>,
127  cpputils::details::OverflowHandler,
128  boost::numeric::Trunc<boost::numeric::conversion_traits<size_t,int>::source_type>,
129  boost::numeric::raw_converter<boost::numeric::conversion_traits<size_t,int> >,
130  cpputils::details::RangeCheckerMF<boost::numeric::conversion_traits<size_t,int> >::type
131  > Int2Size;
132 
133 const Int2Size int2Size=Int2Size();
134 
135 
136 // Size2Double
137 
138 typedef boost::numeric::converter<double,
139  size_t,
140  boost::numeric::conversion_traits<double,size_t>,
141  cpputils::details::OverflowHandler,
142  boost::numeric::Trunc<boost::numeric::conversion_traits<double,size_t>::source_type>,
143  boost::numeric::raw_converter<boost::numeric::conversion_traits<double,size_t> >,
144  cpputils::details::RangeCheckerMF<boost::numeric::conversion_traits<double,size_t> >::type
145  > Size2Double;
146 
147 const Size2Double size2Double=Size2Double();
148 
149 #endif // CPPQEDCORE_UTILS_CONVERSIONS_H_INCLUDED
Namespace comprising otherwise hard-to-classify generic utilities.
Definition: Algorithm.h:10