TestFunctionTypeTraits.cpp (7982B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/FunctionTypeTraits.h" 8 9 #include <functional> 10 11 using mozilla::FunctionTypeTraits; 12 13 void f0() {} 14 15 int f1(char) { return 0; } 16 17 #ifdef NS_HAVE_STDCALL 18 void NS_STDCALL f0s() {} 19 20 int NS_STDCALL f1s(char) { return 0; } 21 #endif // NS_HAVE_STDCALL 22 23 struct S { 24 void f0() {} 25 void f0c() const {} 26 int f1(char) { return 0; } 27 int f1c(char) const { return 0; } 28 #ifdef NS_HAVE_STDCALL 29 void NS_STDCALL f0s() {} 30 void NS_STDCALL f0cs() const {} 31 int NS_STDCALL f1s(char) { return 0; } 32 int NS_STDCALL f1cs(char) const { return 0; } 33 #endif // NS_HAVE_STDCALL 34 }; 35 36 static_assert( 37 std::is_same<typename FunctionTypeTraits<decltype(f0)>::ReturnType, 38 void>::value, 39 "f0 returns void"); 40 static_assert(FunctionTypeTraits<decltype(f0)>::arity == 0, 41 "f0 takes no parameters"); 42 static_assert( 43 std::is_same< 44 typename FunctionTypeTraits<decltype(f0)>::template ParameterType<0>, 45 void>::value, 46 "f0 has no first parameter"); 47 48 static_assert( 49 std::is_same<typename FunctionTypeTraits<decltype(&S::f0)>::ReturnType, 50 void>::value, 51 "S::f0 returns void"); 52 static_assert(FunctionTypeTraits<decltype(&S::f0)>::arity == 0, 53 "S::f0 takes no parameters"); 54 static_assert(std::is_same<typename FunctionTypeTraits< 55 decltype(&S::f0)>::template ParameterType<0>, 56 void>::value, 57 "S::f0 has no first parameter"); 58 59 static_assert( 60 std::is_same<typename FunctionTypeTraits<decltype(&S::f0c)>::ReturnType, 61 void>::value, 62 "S::f0c returns void"); 63 static_assert(FunctionTypeTraits<decltype(&S::f0c)>::arity == 0, 64 "S::f0c takes no parameters"); 65 static_assert(std::is_same<typename FunctionTypeTraits< 66 decltype(&S::f0c)>::template ParameterType<0>, 67 void>::value, 68 "S::f0c has no first parameter"); 69 70 static_assert( 71 std::is_same<typename FunctionTypeTraits<decltype(f1)>::ReturnType, 72 int>::value, 73 "f1 returns int"); 74 static_assert(FunctionTypeTraits<decltype(f1)>::arity == 1, 75 "f1 takes one parameter"); 76 static_assert( 77 std::is_same< 78 typename FunctionTypeTraits<decltype(f1)>::template ParameterType<0>, 79 char>::value, 80 "f1 takes a char"); 81 82 static_assert( 83 std::is_same<typename FunctionTypeTraits<decltype(&S::f1)>::ReturnType, 84 int>::value, 85 "S::f1 returns int"); 86 static_assert(FunctionTypeTraits<decltype(&S::f1)>::arity == 1, 87 "S::f1 takes one parameter"); 88 static_assert(std::is_same<typename FunctionTypeTraits< 89 decltype(&S::f1)>::template ParameterType<0>, 90 char>::value, 91 "S::f1 takes a char"); 92 93 static_assert( 94 std::is_same<typename FunctionTypeTraits<decltype(&S::f1c)>::ReturnType, 95 int>::value, 96 "S::f1c returns int"); 97 static_assert(FunctionTypeTraits<decltype(&S::f1c)>::arity == 1, 98 "S::f1c takes one parameter"); 99 static_assert(std::is_same<typename FunctionTypeTraits< 100 decltype(&S::f1c)>::template ParameterType<0>, 101 char>::value, 102 "S::f1c takes a char"); 103 104 #ifdef NS_HAVE_STDCALL 105 static_assert( 106 std::is_same<typename FunctionTypeTraits<decltype(f0s)>::ReturnType, 107 void>::value, 108 "f0s returns void"); 109 static_assert(FunctionTypeTraits<decltype(f0s)>::arity == 0, 110 "f0s takes no parameters"); 111 static_assert( 112 std::is_same< 113 typename FunctionTypeTraits<decltype(f0s)>::template ParameterType<0>, 114 void>::value, 115 "f0s has no first parameter"); 116 117 static_assert( 118 std::is_same<typename FunctionTypeTraits<decltype(&S::f0s)>::ReturnType, 119 void>::value, 120 "S::f0s returns void"); 121 static_assert(FunctionTypeTraits<decltype(&S::f0s)>::arity == 0, 122 "S::f0s takes no parameters"); 123 static_assert(std::is_same<typename FunctionTypeTraits< 124 decltype(&S::f0s)>::template ParameterType<0>, 125 void>::value, 126 "S::f0s has no first parameter"); 127 128 static_assert( 129 std::is_same<typename FunctionTypeTraits<decltype(&S::f0cs)>::ReturnType, 130 void>::value, 131 "S::f0cs returns void"); 132 static_assert(FunctionTypeTraits<decltype(&S::f0cs)>::arity == 0, 133 "S::f0cs takes no parameters"); 134 static_assert(std::is_same<typename FunctionTypeTraits< 135 decltype(&S::f0cs)>::template ParameterType<0>, 136 void>::value, 137 "S::f0cs has no first parameter"); 138 139 static_assert( 140 std::is_same<typename FunctionTypeTraits<decltype(f1s)>::ReturnType, 141 int>::value, 142 "f1s returns int"); 143 static_assert(FunctionTypeTraits<decltype(f1s)>::arity == 1, 144 "f1s takes one parameter"); 145 static_assert( 146 std::is_same< 147 typename FunctionTypeTraits<decltype(f1s)>::template ParameterType<0>, 148 char>::value, 149 "f1s takes a char"); 150 151 static_assert( 152 std::is_same<typename FunctionTypeTraits<decltype(&S::f1s)>::ReturnType, 153 int>::value, 154 "S::f1s returns int"); 155 static_assert(FunctionTypeTraits<decltype(&S::f1s)>::arity == 1, 156 "S::f1s takes one parameter"); 157 static_assert(std::is_same<typename FunctionTypeTraits< 158 decltype(&S::f1s)>::template ParameterType<0>, 159 char>::value, 160 "S::f1s takes a char"); 161 162 static_assert( 163 std::is_same<typename FunctionTypeTraits<decltype(&S::f1cs)>::ReturnType, 164 int>::value, 165 "S::f1cs returns int"); 166 static_assert(FunctionTypeTraits<decltype(&S::f1cs)>::arity == 1, 167 "S::f1cs takes one parameter"); 168 static_assert(std::is_same<typename FunctionTypeTraits< 169 decltype(&S::f1cs)>::template ParameterType<0>, 170 char>::value, 171 "S::f1cs takes a char"); 172 #endif // NS_HAVE_STDCALL 173 174 template <typename F> 175 void TestVoidVoid(F&&) { 176 static_assert( 177 std::is_same<typename FunctionTypeTraits<F>::ReturnType, void>::value, 178 "Should return void"); 179 static_assert(FunctionTypeTraits<F>::arity == 0, "Should take no parameters"); 180 static_assert( 181 std::is_same<typename FunctionTypeTraits<F>::template ParameterType<0>, 182 void>::value, 183 "Should have no first parameter"); 184 } 185 186 template <typename F> 187 void TestIntChar(F&&) { 188 static_assert( 189 std::is_same<typename FunctionTypeTraits<F>::ReturnType, int>::value, 190 "Should return int"); 191 static_assert(FunctionTypeTraits<F>::arity == 1, "Should take one parameter"); 192 static_assert( 193 std::is_same<typename FunctionTypeTraits<F>::template ParameterType<0>, 194 char>::value, 195 "Should take a char"); 196 } 197 198 int main() { 199 TestVoidVoid(f0); 200 TestVoidVoid(&f0); 201 TestVoidVoid(&S::f0); 202 TestVoidVoid(&S::f0c); 203 TestVoidVoid([]() {}); 204 std::function<void()> ff0 = f0; 205 TestVoidVoid(ff0); 206 207 TestIntChar(f1); 208 TestIntChar(&f1); 209 TestIntChar(&S::f1); 210 TestIntChar(&S::f1c); 211 TestIntChar([](char) { return 0; }); 212 std::function<int(char)> ff1 = f1; 213 TestIntChar(ff1); 214 215 #ifdef NS_HAVE_STDCALL 216 TestVoidVoid(f0s); 217 TestVoidVoid(&f0s); 218 TestVoidVoid(&S::f0s); 219 TestVoidVoid(&S::f0cs); 220 std::function<void()> ff0s = f0s; 221 TestVoidVoid(ff0s); 222 223 TestIntChar(f1s); 224 TestIntChar(&f1s); 225 TestIntChar(&S::f1s); 226 TestIntChar(&S::f1cs); 227 std::function<int(char)> ff1s = f1s; 228 TestIntChar(ff1s); 229 #endif // NS_HAVE_STDCALL 230 231 return 0; 232 }