ddn.adam¶
A compact, memory-efficient dynamic value type for the D programming language.
Overview¶
This module provides adam, a memory-optimized dynamic value type designed to match the semantics and API of ddn.var while minimizing memory footprint. It is suitable for applications where large numbers of dynamic values must be stored or transmitted with minimal overhead.
Key Features:
- Compact representation: Only 16 bytes on 64-bit platforms (vs. typical 24+ bytes for variant types).
- Tagged-length encoding: Type tag and collection length share a single
size_tword. - Full numeric tower: Supports
bool, all D integer types,float,double, anddchar. - Composite types: Arrays (
adam[]) and objects (adam[string]) with JSON-like semantics. - GC-safe: Pointer payloads are never tagged, preserving garbage collector reachability.
- API compatibility: Mirrors
ddn.varoperators, conversions, and serialization methods.
Structs¶
adam¶
A compact, memory-efficient dynamic value type.
Representation:
- A scalar-or-pointer payload word (
_data). - A tagged length word (
_lenTag) that packs theTypetag into the high bits and (forSTRINGandARRAY) the length into the low bits.
Type Normalization:
realvalues are stored asType.DOUBLE(converted todouble).charandwcharvalues are stored asType.DCHAR(converted todchar).
Platform Notes:
| Platform | adam.sizeof |
Max Array/String Length |
|---|---|---|
| 64-bit | 16 bytes | Full size_t range |
| 32-bit | 8 bytes | 2²⁸ - 1 (28 bits) |
Enums¶
adam.Type¶
The concrete type tag for an adam value.
| Member | Description |
|---|---|
NULL |
The absence of a value |
BOOL |
Boolean value (true or false) |
BYTE |
Signed 8-bit integer |
UBYTE |
Unsigned 8-bit integer |
SHORT |
Signed 16-bit integer |
USHORT |
Unsigned 16-bit integer |
INT |
Signed 32-bit integer |
UINT |
Unsigned 32-bit integer |
LONG |
Signed 64-bit integer |
ULONG |
Unsigned 64-bit integer |
FLOAT |
32-bit IEEE-754 floating point |
DOUBLE |
64-bit IEEE-754 floating point |
DCHAR |
Unicode scalar character (normalized from char/wchar) |
ARRAY |
Dynamic array of adam |
STRING |
UTF-8 string |
OBJECT |
Associative array (string → adam) |
Constructors¶
this(T)¶
Constructs an adam from a value of type T.
Parameters:
| Name | Type | Description |
|---|---|---|
value |
T |
The value to store |
Example:
Properties¶
type¶
Returns the current type tag of the stored value.
Returns: The Type enum value indicating the stored type.
isNull¶
Returns true if the value is NULL.
isBool¶
Returns true if the value is a BOOL.
isArray¶
Returns true if the value is an ARRAY.
isString¶
Returns true if the value is a STRING.
isObject¶
Returns true if the value is an OBJECT.
isNumeric¶
Returns true if the value is a numeric type (integer or floating point).
length¶
Returns the length of the stored value.
- For
STRING: returns the string length. - For
ARRAY: returns the number of elements. - For
OBJECT: returns the number of key-value pairs. - For other types: returns
0.
opDollar¶
Returns the length for use with the $ operator in slice expressions.
count¶
Returns the number of elements in an ARRAY or key-value pairs in an OBJECT. Returns 0 for other types.
keys¶
Returns an array of all keys in an OBJECT. Returns an empty array for non-object types.
values¶
Returns an array of all values in an OBJECT. Returns an empty array for non-object types.
Methods¶
opAssign(T)¶
Assigns a value of type T to this adam.
Parameters:
| Name | Type | Description |
|---|---|---|
rhs |
T |
The value to assign |
Returns: Reference to this.
get(T)¶
Extracts the stored value as type T.
Returns: The value converted to type T, or T.init if conversion is not possible.
Example:
contains¶
Checks if an OBJECT contains the specified key.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
string |
The key to check |
Returns: true if the key exists, false otherwise.
toString¶
Converts the adam to a human-readable string representation.
Returns: A string representation of the value.
toJSON¶
Serializes the adam to a JSON string.
Returns: A JSON-formatted string.
Example:
adam data;
data["id"] = 123;
data["active"] = true;
string json = data.toJSON();
// Output: {"active":true,"id":123}
Operators¶
opIndex (string key)¶
Accesses or creates a value in an OBJECT by key.
Example:
opIndex (size_t index)¶
Accesses an element in an ARRAY by index.
opIndexAssign¶
Assigns a value to an OBJECT key.
opEquals¶
Compares two adam values for equality.
opCmp¶
Compares two adam values for ordering.
Returns: Negative if this < rhs, zero if equal, positive if this > rhs.
Usage Comparison¶
When to Use adam vs var¶
| Use Case | Recommended Type |
|---|---|
| Memory-constrained environments | adam |
| Large collections of dynamic values | adam |
| Network serialization with size limits | adam |
| General-purpose dynamic typing | var |
| Maximum compatibility and features | var |
See Also¶
ddn.var— Full-featured dynamic value type with richer semantics.ddn.data.json5— JSON5 parsing and serialization utilities.ddn.util.json_escape— JSON string escaping utilities.