/* * Copyright 2014 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FRUIT_COMPONENT_DEFN_H #define FRUIT_COMPONENT_DEFN_H #include #include #include #include #include namespace fruit { namespace impl { namespace meta { // This is a helper class used in the implementation of Component and PartialComponent. // It's in fruit::impl::meta so that we don't need to qualify everything with fruit::impl::meta. template struct OpForComponent { template using ConvertTo = Eval, ProcessDeferredBindings, Id...), ConstructComponentImpl())>; template using AddBinding = Eval, Id...), ConstructComponentImpl())>; }; } // namespace meta } // namespace impl template template inline Component::Component(PartialComponent&& partial_component) noexcept : storage() { (void)typename fruit::impl::meta::CheckIfError::type(); using Op = typename fruit::impl::meta::OpForComponent::template ConvertTo; (void)typename fruit::impl::meta::CheckIfError::type(); #if !FRUIT_NO_LOOP_CHECK (void)typename fruit::impl::meta::CheckIfError< fruit::impl::meta::Eval>::type(); #endif // !FRUIT_NO_LOOP_CHECK std::size_t num_entries = partial_component.storage.numBindings() + Op().numEntries(); fruit::impl::FixedSizeVector entries(num_entries); Op()(entries); // addBindings may modify the storage member of PartialComponent. // Therefore, it should not be used after this operation. partial_component.storage.addBindings(entries); // TODO: re-enable this check somehow. // component.component.already_converted_to_component = true; FruitAssert(entries.size() == num_entries); storage = fruit::impl::ComponentStorage(std::move(entries)); } inline PartialComponent<> createComponent() { return {{}}; } template template inline PartialComponent, Bindings...> PartialComponent::bind() { using Op = OpFor>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage}}; } template template inline PartialComponent, Bindings...> PartialComponent::registerConstructor() { using Op = OpFor>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage}}; } template template inline PartialComponent, Bindings...> PartialComponent::bindInstance(C& instance) { using Op = OpFor>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage, instance}}; } template template inline PartialComponent, Bindings...> PartialComponent::bindInstance(const C& instance) { using Op = OpFor>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage, instance}}; } template template inline PartialComponent, Bindings...> PartialComponent::bindInstance(C& instance) { using Op = OpFor>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage, instance}}; } template template inline PartialComponent, Bindings...> PartialComponent::bindInstance(const C& instance) { using Op = OpFor>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage, instance}}; } template template inline PartialComponent, Bindings...> PartialComponent::registerProvider(Lambda) { using Op = OpFor>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage}}; } template template inline PartialComponent, Bindings...> PartialComponent::registerProvider(Lambda) { using Op = OpFor>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage}}; } template template inline PartialComponent, Bindings...> PartialComponent::addMultibinding() { using Op = OpFor>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage}}; } template template inline PartialComponent, Bindings...> PartialComponent::addInstanceMultibinding(C& instance) { using Op = fruit::impl::meta::Eval>)>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage, instance}}; } template template inline PartialComponent, Bindings...> PartialComponent::addInstanceMultibinding(C& instance) { using Op = fruit::impl::meta::Eval>)>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage, instance}}; } template template inline PartialComponent, Bindings...> PartialComponent::addInstanceMultibindings(std::vector& instances) { using Op = fruit::impl::meta::Eval>)>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage, instances}}; } template template inline PartialComponent, Bindings...> PartialComponent::addInstanceMultibindings(std::vector& instances) { using Op = fruit::impl::meta::Eval>)>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage, instances}}; } template template inline PartialComponent, Bindings...> PartialComponent::addMultibindingProvider(Lambda) { using Op = OpFor>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage}}; } template template inline PartialComponent, Bindings...> PartialComponent::addMultibindingProvider(Lambda) { using Op = OpFor>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage}}; } template template inline PartialComponent, Bindings...> PartialComponent::registerFactory(Lambda) { using Op = OpFor>; (void)typename fruit::impl::meta::CheckIfError::type(); return {{storage}}; } template inline PartialComponent::PartialComponent(fruit::impl::PartialComponentStorage storage) : storage(std::move(storage)) {} template template inline PartialComponent(FormalArgs...)>, Bindings...> PartialComponent::install(fruit::Component (*getComponent)(FormalArgs...), Args&&... args) { using IntCollector = int[]; (void)IntCollector{0, fruit::impl::checkAcceptableComponentInstallArg()...}; using Op = OpFor(FormalArgs...)>>; (void)typename fruit::impl::meta::CheckIfError::type(); std::tuple args_tuple{std::forward(args)...}; return {{storage, getComponent, std::move(args_tuple)}}; } template template inline PartialComponent, Bindings...> PartialComponent::installComponentFunctions(ComponentFunctions... componentFunctions) { using Op = OpFor>; (void)typename fruit::impl::meta::CheckIfError::type(); std::tuple component_functions_tuple{std::move(componentFunctions)...}; return {{storage, std::move(component_functions_tuple)}}; } template template inline typename PartialComponent::template PartialComponentWithReplacementInProgress< fruit::Component, FormalArgs...> PartialComponent::replace(fruit::Component (*getReplacedComponent)(FormalArgs...), Args&&... args) { using IntCollector = int[]; (void)IntCollector{0, fruit::impl::checkAcceptableComponentInstallArg()...}; std::tuple args_tuple{std::forward(args)...}; return {{storage, getReplacedComponent, std::move(args_tuple)}}; } template template template inline PartialComponent, Bindings...> PartialComponent:: PartialComponentWithReplacementInProgress::with( OtherComponent (*getReplacementComponent)(GetReplacementComponentFormalArgs...), Args&&... args) { using IntCollector = int[]; (void)IntCollector{0, fruit::impl::checkAcceptableComponentInstallArg()...}; std::tuple args_tuple{std::forward(args)...}; return {{storage, getReplacementComponent, std::move(args_tuple)}}; } } // namespace fruit #endif // FRUIT_COMPONENT_DEFN_H