Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 1 | package android |
| 2 | |
| 3 | import ( |
| 4 | "github.com/google/blueprint" |
| 5 | ) |
| 6 | |
| 7 | // OtherModuleProviderContext is a helper interface that is a subset of ModuleContext, BottomUpMutatorContext, or |
| 8 | // TopDownMutatorContext for use in OtherModuleProvider. |
| 9 | type OtherModuleProviderContext interface { |
| 10 | otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) |
| 11 | } |
| 12 | |
| 13 | var _ OtherModuleProviderContext = BaseModuleContext(nil) |
| 14 | var _ OtherModuleProviderContext = ModuleContext(nil) |
| 15 | var _ OtherModuleProviderContext = BottomUpMutatorContext(nil) |
| 16 | var _ OtherModuleProviderContext = TopDownMutatorContext(nil) |
Yu Liu | 663e450 | 2024-08-12 18:23:59 +0000 | [diff] [blame] | 17 | var _ OtherModuleProviderContext = SingletonContext(nil) |
| 18 | var _ OtherModuleProviderContext = (*TestContext)(nil) |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 19 | |
| 20 | // OtherModuleProvider reads the provider for the given module. If the provider has been set the value is |
| 21 | // returned and the boolean is true. If it has not been set the zero value of the provider's type is returned |
| 22 | // and the boolean is false. The value returned may be a deep copy of the value originally passed to SetProvider. |
| 23 | // |
| 24 | // OtherModuleProviderContext is a helper interface that accepts ModuleContext, BottomUpMutatorContext, or |
| 25 | // TopDownMutatorContext. |
| 26 | func OtherModuleProvider[K any](ctx OtherModuleProviderContext, module blueprint.Module, provider blueprint.ProviderKey[K]) (K, bool) { |
Yu Liu | dd9ccb4 | 2024-10-07 17:07:44 +0000 | [diff] [blame] | 27 | value, ok := ctx.otherModuleProvider(getWrappedModule(module), provider) |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 28 | if !ok { |
| 29 | var k K |
| 30 | return k, false |
| 31 | } |
| 32 | return value.(K), ok |
| 33 | } |
| 34 | |
Yu Liu | d46e5ae | 2024-08-15 18:46:17 +0000 | [diff] [blame] | 35 | func OtherModuleProviderOrDefault[K any](ctx OtherModuleProviderContext, module blueprint.Module, provider blueprint.ProviderKey[K]) K { |
| 36 | value, _ := OtherModuleProvider(ctx, module, provider) |
| 37 | return value |
| 38 | } |
| 39 | |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 40 | // ModuleProviderContext is a helper interface that is a subset of ModuleContext, BottomUpMutatorContext, or |
| 41 | // TopDownMutatorContext for use in ModuleProvider. |
| 42 | type ModuleProviderContext interface { |
| 43 | provider(provider blueprint.AnyProviderKey) (any, bool) |
| 44 | } |
| 45 | |
| 46 | var _ ModuleProviderContext = BaseModuleContext(nil) |
| 47 | var _ ModuleProviderContext = ModuleContext(nil) |
| 48 | var _ ModuleProviderContext = BottomUpMutatorContext(nil) |
| 49 | var _ ModuleProviderContext = TopDownMutatorContext(nil) |
| 50 | |
| 51 | // ModuleProvider reads the provider for the current module. If the provider has been set the value is |
| 52 | // returned and the boolean is true. If it has not been set the zero value of the provider's type is returned |
| 53 | // and the boolean is false. The value returned may be a deep copy of the value originally passed to SetProvider. |
| 54 | // |
| 55 | // ModuleProviderContext is a helper interface that accepts ModuleContext, BottomUpMutatorContext, or |
| 56 | // TopDownMutatorContext. |
| 57 | func ModuleProvider[K any](ctx ModuleProviderContext, provider blueprint.ProviderKey[K]) (K, bool) { |
| 58 | value, ok := ctx.provider(provider) |
| 59 | if !ok { |
| 60 | var k K |
| 61 | return k, false |
| 62 | } |
| 63 | return value.(K), ok |
| 64 | } |
| 65 | |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 66 | // SetProviderContext is a helper interface that is a subset of ModuleContext, BottomUpMutatorContext, or |
| 67 | // TopDownMutatorContext for use in SetProvider. |
| 68 | type SetProviderContext interface { |
Colin Cross | 24c1cbe | 2023-12-21 23:42:56 +0000 | [diff] [blame] | 69 | setProvider(provider blueprint.AnyProviderKey, value any) |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | var _ SetProviderContext = BaseModuleContext(nil) |
| 73 | var _ SetProviderContext = ModuleContext(nil) |
| 74 | var _ SetProviderContext = BottomUpMutatorContext(nil) |
| 75 | var _ SetProviderContext = TopDownMutatorContext(nil) |
| 76 | |
| 77 | // SetProvider sets the value for a provider for the current module. It panics if not called |
| 78 | // during the appropriate mutator or GenerateBuildActions pass for the provider, if the value |
| 79 | // is not of the appropriate type, or if the value has already been set. The value should not |
| 80 | // be modified after being passed to SetProvider. |
| 81 | // |
| 82 | // SetProviderContext is a helper interface that accepts ModuleContext, BottomUpMutatorContext, or |
| 83 | // TopDownMutatorContext. |
| 84 | func SetProvider[K any](ctx SetProviderContext, provider blueprint.ProviderKey[K], value K) { |
Colin Cross | 24c1cbe | 2023-12-21 23:42:56 +0000 | [diff] [blame] | 85 | ctx.setProvider(provider, value) |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | var _ OtherModuleProviderContext = (*otherModuleProviderAdaptor)(nil) |
| 89 | |
| 90 | // An OtherModuleProviderFunc can be passed to NewOtherModuleProviderAdaptor to create an OtherModuleProviderContext |
| 91 | // for use in tests. |
| 92 | type OtherModuleProviderFunc func(module blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) |
| 93 | |
| 94 | type otherModuleProviderAdaptor struct { |
| 95 | otherModuleProviderFunc OtherModuleProviderFunc |
| 96 | } |
| 97 | |
| 98 | func (p *otherModuleProviderAdaptor) otherModuleProvider(module blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) { |
| 99 | return p.otherModuleProviderFunc(module, provider) |
| 100 | } |
| 101 | |
| 102 | // NewOtherModuleProviderAdaptor returns an OtherModuleProviderContext that proxies calls to otherModuleProvider to |
| 103 | // the provided OtherModuleProviderFunc. It can be used in tests to unit test methods that need to call |
| 104 | // android.OtherModuleProvider. |
| 105 | func NewOtherModuleProviderAdaptor(otherModuleProviderFunc OtherModuleProviderFunc) OtherModuleProviderContext { |
| 106 | return &otherModuleProviderAdaptor{otherModuleProviderFunc} |
| 107 | } |