-
-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix collection resolve on parent #321
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
40f5bd1
add broken test
adarapata 0f34fea
Passed test but dirty code
adarapata a44595e
Move Annotation Check
adarapata 11a461f
refactor
adarapata 2157ec2
fix indent
adarapata 83a6664
fix line
adarapata ae80e02
Optimize CollectionRegistration
hadashiA f8e2135
Add ContainerLocal<T>
hadashiA 0c6e050
Fix for ecs
hadashiA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
VContainer/Assets/VContainer/Runtime/Internal/CollectionRegistration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace VContainer.Internal | ||
{ | ||
sealed class CollectionRegistration : IRegistration, IEnumerable<IRegistration> | ||
{ | ||
public struct Enumerator : IEnumerator<IRegistration> | ||
{ | ||
public IRegistration Current => listEnumerator.Current; | ||
object IEnumerator.Current => Current; | ||
|
||
List<IRegistration>.Enumerator listEnumerator; | ||
|
||
public Enumerator(CollectionRegistration owner) | ||
{ | ||
listEnumerator = owner.registrations.GetEnumerator(); | ||
} | ||
|
||
public bool MoveNext() => listEnumerator.MoveNext(); | ||
public void Dispose() => listEnumerator.Dispose(); | ||
void IEnumerator.Reset() => ((IEnumerator)listEnumerator).Reset(); | ||
} | ||
|
||
public static bool Match(Type openGenericType) => openGenericType == typeof(IEnumerable<>) || | ||
openGenericType == typeof(IReadOnlyList<>); | ||
|
||
public Enumerator GetEnumerator() => new Enumerator(this); | ||
IEnumerator<IRegistration> IEnumerable<IRegistration>.GetEnumerator() => new Enumerator(this); | ||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
|
||
public Type ImplementationType { get; } | ||
public IReadOnlyList<Type> InterfaceTypes => interfaceTypes; | ||
public Lifetime Lifetime => Lifetime.Transient; // Collection reference is transient. So its members can have each lifetimes. | ||
|
||
readonly Type elementType; | ||
|
||
readonly List<Type> interfaceTypes; | ||
readonly List<IRegistration> registrations = new List<IRegistration>(); | ||
|
||
public CollectionRegistration(Type elementType) | ||
{ | ||
this.elementType = elementType; | ||
ImplementationType = elementType.MakeArrayType(); | ||
interfaceTypes = new List<Type> | ||
{ | ||
RuntimeTypeCache.EnumerableTypeOf(elementType), | ||
RuntimeTypeCache.ReadOnlyListTypeOf(elementType), | ||
}; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
var contractTypes = InterfaceTypes != null ? string.Join(", ", InterfaceTypes) : ""; | ||
return $"CollectionRegistration {ImplementationType} ContractTypes=[{contractTypes}] {Lifetime}"; | ||
} | ||
|
||
public void Add(IRegistration registration) | ||
{ | ||
foreach (var x in registrations) | ||
{ | ||
if (x.Lifetime == Lifetime.Singleton && x.ImplementationType == registration.ImplementationType) | ||
{ | ||
throw new VContainerException(registration.ImplementationType, $"Conflict implementation type : {registration}"); | ||
} | ||
} | ||
registrations.Add(registration); | ||
} | ||
|
||
public void Merge(CollectionRegistration other) | ||
{ | ||
foreach (var x in other.registrations) | ||
{ | ||
Add(x); | ||
} | ||
} | ||
|
||
public object SpawnInstance(IObjectResolver resolver) | ||
{ | ||
var array = Array.CreateInstance(elementType, registrations.Count); | ||
for (var i = 0; i < registrations.Count; i++) | ||
{ | ||
array.SetValue(resolver.Resolve(registrations[i]), i); | ||
} | ||
return array; | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
VContainer/Assets/VContainer/Runtime/Internal/CollectionRegistration.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
VContainer/Assets/VContainer/Runtime/Internal/ContainerLocal.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace VContainer.Internal | ||
{ | ||
public sealed class ContainerLocal<T> | ||
{ | ||
public readonly T Value; | ||
|
||
public ContainerLocal(T value) | ||
{ | ||
Value = value; | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
VContainer/Assets/VContainer/Runtime/Internal/ContainerLocal.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
VContainer/Assets/VContainer/Runtime/Internal/ContainerLocalRegistration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace VContainer.Internal | ||
{ | ||
public sealed class ContainerLocalRegistration : IRegistration | ||
{ | ||
public Type ImplementationType { get; } | ||
public IReadOnlyList<Type> InterfaceTypes { get; } | ||
public Lifetime Lifetime { get; } | ||
|
||
readonly IRegistration valueRegistration; | ||
|
||
public ContainerLocalRegistration(Type implementationType, IRegistration valueRegistration) | ||
{ | ||
ImplementationType = implementationType; | ||
Lifetime = valueRegistration.Lifetime; | ||
this.valueRegistration = valueRegistration; | ||
} | ||
|
||
public object SpawnInstance(IObjectResolver resolver) | ||
{ | ||
var value = resolver.Resolve(valueRegistration); | ||
var parameterValues = CappedArrayPool<object>.Shared8Limit.Rent(1); | ||
try | ||
{ | ||
parameterValues[0] = value; | ||
return Activator.CreateInstance(ImplementationType, parameterValues); | ||
} | ||
finally | ||
{ | ||
CappedArrayPool<object>.Shared8Limit.Return(parameterValues); | ||
} | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
VContainer/Assets/VContainer/Runtime/Internal/ContainerLocalRegistration.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimization for
CollectionRegistration.GetEnumerator()