PrevNextUpHome SophiaFramework UNIVERSE 5.3
SFXAny
Storage class which stores a value of any types.
#include <SFXAny.h.hpp>
class SFXAny;
SFMTYPEDEFCLASS(SFXAny)
        

Collaboration diagram

 Collaboration diagram of SFXAnyClass

Description

The SFXAny class offers a storage area for storing a value of any types with ensuring type safety.

However, the types for storing must meet the following conditions.

  • Copyable.
  • Assignable.

The any_cast operator acquires the value from a storage with specifying the type of a template parameter.

The SFXAny::Contains function checks if the storage contains the same type as a specified one.

The SFXAny::GetType function get the type information of a stored value.

[Caution] Type information
The acquired type information by calling the SFXAny::GetType does not represent a type itself. The information can be used only for checking if the types equal. Usually, the SFXAny::Contains function is preferred for this purpose.

Reference

any_cast | SFXAny::Contains | SFXAny::GetType

Member

Constructor/Destructor
SFXAny( Void )
Constructor of the SFXAny class.
SFXAny( T const & value )
Constructor of the SFXAny class.
SFXAny( SFXAnyConstRef other )
Constructor of the SFXAny class.
~SFXAny( Void )
Destructor of the SFXAny class.
Public Functions
static
Bool
Contains( SFXAnyConstRef ref , T * param = null )
Check if the stored type of the SFXAny instance equals the specified type.
SFXTypeInfo::TypeId GetType( Void )
Get the type of a stored value.
SFXAnyRef operator=( T const & value )
Assign the right value to the left value of the SFXAny instance.
SFXAnyRef operator=( SFXAnyConstRef other )
Assign the right value to the left value of the SFXAny instance.

SFXAny::SFXAny
Constructor of the SFXAny class.
[ public ]
SFXAny(Void);
[ public ]
SFXAny(
    T const & value   // a value of T const reference type
);
[ public ]
SFXAny(
    SFXAnyConstRef other   // other instance of SFXAny
);

Description

No argument specified, a value of the type information (SFXTypeInfo::TypeId) will be null.

An argument specified, the class instance will store the value and keep its type.

The stored value will be held as an internal heap.

Example

SInt32 i(5);
SFXAny a;
SFXAny b(i);
SFXAny c(b);

a = i;

TRACE("a = %d", any_cast<SInt32>(a));
TRACE("b = %d", any_cast<SInt32>(b));
TRACE("c = %d", any_cast<SInt32>(c));

// [Results]
// a = 5
// b = 5
// c = 5

Reference

SFXTypeInfo::TypeId


SFXAny::~SFXAny
Destructor of the SFXAny class.
[ public ]
~SFXAny(Void);

Description

An internal heap of a stored value will be released.


SFXAny::Contains
Check if the stored type of the SFXAny instance equals the specified type.
[ public, static ]
Bool Contains<T>(
    SFXAnyConstRef ref   // const reference type
    T * param = null     // parameter for the specified type (value is not concerned)
);

Return value

  • When the types equals: true
  • Otherwise: false

Example

Descrimination of types

SInt32 i;
SFXAny a;

i = 5;
a = i;

TRACE("a is SInt32 ?: %d", SFXAny::Contains(a, reinterpret_cast<SInt32Ptr>(null)));
TRACE("a is UInt32 ?: %d", SFXAny::Contains(a, reinterpret_cast<UInt32Ptr>(null)));

// This syntax can also be used in the following compilers.
// Visual C++ .NET 2003 or over, RVCT for BREW 3.0 or over, GNUARM 4.1 and YAGARTO 4.4
#if !((defined TARGET_COMPILER_MSVCPP &&  TARGET_COMPILER_VERSION < 1300) || (defined TARGET_COMPILER_ARMCPP && TARGET_COMPILER_VERSION < 300000))
TRACE("Is a SInt32 ?: %d", SFXAny::Contains<SInt32>(a));
TRACE("Is a UInt32 ?: %d", SFXAny::Contains<UInt32>(a));
#endif

// Results
// a is SInt32: 1
// a is UInt32: 0
// a is SInt32: 1
// a is UInt32: 0

SFXAny::GetType
Get the type of a stored value.
[ public, const ]
SFXTypeInfo::TypeId GetType(Void);

Description

When the instance has no value, null will be returned.

Reference

SFXTypeInfo::TypeId


SFXAny::any_cast
Cast a stored value to the specified type.
T const & any_cast<T>(
    SFXAnyConstRef ref   // const reference type
);
T & any_cast<T>(
    SFXAnyRef ref   // reference type [cannot be used in RVCT 1.2 for BREW]
);
T const* any_cast<T>(
    SFXAnyConstPtr ptr   // const pointer type [cannot be used in RVCT 1.2 for BREW]
);
T * any_cast<T>(
    SFXAnyPtr ptr   // pointer type
);

Description

A reference type passed as an argument, the stored value will be cast into the specified type as a template parameter regardless of its original type.

A pointer type passed as an argument, the pointer of the stored value will be cast into the specified type as a template parameter if the original type equals the specified one. Otherwise, a null pointer will be acquired.

A reference type or a pointer type, whichever is passed, a template parameter must be specified as a non-modified type of the stored value (the modifiers about pointer or reference are needless).

Example

SInt32 i;
SFXAnsiString str;
SFXAny a;

i = 5;
str = "abcde";
a = i;
TRACE("a = %d", any_cast<SInt32>(a));
TRACE("&&a = %X", any_cast<SInt32>(&a));
TRACE("&&a = %X", any_cast<UInt32>(&a));
a = str;
TRACE("a = %s", any_cast<SFXAnsiString>(a).GetCString());
TRACE("&&a = %X", any_cast<SFXAnsiString>(&a));
TRACE("&&a = %X", any_cast<SFXWideString>(&a));

// [Results]
// a = 5
// &a = 4CAB524 (The address value depends on the execution environment.)
// &a = 0
// a = abcde
// &a = 4CD1E3C (The address value depends on the execution environment.)
// &a = 0

SFXAny::operator=
Assign the right value to the left value of the SFXAny instance.
[ public ]
SFXAnyRef operator=(
    T const & value   // T-type const reference value
);
[ public ]
SFXAnyRef operator=(
    SFXAnyConstRef other   // SFXAny const reference value
);

Return value

The assigned left value.

Example

SInt32 i;
SFXAny a;
SFXAny b;

i = 5;
a = i;
b = a;

TRACE("a = %d", any_cast<SInt32>(a));
TRACE("b = %d", any_cast<SInt32>(b));

// [Results]
// a = 5
// b = 5