• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_COMPONENT_FUNCTION_DEFN_H
18 #define FRUIT_COMPONENT_FUNCTION_DEFN_H
19 
20 #include <fruit/component_function.h>
21 #include <fruit/impl/util/call_with_tuple.h>
22 #include <fruit/impl/component_install_arg_checks.h>
23 
24 namespace fruit {
25 
26 template <typename ComponentType, typename... ComponentFunctionArgs>
ComponentFunction(ComponentType (* getComponent)(ComponentFunctionArgs...),ComponentFunctionArgs...args)27 inline ComponentFunction<ComponentType, ComponentFunctionArgs...>::ComponentFunction(
28         ComponentType (*getComponent)(ComponentFunctionArgs...), ComponentFunctionArgs... args)
29     : getComponent(getComponent), args_tuple{args...} {
30     using IntCollector = int[];
31     (void)IntCollector{0, fruit::impl::checkAcceptableComponentInstallArg<ComponentFunctionArgs>()...};
32 }
33 
34 template <typename ComponentType, typename... ComponentFunctionArgs>
35 template <typename... ActualArgs>
36 inline ComponentFunction<ComponentType, ComponentFunctionArgs...>
create(ComponentType (* getComponent)(ComponentFunctionArgs...),ActualArgs &&...args)37 ComponentFunction<ComponentType, ComponentFunctionArgs...>::create(
38 	ComponentType(*getComponent)(ComponentFunctionArgs...), ActualArgs&&... args) {
39   return ComponentFunction<ComponentType, ComponentFunctionArgs...>(getComponent, std::forward<ActualArgs>(args)...);
40 }
41 
42 template <typename ComponentType, typename... ComponentFunctionArgs>
operator()43 inline ComponentType ComponentFunction<ComponentType, ComponentFunctionArgs...>::operator()() {
44     return fruit::impl::callWithTuple(getComponent, args_tuple);
45 }
46 
47 template <typename... ComponentParams, typename... FormalArgs, typename... ActualArgs>
componentFunction(fruit::Component<ComponentParams...> (* getComponent)(FormalArgs...),ActualArgs &&...args)48 inline ComponentFunction<fruit::Component<ComponentParams...>, FormalArgs...> componentFunction(
49         fruit::Component<ComponentParams...> (*getComponent)(FormalArgs...),
50         ActualArgs&&... args) {
51     return ComponentFunction<fruit::Component<ComponentParams...>, FormalArgs...>::create(
52         getComponent, std::forward<ActualArgs>(args)...);
53 }
54 
55 }
56 
57 #endif // FRUIT_COMPONENT_FUNCTION_DEFN_H
58