Skip to content

Commit

Permalink
Add IsInitialized + IsUninitialized api
Browse files Browse the repository at this point in the history
Fix tests

Check IsInitialized in ValueLifetimed + add an assertion that the passed lifetime is initialized
  • Loading branch information
Iliya-usov committed Jan 15, 2025
1 parent ebece17 commit 1145b3b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
32 changes: 24 additions & 8 deletions rd-net/Lifetimes/Lifetimes/Lifetime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,24 @@ internal LifetimeDefinition Definition
var def = myDefinition;
if (def != null) return def;

if (LogErrorIfLifetimeIsNotInitialized)
{
Log.Root.Error("Lifetime is not initialized. " +
"This may cause a memory leak as default(Lifetime) is treated as Eternal. " +
"Please provide a properly initialized Lifetime or use `Lifetime?` if you need to handle both cases. " +
"Use Lifetime.Eternal explicitly if that behavior is intended.");
}

AssertInitialized();
return LifetimeDefinition.Eternal;
}
}

internal void AssertInitialized()
{
if (!Mode.IsAssertion) return;

if (LogErrorIfLifetimeIsNotInitialized && myDefinition == null)
{
Log.Root.Error("Lifetime is not initialized. " +
"This may cause a memory leak as default(Lifetime) is treated as Eternal. " +
"Please provide a properly initialized Lifetime or use `Lifetime?` if you need to handle both cases. " +
"Use Lifetime.Eternal explicitly if that behavior is intended.");
}
}

//ctor
internal Lifetime(LifetimeDefinition definition)
{
Expand Down Expand Up @@ -168,6 +174,16 @@ internal Lifetime(LifetimeDefinition definition)
/// </summary>
[PublicAPI] public bool IsEternal => Definition.IsEternal;

/// <summary>
/// Whether current lifetime is properly initialized
/// </summary>
[PublicAPI] public bool IsUninitialized => !IsInitialized;

/// <summary>
/// Whether current lifetime is not properly initialized and created by default(Lifetime)
/// </summary>
[PublicAPI] public bool IsInitialized => myDefinition != null;

/// <summary>
/// Is <see cref="Status"/> of this lifetime equal to <see cref="LifetimeStatus.Alive"/>
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion rd-net/Lifetimes/Lifetimes/ValueLifetimed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public void Deconstruct(out Lifetime lifetime, out T? value)

public ValueLifetimed(Lifetime lifetime, T value)
{
lifetime.AssertInitialized();

Lifetime = lifetime;
Value = value;
}

public void ClearValueIfNotAlive()
{
if (!Lifetime.IsAlive)
if (Lifetime is { IsInitialized: true, IsAlive: false })
Value = default(T);
}
}
Expand Down
25 changes: 17 additions & 8 deletions rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,25 @@ public void TestEternal()
[Test]
public void TestEquals()
{
Lifetime eternal = default;
Assert.AreEqual(Lifetime.Eternal, eternal);
Assert.AreEqual(Lifetime.Eternal, Lifetime.Eternal);
Assert.AreEqual(eternal, eternal);
var old = Lifetime.LogErrorIfLifetimeIsNotInitialized;
Lifetime.LogErrorIfLifetimeIsNotInitialized = false;
try
{
Lifetime eternal = default;
Assert.AreEqual(Lifetime.Eternal, eternal);
Assert.AreEqual(Lifetime.Eternal, Lifetime.Eternal);
Assert.AreEqual(eternal, eternal);

Assert.True(Lifetime.Eternal == eternal);
Assert.True(Lifetime.Eternal == eternal);

Assert.AreNotEqual(Lifetime.Eternal, Lifetime.Terminated);
Assert.False(Lifetime.Eternal == Lifetime.Terminated);
Assert.False(eternal == Lifetime.Terminated);
Assert.AreNotEqual(Lifetime.Eternal, Lifetime.Terminated);
Assert.False(Lifetime.Eternal == Lifetime.Terminated);
Assert.False(eternal == Lifetime.Terminated);
}
finally
{
Lifetime.LogErrorIfLifetimeIsNotInitialized = old;
}
}

[Test]
Expand Down

0 comments on commit 1145b3b

Please sign in to comment.