C++ベースのLightweightなHTTPサーバー
| Revision | 21552c2903cfbd8598ec5be979cacd05f06bd79f (tree) |
|---|---|
| Time | 2013-01-05 10:35:47 |
| Author | Michio Hirai <smg_ykz@user...> |
| Commiter | Michio Hirai |
[Refactor] Separation of typelist algorithm into differnt header files (ForEach, FindIf).
| @@ -2,10 +2,6 @@ | ||
| 2 | 2 | #ifndef INC_MT_TYPELIST_H_ |
| 3 | 3 | #define INC_MT_TYPELIST_H_ |
| 4 | 4 | |
| 5 | -#include "mt_mpl.h" | |
| 6 | -#include "mt_sfinae.h" | |
| 7 | -#include "mt_type_identity.h" | |
| 8 | - | |
| 9 | 5 | namespace mt { |
| 10 | 6 | |
| 11 | 7 | class NullType |
| @@ -144,188 +140,6 @@ protected: | ||
| 144 | 140 | {} |
| 145 | 141 | }; |
| 146 | 142 | |
| 147 | -template <typename TL> | |
| 148 | -struct ForEach; | |
| 149 | - | |
| 150 | -template <typename T, typename U> | |
| 151 | -struct ForEach< Typelist<T, U> > | |
| 152 | -{ | |
| 153 | - template <typename V> | |
| 154 | - void operator()(V& obj) | |
| 155 | - { | |
| 156 | - T().operator()(obj); | |
| 157 | - | |
| 158 | - ForEach<U>()(obj); | |
| 159 | - return; | |
| 160 | - } | |
| 161 | -}; | |
| 162 | - | |
| 163 | -template <typename T> | |
| 164 | -struct ForEach< Typelist<T, NullType> > | |
| 165 | -{ | |
| 166 | - template <typename V> | |
| 167 | - void operator()(V& obj) | |
| 168 | - { | |
| 169 | - T().operator()(obj); | |
| 170 | - } | |
| 171 | -}; | |
| 172 | - | |
| 173 | -template <typename T> | |
| 174 | -struct HasCtorType | |
| 175 | -{ | |
| 176 | - template <typename X> | |
| 177 | - static YesType test(typename X::CtorType*); | |
| 178 | - | |
| 179 | - template <typename X> | |
| 180 | - static NoType test(...); | |
| 181 | - | |
| 182 | - static const bool value = sizeof(test<T>(0)) != sizeof(NoType); | |
| 183 | -}; | |
| 184 | - | |
| 185 | -template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 186 | -T getObject(Arg1&, Arg2&, Arg3&, Arg4&, typename EnableIf<!HasCtorType<T>::value>::Result* = 0) | |
| 187 | -{ | |
| 188 | - return T(); | |
| 189 | -} | |
| 190 | - | |
| 191 | -template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 192 | -T getObject(Arg1& arg1, Arg2&, Arg3&, Arg4&, typename EnableIf<IsIdentical<typename T::CtorType, StaticValue<int, 1> >::value>::Result* = 0) | |
| 193 | -{ | |
| 194 | - return T(arg1); | |
| 195 | -} | |
| 196 | - | |
| 197 | -template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 198 | -T getObject(Arg1& arg1, Arg2& arg2, Arg3&, Arg4&, typename EnableIf<IsIdentical<typename T::CtorType, StaticValue<int, 2> >::value>::Result* = 0) | |
| 199 | -{ | |
| 200 | - return T(arg1, arg2); | |
| 201 | -} | |
| 202 | - | |
| 203 | -template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 204 | -T getObject(Arg1& arg1, Arg2& arg2, Arg3& arg3, Arg4&, typename EnableIf<IsIdentical<typename T::CtorType, StaticValue<int, 3> >::value>::Result* = 0) | |
| 205 | -{ | |
| 206 | - return T(arg1, arg2, arg3); | |
| 207 | -} | |
| 208 | - | |
| 209 | -template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 210 | -T getObject(Arg1& arg1, Arg2& arg2, Arg3& arg3, Arg4& arg4, typename EnableIf<IsIdentical<typename T::CtorType, StaticValue<int, 4> >::value>::Result* = 0) | |
| 211 | -{ | |
| 212 | - return T(arg1, arg2, arg3, arg4); | |
| 213 | -} | |
| 214 | - | |
| 215 | -static NullType nulltype_obj; | |
| 216 | - | |
| 217 | -template <typename TL, typename Arg1 = NullType, typename Arg2 = NullType, | |
| 218 | - typename Arg3 = NullType, typename Arg4 = NullType> | |
| 219 | -struct FindIf_t; | |
| 220 | - | |
| 221 | -// FindIf_t class template would require getFindIf_tKey() overloaded function set | |
| 222 | -// to derive the key of the typelist element types respectively. | |
| 223 | - | |
| 224 | -template <typename T, typename U, | |
| 225 | - typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 226 | -struct FindIf_t< Typelist<T, U>, Arg1, Arg2, Arg3, Arg4 > | |
| 227 | -{ | |
| 228 | - FindIf_t() : arg1_(nulltype_obj), arg2_(nulltype_obj), arg3_(nulltype_obj), arg4_(nulltype_obj) | |
| 229 | - {} | |
| 230 | - FindIf_t(Arg1& arg1) : arg1_(arg1), arg2_(nulltype_obj), arg3_(nulltype_obj), arg4_(nulltype_obj) | |
| 231 | - {} | |
| 232 | - FindIf_t(Arg1& arg1, Arg2& arg2) : arg1_(arg1), arg2_(arg2), arg3_(nulltype_obj), arg4_(nulltype_obj) | |
| 233 | - {} | |
| 234 | - FindIf_t(Arg1& arg1, Arg2& arg2, Arg3& arg3) : arg1_(arg1), arg2_(arg2), arg3_(arg3), arg4_(nulltype_obj) | |
| 235 | - {} | |
| 236 | - FindIf_t(Arg1& arg1, Arg2& arg2, Arg3& arg3, Arg4& arg4) : arg1_(arg1), arg2_(arg2), arg3_(arg3), arg4_(arg4) | |
| 237 | - {} | |
| 238 | - | |
| 239 | - template <typename V, typename Key> | |
| 240 | - bool operator()(V& obj, const Key& key) | |
| 241 | - { | |
| 242 | - T t(getObject<T>(arg1_, arg2_, arg3_, arg4_)); | |
| 243 | - return handleIt(t, obj, key); | |
| 244 | - } | |
| 245 | - | |
| 246 | - template <typename V, typename Key> | |
| 247 | - bool handleIt(T& t, V& obj, const Key& key) | |
| 248 | - { | |
| 249 | - if (key == (getFindIfKey(t))()) { | |
| 250 | - t.operator()(obj); | |
| 251 | - return true; | |
| 252 | - } | |
| 253 | - return FindIf_t<U, Arg1, Arg2, Arg3, Arg4>(arg1_, arg2_, arg3_, arg4_)(obj, key); | |
| 254 | - } | |
| 255 | - | |
| 256 | - Arg1& arg1_; | |
| 257 | - Arg2& arg2_; | |
| 258 | - Arg3& arg3_; | |
| 259 | - Arg4& arg4_; | |
| 260 | -}; | |
| 261 | - | |
| 262 | -template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 263 | -struct FindIf_t< Typelist<T, NullType>, Arg1, Arg2, Arg3, Arg4> | |
| 264 | -{ | |
| 265 | - FindIf_t() : arg1_(nulltype_obj), arg2_(nulltype_obj), arg3_(nulltype_obj), arg4_(nulltype_obj) | |
| 266 | - {} | |
| 267 | - FindIf_t(Arg1& arg1) : arg1_(arg1), arg2_(nulltype_obj), arg3_(nulltype_obj), arg4_(nulltype_obj) | |
| 268 | - {} | |
| 269 | - FindIf_t(Arg1& arg1, Arg2& arg2) : arg1_(arg1), arg2_(arg2), arg3_(nulltype_obj), arg4_(nulltype_obj) | |
| 270 | - {} | |
| 271 | - FindIf_t(Arg1& arg1, Arg2& arg2, Arg3& arg3) : arg1_(arg1), arg2_(arg2), arg3_(arg3), arg4_(nulltype_obj) | |
| 272 | - {} | |
| 273 | - FindIf_t(Arg1& arg1, Arg2& arg2, Arg3& arg3, Arg4& arg4) : arg1_(arg1), arg2_(arg2), arg3_(arg3), arg4_(arg4) | |
| 274 | - {} | |
| 275 | - | |
| 276 | - template <typename V, typename Key> | |
| 277 | - bool operator()(V& obj, const Key& key) | |
| 278 | - { | |
| 279 | - T t(getObject<T>(arg1_, arg2_, arg3_, arg4_)); | |
| 280 | - return handleIt(t, obj, key); | |
| 281 | - } | |
| 282 | - | |
| 283 | - template <typename V, typename Key> | |
| 284 | - bool handleIt(T& t, V& obj, const Key& key) | |
| 285 | - { | |
| 286 | - if (key == (getFindIfKey(t))()) { | |
| 287 | - t.operator()(obj); | |
| 288 | - return true; | |
| 289 | - } | |
| 290 | - return false; | |
| 291 | - } | |
| 292 | - | |
| 293 | - Arg1& arg1_; | |
| 294 | - Arg2& arg2_; | |
| 295 | - Arg3& arg3_; | |
| 296 | - Arg4& arg4_; | |
| 297 | -}; | |
| 298 | - | |
| 299 | -template <typename TL> | |
| 300 | -inline FindIf_t<TL> FindIf() | |
| 301 | -{ | |
| 302 | - return FindIf_t<TL>(); | |
| 303 | -} | |
| 304 | - | |
| 305 | -template <typename TL, typename Arg1> | |
| 306 | -inline FindIf_t<TL, Arg1> FindIf(Arg1& arg1) | |
| 307 | -{ | |
| 308 | - return FindIf_t<TL, Arg1>(arg1); | |
| 309 | -} | |
| 310 | - | |
| 311 | -template <typename TL, typename Arg1, typename Arg2> | |
| 312 | -inline FindIf_t<TL, Arg1, Arg2> FindIf(Arg1& arg1, Arg2& arg2) | |
| 313 | -{ | |
| 314 | - return FindIf_t<TL, Arg1, Arg2>(arg1, arg2); | |
| 315 | -} | |
| 316 | - | |
| 317 | -template <typename TL, typename Arg1, typename Arg2, typename Arg3> | |
| 318 | -inline FindIf_t<TL, Arg1, Arg2, Arg3> FindIf(Arg1& arg1, Arg2& arg2, Arg3& arg3) | |
| 319 | -{ | |
| 320 | - return FindIf_t<TL, Arg1, Arg2, Arg3>(arg1, arg2, arg3); | |
| 321 | -} | |
| 322 | - | |
| 323 | -template <typename TL, typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 324 | -inline FindIf_t<TL, Arg1, Arg2, Arg3, Arg4> FindIf(Arg1& arg1, Arg2& arg2, Arg3& arg3, Arg4& arg4) | |
| 325 | -{ | |
| 326 | - return FindIf_t<TL, Arg1, Arg2, Arg3, Arg4>(arg1, arg2, arg3, arg4); | |
| 327 | -} | |
| 328 | - | |
| 329 | 143 | } // namespace mt |
| 330 | 144 | |
| 331 | 145 | #endif // INC_MT_TYPELIST_H_ |
| @@ -0,0 +1,171 @@ | ||
| 1 | + | |
| 2 | +#ifndef INC_MT_TYPELIST_ALGO_FIND_IF_H_ | |
| 3 | +#define INC_MT_TYPELIST_ALGO_FIND_IF_H_ | |
| 4 | + | |
| 5 | +#include "mt_typelist.h" | |
| 6 | +#include "mt_mpl.h" | |
| 7 | +#include "mt_sfinae.h" | |
| 8 | +#include "mt_type_identity.h" | |
| 9 | + | |
| 10 | +namespace mt { | |
| 11 | + | |
| 12 | +template <typename T> | |
| 13 | +struct HasCtorType | |
| 14 | +{ | |
| 15 | + template <typename X> | |
| 16 | + static YesType test(typename X::CtorType*); | |
| 17 | + | |
| 18 | + template <typename X> | |
| 19 | + static NoType test(...); | |
| 20 | + | |
| 21 | + static const bool value = sizeof(test<T>(0)) != sizeof(NoType); | |
| 22 | +}; | |
| 23 | + | |
| 24 | +template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 25 | +T getObject(Arg1&, Arg2&, Arg3&, Arg4&, typename EnableIf<!HasCtorType<T>::value>::Result* = 0) | |
| 26 | +{ | |
| 27 | + return T(); | |
| 28 | +} | |
| 29 | + | |
| 30 | +template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 31 | +T getObject(Arg1& arg1, Arg2&, Arg3&, Arg4&, typename EnableIf<IsIdentical<typename T::CtorType, StaticValue<int, 1> >::value>::Result* = 0) | |
| 32 | +{ | |
| 33 | + return T(arg1); | |
| 34 | +} | |
| 35 | + | |
| 36 | +template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 37 | +T getObject(Arg1& arg1, Arg2& arg2, Arg3&, Arg4&, typename EnableIf<IsIdentical<typename T::CtorType, StaticValue<int, 2> >::value>::Result* = 0) | |
| 38 | +{ | |
| 39 | + return T(arg1, arg2); | |
| 40 | +} | |
| 41 | + | |
| 42 | +template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 43 | +T getObject(Arg1& arg1, Arg2& arg2, Arg3& arg3, Arg4&, typename EnableIf<IsIdentical<typename T::CtorType, StaticValue<int, 3> >::value>::Result* = 0) | |
| 44 | +{ | |
| 45 | + return T(arg1, arg2, arg3); | |
| 46 | +} | |
| 47 | + | |
| 48 | +template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 49 | +T getObject(Arg1& arg1, Arg2& arg2, Arg3& arg3, Arg4& arg4, typename EnableIf<IsIdentical<typename T::CtorType, StaticValue<int, 4> >::value>::Result* = 0) | |
| 50 | +{ | |
| 51 | + return T(arg1, arg2, arg3, arg4); | |
| 52 | +} | |
| 53 | + | |
| 54 | +static NullType nulltype_obj; | |
| 55 | + | |
| 56 | +template <typename TL, typename Arg1 = NullType, typename Arg2 = NullType, | |
| 57 | + typename Arg3 = NullType, typename Arg4 = NullType> | |
| 58 | +struct FindIf_t; | |
| 59 | + | |
| 60 | +// FindIf_t class template would require getFindIf_tKey() overloaded function set | |
| 61 | +// to derive the key of the typelist element types respectively. | |
| 62 | + | |
| 63 | +template <typename T, typename U, | |
| 64 | + typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 65 | +struct FindIf_t< Typelist<T, U>, Arg1, Arg2, Arg3, Arg4 > | |
| 66 | +{ | |
| 67 | + FindIf_t() : arg1_(nulltype_obj), arg2_(nulltype_obj), arg3_(nulltype_obj), arg4_(nulltype_obj) | |
| 68 | + {} | |
| 69 | + FindIf_t(Arg1& arg1) : arg1_(arg1), arg2_(nulltype_obj), arg3_(nulltype_obj), arg4_(nulltype_obj) | |
| 70 | + {} | |
| 71 | + FindIf_t(Arg1& arg1, Arg2& arg2) : arg1_(arg1), arg2_(arg2), arg3_(nulltype_obj), arg4_(nulltype_obj) | |
| 72 | + {} | |
| 73 | + FindIf_t(Arg1& arg1, Arg2& arg2, Arg3& arg3) : arg1_(arg1), arg2_(arg2), arg3_(arg3), arg4_(nulltype_obj) | |
| 74 | + {} | |
| 75 | + FindIf_t(Arg1& arg1, Arg2& arg2, Arg3& arg3, Arg4& arg4) : arg1_(arg1), arg2_(arg2), arg3_(arg3), arg4_(arg4) | |
| 76 | + {} | |
| 77 | + | |
| 78 | + template <typename V, typename Key> | |
| 79 | + bool operator()(V& obj, const Key& key) | |
| 80 | + { | |
| 81 | + T t(getObject<T>(arg1_, arg2_, arg3_, arg4_)); | |
| 82 | + return handleIt(t, obj, key); | |
| 83 | + } | |
| 84 | + | |
| 85 | + template <typename V, typename Key> | |
| 86 | + bool handleIt(T& t, V& obj, const Key& key) | |
| 87 | + { | |
| 88 | + if (key == (getFindIfKey(t))()) { | |
| 89 | + t.operator()(obj); | |
| 90 | + return true; | |
| 91 | + } | |
| 92 | + return FindIf_t<U, Arg1, Arg2, Arg3, Arg4>(arg1_, arg2_, arg3_, arg4_)(obj, key); | |
| 93 | + } | |
| 94 | + | |
| 95 | + Arg1& arg1_; | |
| 96 | + Arg2& arg2_; | |
| 97 | + Arg3& arg3_; | |
| 98 | + Arg4& arg4_; | |
| 99 | +}; | |
| 100 | + | |
| 101 | +template <typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 102 | +struct FindIf_t< Typelist<T, NullType>, Arg1, Arg2, Arg3, Arg4> | |
| 103 | +{ | |
| 104 | + FindIf_t() : arg1_(nulltype_obj), arg2_(nulltype_obj), arg3_(nulltype_obj), arg4_(nulltype_obj) | |
| 105 | + {} | |
| 106 | + FindIf_t(Arg1& arg1) : arg1_(arg1), arg2_(nulltype_obj), arg3_(nulltype_obj), arg4_(nulltype_obj) | |
| 107 | + {} | |
| 108 | + FindIf_t(Arg1& arg1, Arg2& arg2) : arg1_(arg1), arg2_(arg2), arg3_(nulltype_obj), arg4_(nulltype_obj) | |
| 109 | + {} | |
| 110 | + FindIf_t(Arg1& arg1, Arg2& arg2, Arg3& arg3) : arg1_(arg1), arg2_(arg2), arg3_(arg3), arg4_(nulltype_obj) | |
| 111 | + {} | |
| 112 | + FindIf_t(Arg1& arg1, Arg2& arg2, Arg3& arg3, Arg4& arg4) : arg1_(arg1), arg2_(arg2), arg3_(arg3), arg4_(arg4) | |
| 113 | + {} | |
| 114 | + | |
| 115 | + template <typename V, typename Key> | |
| 116 | + bool operator()(V& obj, const Key& key) | |
| 117 | + { | |
| 118 | + T t(getObject<T>(arg1_, arg2_, arg3_, arg4_)); | |
| 119 | + return handleIt(t, obj, key); | |
| 120 | + } | |
| 121 | + | |
| 122 | + template <typename V, typename Key> | |
| 123 | + bool handleIt(T& t, V& obj, const Key& key) | |
| 124 | + { | |
| 125 | + if (key == (getFindIfKey(t))()) { | |
| 126 | + t.operator()(obj); | |
| 127 | + return true; | |
| 128 | + } | |
| 129 | + return false; | |
| 130 | + } | |
| 131 | + | |
| 132 | + Arg1& arg1_; | |
| 133 | + Arg2& arg2_; | |
| 134 | + Arg3& arg3_; | |
| 135 | + Arg4& arg4_; | |
| 136 | +}; | |
| 137 | + | |
| 138 | +template <typename TL> | |
| 139 | +inline FindIf_t<TL> FindIf() | |
| 140 | +{ | |
| 141 | + return FindIf_t<TL>(); | |
| 142 | +} | |
| 143 | + | |
| 144 | +template <typename TL, typename Arg1> | |
| 145 | +inline FindIf_t<TL, Arg1> FindIf(Arg1& arg1) | |
| 146 | +{ | |
| 147 | + return FindIf_t<TL, Arg1>(arg1); | |
| 148 | +} | |
| 149 | + | |
| 150 | +template <typename TL, typename Arg1, typename Arg2> | |
| 151 | +inline FindIf_t<TL, Arg1, Arg2> FindIf(Arg1& arg1, Arg2& arg2) | |
| 152 | +{ | |
| 153 | + return FindIf_t<TL, Arg1, Arg2>(arg1, arg2); | |
| 154 | +} | |
| 155 | + | |
| 156 | +template <typename TL, typename Arg1, typename Arg2, typename Arg3> | |
| 157 | +inline FindIf_t<TL, Arg1, Arg2, Arg3> FindIf(Arg1& arg1, Arg2& arg2, Arg3& arg3) | |
| 158 | +{ | |
| 159 | + return FindIf_t<TL, Arg1, Arg2, Arg3>(arg1, arg2, arg3); | |
| 160 | +} | |
| 161 | + | |
| 162 | +template <typename TL, typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 163 | +inline FindIf_t<TL, Arg1, Arg2, Arg3, Arg4> FindIf(Arg1& arg1, Arg2& arg2, Arg3& arg3, Arg4& arg4) | |
| 164 | +{ | |
| 165 | + return FindIf_t<TL, Arg1, Arg2, Arg3, Arg4>(arg1, arg2, arg3, arg4); | |
| 166 | +} | |
| 167 | + | |
| 168 | +} // namespace mt | |
| 169 | + | |
| 170 | +#endif // INC_MT_TYPELIST_ALGO_FIND_IF_H_ | |
| 171 | + |
| @@ -0,0 +1,38 @@ | ||
| 1 | + | |
| 2 | +#ifndef INC_MT_TYPELIST_ALGO_FOREACH_H_ | |
| 3 | +#define INC_MT_TYPELIST_ALGO_FOREACH_H_ | |
| 4 | + | |
| 5 | +#include "mt_typelist.h" | |
| 6 | + | |
| 7 | +namespace mt { | |
| 8 | + | |
| 9 | +template <typename TL> | |
| 10 | +struct ForEach; | |
| 11 | + | |
| 12 | +template <typename T, typename U> | |
| 13 | +struct ForEach< Typelist<T, U> > | |
| 14 | +{ | |
| 15 | + template <typename V> | |
| 16 | + void operator()(V& obj) | |
| 17 | + { | |
| 18 | + T().operator()(obj); | |
| 19 | + | |
| 20 | + ForEach<U>()(obj); | |
| 21 | + return; | |
| 22 | + } | |
| 23 | +}; | |
| 24 | + | |
| 25 | +template <typename T> | |
| 26 | +struct ForEach< Typelist<T, NullType> > | |
| 27 | +{ | |
| 28 | + template <typename V> | |
| 29 | + void operator()(V& obj) | |
| 30 | + { | |
| 31 | + T().operator()(obj); | |
| 32 | + } | |
| 33 | +}; | |
| 34 | + | |
| 35 | +} // namespace mt | |
| 36 | + | |
| 37 | +#endif // INC_MT_TYPELIST_ALGO_FOREACH_H_ | |
| 38 | + |
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | |
| 2 | 2 | #include "gtest/gtest.h" |
| 3 | 3 | #include "mt_typelist.h" |
| 4 | +#include "mt_typelist_algo_find_if.h" | |
| 4 | 5 | |
| 5 | 6 | namespace mt { |
| 6 | 7 |
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | |
| 2 | 2 | #include "gtest/gtest.h" |
| 3 | 3 | #include "mt_typelist.h" |
| 4 | +#include "mt_typelist_algo_foreach.h" | |
| 4 | 5 | |
| 5 | 6 | namespace mt { |
| 6 | 7 |