Compare commits

...

6 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 73028d2be8 ConfigValue
- Added docs

ConfigType

- Added docs

ConfigEntry

- Added docs

ConfigException

- Added docs
2024-04-27 13:22:46 +02:00
Tristan B. Velloza Kildaire 8a6c7d20c7 RegistryException
- Doc
2024-04-27 13:18:54 +02:00
Tristan B. Velloza Kildaire 6c93f15572 RegistryEntry
- Fully documented
2024-04-27 13:18:11 +02:00
Tristan B. Velloza Kildaire 54706c29dc Registry
- DOc
2024-04-27 13:16:08 +02:00
Tristan B. Velloza Kildaire 07f25762b4 Registry
- Added more doc
2024-04-27 13:12:32 +02:00
Tristan B. Velloza Kildaire 32fef5ce7b Registry
- Added doc
2024-04-27 13:05:27 +02:00
1 changed files with 166 additions and 12 deletions

View File

@ -8,6 +8,16 @@ version(unittest)
import std.stdio : writeln;
}
/**
* A union which expands to
* the byte-width of its
* biggest member, allowing
* us to have space enough
* for any one exclusive member
* at a time
*
* See_Also: ConfigEntry
*/
private union ConfigValue
{
string text;
@ -16,6 +26,9 @@ private union ConfigValue
string[] textArray;
}
/**
* The type of the entry
*/
public enum ConfigType
{
TEXT,
@ -24,14 +37,23 @@ public enum ConfigType
ARRAY
}
/**
* An exception thrown when you misuse
* a configuration entry
*/
public final class ConfigException : Exception
{
this(string msg)
private this(string msg)
{
super(msg);
}
}
/**
* A configuration entry which
* acts as a typed union and
* supports certain fixed types
*/
public struct ConfigEntry
{
private ConfigValue value;
@ -181,6 +203,11 @@ public struct ConfigEntry
}
}
/**
* Tests out using the configuration
* entry and its various operator
* overloads
*/
unittest
{
ConfigEntry entry = ConfigEntry.ofArray(["hello", "world"]);
@ -196,6 +223,10 @@ unittest
assert(entry);
}
/**
* Tests out the erroneous usage of a
* configuration entry
*/
unittest
{
ConfigEntry entry = ConfigEntry.ofText("hello");
@ -211,6 +242,10 @@ unittest
}
}
/**
* Tests out the erroneous usage of a
* configuration entry
*/
unittest
{
ConfigEntry entry;
@ -226,66 +261,148 @@ unittest
}
}
public final class RegistryException : Exception
{
this(string msg)
{
super(msg);
}
}
/**
* An entry derived from
* the `Registry` containing
* the name and the configuration
* entry itself
*/
public struct RegistryEntry
{
private string name;
private ConfigEntry val;
/**
* Constructs a new `RegistryEntry`
* with the given name and configuration
* entry
*
* Params:
* name = the name
* entry = the entry itself
*/
this(string name, ConfigEntry entry)
{
this.name = name;
this.val = entry;
}
/**
* Obtains the entry's name
*
* Returns: the name
*/
public string getName()
{
return this.name;
}
/**
* Obtains the entry itself
*
* Returns: a `ConfigEntry`
*/
public ConfigEntry getEntry()
{
return this.val;
}
}
/**
* An exception thrown when something
* goes wrong with your usage of the
* `Registry`
*/
public final class RegistryException : Exception
{
private this(string msg)
{
super(msg);
}
}
/**
* A registry for managing
* multiple mappings of
* string-based names to
* configuration entries
*/
public struct Registry
{
private ConfigEntry[string] entries;
private bool allowOverwriteEntry;
/**
* Creates a new `Registry`
* and sets the overwriting policy
*
* Params:
* allowOverwritingOfEntries = `true`
* if you want to allow overwriting of
* previously added entries, otherwise
* `false`
*/
this(bool allowOverwritingOfEntries)
{
setAllowOverwrite(allowOverwritingOfEntries);
}
/**
* Checks if an entry is present
*
* Params:
* name = the name
* Returns: `true` if present,
* otherwise `false`
*/
public bool hasEntry(string name)
{
return getEntry0(name) !is null;
}
/**
* Ontains a pointer to the configuration
* entry at the given key.
*
* Params:
* name = the key
* Returns: a `ConfigEntry*` if found,
* otherwise `null`
*/
private ConfigEntry* getEntry0(string name)
{
ConfigEntry* potEntry = name in this.entries;
return potEntry;
}
/**
* Obtains a pointer to the configuration
* entry at the given key. Allowing you
* to swap out its contents directly if
* you want to.
*
* Params:
* name = the key
* Returns: a `ConfigEntry*` if found,
* otherwise `null`
*/
public ConfigEntry* opBinaryRight(string op)(string name)
if(op == "in")
{
return getEntry0(name);
}
/**
* Obtain a configuration entry
* at the given key
*
* Params:
* name = the key
* entry = the found entry
* (if any)
* Returns: `true` if found,
* otherwise `false`
*/
public bool getEntry_nothrow(string name, ref ConfigEntry entry)
{
ConfigEntry* potEntry = getEntry0(name);
@ -298,6 +415,16 @@ public struct Registry
return true;
}
/**
* Obtain a configuration entry
* at the given key
*
* Params:
* name = the key
* Returns: a configuration entry
* Throws: RegistryException if
* there is no entry at that key
*/
public ConfigEntry opIndex(string name)
{
ConfigEntry entry;
@ -309,11 +436,38 @@ public struct Registry
return entry;
}
/**
* Set whether or not the overwriting
* of an entry should be allowed
*
* Params:
* flag = `true` if to allow, `false`
* if to deny
*/
public void setAllowOverwrite(bool flag)
{
this.allowOverwriteEntry = flag;
}
/**
* Adds a new configuration entry at the
* given key and allows you to choose
* certain behaviors based on the
* existence or non-existence of
* an entry at the same key.
*
* Params:
* name = the name of the entry
* entry = the entry itself
* allowOverWriteNow = if `true`
* then if an entry exists already
* at that key it will be overwritten,
* otherwise an exception will be thrown
* allowSetOnCreation = if there is
* no entry at the given key then,
* if `true`, an entry will be created,
* otherwise an exception will be thrown
*/
private void newEntry(string name, ConfigEntry entry, bool allowOverWriteNow, bool allowSetOnCreation)
{
// Obtain the address of the value that occupies the value