Skip to content

Commit

Permalink
[.NET] Address review comments re: formatting and typos
Browse files Browse the repository at this point in the history
#1331
* Ensures lines in source files are a maximum of 88 characters
* Ensures all if statements use blocks
* Trims spurious end-of-line whitespace
* Adds licensing preamble to source files
  • Loading branch information
burkenyo authored and speth committed Aug 17, 2022
1 parent 0ffe11f commit c8192bf
Show file tree
Hide file tree
Showing 28 changed files with 298 additions and 123 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -21,6 +21,7 @@ Vishesh Devgan (@vdevgan)
Thomas Fiala (@thomasfiala), Technische Universität München
David Fronczek
Mark E. Fuller (@mefuller), Technion
Sammo Gabay (@Burkenyo)
Matteo Giani (@MarcDuQuesne)
Dave Goodwin, California Institute of Technology
China Hagström (@chinahg), Massachusetts Institute of Technology
Expand Down
5 changes: 3 additions & 2 deletions include/cantera/base/ExternalLogger.h
Expand Up @@ -12,8 +12,9 @@ class ExternalLogger : public Logger
{
public:
explicit ExternalLogger(Writer writer) {
if (writer == nullptr){
throw CanteraError("ExternalLogger::ExternalLogger", "Argument “writer” must not be null!");
if (writer == nullptr) {
throw CanteraError("ExternalLogger::ExternalLogger",
"Argument “writer” must not be null!");
}

m_writer = writer;
Expand Down
3 changes: 2 additions & 1 deletion include/cantera/clib/clib_defs.h
Expand Up @@ -38,6 +38,7 @@

// Used by external logger
enum LogLevel { INFO, WARN , ERROR };
typedef void (*Writer)(enum LogLevel logLevel, const char* category, const char* message);
typedef void
(*Writer)(enum LogLevel logLevel, const char* category, const char* message);

#endif
13 changes: 9 additions & 4 deletions interfaces/dotnet/Cantera.Tests/src/ApplicationTest.cs
@@ -1,3 +1,6 @@
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.

using System.Reflection;
using System.Text.RegularExpressions;
using Cantera.Interop;
Expand Down Expand Up @@ -67,7 +70,7 @@ public void LogWriter_ConsoleLogged()
Console.SetOut(consoleOut);
Application.AddConsoleLogging();
ProduceLogOutput();

var output = consoleOut.ToString();

const string prefix = "INFO (Info) ";
Expand All @@ -76,9 +79,11 @@ public void LogWriter_ConsoleLogged()

Assert.Matches('^' + Regex.Escape(prefix), output);

var nowString = output.Substring(prefix.Length, lengthOfIso8601FormattedString);
var nowString = output.Substring(
prefix.Length, lengthOfIso8601FormattedString);

Assert.True(DateTimeOffset.TryParseExact(nowString, iso8601FormatString, null, default, out _));
Assert.True(DateTimeOffset.TryParseExact(
nowString, iso8601FormatString, null, default, out _));
}
finally
{
Expand All @@ -97,4 +102,4 @@ static void ProduceLogOutput()

InteropUtil.CheckReturn(LibCantera.thermo_print(handle, InteropConsts.True, 0));
}
}
}
3 changes: 3 additions & 0 deletions interfaces/dotnet/Cantera.Tests/src/CanteraExceptionTest.cs
@@ -1,3 +1,6 @@
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.

using Cantera.Interop;
using Xunit;

Expand Down
17 changes: 14 additions & 3 deletions interfaces/dotnet/Cantera.Tests/src/EnumsTest.cs
@@ -1,3 +1,6 @@
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.

using System.Reflection;
using System.Runtime.CompilerServices;
using Xunit;
Expand Down Expand Up @@ -42,7 +45,8 @@ public void ThermoPair_ToStringsCorrently()
public void LogLevel_MapsCorrectly() =>
TestInteropEnumInvariants<LogLevel>(true, 0, 2);

