• 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_H
18 #define FRUIT_COMPONENT_FUNCTION_H
19 
20 #include <fruit/impl/fruit_internal_forward_decls.h>
21 
22 namespace fruit {
23 
24 /**
25  * See fruit::componentFunction() helper for how to construct a ComponentFunction, and see
26  * PartialComponent::installComponentFunctions() for more information on using ComponentFunction objects.
27  */
28 template <typename ComponentType, typename... ComponentFunctionArgs>
29 class ComponentFunction {
30 private:
31     ComponentType (*getComponent)(ComponentFunctionArgs...);
32     std::tuple<ComponentFunctionArgs...> args_tuple;
33 
34     /**
35      * This is (intentionally) private, use fruit::componentFunction() to construct ComponentFunction objects.
36      */
37     ComponentFunction(ComponentType (*getComponent)(ComponentFunctionArgs...), ComponentFunctionArgs... args);
38 
39     friend struct fruit::impl::ComponentStorageEntry;
40 
41 public:
42 	// Prefer using the simpler componentFunction() below instead of this.
43 	template <typename... ActualArgs>
44     static ComponentFunction<ComponentType, ComponentFunctionArgs...> create(
45 		ComponentType (*getComponent)(ComponentFunctionArgs...), ActualArgs&&... args);
46 
47     ComponentFunction(const ComponentFunction&) = default;
48     ComponentFunction(ComponentFunction&&) = default;
49 
50     ComponentFunction& operator=(const ComponentFunction&) = default;
51     ComponentFunction& operator=(ComponentFunction&&) = default;
52 
53     ComponentType operator()();
54 };
55 
56 
57 /**
58  * This function allows to easily construct a ComponentFunction without explicitly mentioning its type.
59  * See PartialComponent::installComponentFunctions() for more information on using ComponentFunction.
60  */
61 template <typename... ComponentParams, typename... FormalArgs, typename... ActualArgs>
62 ComponentFunction<fruit::Component<ComponentParams...>, FormalArgs...> componentFunction(
63     fruit::Component<ComponentParams...> (*getComponent)(FormalArgs...),
64     ActualArgs&&... args);
65 
66 }
67 
68 #include <fruit/impl/component_function.defn.h>
69 
70 #endif // FRUIT_COMPONENT_FUNCTION_H
71