LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
float_traits.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014, Sam Schetterer, Nathan Kutz, University of Washington
3 Authors: Sam Schetterer
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 
10 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11 
12 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 
16 */
17 #ifndef FLOAT_TRAITS_HPP
18 #define FLOAT_TRAITS_HPP
19 #include <type_traits>
20 #include <complex>
21 
22 //again, no specialization in class scopes bites me in the ass... save me c++17!!!!
24 template<class T, bool val>
25 struct sub_type{
26  static_assert(val, "Unspecialized type used in float_traits");
27  //This part helps a lot in preventing the rest of the code from exploding
28  //in a mess of template error messages
29  //since double seems to work with many things
30  typedef double type;
31 };
33 template<class T>
34 struct sub_type<T, true>{
35  typedef T type;
36 };
38 /*
39  * This class provides a method for selecting a floating point-type to use given an arbitrary type.
40  * float_traits<T>::type is the underlying floating point type, while dimension represents how big the type is.
41  *
42  * When passed a standard floating point type (double, etc) float_traits<T>::type is the same type, and dim is 1.
43  *
44  * An example on a different type would be std::complex<double>. float_traits<std::complex<double>>::type is double, and dim is 2.
45  * See example_type for another example on a user defined type
46  * \sa example_type
47  */
48 template<class T>
49 struct float_traits{
50  constexpr static bool cast_to_double=false;
51  constexpr static bool is_fp = std::is_floating_point<T>::value;
52  //allows for any floating point type to pass,
53  //and catches unspecialized non-floating point types
54  //Reults in cleaner error messages during instantiation of this class
55  typedef typename sub_type<T, is_fp>::type type;
56  constexpr static size_t dim=1;
57 };
58 template<>
59 struct float_traits<double>{
60  constexpr static bool cast_to_double=true;
61  constexpr static size_t dim = 1;
62  typedef double type;
63 };
65 template<class T>
66 struct float_traits<std::complex<T>>{
68  constexpr static size_t dim=2*float_traits<T>::dim;
69  typedef typename float_traits<T>::type type;
70 };
71 #endif