-/////////////////////////////////////////////////////////////////////////////\r
-// Name: wx/any.h\r
-// Purpose: wxAny class\r
-// Author: Jaakko Salli\r
-// Modified by:\r
-// Created: 07/05/2009\r
-// RCS-ID: $Id$\r
-// Copyright: (c) wxWidgets team\r
-// Licence: wxWindows licence\r
-/////////////////////////////////////////////////////////////////////////////\r
-\r
-#ifndef _WX_ANY_H_\r
-#define _WX_ANY_H_\r
-\r
-#include "wx/defs.h"\r
-\r
-#if wxUSE_ANY\r
-\r
-#include "wx/string.h"\r
-#include "wx/meta/movable.h"\r
-#include "wx/meta/if.h"\r
-\r
-\r
-// Size of the wxAny value buffer.\r
-enum\r
-{\r
- WX_ANY_VALUE_BUFFER_SIZE = 16\r
-};\r
-\r
-union wxAnyValueBuffer\r
-{\r
- void* m_ptr;\r
- wxByte m_buffer[WX_ANY_VALUE_BUFFER_SIZE];\r
-};\r
-\r
-typedef void (*wxAnyClassInfo)();\r
-\r
-\r
-//\r
-// wxAnyValueType is base class for value type functionality for C++ data\r
-// types used with wxAny. Usually the default template (wxAnyValueTypeImpl<>)\r
-// will create a satisfactory wxAnyValueType implementation for a data type.\r
-//\r
-class WXDLLIMPEXP_BASE wxAnyValueType\r
-{\r
-public:\r
- /**\r
- Default constructor.\r
- */\r
- wxAnyValueType();\r
-\r
- /**\r
- Destructor.\r
- */\r
- virtual ~wxAnyValueType()\r
- {\r
- }\r
-\r
- /**\r
- This function is used for internal type matching.\r
- */\r
- virtual wxAnyClassInfo GetClassInfo() const = 0;\r
-\r
- /**\r
- This function is used for internal type matching.\r
- */\r
- virtual bool IsSameType(const wxAnyValueType* otherType) const = 0;\r
-\r
- /**\r
- This function is called every time the data in wxAny\r
- buffer needs to be freed.\r
- */\r
- virtual void DeleteValue(wxAnyValueBuffer& buf) const = 0;\r
-\r
- /**\r
- Implement this for buffer-to-buffer copy. src.m_ptr can\r
- be expected to be NULL if value type of previously stored\r
- data was different.\r
- */\r
- virtual void CopyBuffer(const wxAnyValueBuffer& src,\r
- wxAnyValueBuffer& dst) const = 0;\r
-\r
- /**\r
- Convert value into buffer of different type. Return false if\r
- not possible.\r
- */\r
- virtual bool ConvertValue(const wxAnyValueBuffer& src,\r
- wxAnyValueType* dstType,\r
- wxAnyValueBuffer& dst) const = 0;\r
-\r
- /**\r
- Use this template function for checking if wxAnyValueType represents\r
- a specific C++ data type.\r
-\r
- @remarks This template function does not work on some older compilers\r
- (such as Visual C++ 6.0). For full compiler ccompatibility\r
- please use wxANY_VALUE_TYPE_CHECK_TYPE(valueTypePtr, T) macro\r
- instead.\r
-\r
- @see wxAny::CheckType()\r
- */\r
- // FIXME-VC6: remove this hack when VC6 is no longer supported\r
- template <typename T>\r
- bool CheckType(T* reserved = NULL);\r
-private:\r
-};\r
-\r
-//\r
-// This method of checking the type is compatible with VC6\r
-#define wxANY_VALUE_TYPE_CHECK_TYPE(valueTypePtr, T) \\r
- wxAnyValueTypeImpl<T>::IsSameClass(valueTypePtr)\r
-\r
- //valueTypePtr->CheckType(static_cast<T*>(NULL))\r
-\r
-\r
-/**\r
- Helper macro for defining user value types.\r
-\r
- NB: We really cannot compare sm_classInfo directly in IsSameClass(),\r
- but instead call sm_instance->GetClassInfo(). The former technique\r
- broke at least on GCC 4.2 (but worked on VC8 shared build).\r
-*/\r
-#define WX_DECLARE_ANY_VALUE_TYPE(CLS) \\r
- friend class wxAny; \\r
-public: \\r
- static void sm_classInfo() {} \\r
- \\r
- virtual wxAnyClassInfo GetClassInfo() const \\r
- { \\r
- return sm_classInfo; \\r
- } \\r
- static bool IsSameClass(const wxAnyValueType* otherType) \\r
- { \\r
- return sm_instance->GetClassInfo() == otherType->GetClassInfo(); \\r
- } \\r
- virtual bool IsSameType(const wxAnyValueType* otherType) const \\r
- { \\r
- return IsSameClass(otherType); \\r
- } \\r
-private: \\r
- static CLS* sm_instance; \\r
-public: \\r
- static wxAnyValueType* GetInstance() \\r
- { \\r
- return sm_instance; \\r
- }\r
-\r
-\r
-#define WX_IMPLEMENT_ANY_VALUE_TYPE(CLS) \\r
- CLS* CLS::sm_instance = new CLS();\r
-\r
-\r
-#ifdef __VISUALC6__\r
- // "non dll-interface class 'xxx' used as base interface\r
- #pragma warning (push)\r
- #pragma warning (disable:4275)\r
-#endif\r
-\r
-/**\r
- Following are helper classes for the wxAnyValueTypeImplBase.\r
-*/\r
-namespace wxPrivate\r
-{\r
-\r
-template<typename T>\r
-class wxAnyValueTypeOpsMovable\r
-{\r
-public:\r
- static void DeleteValue(wxAnyValueBuffer& buf)\r
- {\r
- wxUnusedVar(buf);\r
- }\r
-\r
- static void SetValue(const T& value,\r
- wxAnyValueBuffer& buf)\r
- {\r
- memcpy(buf.m_buffer, &value, sizeof(T));\r
- }\r
-\r
- static const T& GetValue(const wxAnyValueBuffer& buf)\r
- {\r
- return *(reinterpret_cast<const T*>(&buf.m_buffer[0]));\r
- }\r
-};\r
-\r
-\r
-template<typename T>\r
-class wxAnyValueTypeOpsGeneric\r
-{\r
-public:\r
- template<typename T2>\r
- class DataHolder\r
- {\r
- public:\r
- DataHolder(const T2& value)\r
- {\r
- m_value = value;\r
- }\r
- virtual ~DataHolder() { }\r
-\r
- T2 m_value;\r
- private:\r
- wxDECLARE_NO_COPY_CLASS(DataHolder);\r
- };\r
-\r
- static void DeleteValue(wxAnyValueBuffer& buf)\r
- {\r
- DataHolder<T>* holder = static_cast<DataHolder<T>*>(buf.m_ptr);\r
- delete holder;\r
- }\r
-\r
- static void SetValue(const T& value,\r
- wxAnyValueBuffer& buf)\r
- {\r
- DataHolder<T>* holder = new DataHolder<T>(value);\r
- buf.m_ptr = holder;\r
- }\r
-\r
- static const T& GetValue(const wxAnyValueBuffer& buf)\r
- {\r
- DataHolder<T>* holder = static_cast<DataHolder<T>*>(buf.m_ptr);\r
- return holder->m_value;\r
- }\r
-};\r
-\r
-} // namespace wxPrivate\r
-\r
-\r
-/**\r
- Intermediate template for the generic value type implementation.\r
- We can derive from this same value type for multiple actual types\r
- (for instance, we can have wxAnyValueTypeImplInt for all signed\r
- integer types), and also easily implement specialized templates\r
- with specific dynamic type conversion.\r
-*/\r
-template<typename T>\r
-class wxAnyValueTypeImplBase : public wxAnyValueType\r
-{\r
- typedef typename wxIf< wxIsMovable<T>::value &&\r
- sizeof(T) <= WX_ANY_VALUE_BUFFER_SIZE,\r
- wxPrivate::wxAnyValueTypeOpsMovable<T>,\r
- wxPrivate::wxAnyValueTypeOpsGeneric<T> >::value\r
- Ops;\r
-\r
-public:\r
- wxAnyValueTypeImplBase() : wxAnyValueType() { }\r
- virtual ~wxAnyValueTypeImplBase() { }\r
-\r
- virtual void DeleteValue(wxAnyValueBuffer& buf) const\r
- {\r
- Ops::DeleteValue(buf);\r
- buf.m_ptr = NULL; // This is important\r
- }\r
-\r
- virtual void CopyBuffer(const wxAnyValueBuffer& src,\r
- wxAnyValueBuffer& dst) const\r
- {\r
- Ops::DeleteValue(dst);\r
- Ops::SetValue(Ops::GetValue(src), dst);\r
- }\r
-\r
- /**\r
- It is important to reimplement this in any specialized template\r
- classes that inherit from wxAnyValueTypeImplBase.\r
- */\r
- static void SetValue(const T& value,\r
- wxAnyValueBuffer& buf)\r
- {\r
- Ops::SetValue(value, buf);\r
- }\r
-\r
- /**\r
- It is important to reimplement this in any specialized template\r
- classes that inherit from wxAnyValueTypeImplBase.\r
- */\r
- static const T& GetValue(const wxAnyValueBuffer& buf)\r
- {\r
- return Ops::GetValue(buf);\r
- }\r
-};\r
-\r
-\r
-/*\r
- Generic value type template. Note that bulk of the implementation\r
- resides in wxAnyValueTypeImplBase.\r
-*/\r
-template<typename T>\r
-class wxAnyValueTypeImpl : public wxAnyValueTypeImplBase<T>\r
-{\r
- WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<T>)\r
-public:\r
- wxAnyValueTypeImpl() : wxAnyValueTypeImplBase<T>() { }\r
- virtual ~wxAnyValueTypeImpl() { }\r
-\r
- virtual bool ConvertValue(const wxAnyValueBuffer& src,\r
- wxAnyValueType* dstType,\r
- wxAnyValueBuffer& dst) const\r
- {\r
- wxUnusedVar(src);\r
- wxUnusedVar(dstType);\r
- wxUnusedVar(dst);\r
- return false;\r
- }\r
-};\r
-\r
-template<typename T>\r
-wxAnyValueTypeImpl<T>* wxAnyValueTypeImpl<T>::sm_instance =\r
- new wxAnyValueTypeImpl<T>();\r
-\r
-\r
-//\r
-// Helper macro for using same base value type implementation for multiple\r
-// actual C++ data types.\r
-//\r
-#define WX_ANY_DEFINE_SUB_TYPE(T, CLSTYPE) \\r
-template<> \\r
-class wxAnyValueTypeImpl<T> : public wxAnyValueTypeImpl##CLSTYPE \\r
-{ \\r
- typedef wxAnyBase##CLSTYPE##Type UseDataType; \\r
-public: \\r
- wxAnyValueTypeImpl() : wxAnyValueTypeImpl##CLSTYPE() { } \\r
- virtual ~wxAnyValueTypeImpl() { } \\r
- static void SetValue(const T& value, wxAnyValueBuffer& buf) \\r
- { \\r
- *(reinterpret_cast<UseDataType*>(&buf.m_buffer[0])) = \\r
- static_cast<UseDataType>(value); \\r
- } \\r
- static T GetValue(const wxAnyValueBuffer& buf) \\r
- { \\r
- return static_cast<T>( \\r
- *(reinterpret_cast<const UseDataType*>(&buf.m_buffer[0]))); \\r
- } \\r
-};\r
-\r
-\r
-//\r
-// Integer value types\r
-//\r
-\r
-#ifdef wxLongLong_t\r
- typedef wxLongLong_t wxAnyBaseIntType;\r
- typedef wxULongLong_t wxAnyBaseUintType;\r
-#else\r
- typedef long wxAnyBaseIntType;\r
- typedef unsigned long wxAnyBaseUintType;\r
-#endif\r
-\r
-\r
-class WXDLLIMPEXP_BASE wxAnyValueTypeImplInt :\r
- public wxAnyValueTypeImplBase<wxAnyBaseIntType>\r
-{\r
- WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplInt)\r
-public:\r
- wxAnyValueTypeImplInt() :\r
- wxAnyValueTypeImplBase<wxAnyBaseIntType>() { }\r
- virtual ~wxAnyValueTypeImplInt() { }\r
-\r
- virtual bool ConvertValue(const wxAnyValueBuffer& src,\r
- wxAnyValueType* dstType,\r
- wxAnyValueBuffer& dst) const;\r
-};\r
-\r
-\r
-class WXDLLIMPEXP_BASE wxAnyValueTypeImplUint :\r
- public wxAnyValueTypeImplBase<wxAnyBaseUintType>\r
-{\r
- WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplUint)\r
-public:\r
- wxAnyValueTypeImplUint() :\r
- wxAnyValueTypeImplBase<wxAnyBaseUintType>() { }\r
- virtual ~wxAnyValueTypeImplUint() { }\r
-\r
- virtual bool ConvertValue(const wxAnyValueBuffer& src,\r
- wxAnyValueType* dstType,\r
- wxAnyValueBuffer& dst) const;\r
-};\r
-\r
-\r
-WX_ANY_DEFINE_SUB_TYPE(signed long, Int)\r
-WX_ANY_DEFINE_SUB_TYPE(signed int, Int)\r
-WX_ANY_DEFINE_SUB_TYPE(signed short, Int)\r
-WX_ANY_DEFINE_SUB_TYPE(signed char, Int)\r
-#ifdef wxLongLong_t\r
-WX_ANY_DEFINE_SUB_TYPE(wxLongLong_t, Int)\r
-#endif\r
-\r
-WX_ANY_DEFINE_SUB_TYPE(unsigned long, Uint)\r
-WX_ANY_DEFINE_SUB_TYPE(unsigned int, Uint)\r
-WX_ANY_DEFINE_SUB_TYPE(unsigned short, Uint)\r
-WX_ANY_DEFINE_SUB_TYPE(unsigned char, Uint)\r
-#ifdef wxLongLong_t\r
-WX_ANY_DEFINE_SUB_TYPE(wxULongLong_t, Uint)\r
-#endif\r
-\r
-\r
-//\r
-// String value type\r
-//\r
-class WXDLLIMPEXP_BASE wxAnyValueTypeImplString :\r
- public wxAnyValueTypeImplBase<wxString>\r
-{\r
- WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplString)\r
-public:\r
- wxAnyValueTypeImplString() :\r
- wxAnyValueTypeImplBase<wxString>() { }\r
- virtual ~wxAnyValueTypeImplString() { }\r
-\r
- /**\r
- Convert value into buffer of different type. Return false if\r
- not possible.\r
- */\r
- virtual bool ConvertValue(const wxAnyValueBuffer& src,\r
- wxAnyValueType* dstType,\r
- wxAnyValueBuffer& dst) const;\r
-\r
-};\r
-\r
-template<>\r
-class wxAnyValueTypeImpl<wxString> : public wxAnyValueTypeImplString\r
-{\r
-public:\r
- wxAnyValueTypeImpl() : wxAnyValueTypeImplString() { }\r
- virtual ~wxAnyValueTypeImpl() { }\r
-};\r
-\r
-\r
-//\r
-// Bool value type\r
-//\r
-template<>\r
-class WXDLLIMPEXP_BASE wxAnyValueTypeImpl<bool> :\r
- public wxAnyValueTypeImplBase<bool>\r
-{\r
- WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<bool>)\r
-public:\r
- wxAnyValueTypeImpl() :\r
- wxAnyValueTypeImplBase<bool>() { }\r
- virtual ~wxAnyValueTypeImpl() { }\r
-\r
- virtual bool ConvertValue(const wxAnyValueBuffer& src,\r
- wxAnyValueType* dstType,\r
- wxAnyValueBuffer& dst) const;\r
-};\r
-\r
-//\r
-// Floating point value type\r
-//\r
-class WXDLLIMPEXP_BASE wxAnyValueTypeImplDouble :\r
- public wxAnyValueTypeImplBase<double>\r
-{\r
- WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplDouble)\r
-public:\r
- wxAnyValueTypeImplDouble() :\r
- wxAnyValueTypeImplBase<double>() { }\r
- virtual ~wxAnyValueTypeImplDouble() { }\r
-\r
- virtual bool ConvertValue(const wxAnyValueBuffer& src,\r
- wxAnyValueType* dstType,\r
- wxAnyValueBuffer& dst) const;\r
-};\r
-\r
-// WX_ANY_DEFINE_SUB_TYPE requires this\r
-typedef double wxAnyBaseDoubleType;\r
-\r
-WX_ANY_DEFINE_SUB_TYPE(float, Double)\r
-WX_ANY_DEFINE_SUB_TYPE(double, Double)\r
-\r
-\r
-#ifdef __VISUALC6__\r
- // Re-enable useless VC6 warnings\r
- #pragma warning (pop)\r
-#endif\r
-\r
-\r
-/*\r
- Let's define a discrete Null value so we don't have to really\r
- ever check if wxAny.m_type pointer is NULL or not. This is an\r
- optimization, mostly. Implementation of this value type is\r
- "hidden" in the source file.\r
-*/\r
-extern WXDLLIMPEXP_DATA_BASE(wxAnyValueType*) wxAnyNullValueType;\r
-\r
-\r
-//\r
-// We need to implement custom signed/unsigned int equals operators\r
-// for signed/unsigned (eg. wxAny(128UL) == 128L) comparisons to work.\r
-#define WXANY_IMPLEMENT_INT_EQ_OP(TS, TUS) \\r
-bool operator==(TS value) const \\r
-{ \\r
- if ( wxAnyValueTypeImpl<TS>::IsSameClass(m_type) ) \\r
- return (value == static_cast<TS> \\r
- (wxAnyValueTypeImpl<TS>::GetValue(m_buffer))); \\r
- if ( wxAnyValueTypeImpl<TUS>::IsSameClass(m_type) ) \\r
- return (value == static_cast<TS> \\r
- (wxAnyValueTypeImpl<TUS>::GetValue(m_buffer))); \\r
- return false; \\r
-} \\r
-bool operator==(TUS value) const \\r
-{ \\r
- if ( wxAnyValueTypeImpl<TUS>::IsSameClass(m_type) ) \\r
- return (value == static_cast<TUS> \\r
- (wxAnyValueTypeImpl<TUS>::GetValue(m_buffer))); \\r
- if ( wxAnyValueTypeImpl<TS>::IsSameClass(m_type) ) \\r
- return (value == static_cast<TUS> \\r
- (wxAnyValueTypeImpl<TS>::GetValue(m_buffer))); \\r
- return false; \\r
-}\r
-\r
-\r
-//\r
-// The wxAny class represents a container for any type. A variant's value\r
-// can be changed at run time, possibly to a different type of value.\r
-//\r
-// As standard, wxAny can store value of almost any type, in a fairly\r
-// optimal manner even.\r
-//\r
-class WXDLLIMPEXP_BASE wxAny\r
-{\r
-public:\r
- /**\r
- Default constructor.\r
- */\r
- wxAny()\r
- {\r
- m_type = wxAnyNullValueType;\r
- }\r
-\r
- /**\r
- Destructor.\r
- */\r
- ~wxAny()\r
- {\r
- m_type->DeleteValue(m_buffer);\r
- }\r
-\r
- //@{\r
- /**\r
- Various constructors.\r
- */\r
- wxAny(const char* value)\r
- {\r
- m_type = wxAnyNullValueType;\r
- Assign(wxString(value));\r
- }\r
- wxAny(const wchar_t* value)\r
- {\r
- m_type = wxAnyNullValueType;\r
- Assign(wxString(value));\r
- }\r
-\r
- wxAny(const wxAny& any)\r
- {\r
- m_type = wxAnyNullValueType;\r
- AssignAny(any);\r
- }\r
-\r
- template<typename T>\r
- wxAny(const T& value)\r
- {\r
- m_type = wxAnyValueTypeImpl<T>::sm_instance;\r
- wxAnyValueTypeImpl<T>::SetValue(value, m_buffer);\r
- }\r
- //@}\r
-\r
- /**\r
- Use this template function for checking if this wxAny holds\r
- a specific C++ data type.\r
-\r
- @remarks This template function does not work on some older compilers\r
- (such as Visual C++ 6.0). For full compiler ccompatibility\r
- please use wxANY_CHECK_TYPE(any, T) macro instead.\r
-\r
- @see wxAnyValueType::CheckType()\r
- */\r
- // FIXME-VC6: remove this hack when VC6 is no longer supported\r
- template <typename T>\r
- bool CheckType(T* = NULL)\r
- {\r
- return m_type->CheckType<T>();\r
- }\r
-\r
- /**\r
- Returns the value type as wxAnyValueType instance.\r
-\r
- @remarks You cannot reliably test whether two wxAnys are of\r
- same value type by simply comparing return values\r
- of wxAny::GetType(). Instead use\r
- wxAnyValueType::CheckType<T>() template function.\r
- */\r
- const wxAnyValueType* GetType() const\r
- {\r
- return m_type;\r
- }\r
-\r
- /**\r
- Tests if wxAny is null (that is, whether there is data).\r
- */\r
- bool IsNull() const\r
- {\r
- return (m_type == wxAnyNullValueType);\r
- }\r
-\r
- /**\r
- Makes wxAny null (that is, clears it).\r
- */\r
- void MakeNull()\r
- {\r
- m_type->DeleteValue(m_buffer);\r
- m_type = wxAnyNullValueType;\r
- }\r
-\r
- //@{\r
- /**\r
- Assignment operators.\r
- */\r
- wxAny& operator=(const wxAny &any)\r
- {\r
- AssignAny(any);\r
- return *this;\r
- }\r
-\r
- template<typename T>\r
- wxAny& operator=(const T &value)\r
- {\r
- m_type->DeleteValue(m_buffer);\r
- m_type = wxAnyValueTypeImpl<T>::sm_instance;\r
- wxAnyValueTypeImpl<T>::SetValue(value, m_buffer);\r
- return *this;\r
- }\r
-\r
- wxAny& operator=(const char* value)\r
- { Assign(wxString(value)); return *this; }\r
- wxAny& operator=(const wchar_t* value)\r
- { Assign(wxString(value)); return *this; }\r
- //@}\r
-\r
- //@{\r
- /**\r
- Equality operators.\r
- */\r
- bool operator==(const wxString& value) const\r
- {\r
- if ( !wxAnyValueTypeImpl<wxString>::IsSameClass(m_type) )\r
- return false;\r
-\r
- return value ==\r
- static_cast<wxString>\r
- (wxAnyValueTypeImpl<wxString>::GetValue(m_buffer));\r
- }\r
-\r
- bool operator==(const char* value) const\r
- { return (*this) == wxString(value); }\r
- bool operator==(const wchar_t* value) const\r
- { return (*this) == wxString(value); }\r
-\r
- //\r
- // We need to implement custom signed/unsigned int equals operators\r
- // for signed/unsigned (eg. wxAny(128UL) == 128L) comparisons to work.\r
- WXANY_IMPLEMENT_INT_EQ_OP(signed char, unsigned char)\r
- WXANY_IMPLEMENT_INT_EQ_OP(signed short, unsigned short)\r
- WXANY_IMPLEMENT_INT_EQ_OP(signed int, unsigned int)\r
- WXANY_IMPLEMENT_INT_EQ_OP(signed long, unsigned long)\r
-#ifdef wxLongLong_t\r
- WXANY_IMPLEMENT_INT_EQ_OP(wxLongLong_t, wxULongLong_t)\r
-#endif\r
-\r
- bool operator==(float value) const\r
- {\r
- if ( !wxAnyValueTypeImpl<float>::IsSameClass(m_type) )\r
- return false;\r
-\r
- return value ==\r
- static_cast<float>\r
- (wxAnyValueTypeImpl<float>::GetValue(m_buffer));\r
- }\r
-\r
- bool operator==(double value) const\r
- {\r
- if ( !wxAnyValueTypeImpl<double>::IsSameClass(m_type) )\r
- return false;\r
-\r
- return value ==\r
- static_cast<double>\r
- (wxAnyValueTypeImpl<double>::GetValue(m_buffer));\r
- }\r
-\r
- bool operator==(bool value) const\r
- {\r
- if ( !wxAnyValueTypeImpl<bool>::IsSameClass(m_type) )\r
- return false;\r
-\r
- return value == (wxAnyValueTypeImpl<bool>::GetValue(m_buffer));\r
- }\r
-\r
- //@}\r
-\r
- //@{\r
- /**\r
- Inequality operators (implement as template).\r
- */\r
- template<typename T>\r
- bool operator!=(const T& value) const\r
- { return !((*this) == value); }\r
- //@}\r
-\r
- /**\r
- This template function converts wxAny into given type. No dynamic\r
- conversion is performed, so if the type is incorrect an assertion\r
- failure will occur in debug builds, and a bogus value is returned\r
- in release ones.\r
-\r
- @remarks This template function does not work on some older compilers\r
- (such as Visual C++ 6.0). For full compiler ccompatibility\r
- please use wxANY_AS(any, T) macro instead.\r
- */\r
- // FIXME-VC6: remove this hack when VC6 is no longer supported\r
- template<typename T>\r
- T As(T* = NULL) const\r
- {\r
- if ( !wxAnyValueTypeImpl<T>::IsSameClass(m_type) )\r
- wxFAIL_MSG("Incorrect or non-convertible data type");\r
- return static_cast<T>(wxAnyValueTypeImpl<T>::GetValue(m_buffer));\r
- }\r
-\r
- /**\r
- Template function that etrieves and converts the value of this\r
- variant to the type that T* value is.\r
-\r
- @return Returns @true if conversion was succesfull.\r
- */\r
- template<typename T>\r
- bool GetAs(T* value) const\r
- {\r
- if ( !wxAnyValueTypeImpl<T>::IsSameClass(m_type) )\r
- {\r
- wxAnyValueType* otherType =\r
- wxAnyValueTypeImpl<T>::sm_instance;\r
- wxAnyValueBuffer temp_buf;\r
-\r
- if ( !m_type->ConvertValue(m_buffer, otherType, temp_buf) )\r
- return false;\r
-\r
- *value =\r
- static_cast<T>(wxAnyValueTypeImpl<T>::GetValue(temp_buf));\r
- otherType->DeleteValue(temp_buf);\r
-\r
- return true;\r
- }\r
- *value = static_cast<T>(wxAnyValueTypeImpl<T>::GetValue(m_buffer));\r
- return true;\r
- }\r
-\r
-private:\r
- // Assignment functions\r
- void AssignAny(const wxAny &any);\r
-\r
- template<typename T>\r
- void Assign(const T &value)\r
- {\r
- m_type->DeleteValue(m_buffer);\r
- m_type = wxAnyValueTypeImpl<T>::sm_instance;\r
- wxAnyValueTypeImpl<T>::SetValue(value, m_buffer);\r
- }\r
-\r
- // Data\r
- wxAnyValueType* m_type;\r
- wxAnyValueBuffer m_buffer;\r
-};\r
-\r
-\r
-//\r
-// This method of checking the type is compatible with VC6\r
-#define wxANY_CHECK_TYPE(any, T) \\r
- wxANY_VALUE_TYPE_CHECK_TYPE(any.GetType(), T)\r
-\r
-\r
-//\r
-// This method of getting the value is compatible with VC6\r
-#define wxANY_AS(any, T) \\r
- any.As(static_cast<T*>(NULL))\r
-\r
-\r
-template<typename T>\r
-inline bool wxAnyValueType::CheckType(T* reserved)\r
-{\r
- wxUnusedVar(reserved);\r
- return wxAnyValueTypeImpl<T>::IsSameClass(this);\r
-}\r
-\r
-\r
-\r
-#endif // wxUSE_ANY\r
-\r
-#endif // _WX_ANY_H_\r
+/////////////////////////////////////////////////////////////////////////////
+// Name: wx/any.h
+// Purpose: wxAny class
+// Author: Jaakko Salli
+// Modified by:
+// Created: 07/05/2009
+// RCS-ID: $Id$
+// Copyright: (c) wxWidgets team
+// Licence: wxWindows licence
+/////////////////////////////////////////////////////////////////////////////
+
+#ifndef _WX_ANY_H_
+#define _WX_ANY_H_
+
+#include "wx/defs.h"
+
+#if wxUSE_ANY
+
+#include "wx/string.h"
+#include "wx/meta/movable.h"
+#include "wx/meta/if.h"
+
+
+// Size of the wxAny value buffer.
+enum
+{
+ WX_ANY_VALUE_BUFFER_SIZE = 16
+};
+
+union wxAnyValueBuffer
+{
+ union Alignment
+ {
+ #if wxHAS_INT64
+ wxInt64 m_int64;
+ #endif
+ long double m_longDouble;
+ void ( *m_funcPtr )(void);
+ void ( wxAnyValueBuffer::*m_mFuncPtr )(void);
+ } m_alignment;
+
+ void* m_ptr;
+ wxByte m_buffer[WX_ANY_VALUE_BUFFER_SIZE];
+};
+
+typedef void (*wxAnyClassInfo)();
+
+
+//
+// wxAnyValueType is base class for value type functionality for C++ data
+// types used with wxAny. Usually the default template (wxAnyValueTypeImpl<>)
+// will create a satisfactory wxAnyValueType implementation for a data type.
+//
+class WXDLLIMPEXP_BASE wxAnyValueType
+{
+public:
+ /**
+ Default constructor.
+ */
+ wxAnyValueType();
+
+ /**
+ Destructor.
+ */
+ virtual ~wxAnyValueType()
+ {
+ }
+
+ /**
+ This function is used for internal type matching.
+ */
+ virtual wxAnyClassInfo GetClassInfo() const = 0;
+
+ /**
+ This function is used for internal type matching.
+ */
+ virtual bool IsSameType(const wxAnyValueType* otherType) const = 0;
+
+ /**
+ This function is called every time the data in wxAny
+ buffer needs to be freed.
+ */
+ virtual void DeleteValue(wxAnyValueBuffer& buf) const = 0;
+
+ /**
+ Implement this for buffer-to-buffer copy.
+
+ @param src
+ This is the source data buffer.
+
+ @param dst
+ This is the destination data buffer that is in either
+ uninitialized or freed state.
+ */
+ virtual void CopyBuffer(const wxAnyValueBuffer& src,
+ wxAnyValueBuffer& dst) const = 0;
+
+ /**
+ Convert value into buffer of different type. Return false if
+ not possible.
+ */
+ virtual bool ConvertValue(const wxAnyValueBuffer& src,
+ wxAnyValueType* dstType,
+ wxAnyValueBuffer& dst) const = 0;
+
+ /**
+ Use this template function for checking if wxAnyValueType represents
+ a specific C++ data type.
+
+ @remarks This template function does not work on some older compilers
+ (such as Visual C++ 6.0). For full compiler ccompatibility
+ please use wxANY_VALUE_TYPE_CHECK_TYPE(valueTypePtr, T) macro
+ instead.
+
+ @see wxAny::CheckType()
+ */
+ // FIXME-VC6: remove this hack when VC6 is no longer supported
+ template <typename T>
+ bool CheckType(T* reserved = NULL);
+private:
+};
+
+//
+// This method of checking the type is compatible with VC6
+#define wxANY_VALUE_TYPE_CHECK_TYPE(valueTypePtr, T) \
+ wxAnyValueTypeImpl<T>::IsSameClass(valueTypePtr)
+
+ //valueTypePtr->CheckType(static_cast<T*>(NULL))
+
+
+/**
+ Helper macro for defining user value types.
+
+ NB: We really cannot compare sm_classInfo directly in IsSameClass(),
+ but instead call sm_instance->GetClassInfo(). The former technique
+ broke at least on GCC 4.2 (but worked on VC8 shared build).
+*/
+#define WX_DECLARE_ANY_VALUE_TYPE(CLS) \
+ friend class wxAny; \
+public: \
+ static void sm_classInfo() {} \
+ \
+ virtual wxAnyClassInfo GetClassInfo() const \
+ { \
+ return sm_classInfo; \
+ } \
+ static bool IsSameClass(const wxAnyValueType* otherType) \
+ { \
+ return sm_instance->GetClassInfo() == otherType->GetClassInfo(); \
+ } \
+ virtual bool IsSameType(const wxAnyValueType* otherType) const \
+ { \
+ return IsSameClass(otherType); \
+ } \
+private: \
+ static CLS* sm_instance; \
+public: \
+ static wxAnyValueType* GetInstance() \
+ { \
+ return sm_instance; \
+ }
+
+
+#define WX_IMPLEMENT_ANY_VALUE_TYPE(CLS) \
+ CLS* CLS::sm_instance = new CLS();
+
+
+#ifdef __VISUALC6__
+ // "non dll-interface class 'xxx' used as base interface
+ #pragma warning (push)
+ #pragma warning (disable:4275)
+#endif
+
+/**
+ Following are helper classes for the wxAnyValueTypeImplBase.
+*/
+namespace wxPrivate
+{
+
+template<typename T>
+class wxAnyValueTypeOpsMovable
+{
+public:
+ static void DeleteValue(wxAnyValueBuffer& buf)
+ {
+ wxUnusedVar(buf);
+ }
+
+ static void SetValue(const T& value,
+ wxAnyValueBuffer& buf)
+ {
+ memcpy(buf.m_buffer, &value, sizeof(T));
+ }
+
+ static const T& GetValue(const wxAnyValueBuffer& buf)
+ {
+ return *(reinterpret_cast<const T*>(&buf.m_buffer[0]));
+ }
+};
+
+
+template<typename T>
+class wxAnyValueTypeOpsGeneric
+{
+public:
+ template<typename T2>
+ class DataHolder
+ {
+ public:
+ DataHolder(const T2& value)
+ {
+ m_value = value;
+ }
+ virtual ~DataHolder() { }
+
+ T2 m_value;
+ private:
+ wxDECLARE_NO_COPY_CLASS(DataHolder);
+ };
+
+ static void DeleteValue(wxAnyValueBuffer& buf)
+ {
+ DataHolder<T>* holder = static_cast<DataHolder<T>*>(buf.m_ptr);
+ delete holder;
+ }
+
+ static void SetValue(const T& value,
+ wxAnyValueBuffer& buf)
+ {
+ DataHolder<T>* holder = new DataHolder<T>(value);
+ buf.m_ptr = holder;
+ }
+
+ static const T& GetValue(const wxAnyValueBuffer& buf)
+ {
+ DataHolder<T>* holder = static_cast<DataHolder<T>*>(buf.m_ptr);
+ return holder->m_value;
+ }
+};
+
+} // namespace wxPrivate
+
+
+/**
+ Intermediate template for the generic value type implementation.
+ We can derive from this same value type for multiple actual types
+ (for instance, we can have wxAnyValueTypeImplInt for all signed
+ integer types), and also easily implement specialized templates
+ with specific dynamic type conversion.
+*/
+template<typename T>
+class wxAnyValueTypeImplBase : public wxAnyValueType
+{
+ typedef typename wxIf< wxIsMovable<T>::value &&
+ sizeof(T) <= WX_ANY_VALUE_BUFFER_SIZE,
+ wxPrivate::wxAnyValueTypeOpsMovable<T>,
+ wxPrivate::wxAnyValueTypeOpsGeneric<T> >::value
+ Ops;
+
+public:
+ wxAnyValueTypeImplBase() : wxAnyValueType() { }
+ virtual ~wxAnyValueTypeImplBase() { }
+
+ virtual void DeleteValue(wxAnyValueBuffer& buf) const
+ {
+ Ops::DeleteValue(buf);
+ }
+
+ virtual void CopyBuffer(const wxAnyValueBuffer& src,
+ wxAnyValueBuffer& dst) const
+ {
+ Ops::SetValue(Ops::GetValue(src), dst);
+ }
+
+ /**
+ It is important to reimplement this in any specialized template
+ classes that inherit from wxAnyValueTypeImplBase.
+ */
+ static void SetValue(const T& value,
+ wxAnyValueBuffer& buf)
+ {
+ Ops::SetValue(value, buf);
+ }
+
+ /**
+ It is important to reimplement this in any specialized template
+ classes that inherit from wxAnyValueTypeImplBase.
+ */
+ static const T& GetValue(const wxAnyValueBuffer& buf)
+ {
+ return Ops::GetValue(buf);
+ }
+};
+
+
+/*
+ Generic value type template. Note that bulk of the implementation
+ resides in wxAnyValueTypeImplBase.
+*/
+template<typename T>
+class wxAnyValueTypeImpl : public wxAnyValueTypeImplBase<T>
+{
+ WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<T>)
+public:
+ wxAnyValueTypeImpl() : wxAnyValueTypeImplBase<T>() { }
+ virtual ~wxAnyValueTypeImpl() { }
+
+ virtual bool ConvertValue(const wxAnyValueBuffer& src,
+ wxAnyValueType* dstType,
+ wxAnyValueBuffer& dst) const
+ {
+ wxUnusedVar(src);
+ wxUnusedVar(dstType);
+ wxUnusedVar(dst);
+ return false;
+ }
+};
+
+template<typename T>
+wxAnyValueTypeImpl<T>* wxAnyValueTypeImpl<T>::sm_instance =
+ new wxAnyValueTypeImpl<T>();
+
+
+//
+// Helper macro for using same base value type implementation for multiple
+// actual C++ data types.
+//
+#define WX_ANY_DEFINE_SUB_TYPE(T, CLSTYPE) \
+template<> \
+class wxAnyValueTypeImpl<T> : public wxAnyValueTypeImpl##CLSTYPE \
+{ \
+ typedef wxAnyBase##CLSTYPE##Type UseDataType; \
+public: \
+ wxAnyValueTypeImpl() : wxAnyValueTypeImpl##CLSTYPE() { } \
+ virtual ~wxAnyValueTypeImpl() { } \
+ static void SetValue(const T& value, wxAnyValueBuffer& buf) \
+ { \
+ *(reinterpret_cast<UseDataType*>(&buf.m_buffer[0])) = \
+ static_cast<UseDataType>(value); \
+ } \
+ static T GetValue(const wxAnyValueBuffer& buf) \
+ { \
+ return static_cast<T>( \
+ *(reinterpret_cast<const UseDataType*>(&buf.m_buffer[0]))); \
+ } \
+};
+
+
+//
+// Integer value types
+//
+
+#ifdef wxLongLong_t
+ typedef wxLongLong_t wxAnyBaseIntType;
+ typedef wxULongLong_t wxAnyBaseUintType;
+#else
+ typedef long wxAnyBaseIntType;
+ typedef unsigned long wxAnyBaseUintType;
+#endif
+
+
+class WXDLLIMPEXP_BASE wxAnyValueTypeImplInt :
+ public wxAnyValueTypeImplBase<wxAnyBaseIntType>
+{
+ WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplInt)
+public:
+ wxAnyValueTypeImplInt() :
+ wxAnyValueTypeImplBase<wxAnyBaseIntType>() { }
+ virtual ~wxAnyValueTypeImplInt() { }
+
+ virtual bool ConvertValue(const wxAnyValueBuffer& src,
+ wxAnyValueType* dstType,
+ wxAnyValueBuffer& dst) const;
+};
+
+
+class WXDLLIMPEXP_BASE wxAnyValueTypeImplUint :
+ public wxAnyValueTypeImplBase<wxAnyBaseUintType>
+{
+ WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplUint)
+public:
+ wxAnyValueTypeImplUint() :
+ wxAnyValueTypeImplBase<wxAnyBaseUintType>() { }
+ virtual ~wxAnyValueTypeImplUint() { }
+
+ virtual bool ConvertValue(const wxAnyValueBuffer& src,
+ wxAnyValueType* dstType,
+ wxAnyValueBuffer& dst) const;
+};
+
+
+WX_ANY_DEFINE_SUB_TYPE(signed long, Int)
+WX_ANY_DEFINE_SUB_TYPE(signed int, Int)
+WX_ANY_DEFINE_SUB_TYPE(signed short, Int)
+WX_ANY_DEFINE_SUB_TYPE(signed char, Int)
+#ifdef wxLongLong_t
+WX_ANY_DEFINE_SUB_TYPE(wxLongLong_t, Int)
+#endif
+
+WX_ANY_DEFINE_SUB_TYPE(unsigned long, Uint)
+WX_ANY_DEFINE_SUB_TYPE(unsigned int, Uint)
+WX_ANY_DEFINE_SUB_TYPE(unsigned short, Uint)
+WX_ANY_DEFINE_SUB_TYPE(unsigned char, Uint)
+#ifdef wxLongLong_t
+WX_ANY_DEFINE_SUB_TYPE(wxULongLong_t, Uint)
+#endif
+
+
+//
+// String value type
+//
+class WXDLLIMPEXP_BASE wxAnyValueTypeImplString :
+ public wxAnyValueTypeImplBase<wxString>
+{
+ WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplString)
+public:
+ wxAnyValueTypeImplString() :
+ wxAnyValueTypeImplBase<wxString>() { }
+ virtual ~wxAnyValueTypeImplString() { }
+
+ /**
+ Convert value into buffer of different type. Return false if
+ not possible.
+ */
+ virtual bool ConvertValue(const wxAnyValueBuffer& src,
+ wxAnyValueType* dstType,
+ wxAnyValueBuffer& dst) const;
+
+};
+
+template<>
+class wxAnyValueTypeImpl<wxString> : public wxAnyValueTypeImplString
+{
+public:
+ wxAnyValueTypeImpl() : wxAnyValueTypeImplString() { }
+ virtual ~wxAnyValueTypeImpl() { }
+};
+
+
+//
+// Bool value type
+//
+template<>
+class WXDLLIMPEXP_BASE wxAnyValueTypeImpl<bool> :
+ public wxAnyValueTypeImplBase<bool>
+{
+ WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<bool>)
+public:
+ wxAnyValueTypeImpl() :
+ wxAnyValueTypeImplBase<bool>() { }
+ virtual ~wxAnyValueTypeImpl() { }
+
+ virtual bool ConvertValue(const wxAnyValueBuffer& src,
+ wxAnyValueType* dstType,
+ wxAnyValueBuffer& dst) const;
+};
+
+//
+// Floating point value type
+//
+class WXDLLIMPEXP_BASE wxAnyValueTypeImplDouble :
+ public wxAnyValueTypeImplBase<double>
+{
+ WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplDouble)
+public:
+ wxAnyValueTypeImplDouble() :
+ wxAnyValueTypeImplBase<double>() { }
+ virtual ~wxAnyValueTypeImplDouble() { }
+
+ virtual bool ConvertValue(const wxAnyValueBuffer& src,
+ wxAnyValueType* dstType,
+ wxAnyValueBuffer& dst) const;
+};
+
+// WX_ANY_DEFINE_SUB_TYPE requires this
+typedef double wxAnyBaseDoubleType;
+
+WX_ANY_DEFINE_SUB_TYPE(float, Double)
+WX_ANY_DEFINE_SUB_TYPE(double, Double)
+
+
+//
+// Defines a dummy wxAnyValueTypeImpl<> with given export
+// declaration. This is needed if a class is used with
+// wxAny in both user shared library and application.
+//
+#define wxDECLARE_ANY_TYPE(CLS, DECL) \
+template<> \
+class DECL wxAnyValueTypeImpl<CLS> : \
+ public wxAnyValueTypeImplBase<CLS> \
+{ \
+ WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<CLS>) \
+public: \
+ wxAnyValueTypeImpl() : \
+ wxAnyValueTypeImplBase<CLS>() { } \
+ virtual ~wxAnyValueTypeImpl() { } \
+ \
+ virtual bool ConvertValue(const wxAnyValueBuffer& src, \
+ wxAnyValueType* dstType, \
+ wxAnyValueBuffer& dst) const \
+ { \
+ wxUnusedVar(src); \
+ wxUnusedVar(dstType); \
+ wxUnusedVar(dst); \
+ return false; \
+ } \
+};
+
+
+// Make sure some of wx's own types get the right wxAnyValueType export
+// (this is needed only for types that are referred to from wxBase.
+// currently we may not use any of these types from there, but let's
+// use the macro on at least one to make sure it compiles since we can't
+// really test it properly in unittests since a separate DLL would
+// be needed).
+#if wxUSE_DATETIME
+ #include "wx/datetime.h"
+ wxDECLARE_ANY_TYPE(wxDateTime, WXDLLIMPEXP_BASE)
+#endif
+
+//#include "wx/object.h"
+//wxDECLARE_ANY_TYPE(wxObject*, WXDLLIMPEXP_BASE)
+
+//#include "wx/arrstr.h"
+//wxDECLARE_ANY_TYPE(wxArrayString, WXDLLIMPEXP_BASE)
+
+
+
+#ifdef __VISUALC6__
+ // Re-enable useless VC6 warnings
+ #pragma warning (pop)
+#endif
+
+
+/*
+ Let's define a discrete Null value so we don't have to really
+ ever check if wxAny.m_type pointer is NULL or not. This is an
+ optimization, mostly. Implementation of this value type is
+ "hidden" in the source file.
+*/
+extern WXDLLIMPEXP_DATA_BASE(wxAnyValueType*) wxAnyNullValueType;
+
+
+//
+// We need to implement custom signed/unsigned int equals operators
+// for signed/unsigned (eg. wxAny(128UL) == 128L) comparisons to work.
+#define WXANY_IMPLEMENT_INT_EQ_OP(TS, TUS) \
+bool operator==(TS value) const \
+{ \
+ if ( wxAnyValueTypeImpl<TS>::IsSameClass(m_type) ) \
+ return (value == static_cast<TS> \
+ (wxAnyValueTypeImpl<TS>::GetValue(m_buffer))); \
+ if ( wxAnyValueTypeImpl<TUS>::IsSameClass(m_type) ) \
+ return (value == static_cast<TS> \
+ (wxAnyValueTypeImpl<TUS>::GetValue(m_buffer))); \
+ return false; \
+} \
+bool operator==(TUS value) const \
+{ \
+ if ( wxAnyValueTypeImpl<TUS>::IsSameClass(m_type) ) \
+ return (value == static_cast<TUS> \
+ (wxAnyValueTypeImpl<TUS>::GetValue(m_buffer))); \
+ if ( wxAnyValueTypeImpl<TS>::IsSameClass(m_type) ) \
+ return (value == static_cast<TUS> \
+ (wxAnyValueTypeImpl<TS>::GetValue(m_buffer))); \
+ return false; \
+}
+
+
+//
+// The wxAny class represents a container for any type. A variant's value
+// can be changed at run time, possibly to a different type of value.
+//
+// As standard, wxAny can store value of almost any type, in a fairly
+// optimal manner even.
+//
+class wxAny
+{
+public:
+ /**
+ Default constructor.
+ */
+ wxAny()
+ {
+ m_type = wxAnyNullValueType;
+ }
+
+ /**
+ Destructor.
+ */
+ ~wxAny()
+ {
+ m_type->DeleteValue(m_buffer);
+ }
+
+ //@{
+ /**
+ Various constructors.
+ */
+ wxAny(const char* value)
+ {
+ m_type = wxAnyNullValueType;
+ Assign(wxString(value));
+ }
+ wxAny(const wchar_t* value)
+ {
+ m_type = wxAnyNullValueType;
+ Assign(wxString(value));
+ }
+
+ wxAny(const wxAny& any)
+ {
+ m_type = wxAnyNullValueType;
+ AssignAny(any);
+ }
+
+ template<typename T>
+ wxAny(const T& value)
+ {
+ m_type = wxAnyValueTypeImpl<T>::sm_instance;
+ wxAnyValueTypeImpl<T>::SetValue(value, m_buffer);
+ }
+ //@}
+
+ /**
+ Use this template function for checking if this wxAny holds
+ a specific C++ data type.
+
+ @remarks This template function does not work on some older compilers
+ (such as Visual C++ 6.0). For full compiler ccompatibility
+ please use wxANY_CHECK_TYPE(any, T) macro instead.
+
+ @see wxAnyValueType::CheckType()
+ */
+ // FIXME-VC6: remove this hack when VC6 is no longer supported
+ template <typename T>
+ bool CheckType(T* = NULL)
+ {
+ return m_type->CheckType<T>();
+ }
+
+ /**
+ 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
+ wxAnyValueType::CheckType<T>() template function.
+ */
+ const wxAnyValueType* GetType() const
+ {
+ return m_type;
+ }
+
+ /**
+ Tests if wxAny is null (that is, whether there is data).
+ */
+ bool IsNull() const
+ {
+ return (m_type == wxAnyNullValueType);
+ }
+
+ /**
+ Makes wxAny null (that is, clears it).
+ */
+ void MakeNull()
+ {
+ m_type->DeleteValue(m_buffer);
+ m_type = wxAnyNullValueType;
+ }
+
+ //@{
+ /**
+ Assignment operators.
+ */
+ wxAny& operator=(const wxAny &any)
+ {
+ AssignAny(any);
+ return *this;
+ }
+
+ template<typename T>
+ wxAny& operator=(const T &value)
+ {
+ m_type->DeleteValue(m_buffer);
+ m_type = wxAnyValueTypeImpl<T>::sm_instance;
+ wxAnyValueTypeImpl<T>::SetValue(value, m_buffer);
+ return *this;
+ }
+
+ wxAny& operator=(const char* value)
+ { Assign(wxString(value)); return *this; }
+ wxAny& operator=(const wchar_t* value)
+ { Assign(wxString(value)); return *this; }
+ //@}
+
+ //@{
+ /**
+ Equality operators.
+ */
+ bool operator==(const wxString& value) const
+ {
+ if ( !wxAnyValueTypeImpl<wxString>::IsSameClass(m_type) )
+ return false;
+
+ return value ==
+ static_cast<wxString>
+ (wxAnyValueTypeImpl<wxString>::GetValue(m_buffer));
+ }
+
+ bool operator==(const char* value) const
+ { return (*this) == wxString(value); }
+ bool operator==(const wchar_t* value) const
+ { return (*this) == wxString(value); }
+
+ //
+ // We need to implement custom signed/unsigned int equals operators
+ // for signed/unsigned (eg. wxAny(128UL) == 128L) comparisons to work.
+ WXANY_IMPLEMENT_INT_EQ_OP(signed char, unsigned char)
+ WXANY_IMPLEMENT_INT_EQ_OP(signed short, unsigned short)
+ WXANY_IMPLEMENT_INT_EQ_OP(signed int, unsigned int)
+ WXANY_IMPLEMENT_INT_EQ_OP(signed long, unsigned long)
+#ifdef wxLongLong_t
+ WXANY_IMPLEMENT_INT_EQ_OP(wxLongLong_t, wxULongLong_t)
+#endif
+
+ bool operator==(float value) const
+ {
+ if ( !wxAnyValueTypeImpl<float>::IsSameClass(m_type) )
+ return false;
+
+ return value ==
+ static_cast<float>
+ (wxAnyValueTypeImpl<float>::GetValue(m_buffer));
+ }
+
+ bool operator==(double value) const
+ {
+ if ( !wxAnyValueTypeImpl<double>::IsSameClass(m_type) )
+ return false;
+
+ return value ==
+ static_cast<double>
+ (wxAnyValueTypeImpl<double>::GetValue(m_buffer));
+ }
+
+ bool operator==(bool value) const
+ {
+ if ( !wxAnyValueTypeImpl<bool>::IsSameClass(m_type) )
+ return false;
+
+ return value == (wxAnyValueTypeImpl<bool>::GetValue(m_buffer));
+ }
+
+ //@}
+
+ //@{
+ /**
+ Inequality operators (implement as template).
+ */
+ template<typename T>
+ bool operator!=(const T& value) const
+ { return !((*this) == value); }
+ //@}
+
+ /**
+ This template function converts wxAny into given type. No dynamic
+ conversion is performed, so if the type is incorrect an assertion
+ failure will occur in debug builds, and a bogus value is returned
+ in release ones.
+
+ @remarks This template function does not work on some older compilers
+ (such as Visual C++ 6.0). For full compiler ccompatibility
+ please use wxANY_AS(any, T) macro instead.
+ */
+ // FIXME-VC6: remove this hack when VC6 is no longer supported
+ template<typename T>
+ T As(T* = NULL) const
+ {
+ if ( !wxAnyValueTypeImpl<T>::IsSameClass(m_type) )
+ {
+ wxFAIL_MSG("Incorrect or non-convertible data type");
+ }
+
+ return static_cast<T>(wxAnyValueTypeImpl<T>::GetValue(m_buffer));
+ }
+
+ /**
+ Template function that etrieves and converts the value of this
+ variant to the type that T* value is.
+
+ @return Returns @true if conversion was succesfull.
+ */
+ template<typename T>
+ bool GetAs(T* value) const
+ {
+ if ( !wxAnyValueTypeImpl<T>::IsSameClass(m_type) )
+ {
+ wxAnyValueType* otherType =
+ wxAnyValueTypeImpl<T>::sm_instance;
+ wxAnyValueBuffer temp_buf;
+
+ if ( !m_type->ConvertValue(m_buffer, otherType, temp_buf) )
+ return false;
+
+ *value =
+ static_cast<T>(wxAnyValueTypeImpl<T>::GetValue(temp_buf));
+ otherType->DeleteValue(temp_buf);
+
+ return true;
+ }
+ *value = static_cast<T>(wxAnyValueTypeImpl<T>::GetValue(m_buffer));
+ return true;
+ }
+
+private:
+ // Assignment functions
+ void AssignAny(const wxAny& any)
+ {
+ // Must delete value - CopyBuffer() never does that
+ m_type->DeleteValue(m_buffer);
+
+ wxAnyValueType* newType = any.m_type;
+
+ if ( !newType->IsSameType(m_type) )
+ m_type = newType;
+
+ newType->CopyBuffer(any.m_buffer, m_buffer);
+ }
+
+ template<typename T>
+ void Assign(const T &value)
+ {
+ m_type->DeleteValue(m_buffer);
+ m_type = wxAnyValueTypeImpl<T>::sm_instance;
+ wxAnyValueTypeImpl<T>::SetValue(value, m_buffer);
+ }
+
+ // Data
+ wxAnyValueBuffer m_buffer;
+ wxAnyValueType* m_type;
+};
+
+
+//
+// This method of checking the type is compatible with VC6
+#define wxANY_CHECK_TYPE(any, T) \
+ wxANY_VALUE_TYPE_CHECK_TYPE(any.GetType(), T)
+
+
+//
+// This method of getting the value is compatible with VC6
+#define wxANY_AS(any, T) \
+ any.As(static_cast<T*>(NULL))
+
+
+template<typename T>
+inline bool wxAnyValueType::CheckType(T* reserved)
+{
+ wxUnusedVar(reserved);
+ return wxAnyValueTypeImpl<T>::IsSameClass(this);
+}
+
+
+
+#endif // wxUSE_ANY
+
+#endif // _WX_ANY_H_