LordKalma (CT7ALW)
LordKalma (CT7ALW)
CC#
Created by LordKalma (CT7ALW) on 12/6/2023 in #help
Smart enums, inheritance and "siblings" behaving weirdly and I don't understand why.
So, I'm using Ardalis.SmartEnum to create some smart enums with data members. I found myself having a lot of boilerplate, so I came up with the following code:
using Ardalis.SmartEnum;

namespace AetherLogger.Models;

public interface IModeInterface : ISmartEnum
{
string ReadableName { get; }
}

public abstract class ModeBase<TEnum> : SmartEnum<TEnum, int>, IModeInterface where TEnum : SmartEnum<TEnum, int>
{
protected ModeBase(string name, string? readableName = null) : base(name, ++_counter)
{
ReadableName = readableName ?? name;
}

private static int _counter;
public string ReadableName { get; }
}

public sealed class Mode : ModeBase<Mode>
{
public static readonly Mode CHIP = new(nameof(CHIP), ChipSubMode.List);
public static readonly Mode CW = new(nameof(CW), "Morse (CW)", CwSubMode.List);

public IEnumerable<IModeInterface>? Submodes { get; }

private Mode(string name, string? readableName = null, IEnumerable<IModeInterface>? submodes = null) : base(name, readableName)
{
Submodes = submodes;
}

public Mode(string name, IEnumerable<IModeInterface> submodes) : this(name, name, submodes)
{
}
}


public sealed class ChipSubMode(string name, string? readableName = null) : ModeBase<ChipSubMode>(name, readableName)
{
public static readonly ChipSubMode CHIP64 = new(nameof(CHIP64), "CHIP 64");
public static readonly ChipSubMode CHIP128 = new(nameof(CHIP128), "CHIP 128");
}

public sealed class CwSubMode(string name, string? readableName = null) : ModeBase<CwSubMode>(name, readableName)
{
public static readonly CwSubMode PCW = new(nameof(PCW), "Precision CW");
}
using Ardalis.SmartEnum;

namespace AetherLogger.Models;

public interface IModeInterface : ISmartEnum
{
string ReadableName { get; }
}

public abstract class ModeBase<TEnum> : SmartEnum<TEnum, int>, IModeInterface where TEnum : SmartEnum<TEnum, int>
{
protected ModeBase(string name, string? readableName = null) : base(name, ++_counter)
{
ReadableName = readableName ?? name;
}

private static int _counter;
public string ReadableName { get; }
}

public sealed class Mode : ModeBase<Mode>
{
public static readonly Mode CHIP = new(nameof(CHIP), ChipSubMode.List);
public static readonly Mode CW = new(nameof(CW), "Morse (CW)", CwSubMode.List);

public IEnumerable<IModeInterface>? Submodes { get; }

private Mode(string name, string? readableName = null, IEnumerable<IModeInterface>? submodes = null) : base(name, readableName)
{
Submodes = submodes;
}

public Mode(string name, IEnumerable<IModeInterface> submodes) : this(name, name, submodes)
{
}
}


public sealed class ChipSubMode(string name, string? readableName = null) : ModeBase<ChipSubMode>(name, readableName)
{
public static readonly ChipSubMode CHIP64 = new(nameof(CHIP64), "CHIP 64");
public static readonly ChipSubMode CHIP128 = new(nameof(CHIP128), "CHIP 128");
}

