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