xref: /aosp_15_r20/external/google-fruit/include/fruit/component_function.h (revision a65addddcf69f38db5b288d787b6b7571a57bb8f)
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     explicit 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&&) noexcept = default;
49 
50     ComponentFunction& operator=(const ComponentFunction&) = default;
51     ComponentFunction& operator=(ComponentFunction&& other) noexcept {
52       args_tuple = std::move(other.args_tuple);
53       return *this;
54     }
55 
56     ComponentType operator()();
57 };
58 
59 
60 /**
61  * This function allows to easily construct a ComponentFunction without explicitly mentioning its type.
62  * See PartialComponent::installComponentFunctions() for more information on using ComponentFunction.
63  */
64 template <typename... ComponentParams, typename... FormalArgs, typename... ActualArgs>
65 ComponentFunction<fruit::Component<ComponentParams...>, FormalArgs...> componentFunction(
66     fruit::Component<ComponentParams...> (*getComponent)(FormalArgs...),
67     ActualArgs&&... args);
68 
69 }
70 
71 #include <fruit/impl/component_function.defn.h>
72 
73 #endif // FRUIT_COMPONENT_FUNCTION_H
74