1 //////////////////////////////////////////////////////////////////////// 2 // 3 // Copyright (C) 2009-2021 The Octave Project Developers 4 // 5 // See the file COPYRIGHT.md in the top-level directory of this 6 // distribution or <https://octave.org/copyright/>. 7 // 8 // This file is part of Octave. 9 // 10 // Octave is free software: you can redistribute it and/or modify it 11 // under the terms of the GNU General Public License as published by 12 // the Free Software Foundation, either version 3 of the License, or 13 // (at your option) any later version. 14 // 15 // Octave is distributed in the hope that it will be useful, but 16 // WITHOUT ANY WARRANTY; without even the implied warranty of 17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 // GNU General Public License for more details. 19 // 20 // You should have received a copy of the GNU General Public License 21 // along with Octave; see the file COPYING. If not, see 22 // <https://www.gnu.org/licenses/>. 23 // 24 //////////////////////////////////////////////////////////////////////// 25 26 #if ! defined (octave_lo_traits_h) 27 #define octave_lo_traits_h 1 28 29 #include "octave-config.h" 30 31 // Ideas for these classes taken from C++ Templates, The Complete 32 // Guide by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley 33 // (2003). 34 35 // Select a type based on the value of a constant expression. 36 37 template <bool cond, typename T1, typename T2> 38 class if_then_else; 39 40 template <typename T1, typename T2> 41 class if_then_else<true, T1, T2> 42 { 43 public: 44 45 typedef T1 result; 46 }; 47 48 template <typename T1, typename T2> 49 class if_then_else<false, T1, T2> 50 { 51 public: 52 53 typedef T2 result; 54 }; 55 56 // Determine whether two types are equal. 57 template <typename T1, typename T2> 58 class equal_types 59 { 60 public: 61 62 static const bool value = false; 63 }; 64 65 template <typename T> 66 class equal_types <T, T> 67 { 68 public: 69 70 static const bool value = true; 71 }; 72 73 // Determine whether a type is an instance of a template. 74 75 template <template <typename> class Template, typename T> 76 class is_instance 77 { 78 public: 79 80 static const bool value = false; 81 }; 82 83 template <template <typename> class Template, typename T> 84 class is_instance <Template, Template<T>> 85 { 86 public: 87 88 static const bool value = true; 89 }; 90 91 // Determine whether a template parameter is a class type. 92 93 template <typename T1> 94 class is_class_type 95 { 96 private: 97 98 typedef char one; 99 typedef struct { char c[2]; } two; 100 101 // Classes can have pointers to members. 102 template <typename T2> static one is_class_type_test (int T2::*); 103 104 // Catch everything else. 105 template <typename T2> static two is_class_type_test (...); 106 107 public: 108 109 enum { yes = sizeof (is_class_type_test<T1> (0)) == 1 }; 110 enum { no = ! yes }; 111 }; 112 113 // Define typename ref_param<T>::type as T const& if T is a class 114 // type. Otherwise, define it to be T. 115 116 template <typename T> 117 class ref_param 118 { 119 public: 120 121 typedef typename if_then_else<is_class_type<T>::no, T, T const&>::result type; 122 }; 123 124 // Will turn TemplatedClass<T> to T, leave T otherwise. 125 // Useful for stripping wrapper classes, like octave_int. 126 127 template <template <typename> class TemplatedClass, typename T> 128 class strip_template_param 129 { 130 public: 131 typedef T type; 132 }; 133 134 template <template <typename> class TemplatedClass, typename T> 135 class strip_template_param<TemplatedClass, TemplatedClass<T>> 136 { 137 public: 138 typedef T type; 139 }; 140 141 // Will turn TemplatedClass<T> to TemplatedClass<S>, T to S otherwise. 142 // Useful for generic promotions. 143 144 template <template <typename> class TemplatedClass, typename T, typename S> 145 class subst_template_param 146 { 147 public: 148 typedef S type; 149 }; 150 151 template <template <typename> class TemplatedClass, typename T, typename S> 152 class subst_template_param<TemplatedClass, TemplatedClass<T>, S> 153 { 154 public: 155 typedef TemplatedClass<S> type; 156 }; 157 158 #endif 159