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