std.boxer
This module is a set of types and functions for converting any object (value or heap) into a generic box type, allowing the user to pass that object around without knowing what's in the box, and then allowing him to recover the value afterwards. WARNING:This module is being phased out. You may want to use std.variant for new code. Example:
// Convert the integer 45 into a box. Box b = box(45); // Recover the integer and cast it to real. real r = unbox!(real)(b);That is the basic interface and will usually be all that you need to understand. If it cannot unbox the object to the given type, it throws UnboxException. As demonstrated, it uses implicit casts to behave in the exact same way that static types behave. So for example, you can unbox from int to real, but you cannot unbox from real to int: that would require an explicit cast. This therefore means that attempting to unbox an int as a string will throw an error instead of formatting it. In general, you can call the toString method on the box and receive a good result, depending upon whether std.string.format accepts it. Boxes can be compared to one another and they can be used as keys for associative arrays. There are also functions for converting to and from arrays of boxes. Example:
// Convert arguments into an array of boxes. Box[] a = boxArray(1, 45.4, "foobar"); // Convert an array of boxes back into arguments. TypeInfo[] arg_types; void* arg_data; boxArrayToArguments(a, arg_types, arg_data); // Convert the arguments back into boxes using a // different form of the function. a = boxArray(arg_types, arg_data);One use of this is to support a variadic function more easily and robustly; simply call "boxArray(arguments, argptr)", then do whatever you need to do with the array. BUGS:
License:
Boost License 1.0. Authors:
Burton Radons
- The type class returned from Box.findTypeClass; the order of entries is important.
- Box is a generic container for objects (both value and heap), allowing the
user to box them in a generic form and recover them later.
A box object contains a value in a generic fashion, allowing it to be
passed from one place to another without having to know its type. It is
created by calling the box function, and you can recover the value by
instantiating the unbox template.
- Return whether this value could be unboxed as the given type without throwing.
- Property for the type contained by the box.
This is initially null and cannot be assigned directly.
Returns:
the type of the contained object. - Property for the data pointer to the value of the box.
This is initially null and cannot be assigned directly.
Returns:
the data array. - Attempt to convert the boxed value into a string using std.string.format; this will throw if that function cannot handle it. If the box is uninitialized then this returns "".
- Compare this box's value with another box. This implicitly casts if the types are different, identical to the regular type system.
- Compare this box's value with another box. This implicitly casts if the types are different, identical to the regular type system.
- Return the value's hash.
- Box the single argument passed to the function. If more or fewer than one argument is passed, this will assert.
- Box the explicitly-defined object. type must not be null; data must not be null if the type's size is greater than zero. The data is copied.
- Convert a list of arguments into a list of boxes.
- Box each argument passed to the function, returning an array of boxes.
- Convert an array of boxes into an array of arguments.
- This class is thrown if unbox is unable to cast the value into the desired
result.
- This is the box that the user attempted to unbox.
- This is the type that the user attempted to unbox the value as.
- this(Box object, TypeInfo outputType);
- Assign parameters and create the message in the form "Could not unbox from type ... to ... ."
- A generic unboxer for the real numeric types.
- A generic unboxer for the integral numeric types.
- A generic unboxer for the complex numeric types.
- A generic unboxer for the imaginary numeric types.
- The unbox template takes a type parameter and returns a function that
takes a box object and returns the specified type.
To use it, instantiate the template with the desired result type, and then
call the function with the box to convert.
This will implicitly cast base types as necessary and in a way consistent
with static types - for example, it will cast a boxed byte into int, but it
won't cast a boxed float into short.
Throws:
UnboxException if it cannot cast Example:
Box b = box(4.5); bit u = unboxable!(real)(b); // This is true. real r = unbox!(real)(b); Box y = box(4); int x = unbox!(int) (y);
- Return whether the value can be unboxed as the given type; if this returns false, attempting to do so will throw UnboxException.