1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/any.cpp
3 // Purpose: wxAny class, container for any type
4 // Author: Jaakko Salli
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
28 #include "wx/vector.h"
29 #include "wx/module.h"
31 using namespace wxPrivate
;
33 //-------------------------------------------------------------------------
34 // wxAnyValueTypeGlobals
35 //-------------------------------------------------------------------------
38 // Helper class to manage wxAnyValueType instances and other
39 // related global variables.
41 // NB: We really need to have wxAnyValueType instances allocated
42 // in heap. They are stored as static template member variables,
43 // and with them we just can't be too careful (eg. not allocating
44 // them in heap broke the type identification in GCC).
46 class wxAnyValueTypeGlobals
49 wxAnyValueTypeGlobals()
52 ~wxAnyValueTypeGlobals()
54 for ( size_t i
=0; i
<m_valueTypes
.size(); i
++ )
55 delete m_valueTypes
[i
];
58 void RegisterValueType(wxAnyValueType
* valueType
)
60 m_valueTypes
.push_back(valueType
);
64 wxVector
<wxAnyValueType
*> m_valueTypes
;
67 static wxAnyValueTypeGlobals
* g_wxAnyValueTypeGlobals
= NULL
;
70 // This class is to make sure that wxAnyValueType instances
71 // etc. get freed correctly. We must use a separate wxAnyValueTypeGlobals
72 // because wxModule itself is instantiated too late.
74 class wxAnyValueTypeGlobalsManager
: public wxModule
76 DECLARE_DYNAMIC_CLASS(wxAnyValueTypeGlobalsManager
)
78 wxAnyValueTypeGlobalsManager() : wxModule() { }
79 virtual ~wxAnyValueTypeGlobalsManager() { }
87 delete g_wxAnyValueTypeGlobals
;
88 g_wxAnyValueTypeGlobals
= NULL
;
93 IMPLEMENT_DYNAMIC_CLASS(wxAnyValueTypeGlobalsManager
, wxModule
)
96 //-------------------------------------------------------------------------
98 //-------------------------------------------------------------------------
100 wxAnyValueType::wxAnyValueType()
102 if ( !g_wxAnyValueTypeGlobals
)
103 g_wxAnyValueTypeGlobals
= new wxAnyValueTypeGlobals();
105 g_wxAnyValueTypeGlobals
->RegisterValueType(this);
108 //-------------------------------------------------------------------------
109 // Dynamic conversion member functions
110 //-------------------------------------------------------------------------
113 // Define integer minimum and maximum as helpers
115 #define UseIntMin (wxINT64_MIN)
116 #define UseIntMax (wxINT64_MAX)
117 #define UseUintMax (wxUINT64_MAX)
119 #define UseIntMin (LONG_MIN)
120 #define UseIntMax (LONG_MAX)
121 #define UseUintMax (ULONG_MAX)
127 const double UseIntMinF
= static_cast<double>(UseIntMin
);
128 const double UseIntMaxF
= static_cast<double>(UseIntMax
);
129 const double UseUintMaxF
= static_cast<double>(UseUintMax
);
131 } // anonymous namespace
133 bool wxAnyValueTypeImplInt::ConvertValue(const wxAnyValueBuffer
& src
,
134 wxAnyValueType
* dstType
,
135 wxAnyValueBuffer
& dst
) const
137 wxAnyBaseIntType value
= GetValue(src
);
138 if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, wxString
) )
141 wxLongLong
ll(value
);
142 wxString s
= ll
.ToString();
144 wxString s
= wxString::Format(wxS("%ld"), (long)value
);
146 wxAnyValueTypeImpl
<wxString
>::SetValue(s
, dst
);
148 else if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, wxAnyBaseUintType
) )
152 wxAnyBaseUintType ul
= (wxAnyBaseUintType
) value
;
153 wxAnyValueTypeImplUint::SetValue(ul
, dst
);
155 else if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, double) )
157 double value2
= static_cast<double>(value
);
158 wxAnyValueTypeImplDouble::SetValue(value2
, dst
);
160 else if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, bool) )
162 bool value2
= value
? true : false;
163 wxAnyValueTypeImpl
<bool>::SetValue(value2
, dst
);
171 bool wxAnyValueTypeImplUint::ConvertValue(const wxAnyValueBuffer
& src
,
172 wxAnyValueType
* dstType
,
173 wxAnyValueBuffer
& dst
) const
175 wxAnyBaseUintType value
= GetValue(src
);
176 if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, wxString
) )
179 wxULongLong
ull(value
);
180 wxString s
= ull
.ToString();
182 wxString s
= wxString::Format(wxS("%lu"), (long)value
);
184 wxAnyValueTypeImpl
<wxString
>::SetValue(s
, dst
);
186 else if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, wxAnyBaseIntType
) )
188 if ( value
> UseIntMax
)
190 wxAnyBaseIntType l
= (wxAnyBaseIntType
) value
;
191 wxAnyValueTypeImplInt::SetValue(l
, dst
);
193 else if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, double) )
196 double value2
= static_cast<double>(value
);
198 // VC6 doesn't implement conversion from unsigned __int64 to double
199 wxAnyBaseIntType value0
= static_cast<wxAnyBaseIntType
>(value
);
200 double value2
= static_cast<double>(value0
);
202 wxAnyValueTypeImplDouble::SetValue(value2
, dst
);
204 else if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, bool) )
206 bool value2
= value
? true : false;
207 wxAnyValueTypeImpl
<bool>::SetValue(value2
, dst
);
215 bool wxAnyValueTypeImplString::ConvertValue(const wxAnyValueBuffer
& src
,
216 wxAnyValueType
* dstType
,
217 wxAnyValueBuffer
& dst
) const
219 wxString value
= GetValue(src
);
220 if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, wxAnyBaseIntType
) )
222 wxAnyBaseIntType value2
;
224 if ( !value
.ToLongLong(&value2
) )
226 if ( !value
.ToLong(&value2
) )
229 wxAnyValueTypeImplInt::SetValue(value2
, dst
);
231 else if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, wxAnyBaseUintType
) )
233 wxAnyBaseUintType value2
;
235 if ( !value
.ToULongLong(&value2
) )
237 if ( !value
.ToULong(&value2
) )
240 wxAnyValueTypeImplUint::SetValue(value2
, dst
);
242 else if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, double) )
245 if ( !value
.ToDouble(&value2
) )
247 wxAnyValueTypeImplDouble::SetValue(value2
, dst
);
249 else if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, bool) )
253 if ( value
== wxS("true") ||
254 value
== wxS("yes") ||
257 else if ( value
== wxS("false") ||
258 value
== wxS("no") ||
264 wxAnyValueTypeImpl
<bool>::SetValue(value2
, dst
);
272 bool wxAnyValueTypeImpl
<bool>::ConvertValue(const wxAnyValueBuffer
& src
,
273 wxAnyValueType
* dstType
,
274 wxAnyValueBuffer
& dst
) const
276 bool value
= GetValue(src
);
277 if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, wxAnyBaseIntType
) )
279 wxAnyBaseIntType value2
= static_cast<wxAnyBaseIntType
>(value
);
280 wxAnyValueTypeImplInt::SetValue(value2
, dst
);
282 else if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, wxAnyBaseUintType
) )
284 wxAnyBaseIntType value2
= static_cast<wxAnyBaseUintType
>(value
);
285 wxAnyValueTypeImplUint::SetValue(value2
, dst
);
287 else if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, wxString
) )
294 wxAnyValueTypeImpl
<wxString
>::SetValue(s
, dst
);
302 bool wxAnyValueTypeImplDouble::ConvertValue(const wxAnyValueBuffer
& src
,
303 wxAnyValueType
* dstType
,
304 wxAnyValueBuffer
& dst
) const
306 double value
= GetValue(src
);
307 if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, wxAnyBaseIntType
) )
309 if ( value
< UseIntMinF
|| value
> UseIntMaxF
)
311 wxAnyBaseUintType ul
= static_cast<wxAnyBaseUintType
>(value
);
312 wxAnyValueTypeImplUint::SetValue(ul
, dst
);
314 else if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, wxAnyBaseUintType
) )
316 if ( value
< 0.0 || value
> UseUintMaxF
)
318 wxAnyBaseUintType ul
= static_cast<wxAnyBaseUintType
>(value
);
319 wxAnyValueTypeImplUint::SetValue(ul
, dst
);
321 else if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, wxString
) )
323 wxString s
= wxString::Format(wxS("%.14g"), value
);
324 wxAnyValueTypeImpl
<wxString
>::SetValue(s
, dst
);
332 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImplInt
)
333 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImplUint
)
334 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImplString
)
335 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl
<bool>)
336 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImplDouble
)
338 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl
<wxDateTime
>)
339 //WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<wxObject*>)
340 //WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<wxArrayString>)
342 //-------------------------------------------------------------------------
343 // wxAnyNullValueType implementation
344 //-------------------------------------------------------------------------
353 class wxAnyValueTypeImpl
<wxAnyNullValue
> : public wxAnyValueType
355 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl
<wxAnyNullValue
>)
357 // Dummy implementations
358 virtual void DeleteValue(wxAnyValueBuffer
& buf
) const
363 virtual void CopyBuffer(const wxAnyValueBuffer
& src
,
364 wxAnyValueBuffer
& dst
) const
370 virtual bool ConvertValue(const wxAnyValueBuffer
& src
,
371 wxAnyValueType
* dstType
,
372 wxAnyValueBuffer
& dst
) const
375 wxUnusedVar(dstType
);
383 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl
<wxAnyNullValue
>)
385 wxAnyValueType
* wxAnyNullValueType
=
386 wxAnyValueTypeImpl
<wxAnyNullValue
>::GetInstance();