2009-09-19 15:41:08 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/any.h
|
|
|
|
// Purpose: wxAny class
|
|
|
|
// Author: Jaakko Salli
|
|
|
|
// Modified by:
|
|
|
|
// Created: 07/05/2009
|
|
|
|
// Copyright: (c) wxWidgets team
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_ANY_H_
|
|
|
|
#define _WX_ANY_H_
|
|
|
|
|
|
|
|
#include "wx/defs.h"
|
|
|
|
|
|
|
|
#if wxUSE_ANY
|
|
|
|
|
2010-06-18 13:18:22 +00:00
|
|
|
#include <new> // for placement new
|
2009-09-19 15:41:08 +00:00
|
|
|
#include "wx/string.h"
|
|
|
|
#include "wx/meta/if.h"
|
2009-11-19 19:27:24 +00:00
|
|
|
#include "wx/typeinfo.h"
|
2011-01-06 11:06:12 +00:00
|
|
|
#include "wx/list.h"
|
2009-09-19 15:41:08 +00:00
|
|
|
|
|
|
|
// Size of the wxAny value buffer.
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
WX_ANY_VALUE_BUFFER_SIZE = 16
|
|
|
|
};
|
|
|
|
|
|
|
|
union wxAnyValueBuffer
|
|
|
|
{
|
2009-09-27 16:30:26 +00:00
|
|
|
union Alignment
|
|
|
|
{
|
|
|
|
#if wxHAS_INT64
|
|
|
|
wxInt64 m_int64;
|
|
|
|
#endif
|
|
|
|
long double m_longDouble;
|
|
|
|
void ( *m_funcPtr )(void);
|
|
|
|
void ( wxAnyValueBuffer::*m_mFuncPtr )(void);
|
|
|
|
} m_alignment;
|
|
|
|
|
2009-09-19 15:41:08 +00:00
|
|
|
void* m_ptr;
|
|
|
|
wxByte m_buffer[WX_ANY_VALUE_BUFFER_SIZE];
|
2019-10-14 18:34:00 +00:00
|
|
|
|
|
|
|
wxAnyValueBuffer()
|
|
|
|
{
|
|
|
|
m_ptr = NULL;
|
|
|
|
}
|
2009-09-19 15:41:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
{
|
2009-11-19 19:27:24 +00:00
|
|
|
WX_DECLARE_ABSTRACT_TYPEINFO(wxAnyValueType)
|
2009-09-19 15:41:08 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
Default constructor.
|
|
|
|
*/
|
2010-04-30 13:32:41 +00:00
|
|
|
wxAnyValueType()
|
|
|
|
{
|
|
|
|
}
|
2009-09-19 15:41:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Destructor.
|
|
|
|
*/
|
|
|
|
virtual ~wxAnyValueType()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
2009-09-29 13:38:20 +00:00
|
|
|
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.
|
2009-09-19 15:41:08 +00:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
|
|
|
|
@see wxAny::CheckType()
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2014-05-15 22:32:17 +00:00
|
|
|
bool CheckType() const;
|
2011-01-07 17:22:59 +00:00
|
|
|
|
|
|
|
#if wxUSE_EXTENDED_RTTI
|
|
|
|
virtual const wxTypeInfo* GetTypeInfo() const = 0;
|
|
|
|
#endif
|
2009-09-19 15:41:08 +00:00
|
|
|
private:
|
|
|
|
};
|
|
|
|
|
2010-04-30 13:32:41 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// We need to allocate wxAnyValueType instances in heap, and need to use
|
|
|
|
// scoped ptr to properly deallocate them in dynamic library use cases.
|
|
|
|
// Here we have a minimal specialized scoped ptr implementation to deal
|
|
|
|
// with various compiler-specific problems with template class' static
|
|
|
|
// member variable of template type with explicit constructor which
|
|
|
|
// is initialized in global scope.
|
|
|
|
//
|
|
|
|
class wxAnyValueTypeScopedPtr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxAnyValueTypeScopedPtr(wxAnyValueType* ptr) : m_ptr(ptr) { }
|
|
|
|
~wxAnyValueTypeScopedPtr() { delete m_ptr; }
|
|
|
|
wxAnyValueType* get() const { return m_ptr; }
|
|
|
|
private:
|
|
|
|
wxAnyValueType* m_ptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-05-15 22:32:17 +00:00
|
|
|
// Deprecated macro for checking the type which was originally introduced for
|
|
|
|
// MSVC6 compatibility and is not needed any longer now that this compiler is
|
|
|
|
// not supported any more.
|
2009-09-19 15:41:08 +00:00
|
|
|
#define wxANY_VALUE_TYPE_CHECK_TYPE(valueTypePtr, T) \
|
|
|
|
wxAnyValueTypeImpl<T>::IsSameClass(valueTypePtr)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Helper macro for defining user value types.
|
|
|
|
|
2009-11-19 19:27:24 +00:00
|
|
|
Even though C++ RTTI would be fully available to use, we'd have to to
|
|
|
|
facilitate sub-type system which allows, for instance, wxAny with
|
|
|
|
signed short '15' to be treated equal to wxAny with signed long long '15'.
|
|
|
|
Having sm_instance is important here.
|
2010-04-30 13:32:41 +00:00
|
|
|
|
|
|
|
NB: We really need to have wxAnyValueType instances allocated
|
|
|
|
in heap. They are stored as static template member variables,
|
|
|
|
and with them we just can't be too careful (eg. not allocating
|
|
|
|
them in heap broke the type identification in GCC).
|
2009-09-19 15:41:08 +00:00
|
|
|
*/
|
|
|
|
#define WX_DECLARE_ANY_VALUE_TYPE(CLS) \
|
|
|
|
friend class wxAny; \
|
2009-11-19 19:27:24 +00:00
|
|
|
WX_DECLARE_TYPEINFO_INLINE(CLS) \
|
2009-09-19 15:41:08 +00:00
|
|
|
public: \
|
|
|
|
static bool IsSameClass(const wxAnyValueType* otherType) \
|
|
|
|
{ \
|
2015-10-10 07:51:47 +00:00
|
|
|
return AreSameClasses(*sm_instance.get(), *otherType); \
|
2009-09-19 15:41:08 +00:00
|
|
|
} \
|
2015-09-07 00:20:42 +00:00
|
|
|
virtual bool IsSameType(const wxAnyValueType* otherType) const wxOVERRIDE \
|
2009-09-19 15:41:08 +00:00
|
|
|
{ \
|
|
|
|
return IsSameClass(otherType); \
|
|
|
|
} \
|
|
|
|
private: \
|
2015-10-10 07:51:47 +00:00
|
|
|
static bool AreSameClasses(const wxAnyValueType& a, const wxAnyValueType& b) \
|
|
|
|
{ \
|
|
|
|
return wxTypeId(a) == wxTypeId(b); \
|
|
|
|
} \
|
2010-04-30 13:32:41 +00:00
|
|
|
static wxAnyValueTypeScopedPtr sm_instance; \
|
2009-09-19 15:41:08 +00:00
|
|
|
public: \
|
|
|
|
static wxAnyValueType* GetInstance() \
|
|
|
|
{ \
|
2010-04-30 13:32:41 +00:00
|
|
|
return sm_instance.get(); \
|
2009-09-19 15:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define WX_IMPLEMENT_ANY_VALUE_TYPE(CLS) \
|
2010-04-30 13:32:41 +00:00
|
|
|
wxAnyValueTypeScopedPtr CLS::sm_instance(new CLS());
|
2009-09-19 15:41:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Following are helper classes for the wxAnyValueTypeImplBase.
|
|
|
|
*/
|
|
|
|
namespace wxPrivate
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename T>
|
2010-06-18 13:18:22 +00:00
|
|
|
class wxAnyValueTypeOpsInplace
|
2009-09-19 15:41:08 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void DeleteValue(wxAnyValueBuffer& buf)
|
|
|
|
{
|
2015-09-19 01:25:57 +00:00
|
|
|
GetValue(buf).~T();
|
2009-09-19 15:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void SetValue(const T& value,
|
|
|
|
wxAnyValueBuffer& buf)
|
|
|
|
{
|
2010-06-18 13:18:22 +00:00
|
|
|
// Use placement new
|
|
|
|
void* const place = buf.m_buffer;
|
|
|
|
::new(place) T(value);
|
2009-09-19 15:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const T& GetValue(const wxAnyValueBuffer& buf)
|
|
|
|
{
|
2015-09-19 01:25:57 +00:00
|
|
|
// Use a union to avoid undefined behaviour (and gcc -Wstrict-alias
|
|
|
|
// warnings about it) which would occur if we just casted a wxByte
|
|
|
|
// pointer to a T one.
|
|
|
|
union
|
|
|
|
{
|
|
|
|
const T* ptr;
|
|
|
|
const wxByte *buf;
|
|
|
|
} u;
|
|
|
|
u.buf = buf.m_buffer;
|
|
|
|
|
|
|
|
return *u.ptr;
|
2009-09-19 15:41:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class wxAnyValueTypeOpsGeneric
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template<typename T2>
|
|
|
|
class DataHolder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DataHolder(const T2& value)
|
2019-02-25 18:23:35 +00:00
|
|
|
: m_value(value)
|
2009-09-19 15:41:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-15 22:32:17 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct wxAnyAsImpl;
|
|
|
|
|
2009-09-19 15:41:08 +00:00
|
|
|
} // 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
|
|
|
|
{
|
2010-06-18 13:18:22 +00:00
|
|
|
typedef typename wxIf< sizeof(T) <= WX_ANY_VALUE_BUFFER_SIZE,
|
|
|
|
wxPrivate::wxAnyValueTypeOpsInplace<T>,
|
2009-09-19 15:41:08 +00:00
|
|
|
wxPrivate::wxAnyValueTypeOpsGeneric<T> >::value
|
|
|
|
Ops;
|
|
|
|
|
|
|
|
public:
|
|
|
|
wxAnyValueTypeImplBase() : wxAnyValueType() { }
|
|
|
|
virtual ~wxAnyValueTypeImplBase() { }
|
|
|
|
|
2014-03-30 00:02:23 +00:00
|
|
|
virtual void DeleteValue(wxAnyValueBuffer& buf) const wxOVERRIDE
|
2009-09-19 15:41:08 +00:00
|
|
|
{
|
|
|
|
Ops::DeleteValue(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void CopyBuffer(const wxAnyValueBuffer& src,
|
2014-03-30 00:02:23 +00:00
|
|
|
wxAnyValueBuffer& dst) const wxOVERRIDE
|
2009-09-19 15:41:08 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2011-01-07 17:22:59 +00:00
|
|
|
#if wxUSE_EXTENDED_RTTI
|
2018-04-11 14:15:09 +00:00
|
|
|
virtual const wxTypeInfo* GetTypeInfo() const
|
2011-01-07 17:22:59 +00:00
|
|
|
{
|
|
|
|
return wxGetTypeInfo((T*)NULL);
|
|
|
|
}
|
|
|
|
#endif
|
2009-09-19 15:41:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
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,
|
2015-09-07 00:20:42 +00:00
|
|
|
wxAnyValueBuffer& dst) const wxOVERRIDE
|
2009-09-19 15:41:08 +00:00
|
|
|
{
|
|
|
|
wxUnusedVar(src);
|
|
|
|
wxUnusedVar(dstType);
|
|
|
|
wxUnusedVar(dst);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2010-04-30 13:32:41 +00:00
|
|
|
wxAnyValueTypeScopedPtr wxAnyValueTypeImpl<T>::sm_instance = new wxAnyValueTypeImpl<T>();
|
2009-09-19 15:41:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Helper macro for using same base value type implementation for multiple
|
|
|
|
// actual C++ data types.
|
|
|
|
//
|
2011-01-07 17:22:59 +00:00
|
|
|
#define _WX_ANY_DEFINE_SUB_TYPE(T, CLSTYPE) \
|
2009-09-19 15:41:08 +00:00
|
|
|
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) \
|
|
|
|
{ \
|
2010-04-04 15:22:43 +00:00
|
|
|
void* voidPtr = reinterpret_cast<void*>(&buf.m_buffer[0]); \
|
|
|
|
UseDataType* dptr = reinterpret_cast<UseDataType*>(voidPtr); \
|
|
|
|
*dptr = static_cast<UseDataType>(value); \
|
2009-09-19 15:41:08 +00:00
|
|
|
} \
|
|
|
|
static T GetValue(const wxAnyValueBuffer& buf) \
|
|
|
|
{ \
|
2010-04-04 15:22:43 +00:00
|
|
|
const void* voidPtr = \
|
|
|
|
reinterpret_cast<const void*>(&buf.m_buffer[0]); \
|
|
|
|
const UseDataType* sptr = \
|
|
|
|
reinterpret_cast<const UseDataType*>(voidPtr); \
|
|
|
|
return static_cast<T>(*sptr); \
|
2018-04-11 14:15:09 +00:00
|
|
|
}
|
2011-01-07 17:22:59 +00:00
|
|
|
|
|
|
|
#if wxUSE_EXTENDED_RTTI
|
|
|
|
#define WX_ANY_DEFINE_SUB_TYPE(T, CLSTYPE) \
|
|
|
|
_WX_ANY_DEFINE_SUB_TYPE(T, CLSTYPE)\
|
|
|
|
virtual const wxTypeInfo* GetTypeInfo() const \
|
|
|
|
{ \
|
|
|
|
return wxGetTypeInfo((T*)NULL); \
|
2009-09-19 15:41:08 +00:00
|
|
|
} \
|
|
|
|
};
|
2011-01-07 17:22:59 +00:00
|
|
|
#else
|
|
|
|
#define WX_ANY_DEFINE_SUB_TYPE(T, CLSTYPE) \
|
|
|
|
_WX_ANY_DEFINE_SUB_TYPE(T, CLSTYPE)\
|
|
|
|
};
|
|
|
|
#endif
|
2009-09-19 15:41:08 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// 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,
|
2014-03-30 00:02:23 +00:00
|
|
|
wxAnyValueBuffer& dst) const wxOVERRIDE;
|
2009-09-19 15:41:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
2014-03-30 00:02:23 +00:00
|
|
|
wxAnyValueBuffer& dst) const wxOVERRIDE;
|
2009-09-19 15:41:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2010-04-22 13:51:38 +00:00
|
|
|
// This macro is used in header, but then in source file we must have:
|
|
|
|
// WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl##TYPENAME)
|
2009-09-19 15:41:08 +00:00
|
|
|
//
|
2010-04-22 13:51:38 +00:00
|
|
|
#define _WX_ANY_DEFINE_CONVERTIBLE_TYPE(T, TYPENAME, CONVFUNC, GV) \
|
|
|
|
class WXDLLIMPEXP_BASE wxAnyValueTypeImpl##TYPENAME : \
|
|
|
|
public wxAnyValueTypeImplBase<T> \
|
|
|
|
{ \
|
|
|
|
WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl##TYPENAME) \
|
|
|
|
public: \
|
|
|
|
wxAnyValueTypeImpl##TYPENAME() : \
|
|
|
|
wxAnyValueTypeImplBase<T>() { } \
|
|
|
|
virtual ~wxAnyValueTypeImpl##TYPENAME() { } \
|
|
|
|
virtual bool ConvertValue(const wxAnyValueBuffer& src, \
|
|
|
|
wxAnyValueType* dstType, \
|
2015-09-07 00:20:42 +00:00
|
|
|
wxAnyValueBuffer& dst) const wxOVERRIDE \
|
2010-04-22 13:51:38 +00:00
|
|
|
{ \
|
|
|
|
GV value = GetValue(src); \
|
|
|
|
return CONVFUNC(value, dstType, dst); \
|
|
|
|
} \
|
|
|
|
}; \
|
|
|
|
template<> \
|
|
|
|
class wxAnyValueTypeImpl<T> : public wxAnyValueTypeImpl##TYPENAME \
|
|
|
|
{ \
|
|
|
|
public: \
|
|
|
|
wxAnyValueTypeImpl() : wxAnyValueTypeImpl##TYPENAME() { } \
|
|
|
|
virtual ~wxAnyValueTypeImpl() { } \
|
|
|
|
};
|
2009-09-19 15:41:08 +00:00
|
|
|
|
2010-04-22 13:51:38 +00:00
|
|
|
#define WX_ANY_DEFINE_CONVERTIBLE_TYPE(T, TYPENAME, CONVFUNC, BT) \
|
|
|
|
_WX_ANY_DEFINE_CONVERTIBLE_TYPE(T, TYPENAME, CONVFUNC, BT) \
|
2009-09-19 15:41:08 +00:00
|
|
|
|
2010-04-22 13:51:38 +00:00
|
|
|
#define WX_ANY_DEFINE_CONVERTIBLE_TYPE_BASE(T, TYPENAME, CONVFUNC) \
|
|
|
|
_WX_ANY_DEFINE_CONVERTIBLE_TYPE(T, TYPENAME, \
|
|
|
|
CONVFUNC, const T&) \
|
2009-09-19 15:41:08 +00:00
|
|
|
|
2010-04-22 13:51:38 +00:00
|
|
|
//
|
|
|
|
// String value type
|
|
|
|
//
|
2009-09-19 15:41:08 +00:00
|
|
|
|
2010-04-22 13:51:38 +00:00
|
|
|
// Convert wxString to destination wxAny value type
|
|
|
|
extern WXDLLIMPEXP_BASE bool wxAnyConvertString(const wxString& value,
|
|
|
|
wxAnyValueType* dstType,
|
|
|
|
wxAnyValueBuffer& dst);
|
|
|
|
|
|
|
|
WX_ANY_DEFINE_CONVERTIBLE_TYPE_BASE(wxString, wxString, wxAnyConvertString)
|
|
|
|
WX_ANY_DEFINE_CONVERTIBLE_TYPE(const char*, ConstCharPtr,
|
|
|
|
wxAnyConvertString, wxString)
|
|
|
|
WX_ANY_DEFINE_CONVERTIBLE_TYPE(const wchar_t*, ConstWchar_tPtr,
|
|
|
|
wxAnyConvertString, wxString)
|
2009-09-19 15:41:08 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// 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,
|
2014-03-30 00:02:23 +00:00
|
|
|
wxAnyValueBuffer& dst) const wxOVERRIDE;
|
2009-09-19 15:41:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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,
|
2014-03-30 00:02:23 +00:00
|
|
|
wxAnyValueBuffer& dst) const wxOVERRIDE;
|
2009-09-19 15:41:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// WX_ANY_DEFINE_SUB_TYPE requires this
|
|
|
|
typedef double wxAnyBaseDoubleType;
|
|
|
|
|
|
|
|
WX_ANY_DEFINE_SUB_TYPE(float, Double)
|
|
|
|
WX_ANY_DEFINE_SUB_TYPE(double, Double)
|
|
|
|
|
|
|
|
|
2009-10-03 10:43:21 +00:00
|
|
|
//
|
|
|
|
// 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, \
|
2015-09-07 00:20:42 +00:00
|
|
|
wxAnyValueBuffer& dst) const wxOVERRIDE \
|
2009-10-03 10:43:21 +00:00
|
|
|
{ \
|
|
|
|
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
|
2011-01-06 11:06:12 +00:00
|
|
|
// really test it properly in unit tests since a separate DLL would
|
2009-10-03 10:43:21 +00:00
|
|
|
// 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)
|
|
|
|
|
|
|
|
|
2010-04-16 14:33:52 +00:00
|
|
|
#if wxUSE_VARIANT
|
|
|
|
|
|
|
|
class WXDLLIMPEXP_FWD_BASE wxAnyToVariantRegistration;
|
|
|
|
|
|
|
|
// Because of header inter-dependencies, cannot include this earlier
|
|
|
|
#include "wx/variant.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// wxVariantData* data type implementation. For cases when appropriate
|
|
|
|
// wxAny<->wxVariant conversion code is missing.
|
|
|
|
//
|
|
|
|
|
|
|
|
class WXDLLIMPEXP_BASE wxAnyValueTypeImplVariantData :
|
|
|
|
public wxAnyValueTypeImplBase<wxVariantData*>
|
|
|
|
{
|
|
|
|
WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplVariantData)
|
|
|
|
public:
|
|
|
|
wxAnyValueTypeImplVariantData() :
|
|
|
|
wxAnyValueTypeImplBase<wxVariantData*>() { }
|
|
|
|
virtual ~wxAnyValueTypeImplVariantData() { }
|
|
|
|
|
2014-03-30 00:02:23 +00:00
|
|
|
virtual void DeleteValue(wxAnyValueBuffer& buf) const wxOVERRIDE
|
2010-04-16 14:33:52 +00:00
|
|
|
{
|
|
|
|
wxVariantData* data = static_cast<wxVariantData*>(buf.m_ptr);
|
|
|
|
if ( data )
|
|
|
|
data->DecRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void CopyBuffer(const wxAnyValueBuffer& src,
|
2014-03-30 00:02:23 +00:00
|
|
|
wxAnyValueBuffer& dst) const wxOVERRIDE
|
2010-04-16 14:33:52 +00:00
|
|
|
{
|
|
|
|
wxVariantData* data = static_cast<wxVariantData*>(src.m_ptr);
|
|
|
|
if ( data )
|
|
|
|
data->IncRef();
|
|
|
|
dst.m_ptr = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetValue(wxVariantData* value,
|
|
|
|
wxAnyValueBuffer& buf)
|
|
|
|
{
|
|
|
|
value->IncRef();
|
|
|
|
buf.m_ptr = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static wxVariantData* GetValue(const wxAnyValueBuffer& buf)
|
|
|
|
{
|
|
|
|
return static_cast<wxVariantData*>(buf.m_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool ConvertValue(const wxAnyValueBuffer& src,
|
|
|
|
wxAnyValueType* dstType,
|
2014-03-30 00:02:23 +00:00
|
|
|
wxAnyValueBuffer& dst) const wxOVERRIDE
|
2010-04-16 14:33:52 +00:00
|
|
|
{
|
|
|
|
wxUnusedVar(src);
|
|
|
|
wxUnusedVar(dstType);
|
|
|
|
wxUnusedVar(dst);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
class wxAnyValueTypeImpl<wxVariantData*> :
|
|
|
|
public wxAnyValueTypeImplVariantData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxAnyValueTypeImpl() : wxAnyValueTypeImplVariantData() { }
|
|
|
|
virtual ~wxAnyValueTypeImpl() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // wxUSE_VARIANT
|
2009-10-03 10:43:21 +00:00
|
|
|
|
2009-09-19 15:41:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
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; \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-16 14:33:52 +00:00
|
|
|
#if wxUSE_VARIANT
|
|
|
|
|
|
|
|
// Note that the following functions are implemented outside wxAny class
|
|
|
|
// so that it can reside entirely in header and lack the export declaration.
|
|
|
|
|
|
|
|
// Helper function used to associate wxAnyValueType with a wxVariantData.
|
|
|
|
extern WXDLLIMPEXP_BASE void
|
|
|
|
wxPreRegisterAnyToVariant(wxAnyToVariantRegistration* reg);
|
|
|
|
|
|
|
|
// This function performs main wxAny to wxVariant conversion duties.
|
|
|
|
extern WXDLLIMPEXP_BASE bool
|
|
|
|
wxConvertAnyToVariant(const wxAny& any, wxVariant* variant);
|
|
|
|
|
|
|
|
#endif // wxUSE_VARIANT
|
|
|
|
|
2009-09-19 15:41:08 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2009-09-20 12:37:16 +00:00
|
|
|
class wxAny
|
2009-09-19 15:41:08 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
Default constructor.
|
|
|
|
*/
|
|
|
|
wxAny()
|
|
|
|
{
|
|
|
|
m_type = wxAnyNullValueType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Destructor.
|
|
|
|
*/
|
|
|
|
~wxAny()
|
|
|
|
{
|
|
|
|
m_type->DeleteValue(m_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
//@{
|
|
|
|
/**
|
|
|
|
Various constructors.
|
|
|
|
*/
|
2010-04-17 08:59:13 +00:00
|
|
|
template<typename T>
|
|
|
|
wxAny(const T& value)
|
|
|
|
{
|
2010-04-30 13:32:41 +00:00
|
|
|
m_type = wxAnyValueTypeImpl<T>::sm_instance.get();
|
2010-04-17 08:59:13 +00:00
|
|
|
wxAnyValueTypeImpl<T>::SetValue(value, m_buffer);
|
|
|
|
}
|
|
|
|
|
2010-04-22 13:51:38 +00:00
|
|
|
// These two constructors are needed to deal with string literals
|
2009-09-19 15:41:08 +00:00
|
|
|
wxAny(const char* value)
|
|
|
|
{
|
2010-04-30 13:32:41 +00:00
|
|
|
m_type = wxAnyValueTypeImpl<const char*>::sm_instance.get();
|
2010-04-22 13:51:38 +00:00
|
|
|
wxAnyValueTypeImpl<const char*>::SetValue(value, m_buffer);
|
2009-09-19 15:41:08 +00:00
|
|
|
}
|
|
|
|
wxAny(const wchar_t* value)
|
|
|
|
{
|
2010-04-30 13:32:41 +00:00
|
|
|
m_type = wxAnyValueTypeImpl<const wchar_t*>::sm_instance.get();
|
2010-04-22 13:51:38 +00:00
|
|
|
wxAnyValueTypeImpl<const wchar_t*>::SetValue(value, m_buffer);
|
2009-09-19 15:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxAny(const wxAny& any)
|
|
|
|
{
|
|
|
|
m_type = wxAnyNullValueType;
|
|
|
|
AssignAny(any);
|
|
|
|
}
|
|
|
|
|
2010-04-16 14:33:52 +00:00
|
|
|
#if wxUSE_VARIANT
|
|
|
|
wxAny(const wxVariant& variant)
|
|
|
|
{
|
|
|
|
m_type = wxAnyNullValueType;
|
|
|
|
AssignVariant(variant);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-09-19 15:41:08 +00:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Use this template function for checking if this wxAny holds
|
|
|
|
a specific C++ data type.
|
|
|
|
|
|
|
|
@see wxAnyValueType::CheckType()
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2014-05-15 22:32:17 +00:00
|
|
|
bool CheckType() const
|
2009-09-19 15:41:08 +00:00
|
|
|
{
|
|
|
|
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
|
2010-06-18 13:30:47 +00:00
|
|
|
of wxAny::GetType(). Instead, use wxAny::HasSameType().
|
|
|
|
|
|
|
|
@see HasSameType()
|
2009-09-19 15:41:08 +00:00
|
|
|
*/
|
|
|
|
const wxAnyValueType* GetType() const
|
|
|
|
{
|
|
|
|
return m_type;
|
|
|
|
}
|
|
|
|
|
2010-06-18 13:30:47 +00:00
|
|
|
/**
|
|
|
|
Returns @true if this and another wxAny have the same
|
|
|
|
value type.
|
|
|
|
*/
|
|
|
|
bool HasSameType(const wxAny& other) const
|
|
|
|
{
|
|
|
|
return GetType()->IsSameType(other.GetType());
|
|
|
|
}
|
|
|
|
|
2009-09-19 15:41:08 +00:00
|
|
|
/**
|
2010-06-18 13:32:12 +00:00
|
|
|
Tests if wxAny is null (that is, whether there is no data).
|
2009-09-19 15:41:08 +00:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*/
|
2010-04-20 12:55:34 +00:00
|
|
|
template<typename T>
|
|
|
|
wxAny& operator=(const T &value)
|
|
|
|
{
|
|
|
|
m_type->DeleteValue(m_buffer);
|
2010-04-30 13:32:41 +00:00
|
|
|
m_type = wxAnyValueTypeImpl<T>::sm_instance.get();
|
2010-04-20 12:55:34 +00:00
|
|
|
wxAnyValueTypeImpl<T>::SetValue(value, m_buffer);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-09-19 15:41:08 +00:00
|
|
|
wxAny& operator=(const wxAny &any)
|
|
|
|
{
|
2009-12-02 16:36:50 +00:00
|
|
|
if (this != &any)
|
|
|
|
AssignAny(any);
|
2009-09-19 15:41:08 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-04-20 12:55:34 +00:00
|
|
|
#if wxUSE_VARIANT
|
2010-04-16 14:33:52 +00:00
|
|
|
wxAny& operator=(const wxVariant &variant)
|
|
|
|
{
|
|
|
|
AssignVariant(variant);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-04-22 13:51:38 +00:00
|
|
|
// These two operators are needed to deal with string literals
|
2009-09-19 15:41:08 +00:00
|
|
|
wxAny& operator=(const char* value)
|
2010-04-22 13:51:38 +00:00
|
|
|
{
|
|
|
|
Assign(value);
|
|
|
|
return *this;
|
|
|
|
}
|
2009-09-19 15:41:08 +00:00
|
|
|
wxAny& operator=(const wchar_t* value)
|
2010-04-22 13:51:38 +00:00
|
|
|
{
|
|
|
|
Assign(value);
|
|
|
|
return *this;
|
|
|
|
}
|
2009-09-19 15:41:08 +00:00
|
|
|
|
|
|
|
//@{
|
|
|
|
/**
|
|
|
|
Equality operators.
|
|
|
|
*/
|
|
|
|
bool operator==(const wxString& value) const
|
|
|
|
{
|
2010-04-22 13:51:38 +00:00
|
|
|
wxString value2;
|
|
|
|
if ( !GetAs(&value2) )
|
2009-09-19 15:41:08 +00:00
|
|
|
return false;
|
2010-04-22 13:51:38 +00:00
|
|
|
return value == value2;
|
2009-09-19 15:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-12-28 00:44:01 +00:00
|
|
|
wxGCC_WARNING_SUPPRESS(float-equal)
|
|
|
|
|
2009-09-19 15:41:08 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2012-12-28 00:44:01 +00:00
|
|
|
wxGCC_WARNING_RESTORE(float-equal)
|
|
|
|
|
2009-09-19 15:41:08 +00:00
|
|
|
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); }
|
|
|
|
//@}
|
|
|
|
|
|
|
|
/**
|
2010-04-22 13:51:38 +00:00
|
|
|
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.
|
2009-09-19 15:41:08 +00:00
|
|
|
|
2011-01-06 11:06:12 +00:00
|
|
|
@remarks For convenience, conversion is done when T is wxString. This
|
2010-04-22 13:51:38 +00:00
|
|
|
is useful when a string literal (which are treated as
|
|
|
|
const char* and const wchar_t*) has been assigned to wxAny.
|
2009-09-19 15:41:08 +00:00
|
|
|
*/
|
2014-05-15 22:32:17 +00:00
|
|
|
template <typename T>
|
2009-09-19 15:41:08 +00:00
|
|
|
T As(T* = NULL) const
|
|
|
|
{
|
2014-05-15 22:32:17 +00:00
|
|
|
return wxPrivate::wxAnyAsImpl<T>::DoAs(*this);
|
2009-09-19 15:41:08 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 22:32:17 +00:00
|
|
|
// Semi private helper: get the value without coercion, for all types.
|
|
|
|
template <typename T>
|
|
|
|
T RawAs() const
|
2010-04-22 13:51:38 +00:00
|
|
|
{
|
2014-05-15 22:32:17 +00:00
|
|
|
if ( !wxAnyValueTypeImpl<T>::IsSameClass(m_type) )
|
2010-04-22 13:51:38 +00:00
|
|
|
{
|
|
|
|
wxFAIL_MSG("Incorrect or non-convertible data type");
|
|
|
|
}
|
2014-05-15 22:32:17 +00:00
|
|
|
|
|
|
|
return static_cast<T>(wxAnyValueTypeImpl<T>::GetValue(m_buffer));
|
2010-04-22 13:51:38 +00:00
|
|
|
}
|
|
|
|
|
2011-01-07 17:22:59 +00:00
|
|
|
#if wxUSE_EXTENDED_RTTI
|
|
|
|
const wxTypeInfo* GetTypeInfo() const
|
|
|
|
{
|
|
|
|
return m_type->GetTypeInfo();
|
|
|
|
}
|
|
|
|
#endif
|
2009-09-19 15:41:08 +00:00
|
|
|
/**
|
2011-01-06 11:06:12 +00:00
|
|
|
Template function that retrieves and converts the value of this
|
2009-09-19 15:41:08 +00:00
|
|
|
variant to the type that T* value is.
|
|
|
|
|
2011-01-06 11:06:12 +00:00
|
|
|
@return Returns @true if conversion was successful.
|
2009-09-19 15:41:08 +00:00
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
bool GetAs(T* value) const
|
|
|
|
{
|
|
|
|
if ( !wxAnyValueTypeImpl<T>::IsSameClass(m_type) )
|
|
|
|
{
|
|
|
|
wxAnyValueType* otherType =
|
2010-04-30 13:32:41 +00:00
|
|
|
wxAnyValueTypeImpl<T>::sm_instance.get();
|
2009-09-19 15:41:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-04-16 14:33:52 +00:00
|
|
|
#if wxUSE_VARIANT
|
|
|
|
// GetAs() wxVariant specialization
|
|
|
|
bool GetAs(wxVariant* value) const
|
|
|
|
{
|
|
|
|
return wxConvertAnyToVariant(*this, value);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-09-19 15:41:08 +00:00
|
|
|
private:
|
|
|
|
// Assignment functions
|
2009-09-20 12:37:16 +00:00
|
|
|
void AssignAny(const wxAny& any)
|
|
|
|
{
|
2011-01-06 12:05:41 +00:00
|
|
|
// Must delete value - CopyBuffer() never does that
|
|
|
|
m_type->DeleteValue(m_buffer);
|
2009-09-29 13:38:20 +00:00
|
|
|
|
|
|
|
wxAnyValueType* newType = any.m_type;
|
|
|
|
|
2011-01-06 12:05:41 +00:00
|
|
|
if ( !newType->IsSameType(m_type) )
|
2009-09-29 13:38:20 +00:00
|
|
|
m_type = newType;
|
|
|
|
|
|
|
|
newType->CopyBuffer(any.m_buffer, m_buffer);
|
2009-09-20 12:37:16 +00:00
|
|
|
}
|
2009-09-19 15:41:08 +00:00
|
|
|
|
2010-04-16 14:33:52 +00:00
|
|
|
#if wxUSE_VARIANT
|
|
|
|
void AssignVariant(const wxVariant& variant)
|
|
|
|
{
|
|
|
|
wxVariantData* data = variant.GetData();
|
|
|
|
|
|
|
|
if ( data && data->GetAsAny(this) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_type->DeleteValue(m_buffer);
|
|
|
|
|
|
|
|
if ( variant.IsNull() )
|
|
|
|
{
|
|
|
|
// Init as Null
|
|
|
|
m_type = wxAnyNullValueType;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If everything else fails, wrap the whole wxVariantData
|
2010-04-30 13:32:41 +00:00
|
|
|
m_type = wxAnyValueTypeImpl<wxVariantData*>::sm_instance.get();
|
2010-04-16 14:33:52 +00:00
|
|
|
wxAnyValueTypeImpl<wxVariantData*>::SetValue(data, m_buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-09-19 15:41:08 +00:00
|
|
|
template<typename T>
|
|
|
|
void Assign(const T &value)
|
|
|
|
{
|
|
|
|
m_type->DeleteValue(m_buffer);
|
2010-04-30 13:32:41 +00:00
|
|
|
m_type = wxAnyValueTypeImpl<T>::sm_instance.get();
|
2009-09-19 15:41:08 +00:00
|
|
|
wxAnyValueTypeImpl<T>::SetValue(value, m_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data
|
|
|
|
wxAnyValueBuffer m_buffer;
|
2009-09-27 14:52:34 +00:00
|
|
|
wxAnyValueType* m_type;
|
2009-09-19 15:41:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-05-15 22:32:17 +00:00
|
|
|
namespace wxPrivate
|
|
|
|
{
|
|
|
|
|
|
|
|
// Dispatcher for template wxAny::As() implementation which is different for
|
|
|
|
// wxString and all the other types: the generic implementation check if the
|
|
|
|
// value is of the right type and returns it.
|
|
|
|
template <typename T>
|
|
|
|
struct wxAnyAsImpl
|
|
|
|
{
|
|
|
|
static T DoAs(const wxAny& any)
|
|
|
|
{
|
|
|
|
return any.RawAs<T>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Specialization for wxString does coercion.
|
|
|
|
template <>
|
|
|
|
struct wxAnyAsImpl<wxString>
|
|
|
|
{
|
|
|
|
static wxString DoAs(const wxAny& any)
|
|
|
|
{
|
|
|
|
wxString value;
|
|
|
|
if ( !any.GetAs(&value) )
|
|
|
|
{
|
|
|
|
wxFAIL_MSG("Incorrect or non-convertible data type");
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// See comment for wxANY_VALUE_TYPE_CHECK_TYPE.
|
2009-09-19 15:41:08 +00:00
|
|
|
#define wxANY_CHECK_TYPE(any, T) \
|
2010-04-22 14:06:00 +00:00
|
|
|
wxANY_VALUE_TYPE_CHECK_TYPE((any).GetType(), T)
|
2009-09-19 15:41:08 +00:00
|
|
|
|
|
|
|
|
2014-05-15 22:32:17 +00:00
|
|
|
// This macro shouldn't be used any longer for the same reasons as
|
|
|
|
// wxANY_VALUE_TYPE_CHECK_TYPE(), just call As() directly.
|
2009-09-19 15:41:08 +00:00
|
|
|
#define wxANY_AS(any, T) \
|
2010-04-22 14:06:00 +00:00
|
|
|
(any).As(static_cast<T*>(NULL))
|
2009-09-19 15:41:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2014-05-15 22:32:17 +00:00
|
|
|
inline bool wxAnyValueType::CheckType() const
|
2009-09-19 15:41:08 +00:00
|
|
|
{
|
|
|
|
return wxAnyValueTypeImpl<T>::IsSameClass(this);
|
|
|
|
}
|
|
|
|
|
2011-01-06 11:06:12 +00:00
|
|
|
WX_DECLARE_LIST_WITH_DECL(wxAny, wxAnyList, class WXDLLIMPEXP_BASE);
|
2009-09-19 15:41:08 +00:00
|
|
|
|
|
|
|
#endif // wxUSE_ANY
|
|
|
|
|
|
|
|
#endif // _WX_ANY_H_
|