Marco Poletti | a5f49d4 | 2014-06-29 10:41:12 +0100 | [diff] [blame^] | 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_PROVIDER_H |
| 18 | #define FRUIT_PROVIDER_H |
| 19 | |
| 20 | namespace fruit { |
| 21 | |
| 22 | namespace impl { |
| 23 | |
| 24 | class ComponentStorage; |
| 25 | |
| 26 | template <typename C> |
| 27 | struct GetHelper; |
| 28 | |
| 29 | } // namespace impl; |
| 30 | |
| 31 | template <typename... P> |
| 32 | class Provider { |
| 33 | private: |
| 34 | using Ps = fruit::impl::List<P...>; |
| 35 | using Comp = Component<P...>; |
| 36 | |
| 37 | FruitDelegateCheck(fruit::impl::CheckNoRequirementsInProvider<typename Comp::Rs>); |
| 38 | FruitDelegateChecks(fruit::impl::CheckClassType<P, fruit::impl::GetClassForType<P>>); |
| 39 | |
| 40 | // This is NOT owned by the provider object. It is not deleted on destruction. |
| 41 | // This is never nullptr. |
| 42 | fruit::impl::ComponentStorage* storage; |
| 43 | |
| 44 | Provider(fruit::impl::ComponentStorage* storage); |
| 45 | |
| 46 | friend class fruit::impl::ComponentStorage; |
| 47 | |
| 48 | template <typename C> |
| 49 | friend struct fruit::impl::GetHelper; |
| 50 | |
| 51 | template <typename... OtherPs> |
| 52 | friend class Injector; |
| 53 | |
| 54 | public: |
| 55 | template <typename T> |
| 56 | T get(); |
| 57 | |
| 58 | /** |
| 59 | * This is a convenient way to call get(). E.g.: |
| 60 | * |
| 61 | * MyInterface* x(injector); |
| 62 | * |
| 63 | * is equivalent to: |
| 64 | * |
| 65 | * MyInterface* x = injector.get<MyInterface*>(); |
| 66 | */ |
| 67 | template <typename T> |
| 68 | explicit operator T(); |
| 69 | }; |
| 70 | |
| 71 | } // namespace fruit |
| 72 | |
| 73 | #include "impl/provider.templates.h" |
| 74 | |
| 75 | #endif // FRUIT_PROVIDER_H |