Version: 3.2.7
wxAny Class Reference

#include <wx/any.h>

Detailed Description

The wxAny class represents a container for any type.

Its value can be changed at run time, possibly to a different type of value.

wxAny is a backwards-incompatible (but convertible) successor class for wxVariant, essentially doing the same thing in a more modern, template- based manner and with transparent support for any user data type.

Some pseudo-code'ish example of use with arbitrary user data:

void SomeFunction()
{
MyClass myObject;
wxAny any = myObject;
// Do something
// ...
// Let's do a sanity check to make sure that any still holds
// data of correct type.
if ( any.CheckType<MyClass>() )
{
// Thank goodness, still a correct type.
MyClass myObject2 = any.As<MyClass>();
}
else
{
// Something has gone horribly wrong!
wxFAIL();
}
}
The wxAny class represents a container for any type.
Definition: any.h:61
bool CheckType() const
Use this template function for checking if this wxAny holds a specific C++ data type.
T As() const
This template function converts wxAny into given type.
#define wxFAIL
Will always generate an assert error if this code is reached (in debug mode).
Definition: debug.h:309

When compared to wxVariant, there are various internal implementation differences as well. For instance, wxAny only allocates separate data object in heap for large objects (i.e. ones with size more than WX_ANY_VALUE_BUFFER_SIZE, which at the time of writing is 16 bytes).

Note
When performing conversions between strings and floating point numbers, the representation of numbers in C locale is always used. I.e.
wxAny("1.23").GetAs<double>()
wxAny()
Default constructor.
will always work, even if the current locale uses comma as decimal separator.

Library:  wxBase
Category:  Data Structures
See also
wxAnyValueType, wxVariant, Caveats When Not Using C++ RTTI

Public Member Functions

 wxAny ()
 Default constructor. More...
 
template<typename T >
 wxAny (const T &value)
 Constructs wxAny from data. More...
 
 wxAny (const wxAny &any)
 Constructs wxAny from another wxAny. More...
 
 wxAny (const wxVariant &variant)
 Constructs wxAny, converting value from wxVariant. More...
 
 ~wxAny ()
 Destructor. More...
 
template<typename T >
As () const
 This template function converts wxAny into given type. More...
 
template<typename T >
bool CheckType () const
 Use this template function for checking if this wxAny holds a specific C++ data type. More...
 
template<typename T >
bool GetAs (T *value) const
 Template function that retrieves and converts the value of this wxAny to the type that T* value is. More...
 
bool GetAs (wxVariant *value) const
 Specialization of GetAs() that allows conversion of wxAny into wxVariant. More...
 
const wxAnyValueTypeGetType () const
 Returns the value type as wxAnyValueType instance. More...
 
bool HasSameType (const wxAny &other) const
 Returns true if this and another wxAny have the same value type. More...
 
bool IsNull () const
 Tests if wxAny is null (that is, whether there is no data). More...
 
void MakeNull ()
 Makes wxAny null (that is, clears it). More...
 
Assignment operators
template<typename T >
wxAnyoperator= (const T &value)
 
wxAnyoperator= (const wxAny &any)
 
wxAnyoperator= (const wxVariant &variant)
 
Equality operators
bool operator== (signed char value) const
 
bool operator== (signed short value) const
 
bool operator== (signed int value) const
 
bool operator== (signed long value) const
 
bool operator== (wxLongLong_t value) const
 
bool operator== (unsigned char value) const
 
bool operator== (unsigned short value) const
 
bool operator== (unsigned int value) const
 
bool operator== (unsigned long value) const
 
bool operator== (wxULongLong_t value) const
 
bool operator== (float value) const
 
bool operator== (double value) const
 
bool operator== (bool value) const
 
bool operator== (const char *value) const
 
bool operator== (const wchar_t *value) const
 
bool operator== (const wxString &value) const
 
Inequality operators
bool operator!= (signed char value) const
 
bool operator!= (signed short value) const
 
bool operator!= (signed int value) const
 
bool operator!= (signed long value) const
 
bool operator!= (wxLongLong_t value) const
 
bool operator!= (unsigned char value) const
 
bool operator!= (unsigned short value) const
 
bool operator!= (unsigned int value) const
 
bool operator!= (unsigned long value) const
 
bool operator!= (wxULongLong_t value) const
 
bool operator!= (float value) const
 
bool operator!= (double value) const
 
bool operator!= (bool value) const
 
bool operator!= (const char *value) const
 
bool operator!= (const wchar_t *value) const
 
bool operator!= (const wxString &value) const
 

Constructor & Destructor Documentation

◆ wxAny() [1/4]

wxAny::wxAny ( )

Default constructor.

It seeds the object with a null value.

◆ wxAny() [2/4]

template<typename T >
wxAny::wxAny ( const T &  value)

Constructs wxAny from data.

◆ wxAny() [3/4]

wxAny::wxAny ( const wxAny any)

Constructs wxAny from another wxAny.

◆ wxAny() [4/4]

wxAny::wxAny ( const wxVariant variant)

Constructs wxAny, converting value from wxVariant.

Remarks
Because of this conversion, it is not usually possible to have wxAny that actually holds a wxVariant. If wxVariant cannot be converted to a specific data type, wxAny will then hold and manage reference to wxVariantData* similar to how wxVariant does.

Note that objects constructed from list-valued variants require the list to be explicitly cleared using WX_CLEAR_LIST to avoid leaking memory. This unfortunate behaviour will not be changed to prevent breaking the existing code relying on it.

