Home | History | Annotate | Download | only in meta
      1 /*
      2  * Copyright 2014 Google Inc. All rights reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef FRUIT_META_ERRORS_H
     18 #define FRUIT_META_ERRORS_H
     19 
     20 #include <fruit/impl/meta/basics.h>
     21 #include <fruit/impl/meta/logical_operations.h>
     22 
     23 namespace fruit {
     24 namespace impl {
     25 namespace meta {
     26 
     27 template <typename T>
     28 struct CheckIfError {
     29   using type = T;
     30 };
     31 
     32 template <typename ErrorTag, typename... ErrorArgs>
     33 struct CheckIfError<Error<ErrorTag, ErrorArgs...>> {
     34   using type = typename ErrorTag::template apply<ErrorArgs...>;
     35 };
     36 
     37 // ConstructError(ErrorTag, Args...) returns Error<ErrorTag, Args...>.
     38 // Never construct an Error<...> directly, using this metafunction makes debugging easier.
     39 struct ConstructError {
     40   template <typename ErrorTag, typename... Args>
     41   struct apply {
     42 #if FRUIT_DEEP_TEMPLATE_INSTANTIATION_STACKTRACES_FOR_ERRORS
     43     static_assert(true || sizeof(typename CheckIfError<Error<ErrorTag, UnwrapType<Args>...>>::type), "");
     44 #endif
     45     using type = Error<ErrorTag, typename TypeUnwrapper<Args>::type...>;
     46   };
     47 };
     48 
     49 // Extracts the first error in the given types.
     50 struct ExtractFirstError {
     51   template <typename... Types>
     52   struct apply;
     53 
     54   template <typename Type, typename... Types>
     55   struct apply<Type, Types...> : public apply<Types...> {};
     56 
     57   template <typename ErrorTag, typename... ErrorParams, typename... Types>
     58   struct apply<Error<ErrorTag, ErrorParams...>, Types...> {
     59     using type = Error<ErrorTag, ErrorParams...>;
     60   };
     61 };
     62 
     63 } // namespace meta
     64 } // namespace impl
     65 } // namespace fruit
     66 
     67 #endif // FRUIT_META_ERRORS_H
     68