NoesisGUI

Boxing and Unboxing values

Sometimes basic types or simple classes (see SDKHierarchy) need to be converted to components. A component adds the overhead of the polymorphism to the class, the reference counter and the allocation in the heap, but in some scenarios this is needed. For example, the TypeConverter architecture uses this mechanism.

The mechanism of converting a non-component instance to a component is called boxing and is performed using the Boxing::Box() operation:

Ptr<BaseComponent> boxed = Boxing::Box<NsFloat32>(50.0f);

The reverse operation is called unboxing. A boxed value is unboxed using Boxing::Unbox():

NsFloat32 val = Boxing::Unbox<NsFloat32>(boxed);

Although boxing is optimized internally (for example using pools to avoid memory allocations) it should be avoided whenever possible because it cannot be considered a fast operation.

© 2017 Noesis Technologies