Jump to: TypeClass Bool Integer Float Complex Imaginary Class Pointer Array Other Box unboxable type data toString opEquals opCmp toHash box boxArray boxArrayToArguments UnboxException object outputType unboxCastReal unboxCastInteger unboxCastComplex unboxCastImaginary unbox
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.
Authors:
Burton Radons
License:
Public Domain
enum TypeClass;
The type class returned from Box.findTypeClass; the order of entries is important.
Bool
< bool
Integer
< byte, ubyte, short, ushort, int, uint, long, ulong
Float
< float, double, real
Complex
< cfloat, cdouble, creal
Imaginary
< ifloat, idouble, ireal
Class
< Inherits from Object
Pointer
< Pointer type (T *)
Array
< Array type (T [])
Other
< Any other type, such as delegates, function pointers, struct, void...
struct Box;
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.
bool unboxable(TypeInfo test);
Return whether this value could be unboxed as the given type without throwing.
const TypeInfo type();
Property for the type contained by the box. This is initially null and cannot be assigned directly.
Returns:
the type of the contained object.
const const(void)[] data();
Property for the data pointer to the value of the box. This is initially null and cannot be assigned directly.
Returns:
the data array.
string toString();
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 "".
bool opEquals(Box other);
Compare this box's value with another box. This implicitly casts if the types are different, identical to the regular type system.
float opCmp(Box other);
Compare this box's value with another box. This implicitly casts if the types are different, identical to the regular type system.
hash_t toHash();
Return the value's hash.
Box box(...);
Box the single argument passed to the function. If more or fewer than one argument is passed, this will assert.
Box box(TypeInfo type, void* data);
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.
Box[] boxArray(TypeInfo[] types, void* data);
Convert a list of arguments into a list of boxes.
Box[] boxArray(...);
Box each argument passed to the function, returning an array of boxes.
void boxArrayToArguments(Box[] arguments, out TypeInfo[] types, out void* data);
Convert an array of boxes into an array of arguments.
class UnboxException: object.Exception;
This class is thrown if unbox is unable to cast the value into the desired result.
Box object;
This is the box that the user attempted to unbox.
TypeInfo outputType;
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 ... ."
T unboxCastReal(T)(Box value);
A generic unboxer for the real numeric types.
T unboxCastInteger(T)(Box value);
A generic unboxer for the integral numeric types.
T unboxCastComplex(T)(Box value);
A generic unboxer for the complex numeric types.
T unboxCastImaginary(T)(Box value);
A generic unboxer for the imaginary numeric types.
T unbox(T)(Box value);
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);
bool unboxable(T)(Box value);
Return whether the value can be unboxed as the given type; if this returns false, attempting to do so will throw UnboxException.
分享到:
相关推荐
响应式设计是现代网页开发的重要趋势,它使得网站在不同设备上都能呈现良好的用户体验,无论是桌面电脑、平板还是智能手机。教育培训机构网站源码基于这一理念,提供了适应多种屏幕尺寸的解决方案,确保用户无论何时...
Boxer Text Editor是一款高效、功能强大的文本编辑器,尤其适合程序员和高级用户使用。这款编辑器以其出色的性能和丰富的特性在IT行业中获得了广泛的认可。它不仅提供了基础的文本编辑功能,还具有许多高级特性,如...
"开源项目-sermodigital-boxer.zip" 是一个与开源软件相关的压缩文件,其核心内容是 "boxer" 项目。Boxer 是一个流式加密和解密工具,它遵循了 Automotive Grade Linux(AGL)文章中所描述的技术实现。Automotive ...
【标题】"templatemo_446_boxer.zip" 是一个包含模板设计资源的压缩文件,主要用于网页或网站设计。"Boxer"可能是指该模板的主题或风格,暗示这是一套与拳击、运动或者刚毅风格相关的设计元素。 【描述】...
Boxer text editor 老朽痴拙汉化版(拳击手文本编辑器) 拳击手是简单易用,足够强大完美的工具,甚至能编辑最复杂的文本任务,是窗口功能全面的文本编辑器。 用于编辑 HTML 文件、 程序源代码、 平构数据库文件、 二...
Boxer双缸发动机3D数模图纸 STP格式.zip
boxer 是一款简单的用户自定义模板,用来生成 JSON 对象模型表示。 标签:boxer
《LDraw Boxer:开源工具助力LDraw零件优化》 LDraw Boxer是一款专为LDraw社区设计的开源软件,其主要功能在于帮助用户优化LDraw零件的表现,尤其是在处理那些没有可见螺柱或需要添加盒子的管状部分时。这款工具的...
深度学习与Transformer技术在计算机视觉领域中的应用正在不断拓展,其中BoxeR:Box-Attention for 2D and 3D Transformers这篇论文提出了一种新的注意力机制,旨在提高2D和3D Transformer模型的性能。BoxeR的核心是...
此存储库包含与src/data/boxer目录中的xdrs.dtd文件对应的类层次结构。 它允许将带注释的 XML Boxer 输出快速反序列化为 Java 对象。 先决条件是 maven(用于解析)和构建和运行的(用于输出)。 修改属性文件并...
标题“BOXERCT100_parts_ct100_bajajboxerat_”和描述“bajaj boxcer ct100 parts catalogue”都指向一个主题:Bajaj Boxer CT100摩托车的零件目录。这个压缩包中的文件“BOXERCT100.pdf”很可能是一个详细的配件...
Boxer于1983年提出,它提供了一种量化衡量系统对不确定性和外部干扰抵抗能力的方法。本资料包"利用matlab实现H-infinity鲁棒控制.zip"专门针对H-infinity控制理论及其在MATLAB环境中的实现进行深入探讨。 H-...
sketch-bounding-boxer, 在草图文档中,切换'boundingBox'图层的可见性 :为什么草图绑定拳击手在我们的草图文档中,我们经常使用"boundingbox"层,例如。 icon 层组查看( 并使用)的实际尺寸和边界。 由于我们不想...
示例 vagrant-boxer 和 vagrant-catalog 设置这个存储库是如何设置一个服务器然后在多个 vagrant 项目中使用它的示例。 在一个项目中,我们创建了一个基本框,然后我们使用在 vagrant-catalog 服务器上发布它。 在另...
因此,这里的boxer(名称可能会更改),您可以使用它来快速运行node或npm命令,以执行诸如安装typscript编译器并按需运行该编译器以创建javascript资源之类的工作。 库是一个WIP,因此其功能不完整,不缺少任何...
- Android.Boxer:发送短信至特定号码。 - Android.Carberp:试图窃取机密银行认证码并发送至远程控制端。 - Android.DroidDeluxe:利用root权限修改数据库文件,并收集账户信息。 - Android.DroidDream:智能手机...
5. Boxer 骨折:第五掌骨颈骨折,特征是掌骨头向掌侧移位。 6. Burst 骨折:椎体受轴向压力造成的骨折,常见骨折碎片向外侧移位,可能发生在颈椎、胸椎或腰椎。 7. Chance 骨折:胸腰段椎体的分离性骨折,伴有脊髓...
有趣的拳击手狗和可爱的小狗,每个新选项卡的不同高清背景。 此附加组件是专门为所有爱狗人士设计的。 我们收集了可爱又有趣的拳击手和小狗的高清壁纸,每当您打开一个新标签页时,您都可以享受不同的背景。...
流浪拳击手 管理装箱 Vagrant VM 的应用程序,用作专用盒分发系统的基本盒。... 然后,您可以选择将 /usr/local/bin/boxer.php 之类的内容符号链接到此内容,以便只需键入boxer.php从您的路径运行 $ sudo ln -sf /pa
findById ( 356831 , function ( err , boxer ) { console . log ( boxer . name ) ; // 'Gennady Golovkin'} ) ;boxrec . findByName ( 'Carl Froch' , function ( err , boxer ) { console . log ( boxer . ...