Skip to content

Commit

Permalink
more documentations
Browse files Browse the repository at this point in the history
  • Loading branch information
Saeed Barari committed Jul 22, 2023
1 parent 9f3e92d commit ee99dda
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
23 changes: 19 additions & 4 deletions BContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ void OnValidate() {
#endif

void Awake() {
_structDependencies.AddRange( StructDependencies_Serialized );
_structDependencies.Clear();
AddAllSerializedStructs();
if (!_initialized) {
SyncAllDependencyTypes( true );
BinjectManager.AddContext( this );
Expand Down Expand Up @@ -173,7 +174,7 @@ public void Bind<T>(T dependency) {
/// <summary>
/// Unbinds a dependency from this context.
/// </summary>
void Unbind<T>() {
public void Unbind<T>() {
if (_dependencyTypes.Remove( typeof(T) )) {
if (typeof(T).IsValueType) {
for (int i = 0; i < _structDependencies.Count; i++) {
Expand Down Expand Up @@ -232,10 +233,19 @@ public T GetDependencyNoCheck<T>() {
Debug.LogWarning( $"No dependency of type {typeof(T).FullName} found. returning default/null." );
return default;
}


[MethodImpl( MethodImplOptions.AggressiveInlining )]
internal void SyncAllDependencyTypes(bool clear) {
void AddAllSerializedStructs() {
for (int i = 0; i < StructDependencies_Serialized.Count; i++)
_structDependencies.Add( StructDependencies_Serialized[i] );
}

/// <summary>
/// Stores types of all dependencies to <see cref="_dependencyTypes"/>.
/// </summary>
[MethodImpl( MethodImplOptions.AggressiveInlining )]
void SyncAllDependencyTypes(bool clear) {
if (clear) _dependencyTypes.Clear();
for (int i = 0; i < ClassDependencies.Count; i++)
_dependencyTypes.Add( ClassDependencies[i].GetType() );
Expand All @@ -245,6 +255,11 @@ internal void SyncAllDependencyTypes(bool clear) {
_dependencyTypes.Add( _structDependencies[i].GetValueType() );
}

/// <summary>
/// Tries to find the dependency from the given type, from fields <see cref="_structDependencies"/>,
/// <see cref="UnityObjectDependencies"/> and <see cref="ClassDependencies"/> respectively. <para/>
/// (It won't allocate garbage if <see cref="T"/> is a value type.)
/// </summary>
[MethodImpl( MethodImplOptions.AggressiveInlining )]
bool TryFindDependency<T>(out T result) {
if (typeof(T).IsValueType) {
Expand Down
17 changes: 13 additions & 4 deletions RealValueHolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
using UnityEngine;

namespace Binject {

/// <summary>
/// Types implementing this will be responsible for saving a <see cref="ValueType"/> data inside and provide
/// external access to it. This interface makes sure a <see cref="System.Collections.Generic.List{T}"/> can use any
/// structs.
/// </summary>
internal interface IValueHolder {
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public Type GetValueType();
Expand All @@ -14,6 +20,9 @@ internal interface IValueHolder {
public void BoxAndSetValue(object value);
}

/// <summary>
/// Any data stored in this will be boxed, but it's useful for showing the data inside in Unity Editor.
/// </summary>
[Serializable]
internal class BoxedValueHolder : IValueHolder {
[SerializeReference] public object Value;
Expand All @@ -24,11 +33,11 @@ internal class BoxedValueHolder : IValueHolder {
public void BoxAndSetValue(object value) => Value = value;
}

internal interface ITest<T>{ }


/// <summary>
/// This will store real data and provides direct access to it without boxing.
/// </summary>
[Serializable]
internal struct RealValueHolder<T> : IValueHolder {
internal class RealValueHolder<T> : IValueHolder {
public T Value;
public RealValueHolder(T value) => Value = value;
public Type GetValueType() => typeof(T);
Expand Down
12 changes: 10 additions & 2 deletions RealValueHolder.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ee99dda

Please sign in to comment.