]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/any.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxAny
4 // Author: wxWidgets team
6 // Licence: wxWindows licence
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 wxAny::HasSameType().
148 const wxAnyValueType
* GetType() const;
151 Returns @true if this and another wxAny have the same
154 bool HasSameType(const wxAny
& other
) const;
157 Tests if wxAny is null (that is, whether there is no data).
162 Makes wxAny null (that is, clears it).
168 @name Assignment operators
171 wxAny
& operator=(const T
&value
);
172 wxAny
& operator=(const wxAny
&any
);
173 wxAny
& operator=(const wxVariant
&variant
);
178 @name Equality operators
180 @remarks Generic template-based comparison operators have not been
181 provided for various code consistency reasons, so for custom
182 data types you have do something like this:
185 if ( any.CheckType<MyClass*>() &&
186 any.As<MyClass*>() == myObjectPtr )
188 // Do something if any stores myObjectPtr
192 bool operator==(signed char value
) const;
193 bool operator==(signed short value
) const;
194 bool operator==(signed int value
) const;
195 bool operator==(signed long value
) const;
196 bool operator==(wxLongLong_t value
) const;
197 bool operator==(unsigned char value
) const;
198 bool operator==(unsigned short value
) const;
199 bool operator==(unsigned int value
) const;
200 bool operator==(unsigned long value
) const;
201 bool operator==(wxULongLong_t value
) const;
202 bool operator==(float value
) const;
203 bool operator==(double value
) const;
204 bool operator==(bool value
) const;
205 bool operator==(const char* value
) const;
206 bool operator==(const wchar_t* value
) const;
207 bool operator==(const wxString
& value
) const;
212 @name Inequality operators
214 bool operator!=(signed char value
) const;
215 bool operator!=(signed short value
) const;
216 bool operator!=(signed int value
) const;
217 bool operator!=(signed long value
) const;
218 bool operator!=(wxLongLong_t value
) const;
219 bool operator!=(unsigned char value
) const;
220 bool operator!=(unsigned short value
) const;
221 bool operator!=(unsigned int value
) const;
222 bool operator!=(unsigned long value
) const;
223 bool operator!=(wxULongLong_t value
) const;
224 bool operator!=(float value
) const;
225 bool operator!=(double value
) const;
226 bool operator!=(bool value
) const;
227 bool operator!=(const char* value
) const;
228 bool operator!=(const wchar_t* value
) const;
229 bool operator!=(const wxString
& value
) const;
234 This is value getter macro that is more compatible with older
235 compilers, such as Visual C++ 6.0.
237 #define wxANY_AS(any, T)
241 This is type checking macro that is more compatible with older
242 compilers, such as Visual C++ 6.0.
244 #define wxANY_CHECK_TYPE(any, T)
248 Size of the wxAny value buffer.
252 WX_ANY_VALUE_BUFFER_SIZE
= 16
256 Type for buffer within wxAny for holding data.
258 union wxAnyValueBuffer
261 wxByte m_buffer
[WX_ANY_VALUE_BUFFER_SIZE
];
266 @class wxAnyValueType
268 wxAnyValueType is base class for value type functionality for C++ data
269 types used with wxAny. Usually the default template will create a
270 satisfactory wxAnyValueType implementation for a data type, but
271 sometimes you may need to add some customization. To do this you will need
272 to add specialized template of wxAnyValueTypeImpl<>. Often your only
273 need may be to add dynamic type conversion which would be done like
278 class wxAnyValueTypeImpl<MyClass> :
279 public wxAnyValueTypeImplBase<MyClass>
281 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>)
283 wxAnyValueTypeImpl() :
284 wxAnyValueTypeImplBase<MyClass>() { }
285 virtual ~wxAnyValueTypeImpl() { }
287 virtual bool ConvertValue(const wxAnyValueBuffer& src,
288 wxAnyValueType* dstType,
289 wxAnyValueBuffer& dst) const
291 // GetValue() is a static member function implemented
292 // in wxAnyValueTypeImplBase<>.
293 MyClass value = GetValue(src);
295 // TODO: Convert value from src buffer to destination
296 // type and buffer. If cannot be done, return
297 // false. This is a simple sample.
298 if ( dstType->CheckType<wxString>() )
300 wxString s = value.ToString();
301 wxAnyValueTypeImpl<wxString>::SetValue(s, dst);
311 // Following must be placed somewhere in your source code
312 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>)
315 wxAnyValueTypeImplBase<> template, from which we inherit in the above
316 example, contains the bulk of the default wxAnyValueTypeImpl<> template
317 implementation, and as such allows you to easily add some minor
320 If you need a have complete control over the type interpretation, you
321 will need to derive a class directly from wxAnyValueType, like this:
325 class wxAnyValueTypeImpl<MyClass> : public wxAnyValueType
327 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>)
329 virtual void DeleteValue(wxAnyValueBuffer& buf) const
331 // TODO: Free the data in buffer
332 // It is important to clear the buffer like this
333 // at the end of DeleteValue().
337 virtual void CopyBuffer(const wxAnyValueBuffer& src,
338 wxAnyValueBuffer& dst) const
340 // TODO: Copy value from one buffer to another.
341 // dst is already uninitialized and does not
345 virtual bool ConvertValue(const wxAnyValueBuffer& src,
346 wxAnyValueType* dstType,
347 wxAnyValueBuffer& dst) const
349 // TODO: Convert value from src buffer to destination
354 // Following static functions must be implemented
357 static void SetValue(const T& value,
358 wxAnyValueBuffer& buf)
360 // TODO: Store value into buf.
363 static const T& GetValue(const wxAnyValueBuffer& buf)
365 // TODO: Return reference to value stored in buffer.
370 // Following must be placed somewhere in your source code
371 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>)
391 virtual ~wxAnyValueType();
394 Use this template function for checking if wxAnyValueType represents
395 a specific C++ data type.
397 @remarks This template function does not work on some older compilers
398 (such as Visual C++ 6.0). For full compiler compatibility
399 please use wxANY_VALUE_TYPE_CHECK_TYPE(valueTypePtr, T) macro
402 @see wxAny::CheckType()
404 template <typename T
>
405 bool CheckType() const;
408 Convert value into buffer of different type. Return false if
411 virtual bool ConvertValue(const wxAnyValueBuffer
& src
,
412 wxAnyValueType
* dstType
,
413 wxAnyValueBuffer
& dst
) const = 0;
416 Implement this for buffer-to-buffer copy.
419 This is the source data buffer.
422 This is the destination data buffer that is in either
423 uninitialized or freed state.
425 virtual void CopyBuffer(const wxAnyValueBuffer
& src
,
426 wxAnyValueBuffer
& dst
) const = 0;
429 This function is called every time the data in wxAny
430 buffer needs to be freed.
432 virtual void DeleteValue(wxAnyValueBuffer
& buf
) const = 0;
435 This function is used for internal type matching.
437 virtual bool IsSameType(const wxAnyValueType
* otherType
) const = 0;
441 This is type checking macro that is more compatible with older
442 compilers, such as Visual C++ 6.0.
444 #define wxANY_VALUE_TYPE_CHECK_TYPE(valueTypePtr, T)