wxVariant vList;
vList.NullList();
vList.Append(15);
vList.Append("abc");
// Create wxAny from the list variant.
wxAny any = wxAny(vList);
// Clear the list to avoid the memory leak.
wxAnyList anyList = any.As<wxAnyList>();
WX_CLEAR_LIST(wxAnyList, anyList);
The wxVariant class represents a container for any type.
Definition: variant.h:163
void NullList()
Makes an empty list.
void Append(const wxVariant &value)
Appends a value to the list.

◆ ~wxAny()

wxAny::~wxAny ( )

Destructor.

Member Function Documentation

◆ As()

template<typename T >
T wxAny::As ( ) const

This template function converts wxAny into given type.

In most cases no type conversion is performed, so if the type is incorrect an assertion failure will occur.

Remarks
For convenience, conversion is done when T is wxString. This is useful when a string literal (which are treated as const char* and const wchar_t*) has been assigned to wxAny.

◆ CheckType()

template<typename T >
bool wxAny::CheckType ( ) const

Use this template function for checking if this wxAny holds a specific C++ data type.

See also
wxAnyValueType::CheckType()

◆ GetAs() [1/2]

template<typename T >
bool wxAny::GetAs ( T *  value) const

Template function that retrieves and converts the value of this wxAny to the type that T* value is.

Returns
Returns true if conversion was successful.

◆ GetAs() [2/2]

bool wxAny::GetAs ( wxVariant value) const

Specialization of GetAs() that allows conversion of wxAny into wxVariant.

Returns
Returns true if conversion was successful. Conversion usually only fails if variant used custom wxVariantData that did not implement the wxAny to wxVariant conversion functions.

◆ GetType()

const wxAnyValueType* wxAny::GetType ( ) const

Returns the value type as wxAnyValueType instance.

Remarks
You cannot reliably test whether two wxAnys are of same value type by simply comparing return values of wxAny::GetType(). Instead, use wxAny::HasSameType().
See also
HasSameType()

◆ HasSameType()

bool wxAny::HasSameType ( const wxAny other) const

Returns true if this and another wxAny have the same value type.

◆ IsNull()

bool wxAny::IsNull ( ) const

Tests if wxAny is null (that is, whether there is no data).

◆ MakeNull()

void wxAny::MakeNull ( )

Makes wxAny null (that is, clears it).

◆ operator!=() [1/16]

bool wxAny::operator!= ( bool  value) const

◆ operator!=() [2/16]

bool wxAny::operator!= ( const char *  value) const

◆ operator!=() [3/16]

bool wxAny::operator!= ( const wchar_t *  value) const

◆ operator!=() [4/16]

bool wxAny::operator!= ( const wxString value) const

◆ operator!=() [5/16]

bool wxAny::operator!= ( double  value) const

◆ operator!=() [6/16]

bool wxAny::operator!= ( float  value) const

◆ operator!=() [7/16]

bool wxAny::operator!= ( signed char  value) const

◆ operator!=() [8/16]

bool wxAny::operator!= ( signed int  value) const

◆ operator!=() [9/16]

bool wxAny::operator!= ( signed long  value) const

◆ operator!=() [10/16]

bool wxAny::operator!= ( signed short  value) const

◆ operator!=() [11/16]

bool wxAny::operator!= ( unsigned char  value) const

◆ operator!=() [12/16]

bool wxAny::operator!= ( unsigned int  value) const

◆ operator!=() [13/16]

bool wxAny::operator!= ( unsigned long  value) const

◆ operator!=() [14/16]

bool wxAny::operator!= ( unsigned short  value) const

◆ operator!=() [15/16]

bool wxAny::operator!= ( wxLongLong_t  value) const

◆ operator!=() [16/16]

bool wxAny::operator!= ( wxULongLong_t  value) const

◆ operator=() [1/3]

template<typename T >
wxAny& wxAny::operator= ( const T &  value)

◆ operator=() [2/3]

wxAny& wxAny::operator= ( const wxAny any)

◆ operator=() [3/3]

wxAny& wxAny::operator= ( const wxVariant variant)

◆ operator==() [1/16]

bool wxAny::operator== ( bool  value) const

◆ operator==() [2/16]

bool wxAny::operator== ( const char *  value) const

◆ operator==() [3/16]

bool wxAny::operator== ( const wchar_t *  value) const

◆ operator==() [4/16]

bool wxAny::operator== ( const wxString value) const

◆ operator==() [5/16]

bool wxAny::operator== ( double  value) const

◆ operator==() [6/16]

bool wxAny::operator== ( float  value) const

◆ operator==() [7/16]

bool wxAny::operator== ( signed char  value) const

◆ operator==() [8/16]

bool wxAny::operator== ( signed int  value) const

◆ operator==() [9/16]

bool wxAny::operator== ( signed long  value) const

◆ operator==() [10/16]

bool wxAny::operator== ( signed short  value) const

◆ operator==() [11/16]

bool wxAny::operator== ( unsigned char  value) const

◆ operator==() [12/16]

bool wxAny::operator== ( unsigned int  value) const

◆ operator==() [13/16]

bool wxAny::operator== ( unsigned long  value) const

◆ operator==() [14/16]

bool wxAny::operator== ( unsigned short  value) const

◆ operator==() [15/16]

bool wxAny::operator== ( wxLongLong_t  value) const

◆ operator==() [16/16]

bool wxAny::operator== ( wxULongLong_t  value) const