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