1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxAny
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 The wxAny class represents a container for any type. Its value
14 can be changed at run time, possibly to a different type of value.
16 wxAny is a backwards-incompatible (but convertible) successor class for
17 wxVariant, essentially doing the same thing in a more modern, template-
18 based manner and with transparent support for any user data type.
20 Some pseudo-code'ish example of use with arbitrary user data:
31 // Let's do a sanity check to make sure that any still holds
32 // data of correct type.
33 if ( any.CheckType<MyClass>() )
35 // Thank goodness, still a correct type.
36 MyClass myObject2 = any.As<MyClass>();
40 // Something has gone horribly wrong!
46 When compared to wxVariant, there are various internal implementation
47 differences as well. For instance, wxAny only allocates separate data
48 object in heap for large objects (i.e. ones with size more than
49 WX_ANY_VALUE_BUFFER_SIZE, which at the time of writing is 16 bytes).
54 @see wxAnyValueType, wxVariant, @ref overview_cpp_rtti_disabled
60 Default constructor. It seeds the object with a null value.
65 Constructs wxAny from data.
68 wxAny(const T
& value
);
71 Constructs wxAny from another wxAny.
73 wxAny(const wxAny
& any
);
76 Constructs wxAny, converting value from wxVariant.
78 @remarks Because of this conversion, it is not usually possible to
79 have wxAny that actually holds a wxVariant. If wxVariant
80 cannot be converted to a specific data type, wxAny will then
81 hold and manage reference to wxVariantData* similar to how
84 wxAny(const wxVariant
& variant
);
92 This template function converts wxAny into given type. In most cases
93 no type conversion is performed, so if the type is incorrect an
94 assertion failure will occur.
96 @remarks For conveniency, conversion is done when T is wxString. This
97 is useful when a string literal (which are treated as
98 const char* and const wchar_t*) has been assigned to wxAny.
100 This template function may not work properly with Visual C++
101 6. For full compiler compatibility, please use
102 wxANY_AS(any, T) macro instead.
108 Use this template function for checking if this wxAny holds
109 a specific C++ data type.
111 @remarks This template function may not work properly with Visual C++
112 6. For full compiler compatibility, please use
113 wxANY_CHECK_TYPE(any, T) macro instead.
115 @see wxAnyValueType::CheckType()
118 bool CheckType() const;
121 Template function that retrieves and converts the value of this
122 wxAny to the type that T* value is.
124 @return Returns @true if conversion was successful.
127 bool GetAs(T
* value
) const;
130 Specialization of GetAs() that allows conversion of wxAny into
133 @return Returns @true if conversion was successful. Conversion usually
134 only fails if variant used custom wxVariantData that did not
135 implement the wxAny to wxVariant conversion functions.
137 bool GetAs(wxVariant
* value
) const;
140 Returns the value type as wxAnyValueType instance.
142 @remarks You cannot reliably test whether two wxAnys are of
143 same value type by simply comparing return values
144 of wxAny::GetType(). Instead use
145 wxAnyValueType::CheckType<T>() template function.
147 const wxAnyValueType
* GetType() const;
150 Tests if wxAny is null (that is, whether there is data).
155 Makes wxAny null (that is, clears it).
161 @name Assignment operators
164 wxAny
& operator=(const T
&value
);
165 wxAny
& operator=(const wxAny
&any
);
166 wxAny
& operator=(const wxVariant
&variant
);
171 @name Equality operators
173 @remarks Generic template-based comparison operators have not been
174 provided for various code consistency reasons, so for custom
175 data types you have do something like this:
178 if ( any.CheckType<MyClass*>() &&
179 any.As<MyClass*>() == myObjectPtr )
181 // Do something if any stores myObjectPtr
185 bool operator==(signed char value
) const;
186 bool operator==(signed short value
) const;
187 bool operator==(signed int value
) const;
188 bool operator==(signed long value
) const;
189 bool operator==(wxLongLong_t value
) const;
190 bool operator==(unsigned char value
) const;
191 bool operator==(unsigned short value
) const;
192 bool operator==(unsigned int value
) const;
193 bool operator==(unsigned long value
) const;
194 bool operator==(wxULongLong_t value
) const;
195 bool operator==(float value
) const;
196 bool operator==(double value
) const;
197 bool operator==(bool value
) const;
198 bool operator==(const char* value
) const;
199 bool operator==(const wchar_t* value
) const;
200 bool operator==(const wxString
& value
) const;
205 @name Inequality operators
207 bool operator!=(signed char value
) const;
208 bool operator!=(signed short value
) const;
209 bool operator!=(signed int value
) const;
210 bool operator!=(signed long value
) const;
211 bool operator!=(wxLongLong_t value
) const;
212 bool operator!=(unsigned char value
) const;
213 bool operator!=(unsigned short value
) const;
214 bool operator!=(unsigned int value
) const;
215 bool operator!=(unsigned long value
) const;
216 bool operator!=(wxULongLong_t value
) const;
217 bool operator!=(float value
) const;
218 bool operator!=(double value
) const;
219 bool operator!=(bool value
) const;
220 bool operator!=(const char* value
) const;
221 bool operator!=(const wchar_t* value
) const;
222 bool operator!=(const wxString
& value
) const;
227 This is value getter macro that is more compatible with older
228 compilers, such as Visual C++ 6.0.
230 #define wxANY_AS(any, T)
234 This is type checking macro that is more compatible with older
235 compilers, such as Visual C++ 6.0.
237 #define wxANY_CHECK_TYPE(any, T)
241 Size of the wxAny value buffer.
245 WX_ANY_VALUE_BUFFER_SIZE
= 16
249 Type for buffer within wxAny for holding data.
251 union wxAnyValueBuffer
254 wxByte m_buffer
[WX_ANY_VALUE_BUFFER_SIZE
];
259 @class wxAnyValueType
261 wxAnyValueType is base class for value type functionality for C++ data
262 types used with wxAny. Usually the default template will create a
263 satisfactory wxAnyValueType implementation for a data type, but
264 sometimes you may need to add some customization. To do this you will need
265 to add specialized template of wxAnyValueTypeImpl<>. Often your only
266 need may be to add dynamic type conversion which would be done like
271 class wxAnyValueTypeImpl<MyClass> :
272 public wxAnyValueTypeImplBase<MyClass>
274 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>)
276 wxAnyValueTypeImpl() :
277 wxAnyValueTypeImplBase<MyClass>() { }
278 virtual ~wxAnyValueTypeImpl() { }
280 virtual bool ConvertValue(const wxAnyValueBuffer& src,
281 wxAnyValueType* dstType,
282 wxAnyValueBuffer& dst) const
284 // GetValue() is a static member function implemented
285 // in wxAnyValueTypeImplBase<>.
286 MyClass value = GetValue(src);
288 // TODO: Convert value from src buffer to destination
289 // type and buffer. If cannot be done, return
290 // false. This is a simple sample.
291 if ( dstType->CheckType<wxString>() )
293 wxString s = value.ToString();
294 wxAnyValueTypeImpl<wxString>::SetValue(s, dst);
304 // Following must be placed somewhere in your source code
305 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>)
308 wxAnyValueTypeImplBase<> template, from which we inherit in the above
309 example, contains the bulk of the default wxAnyValueTypeImpl<> template
310 implementation, and as such allows you to easily add some minor
313 If you need a have complete control over the type interpretation, you
314 will need to derive a class directly from wxAnyValueType, like this:
318 class wxAnyValueTypeImpl<MyClass> : public wxAnyValueType
320 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>)
322 virtual void DeleteValue(wxAnyValueBuffer& buf) const
324 // TODO: Free the data in buffer
325 // It is important to clear the buffer like this
326 // at the end of DeleteValue().
330 virtual void CopyBuffer(const wxAnyValueBuffer& src,
331 wxAnyValueBuffer& dst) const
333 // TODO: Copy value from one buffer to another.
334 // dst is already uninitialized and does not
338 virtual bool ConvertValue(const wxAnyValueBuffer& src,
339 wxAnyValueType* dstType,
340 wxAnyValueBuffer& dst) const
342 // TODO: Convert value from src buffer to destination
347 // Following static functions must be implemented
350 static void SetValue(const T& value,
351 wxAnyValueBuffer& buf)
353 // TODO: Store value into buf.
356 static const T& GetValue(const wxAnyValueBuffer& buf)
358 // TODO: Return reference to value stored in buffer.
363 // Following must be placed somewhere in your source code
364 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>)
384 virtual ~wxAnyValueType();
387 Use this template function for checking if wxAnyValueType represents
388 a specific C++ data type.
390 @remarks This template function does not work on some older compilers
391 (such as Visual C++ 6.0). For full compiler compatibility
392 please use wxANY_VALUE_TYPE_CHECK_TYPE(valueTypePtr, T) macro
395 @see wxAny::CheckType()
397 template <typename T
>
398 bool CheckType() const;
401 Convert value into buffer of different type. Return false if
404 virtual bool ConvertValue(const wxAnyValueBuffer
& src
,
405 wxAnyValueType
* dstType
,
406 wxAnyValueBuffer
& dst
) const = 0;
409 Implement this for buffer-to-buffer copy.
412 This is the source data buffer.
415 This is the destination data buffer that is in either
416 uninitialized or freed state.
418 virtual void CopyBuffer(const wxAnyValueBuffer
& src
,
419 wxAnyValueBuffer
& dst
) const = 0;
422 This function is called every time the data in wxAny
423 buffer needs to be freed.
425 virtual void DeleteValue(wxAnyValueBuffer
& buf
) const = 0;
428 This function is used for internal type matching.
430 virtual bool IsSameType(const wxAnyValueType
* otherType
) const = 0;
434 This is type checking macro that is more compatible with older
435 compilers, such as Visual C++ 6.0.
437 #define wxANY_VALUE_TYPE_CHECK_TYPE(valueTypePtr, T)