public sealed class CwSubMode(string name, string? readableName = null) : ModeBase<CwSubMode>(name, readableName)
{
public static readonly CwSubMode PCW = new(nameof(PCW), "Precision CW");
}
This code works. However, note ModeBase<TEnum> : SmartEnum<TEnum, int>. What I found is that this code doesn't work if I don't template ModelBase and I don't know why. [continues in the comments]
19 replies
CC#
Created by LordKalma (CT7ALW) on 4/29/2023 in #help
❔ Capturing value-types by value (i.e., making a copy) in λs. Or rather, using a Span<T> in a λ.
I have some code using a Span<T>. I wanted to use some LINQ with it (e.g. the Enumerable.Range.Select method), but whenever I try to use the Span<T> object inside a lambda, I get the error CS8175:
Error CS8175 Cannot use ref local <mySpanVariable> inside an anonymous method, lambda expression, or query expression.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs8175 How can I work around this?
11 replies
CC#
Created by LordKalma (CT7ALW) on 4/17/2023 in #help
❔ Specializing a Generic?
If I make a MyClass<T>, can I make a, for example, couple specializations like MyClass<int> and MyClass<double> implementing some specific behaviour?
56 replies
CC#
Created by LordKalma (CT7ALW) on 3/7/2023 in #help
❔ App.config: "special one" for testing/debugging
Hello, I'm making a small console application that needs some configuration (an API key, and a list of "filters"). I thought of saving that kind of configuration in an App.config file. Question is: how can I just have a "template" file for VCS storage, and have a "local" one filled with my secrets for testing/debugging?
18 replies
CC#
Created by LordKalma (CT7ALW) on 12/29/2022 in #help
❔ Deserialize serial data to struct (port from C)
Hello, I'm not sure how you do this in C#: I have this code I'm porting to C#. It's basically a stream from a serial port that needs to be de-serialized into a struct. This is the C code for the struct:
#define AETHER_X6100CTRL_BB_FRAME_IQ_SAMPLES_COUNT 512
#define AETHER_X6100CTRL_BB_FRAME_MAGIC 0xAA5555AA

typedef struct
{
bool resync : 1;
bool tx : 1;
bool chg : 1;
bool vext : 1;
uint32_t reserved : 28;
} aether_x6100ctrl_bb_frame_flags_t;

typedef struct AETHER_X6100CTRL_PACKED
{
uint32_t magic; /*!< Every frame starts with AETHER_X6100CTRL_BB_FRAME_MAGIC */
aether_x6100ctrl_fcomplex_t bb_iq_samples[AETHER_X6100CTRL_BB_FRAME_IQ_SAMPLES_COUNT];
aether_x6100ctrl_bb_frame_flags_t flags;
uint8_t reserved_1;
uint8_t tx_power;
uint8_t vswr;
uint8_t alc_level;
uint8_t vext;
uint8_t vbat;
uint8_t batcap;
uint8_t reserved_2;
uint32_t atu_params;
uint32_t reserved_3;
uint32_t reserved_4;
uint32_t reserved_5;
uint32_t reserved_6;
uint32_t crc;
} aether_x6100ctrl_bb_frame_t;
#define AETHER_X6100CTRL_BB_FRAME_IQ_SAMPLES_COUNT 512
#define AETHER_X6100CTRL_BB_FRAME_MAGIC 0xAA5555AA

typedef struct
{
bool resync : 1;
bool tx : 1;
bool chg : 1;
bool vext : 1;
uint32_t reserved : 28;
} aether_x6100ctrl_bb_frame_flags_t;

typedef struct AETHER_X6100CTRL_PACKED
{
uint32_t magic; /*!< Every frame starts with AETHER_X6100CTRL_BB_FRAME_MAGIC */
aether_x6100ctrl_fcomplex_t bb_iq_samples[AETHER_X6100CTRL_BB_FRAME_IQ_SAMPLES_COUNT];
aether_x6100ctrl_bb_frame_flags_t flags;
uint8_t reserved_1;
uint8_t tx_power;
uint8_t vswr;
uint8_t alc_level;
uint8_t vext;
uint8_t vbat;
uint8_t batcap;
uint8_t reserved_2;
uint32_t atu_params;
uint32_t reserved_3;
uint32_t reserved_4;
uint32_t reserved_5;
uint32_t reserved_6;
uint32_t crc;
} aether_x6100ctrl_bb_frame_t;
You can explore the code better if you want on this header file and this C file. This is a bit of an on-going work that's a bit rough. Since I want to move to C# and all the code is doing is writing and reading to serial, I thought I could just do away with the base C(++) library. What tools in C# do you use to define and de-serialize a struct like this? Thanks
7 replies