static void TestInteropEnumInvariants<TEnum>(bool contiguous = false, int? min = null, int? max = null)
static void TestInteropEnumInvariants<TEnum>(bool contiguous = false,
int? min = null, int? max = null)
where TEnum : struct, Enum
{
Assert.Equal(typeof(int), Enum.GetUnderlyingType(typeof(TEnum)));
Expand All @@ -53,15 +57,22 @@ static void TestInteropEnumInvariants<TEnum>(bool contiguous = false, int? min =
.OrderBy(v => v)
.ToList();

if (contiguous) for (var i = 1; i < values.Count; i++)
if (contiguous)
{
Assert.Equal(1, values[i] - values[i - 1]);
for (var i = 1; i < values.Count; i++)
{
Assert.Equal(1, values[i] - values[i - 1]);
}
}

if (min is not null)
{
Assert.Equal(min, values.Min());
}

if (max is not null)
{
Assert.Equal(max, values.Max());
}
}
}
5 changes: 4 additions & 1 deletion interfaces/dotnet/Cantera.Tests/src/TestFixtures.cs
@@ -1,11 +1,14 @@
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.

using Xunit;

namespace Cantera.Tests;

public class ApplicationFixture
{
public ApplicationFixture()
{
{
Application.DataDirectories.AddAssemblyDirectory();
}
}
Expand Down
5 changes: 4 additions & 1 deletion interfaces/dotnet/Cantera.Tests/src/ThermoPhaseTest.cs
@@ -1,3 +1,6 @@
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.

using Xunit;

namespace Cantera.Tests;
Expand All @@ -7,7 +10,7 @@ public class ThermoPhaseTest
{
[Fact]
public void ThermoPhase_SpeciesRetrieved()
{
{
using var thermo = Application.CreateThermoPhase("gri30.yaml");

Assert.NotEmpty(thermo.Species);
Expand Down
21 changes: 16 additions & 5 deletions interfaces/dotnet/Cantera/src/Application.cs
@@ -1,3 +1,6 @@
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.

using Cantera.Interop;

namespace Cantera;
Expand Down Expand Up @@ -29,9 +32,11 @@ public static class Application
static Application()
{
_invokeMessageLoggedDelegate = (level, category, message) =>
MessageLogged?.Invoke(null, new LogMessageEventArgs(level, category, message));
MessageLogged
?.Invoke(null, new LogMessageEventArgs(level, category, message));

InteropUtil.CheckReturn(LibCantera.ct_setLogWriter(_invokeMessageLoggedDelegate));
InteropUtil.CheckReturn(
LibCantera.ct_setLogWriter(_invokeMessageLoggedDelegate));
}

/// <summary>
Expand All @@ -40,8 +45,9 @@ static Application()
/// <remarks>
/// ct_setLogWriter() needs a delegate which is marshalled as a function pointer to
/// the C++ Cantera library. We could create one implicitly when calling
/// <c>LibCantera.ct_setLogWriter(LogMessage)</c>, but the garbage collector would not know
/// the native method is using it and could reclaim it. By explicitly storing it as
/// <c>LibCantera.ct_setLogWriter(LogMessage)</c>, but the garbage collector would
/// not know the native method is using it and could reclaim it. By explicitly
/// storing it as
/// a class member, we ensure it is not collected until the class is.
/// </remarks>
static LibCantera.Writer? _invokeMessageLoggedDelegate;
Expand Down Expand Up @@ -87,11 +93,16 @@ static void LogToConsole(object? sender, LogMessageEventArgs e)
var message = $"{logLevel} ({e.Category}) {nowString}: {e.Message}";

if (e.LogLevel == LogLevel.Error)
{
Console.Error.WriteLine(message);
}
else
{
Console.WriteLine(message);
}
}

public static ThermoPhase CreateThermoPhase(string filename, string? phasename = null) =>
public static ThermoPhase CreateThermoPhase(string filename,
string? phasename = null) =>
new ThermoPhase(filename, phasename);
}
3 changes: 3 additions & 0 deletions interfaces/dotnet/Cantera/src/CanteraException.cs
@@ -1,3 +1,6 @@
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.

using System.Runtime.InteropServices;
using Cantera.Interop;

Expand Down
3 changes: 3 additions & 0 deletions interfaces/dotnet/Cantera/src/Consts.cs
@@ -1,3 +1,6 @@
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.

namespace Cantera;

public static class Consts
Expand Down
6 changes: 5 additions & 1 deletion interfaces/dotnet/Cantera/src/DataDirectoryCollection.cs
@@ -1,3 +1,6 @@
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.

using System.Collections;
using Cantera.Interop;

Expand Down Expand Up @@ -32,7 +35,8 @@ internal DataDirectoryCollection()

public void Add(DirectoryInfo dir)
{
InteropUtil.CheckReturn(LibCantera.ct_addCanteraDirectory((nuint)dir.FullName.Length, dir.FullName));
InteropUtil.CheckReturn(LibCantera.ct_addCanteraDirectory(
(nuint) dir.FullName.Length, dir.FullName));

_dirs.Clear();
_dirs.AddRange(GetDirs());
Expand Down
20 changes: 14 additions & 6 deletions interfaces/dotnet/Cantera/src/Enums.cs
@@ -1,3 +1,6 @@
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.

using System.Reflection;
using System.Text.RegularExpressions;

Expand All @@ -6,7 +9,7 @@ namespace Cantera;
public enum ThermoPair
{
RP, DensityPressure = RP,

TV, TemperatureVolume = TV,

HP, EnthalpyPressure = HP,
Expand Down Expand Up @@ -34,16 +37,21 @@ public enum ThermoPair

static class ThermoPairExtensions
{
readonly static Lazy<IReadOnlyDictionary<ThermoPair, String>> InteropStringMap =
new(() => typeof(ThermoPair)
internal static IEnumerable<FieldInfo> GetThermoPairEnumFieldWithTwoCharNames() =>
typeof(ThermoPair)
.GetFields(BindingFlags.Static | BindingFlags.Public)
.Where(f => Regex.IsMatch(f.Name, "^[A-Z]{2}$")) // match exactly two uppercase
.Where(f => Regex.IsMatch(f.Name, "^[A-Z]{2}$")); // exactly two uppercase

readonly static Lazy<IReadOnlyDictionary<ThermoPair, string>> InteropStringMap =
new(() => GetThermoPairEnumFieldWithTwoCharNames()
.ToDictionary(f => (ThermoPair) f.GetValue(null)!, f => f.Name));

public static string ToInteropString(this ThermoPair thermoPair)
{
if(InteropStringMap.Value.TryGetValue(thermoPair, out var interopString))
if (InteropStringMap.Value.TryGetValue(thermoPair, out var interopString))
{
return interopString;
}

throw new ArgumentOutOfRangeException(nameof(thermoPair));
}
Expand Down
8 changes: 6 additions & 2 deletions interfaces/dotnet/Cantera/src/ExtensionMethods.cs
@@ -1,12 +1,16 @@
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.

using System.Buffers;

namespace Cantera;

static class MemoryExtensions
{
public static IMemoryOwner<T> Rent<T>(this MemoryPool<T> pool, int minmumSize, out Span<T> span)
public static IMemoryOwner<T> Rent<T>(this MemoryPool<T> pool, int minimumSize,
out Span<T> span)
{
var owner = pool.Rent(minmumSize);
var owner = pool.Rent(minimumSize);
span = owner.Memory.Span;

return owner;
Expand Down
11 changes: 8 additions & 3 deletions interfaces/dotnet/Cantera/src/Interop/CanteraHandle.cs
@@ -1,3 +1,6 @@
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.

using System.Runtime.InteropServices;

namespace Cantera.Interop;
Expand All @@ -9,9 +12,9 @@ namespace Cantera.Interop;
/// We use the SafeHandle class, which has low-level support in the runtime to ensure
/// proper reference counting and cleanup and is thread-safe. This allows us to use
/// the dispose pattern easily and safely and without having to write our own finalizer.
/// Cantera uses signed 32-bit ints for handles, yet SafeHandle uses "native int" IntPtr.
/// The Value and IsValid properties are designed to account for this and only consider
/// the lower 32-bit of the IntPtr on 64-bit systems.
/// Cantera uses signed 32-bit ints for handles, yet SafeHandle uses
/// "native int" IntPtr. The Value and IsValid properties are designed to account for
/// this and only consider the lower 32-bit of the IntPtr on 64-bit systems.
/// <remarks>
abstract class CanteraHandle : SafeHandle
{
Expand All @@ -32,6 +35,8 @@ abstract class CanteraHandle : SafeHandle
public void EnsureValid()
{
if (IsInvalid)
{
CanteraException.ThrowLatest();
}
}
}
3 changes: 3 additions & 0 deletions interfaces/dotnet/Cantera/src/Interop/InteropConsts.cs
@@ -1,3 +1,6 @@
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.

namespace Cantera.Interop;

static class InteropConsts
Expand Down

0 comments on commit c8192bf

Please sign in to comment.