xref: /aosp_15_r20/external/google-fruit/include/fruit/component_function.h (revision a65addddcf69f38db5b288d787b6b7571a57bb8f)
1*a65addddSAndroid Build Coastguard Worker /*
2*a65addddSAndroid Build Coastguard Worker  * Copyright 2014 Google Inc. All rights reserved.
3*a65addddSAndroid Build Coastguard Worker  *
4*a65addddSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*a65addddSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*a65addddSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*a65addddSAndroid Build Coastguard Worker  *
8*a65addddSAndroid Build Coastguard Worker  *     http://www.apache.org/licenses/LICENSE-2.0
9*a65addddSAndroid Build Coastguard Worker  *
10*a65addddSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*a65addddSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*a65addddSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*a65addddSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*a65addddSAndroid Build Coastguard Worker  * limitations under the License.
15*a65addddSAndroid Build Coastguard Worker  */
16*a65addddSAndroid Build Coastguard Worker 
17*a65addddSAndroid Build Coastguard Worker #ifndef FRUIT_COMPONENT_FUNCTION_H
18*a65addddSAndroid Build Coastguard Worker #define FRUIT_COMPONENT_FUNCTION_H
19*a65addddSAndroid Build Coastguard Worker 
20*a65addddSAndroid Build Coastguard Worker #include <fruit/impl/fruit_internal_forward_decls.h>
21*a65addddSAndroid Build Coastguard Worker 
22*a65addddSAndroid Build Coastguard Worker namespace fruit {
23*a65addddSAndroid Build Coastguard Worker 
24*a65addddSAndroid Build Coastguard Worker /**
25*a65addddSAndroid Build Coastguard Worker  * See fruit::componentFunction() helper for how to construct a ComponentFunction, and see
26*a65addddSAndroid Build Coastguard Worker  * PartialComponent::installComponentFunctions() for more information on using ComponentFunction objects.
27*a65addddSAndroid Build Coastguard Worker  */
28*a65addddSAndroid Build Coastguard Worker template <typename ComponentType, typename... ComponentFunctionArgs>
29*a65addddSAndroid Build Coastguard Worker class ComponentFunction {
30*a65addddSAndroid Build Coastguard Worker private:
31*a65addddSAndroid Build Coastguard Worker     ComponentType (*getComponent)(ComponentFunctionArgs...);
32*a65addddSAndroid Build Coastguard Worker     std::tuple<ComponentFunctionArgs...> args_tuple;
33*a65addddSAndroid Build Coastguard Worker 
34*a65addddSAndroid Build Coastguard Worker     /**
35*a65addddSAndroid Build Coastguard Worker      * This is (intentionally) private, use fruit::componentFunction() to construct ComponentFunction objects.
36*a65addddSAndroid Build Coastguard Worker      */
37*a65addddSAndroid Build Coastguard Worker     explicit ComponentFunction(ComponentType (*getComponent)(ComponentFunctionArgs...), ComponentFunctionArgs... args);
38*a65addddSAndroid Build Coastguard Worker 
39*a65addddSAndroid Build Coastguard Worker     friend struct fruit::impl::ComponentStorageEntry;
40*a65addddSAndroid Build Coastguard Worker 
41*a65addddSAndroid Build Coastguard Worker public:
42*a65addddSAndroid Build Coastguard Worker 	// Prefer using the simpler componentFunction() below instead of this.
43*a65addddSAndroid Build Coastguard Worker 	template <typename... ActualArgs>
44*a65addddSAndroid Build Coastguard Worker     static ComponentFunction<ComponentType, ComponentFunctionArgs...> create(
45*a65addddSAndroid Build Coastguard Worker 		ComponentType (*getComponent)(ComponentFunctionArgs...), ActualArgs&&... args);
46*a65addddSAndroid Build Coastguard Worker 
47*a65addddSAndroid Build Coastguard Worker     ComponentFunction(const ComponentFunction&) = default;
48*a65addddSAndroid Build Coastguard Worker     ComponentFunction(ComponentFunction&&) noexcept = default;
49*a65addddSAndroid Build Coastguard Worker 
50*a65addddSAndroid Build Coastguard Worker     ComponentFunction& operator=(const ComponentFunction&) = default;
51*a65addddSAndroid Build Coastguard Worker     ComponentFunction& operator=(ComponentFunction&& other) noexcept {
52*a65addddSAndroid Build Coastguard Worker       args_tuple = std::move(other.args_tuple);
53*a65addddSAndroid Build Coastguard Worker       return *this;
54*a65addddSAndroid Build Coastguard Worker     }
55*a65addddSAndroid Build Coastguard Worker 
56*a65addddSAndroid Build Coastguard Worker     ComponentType operator()();
57*a65addddSAndroid Build Coastguard Worker };
58*a65addddSAndroid Build Coastguard Worker 
59*a65addddSAndroid Build Coastguard Worker 
60*a65addddSAndroid Build Coastguard Worker /**
61*a65addddSAndroid Build Coastguard Worker  * This function allows to easily construct a ComponentFunction without explicitly mentioning its type.
62*a65addddSAndroid Build Coastguard Worker  * See PartialComponent::installComponentFunctions() for more information on using ComponentFunction.
63*a65addddSAndroid Build Coastguard Worker  */
64*a65addddSAndroid Build Coastguard Worker template <typename... ComponentParams, typename... FormalArgs, typename... ActualArgs>
65*a65addddSAndroid Build Coastguard Worker ComponentFunction<fruit::Component<ComponentParams...>, FormalArgs...> componentFunction(
66*a65addddSAndroid Build Coastguard Worker     fruit::Component<ComponentParams...> (*getComponent)(FormalArgs...),
67*a65addddSAndroid Build Coastguard Worker     ActualArgs&&... args);
68*a65addddSAndroid Build Coastguard Worker 
69*a65addddSAndroid Build Coastguard Worker }
70*a65addddSAndroid Build Coastguard Worker 
71*a65addddSAndroid Build Coastguard Worker #include <fruit/impl/component_function.defn.h>
72*a65addddSAndroid Build Coastguard Worker 
73*a65addddSAndroid Build Coastguard Worker #endif // FRUIT_COMPONENT_FUNCTION_H
74