Skip to content

Commit

Permalink
ConfigEntry
Browse files Browse the repository at this point in the history
- `opCast()` should explicitly cast our `size_t` type from `numeric()` to `T` else it is implicit cast and it fails in cases where `size_t` (from `numeric()`) is like `ulong` and `T` from `cast(T)` is `int`
- Fixed `isIntegral(T)` usage
  • Loading branch information
deavmi committed Nov 5, 2024
1 parent 543ac59 commit 9a7f5cb
Showing 1 changed file with 45 additions and 9 deletions.
54 changes: 45 additions & 9 deletions source/niknaks/config.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ version(unittest)
private union ConfigValue
{
string text;
int integer;
size_t integer;
bool flag;
string[] textArray;
}
Expand Down Expand Up @@ -115,6 +115,42 @@ public struct ConfigEntry
// TODO: Must have an unset flag
// @disable
// private this();
import std.traits : isIntegral;

this(EntryType)(EntryType value)
if
(
__traits(isSame, EntryType, string[]) ||
__traits(isSame, EntryType, string) ||
isIntegral!(EntryType) ||
__traits(isSame, EntryType, bool)
)
{
ConfigValue _v;
ConfigType _t;
static if(__traits(isSame, EntryType, string[]))
{
_v.textArray = value;
_t = ConfigType.ARRAY;
}
else static if(__traits(isSame, EntryType, string))
{
_v.text = value;
_t = ConfigType.TEXT;
}
else static if(isIntegral!(EntryType))
{
_v.integer = cast(size_t)value;
_t = ConfigType.NUMERIC;
}
else static if(__traits(isSame, EntryType, bool))
{
_v.flag = value;
_t = ConfigType.FLAG;
}

this(_v, _t);
}

/**
* Constructs a new `ConfigEntry`
Expand Down Expand Up @@ -155,7 +191,7 @@ public struct ConfigEntry
* i = the integer
* Returns: a `ConfigEntry`
*/
public static ConfigEntry ofNumeric(int i)
public static ConfigEntry ofNumeric(size_t i)
{
ConfigValue tmp;
tmp.integer = i;
Expand Down Expand Up @@ -243,7 +279,7 @@ public struct ConfigEntry
* the type of the value in this
* entry is not numeric
*/
public int numeric()
public size_t numeric()
{
ensureSet;
ensureTypeMatch(ConfigType.NUMERIC);
Expand Down Expand Up @@ -341,9 +377,9 @@ public struct ConfigEntry
{
return text();
}
else static if(__traits(isSame, T, int))
else static if(isIntegral!(T))
{
return numeric();
return cast(T)numeric();
}
else static if(__traits(isSame, T, string[]))
{
Expand Down Expand Up @@ -744,7 +780,7 @@ public struct Registry
/**
* See_Also: `opIndexAssign(ConfigEntry, string)`
*/
public void opIndexAssign(int numeric, string name)
public void opIndexAssign(size_t numeric, string name)
{
opIndexAssign(ConfigEntry.ofNumeric(numeric), name);
}
Expand Down Expand Up @@ -824,11 +860,11 @@ unittest
// Check that the entry still has the right value
assert(cast(string)reg["name"] == "Tristan");

// Add a new entry and test its prescence
// // Add a new entry and test its prescence
reg["age"] = 24;
assert(cast(int)reg["age"] == 24);
assert(cast(int)reg["age"]);

// Update it
// // Update it
reg["age"] = 25;
assert(cast(int)reg["age"] == 25);

Expand Down

0 comments on commit 9a7f5cb

Please sign in to comment.