mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
typeTraits.hpp
Go to the documentation of this file.
1 /** \file typeTraits.hpp
2  * \brief A collection of type trait evaluations.
3  * \ingroup utils_files
4  * \author Jared R. Males (jaredmales@gmail.com)
5  *
6  */
7 
8 //***********************************************************************//
9 // Copyright 2015, 2016, 2017, 2018 Jared R. Males (jaredmales@gmail.com)
10 //
11 // This file is part of mxlib.
12 //
13 // mxlib is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // mxlib is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25 //***********************************************************************//
26 
27 #ifndef typeTraits_hpp
28 #define typeTraits_hpp
29 
30 namespace mx
31 {
32 
33 namespace meta
34 {
35 
36 template <typename... Ts> using void_t = void;
37 
38 template <typename T, typename = void>
39 struct has_value_type : trueFalseT<false> {};
40 
41 ///Test whether a type has a typedef of "value_type"
42 /** Used for compile-time determination of type.
43  *
44  * Example usage:
45  * \code
46  * bool has_vt = has_value_type<std::vector<float> >; //Evaluates to true
47  * bool has_not_vt = has_value_type<float>; //Evaluates to false
48  * \endcode
49  *
50  * This was taken directly from the example at http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error
51  *
52  * \ingroup meta
53  */
54 template <typename T>
55 struct has_value_type<T, void_t<typename T::value_type>> : trueFalseT<true> {};
56 
57 
58 
59 ///Check whether a type is std::vector or not.
60 /** First resolves whether the type as a typedef value_type. If not, then member value will be false.
61  * If it does have a member type value_type, then it uses std::is_same to compare the type to std::vector.
62  * Then value is equal to std::is_same<T, std::vector<typenameT::value_type>>::value.
63  *
64  * \tparam T is the type to evaulate for vector-ness.
65  * \tparam has_value_type is a boolean type based on has_value_type SFINAE test.
66  *
67  * \ingroup meta
68  */
69 template <typename T, bool if_value_type = has_value_type<T>::value >
70 struct is_std_vector;
71 
72 ///Partial specialization for the case with value_type member type in T, invoking std::is_same.
73 /** \ingroup meta
74  */
75 template <typename T>
76 struct is_std_vector<T, true>
77 {
78  static const bool value = std::is_same< T, std::vector< typename T::value_type> >::value;
79 };
80 
81 ///Partial specialization for the case with no value_type member type in T.
82 /** \ingroup meta
83  */
84 template <typename T>
85 struct is_std_vector<T,false>
86 {
87  static const bool value = false;
88 };
89 
90 } //namespace meta
91 } //namespace mx
92 
93 
94 #endif //typeTraits_hpp
The mxlib c++ namespace.
Definition: mxError.hpp:107
Check whether a type is std::vector or not.
Definition: typeTraits.hpp:70
Template declaration of a trueFalseT type.
Definition: trueFalseT.hpp:51