]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/any.h
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 (i.e. size in bytes more than
49 WX_ANY_VALUE_BUFFER_SIZE) or non-POD (Plain Old Data) data types.
50 Pointers, integers, bools etc. are fitted in the wxAny's internal buffer
51 without need for any extra allocation. It is possible that wxAny cannot
52 automatically determine if your own data structure is considered a
53 POD or not, so you may need to declare it as such explicitly, using
57 #include "wx/meta/pod.h"
58 WX_DECLARE_TYPE_POD(MyPodStruct)
61 Be extra careful what you declare as Plain Old Data. It must be such data
62 that can be copied with memcpy() without corrupting program integrity. For
63 instance, POD structures usually cannot contain pointers or references to
64 other data. wxRect, wxPoint, and wxSize are good examples of POD
67 Note that pointers to any and all types are already automatically
68 declared as Plain Old Data.
73 @see wxAnyValueType, wxVariant, @ref overview_cpp_rtti_disabled
79 Default constructor. It seeds the object with a null value.
84 Constructs wxAny from data.
87 wxAny(const T
& value
);
90 Constructs wxAny from another wxAny.
92 wxAny(const wxAny
& any
);
95 Constructs wxAny, converting value from wxVariant.
97 @remarks Because of this conversion, it is not usually possible to
98 have wxAny that actually holds a wxVariant. If wxVariant
99 cannot be converted to a specific data type, wxAny will then
100 hold and manage reference to wxVariantData* similar to how
103 wxAny(const wxVariant
& variant
);
111 This template function converts wxAny into given type. In most cases
112 no type conversion is performed, so if the type is incorrect an
113 assertion failure will occur.
115 @remarks For conveniency, conversion is done when T is wxString. This
116 is useful when a string literal (which are treated as
117 const char* and const wchar_t*) has been assigned to wxAny.
119 This template function may not work properly with Visual C++
120 6. For full compiler compatibility, please use
121 wxANY_AS(any, T) macro instead.
127 Use this template function for checking if this wxAny holds
128 a specific C++ data type.
130 @remarks This template function may not work properly with Visual C++
131 6. For full compiler compatibility, please use
132 wxANY_CHECK_TYPE(any, T) macro instead.
134 @see wxAnyValueType::CheckType()
137 bool CheckType() const;
140 Template function that retrieves and converts the value of this
141 wxAny to the type that T* value is.
143 @return Returns @true if conversion was successful.
146 bool GetAs(T
* value
) const;
149 Specialization of GetAs() that allows conversion of wxAny into
152 @return Returns @true if conversion was successful. Conversion usually
153 only fails if variant used custom wxVariantData that did not
154 implement the wxAny to wxVariant conversion functions.
156 bool GetAs(wxVariant
* value
) const;
159 Returns the value type as wxAnyValueType instance.
161 @remarks You cannot reliably test whether two wxAnys are of
162 same value type by simply comparing return values
163 of wxAny::GetType(). Instead use
164 wxAnyValueType::CheckType<T>() template function.
166 const wxAnyValueType
* GetType() const;
169 Tests if wxAny is null (that is, whether there is data).
174 Makes wxAny null (that is, clears it).
180 @name Assignment operators
183 wxAny
& operator=(const T
&value
);
184 wxAny
& operator=(const wxAny
&any
);
185 wxAny
& operator=(const wxVariant
&variant
);
190 @name Equality operators
192 @remarks Generic template-based comparison operators have not been
193 provided for various code consistency reasons, so for custom
194 data types you have do something like this:
197 if ( any.CheckType<MyClass*>() &&
198 any.As<MyClass*>() == myObjectPtr )
200 // Do something if any stores myObjectPtr
204 bool operator==(signed char value
) const;
205 bool operator==(signed short value
) const;
206 bool operator==(signed int value
) const;
207 bool operator==(signed long value
) const;
208 bool operator==(wxLongLong_t value
) const;
209 bool operator==(unsigned char value
) const;
210 bool operator==(unsigned short value
) const;
211 bool operator==(unsigned int value
) const;
212 bool operator==(unsigned long value
) const;
213 bool operator==(wxULongLong_t value
) const;
214 bool operator==(float value
) const;
215 bool operator==(double value
) const;
216 bool operator==(bool value
) const;
217 bool operator==(const char* value
) const;
218 bool operator==(const wchar_t* value
) const;
219 bool operator==(const wxString
& value
) const;
224 @name Inequality operators
226 bool operator!=(signed char value
) const;
227 bool operator!=(signed short value
) const;
228 bool operator!=(signed int value
) const;
229 bool operator!=(signed long value
) const;
230 bool operator!=(wxLongLong_t value
) const;
231 bool operator!=(unsigned char value
) const;
232 bool operator!=(unsigned short value
) const;
233 bool operator!=(unsigned int value
) const;
234 bool operator!=(unsigned long value
) const;
235 bool operator!=(wxULongLong_t value
) const;
236 bool operator!=(float value
) const;
237 bool operator!=(double value
) const;
238 bool operator!=(bool value
) const;
239 bool operator!=(const char* value
) const;
240 bool operator!=(const wchar_t* value
) const;
241 bool operator!=(const wxString
& value
) const;
246 This is value getter macro that is more compatible with older
247 compilers, such as Visual C++ 6.0.
249 #define wxANY_AS(any, T)
253 This is type checking macro that is more compatible with older
254 compilers, such as Visual C++ 6.0.
256 #define wxANY_CHECK_TYPE(any, T)
260 Size of the wxAny value buffer.
264 WX_ANY_VALUE_BUFFER_SIZE
= 16
268 Type for buffer within wxAny for holding data.
270 union wxAnyValueBuffer
273 wxByte m_buffer
[WX_ANY_VALUE_BUFFER_SIZE
];
278 @class wxAnyValueType
280 wxAnyValueType is base class for value type functionality for C++ data
281 types used with wxAny. Usually the default template will create a
282 satisfactory wxAnyValueType implementation for a data type, but
283 sometimes you may need to add some customization. To do this you will need
284 to add specialized template of wxAnyValueTypeImpl<>. Often your only
285 need may be to add dynamic type conversion which would be done like
290 class wxAnyValueTypeImpl<MyClass> :
291 public wxAnyValueTypeImplBase<MyClass>
293 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>)
295 wxAnyValueTypeImpl() :
296 wxAnyValueTypeImplBase<MyClass>() { }
297 virtual ~wxAnyValueTypeImpl() { }
299 virtual bool ConvertValue(const wxAnyValueBuffer& src,
300 wxAnyValueType* dstType,
301 wxAnyValueBuffer& dst) const
303 // GetValue() is a static member function implemented
304 // in wxAnyValueTypeImplBase<>.
305 MyClass value = GetValue(src);
307 // TODO: Convert value from src buffer to destination
308 // type and buffer. If cannot be done, return
309 // false. This is a simple sample.
310 if ( dstType->CheckType<wxString>() )
312 wxString s = value.ToString();
313 wxAnyValueTypeImpl<wxString>::SetValue(s, dst);
323 // Following must be placed somewhere in your source code
324 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>)
327 wxAnyValueTypeImplBase<> template, from which we inherit in the above
328 example, contains the bulk of the default wxAnyValueTypeImpl<> template
329 implementation, and as such allows you to easily add some minor
332 If you need a have complete control over the type interpretation, you
333 will need to derive a class directly from wxAnyValueType, like this:
337 class wxAnyValueTypeImpl<MyClass> : public wxAnyValueType
339 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>)
341 virtual void DeleteValue(wxAnyValueBuffer& buf) const
343 // TODO: Free the data in buffer
344 // It is important to clear the buffer like this
345 // at the end of DeleteValue().
349 virtual void CopyBuffer(const wxAnyValueBuffer& src,
350 wxAnyValueBuffer& dst) const
352 // TODO: Copy value from one buffer to another.
353 // dst is already uninitialized and does not
357 virtual bool ConvertValue(const wxAnyValueBuffer& src,
358 wxAnyValueType* dstType,
359 wxAnyValueBuffer& dst) const
361 // TODO: Convert value from src buffer to destination
366 // Following static functions must be implemented
369 static void SetValue(const T& value,
370 wxAnyValueBuffer& buf)
372 // TODO: Store value into buf.
375 static const T& GetValue(const wxAnyValueBuffer& buf)
377 // TODO: Return reference to value stored in buffer.
382 // Following must be placed somewhere in your source code
383 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>)
403 virtual ~wxAnyValueType();
406 Use this template function for checking if wxAnyValueType represents
407 a specific C++ data type.
409 @remarks This template function does not work on some older compilers
410 (such as Visual C++ 6.0). For full compiler compatibility
411 please use wxANY_VALUE_TYPE_CHECK_TYPE(valueTypePtr, T) macro
414 @see wxAny::CheckType()
416 template <typename T
>
417 bool CheckType() const;
420 Convert value into buffer of different type. Return false if
423 virtual bool ConvertValue(const wxAnyValueBuffer
& src
,
424 wxAnyValueType
* dstType
,
425 wxAnyValueBuffer
& dst
) const = 0;
428 Implement this for buffer-to-buffer copy.
431 This is the source data buffer.
434 This is the destination data buffer that is in either
435 uninitialized or freed state.
437 virtual void CopyBuffer(const wxAnyValueBuffer
& src
,
438 wxAnyValueBuffer
& dst
) const = 0;
441 This function is called every time the data in wxAny
442 buffer needs to be freed.
444 virtual void DeleteValue(wxAnyValueBuffer
& buf
) const = 0;
447 This function is used for internal type matching.
449 virtual bool IsSameType(const wxAnyValueType
* otherType
) const = 0;
453 This is type checking macro that is more compatible with older
454 compilers, such as Visual C++ 6.0.
456 #define wxANY_VALUE_TYPE_CHECK_TYPE(valueTypePtr, T)