any.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/any.h
  3. // Purpose: wxAny class
  4. // Author: Jaakko Salli
  5. // Modified by:
  6. // Created: 07/05/2009
  7. // Copyright: (c) wxWidgets team
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_ANY_H_
  11. #define _WX_ANY_H_
  12. #include "wx/defs.h"
  13. #if wxUSE_ANY
  14. #include <new> // for placement new
  15. #include "wx/string.h"
  16. #include "wx/meta/if.h"
  17. #include "wx/typeinfo.h"
  18. #include "wx/list.h"
  19. // Size of the wxAny value buffer.
  20. enum
  21. {
  22. WX_ANY_VALUE_BUFFER_SIZE = 16
  23. };
  24. union wxAnyValueBuffer
  25. {
  26. union Alignment
  27. {
  28. #if wxHAS_INT64
  29. wxInt64 m_int64;
  30. #endif
  31. long double m_longDouble;
  32. void ( *m_funcPtr )(void);
  33. void ( wxAnyValueBuffer::*m_mFuncPtr )(void);
  34. } m_alignment;
  35. void* m_ptr;
  36. wxByte m_buffer[WX_ANY_VALUE_BUFFER_SIZE];
  37. };
  38. //
  39. // wxAnyValueType is base class for value type functionality for C++ data
  40. // types used with wxAny. Usually the default template (wxAnyValueTypeImpl<>)
  41. // will create a satisfactory wxAnyValueType implementation for a data type.
  42. //
  43. class WXDLLIMPEXP_BASE wxAnyValueType
  44. {
  45. WX_DECLARE_ABSTRACT_TYPEINFO(wxAnyValueType)
  46. public:
  47. /**
  48. Default constructor.
  49. */
  50. wxAnyValueType()
  51. {
  52. }
  53. /**
  54. Destructor.
  55. */
  56. virtual ~wxAnyValueType()
  57. {
  58. }
  59. /**
  60. This function is used for internal type matching.
  61. */
  62. virtual bool IsSameType(const wxAnyValueType* otherType) const = 0;
  63. /**
  64. This function is called every time the data in wxAny
  65. buffer needs to be freed.
  66. */
  67. virtual void DeleteValue(wxAnyValueBuffer& buf) const = 0;
  68. /**
  69. Implement this for buffer-to-buffer copy.
  70. @param src
  71. This is the source data buffer.
  72. @param dst
  73. This is the destination data buffer that is in either
  74. uninitialized or freed state.
  75. */
  76. virtual void CopyBuffer(const wxAnyValueBuffer& src,
  77. wxAnyValueBuffer& dst) const = 0;
  78. /**
  79. Convert value into buffer of different type. Return false if
  80. not possible.
  81. */
  82. virtual bool ConvertValue(const wxAnyValueBuffer& src,
  83. wxAnyValueType* dstType,
  84. wxAnyValueBuffer& dst) const = 0;
  85. /**
  86. Use this template function for checking if wxAnyValueType represents
  87. a specific C++ data type.
  88. @remarks This template function does not work on some older compilers
  89. (such as Visual C++ 6.0). For full compiler compatibility
  90. please use wxANY_VALUE_TYPE_CHECK_TYPE(valueTypePtr, T) macro
  91. instead.
  92. @see wxAny::CheckType()
  93. */
  94. // FIXME-VC6: remove this hack when VC6 is no longer supported
  95. template <typename T>
  96. bool CheckType(T* reserved = NULL) const;
  97. #if wxUSE_EXTENDED_RTTI
  98. virtual const wxTypeInfo* GetTypeInfo() const = 0;
  99. #endif
  100. private:
  101. };
  102. //
  103. // We need to allocate wxAnyValueType instances in heap, and need to use
  104. // scoped ptr to properly deallocate them in dynamic library use cases.
  105. // Here we have a minimal specialized scoped ptr implementation to deal
  106. // with various compiler-specific problems with template class' static
  107. // member variable of template type with explicit constructor which
  108. // is initialized in global scope.
  109. //
  110. class wxAnyValueTypeScopedPtr
  111. {
  112. public:
  113. wxAnyValueTypeScopedPtr(wxAnyValueType* ptr) : m_ptr(ptr) { }
  114. ~wxAnyValueTypeScopedPtr() { delete m_ptr; }
  115. wxAnyValueType* get() const { return m_ptr; }
  116. private:
  117. wxAnyValueType* m_ptr;
  118. };
  119. //
  120. // This method of checking the type is compatible with VC6
  121. #define wxANY_VALUE_TYPE_CHECK_TYPE(valueTypePtr, T) \
  122. wxAnyValueTypeImpl<T>::IsSameClass(valueTypePtr)
  123. /**
  124. Helper macro for defining user value types.
  125. Even though C++ RTTI would be fully available to use, we'd have to to
  126. facilitate sub-type system which allows, for instance, wxAny with
  127. signed short '15' to be treated equal to wxAny with signed long long '15'.
  128. Having sm_instance is important here.
  129. NB: We really need to have wxAnyValueType instances allocated
  130. in heap. They are stored as static template member variables,
  131. and with them we just can't be too careful (eg. not allocating
  132. them in heap broke the type identification in GCC).
  133. */
  134. #define WX_DECLARE_ANY_VALUE_TYPE(CLS) \
  135. friend class wxAny; \
  136. WX_DECLARE_TYPEINFO_INLINE(CLS) \
  137. public: \
  138. static bool IsSameClass(const wxAnyValueType* otherType) \
  139. { \
  140. return wxTypeId(*sm_instance.get()) == wxTypeId(*otherType); \
  141. } \
  142. virtual bool IsSameType(const wxAnyValueType* otherType) const \
  143. { \
  144. return IsSameClass(otherType); \
  145. } \
  146. private: \
  147. static wxAnyValueTypeScopedPtr sm_instance; \
  148. public: \
  149. static wxAnyValueType* GetInstance() \
  150. { \
  151. return sm_instance.get(); \
  152. }
  153. #define WX_IMPLEMENT_ANY_VALUE_TYPE(CLS) \
  154. wxAnyValueTypeScopedPtr CLS::sm_instance(new CLS());
  155. #ifdef __VISUALC6__
  156. // "non dll-interface class 'xxx' used as base interface
  157. #pragma warning (push)
  158. #pragma warning (disable:4275)
  159. #endif
  160. /**
  161. Following are helper classes for the wxAnyValueTypeImplBase.
  162. */
  163. namespace wxPrivate
  164. {
  165. template<typename T>
  166. class wxAnyValueTypeOpsInplace
  167. {
  168. public:
  169. static void DeleteValue(wxAnyValueBuffer& buf)
  170. {
  171. T* value = reinterpret_cast<T*>(&buf.m_buffer[0]);
  172. value->~T();
  173. // Some compiler may given 'unused variable' warnings without this
  174. wxUnusedVar(value);
  175. }
  176. static void SetValue(const T& value,
  177. wxAnyValueBuffer& buf)
  178. {
  179. // Use placement new
  180. void* const place = buf.m_buffer;
  181. ::new(place) T(value);
  182. }
  183. static const T& GetValue(const wxAnyValueBuffer& buf)
  184. {
  185. // Breaking this code into two lines should suppress
  186. // GCC's 'type-punned pointer will break strict-aliasing rules'
  187. // warning.
  188. const T* value = reinterpret_cast<const T*>(&buf.m_buffer[0]);
  189. return *value;
  190. }
  191. };
  192. template<typename T>
  193. class wxAnyValueTypeOpsGeneric
  194. {
  195. public:
  196. template<typename T2>
  197. class DataHolder
  198. {
  199. public:
  200. DataHolder(const T2& value)
  201. {
  202. m_value = value;
  203. }
  204. virtual ~DataHolder() { }
  205. T2 m_value;
  206. private:
  207. wxDECLARE_NO_COPY_CLASS(DataHolder);
  208. };
  209. static void DeleteValue(wxAnyValueBuffer& buf)
  210. {
  211. DataHolder<T>* holder = static_cast<DataHolder<T>*>(buf.m_ptr);
  212. delete holder;
  213. }
  214. static void SetValue(const T& value,
  215. wxAnyValueBuffer& buf)
  216. {
  217. DataHolder<T>* holder = new DataHolder<T>(value);
  218. buf.m_ptr = holder;
  219. }
  220. static const T& GetValue(const wxAnyValueBuffer& buf)
  221. {
  222. DataHolder<T>* holder = static_cast<DataHolder<T>*>(buf.m_ptr);
  223. return holder->m_value;
  224. }
  225. };
  226. } // namespace wxPrivate
  227. /**
  228. Intermediate template for the generic value type implementation.
  229. We can derive from this same value type for multiple actual types
  230. (for instance, we can have wxAnyValueTypeImplInt for all signed
  231. integer types), and also easily implement specialized templates
  232. with specific dynamic type conversion.
  233. */
  234. template<typename T>
  235. class wxAnyValueTypeImplBase : public wxAnyValueType
  236. {
  237. typedef typename wxIf< sizeof(T) <= WX_ANY_VALUE_BUFFER_SIZE,
  238. wxPrivate::wxAnyValueTypeOpsInplace<T>,
  239. wxPrivate::wxAnyValueTypeOpsGeneric<T> >::value
  240. Ops;
  241. public:
  242. wxAnyValueTypeImplBase() : wxAnyValueType() { }
  243. virtual ~wxAnyValueTypeImplBase() { }
  244. virtual void DeleteValue(wxAnyValueBuffer& buf) const
  245. {
  246. Ops::DeleteValue(buf);
  247. }
  248. virtual void CopyBuffer(const wxAnyValueBuffer& src,
  249. wxAnyValueBuffer& dst) const
  250. {
  251. Ops::SetValue(Ops::GetValue(src), dst);
  252. }
  253. /**
  254. It is important to reimplement this in any specialized template
  255. classes that inherit from wxAnyValueTypeImplBase.
  256. */
  257. static void SetValue(const T& value,
  258. wxAnyValueBuffer& buf)
  259. {
  260. Ops::SetValue(value, buf);
  261. }
  262. /**
  263. It is important to reimplement this in any specialized template
  264. classes that inherit from wxAnyValueTypeImplBase.
  265. */
  266. static const T& GetValue(const wxAnyValueBuffer& buf)
  267. {
  268. return Ops::GetValue(buf);
  269. }
  270. #if wxUSE_EXTENDED_RTTI
  271. virtual const wxTypeInfo* GetTypeInfo() const
  272. {
  273. return wxGetTypeInfo((T*)NULL);
  274. }
  275. #endif
  276. };
  277. /*
  278. Generic value type template. Note that bulk of the implementation
  279. resides in wxAnyValueTypeImplBase.
  280. */
  281. template<typename T>
  282. class wxAnyValueTypeImpl : public wxAnyValueTypeImplBase<T>
  283. {
  284. WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<T>)
  285. public:
  286. wxAnyValueTypeImpl() : wxAnyValueTypeImplBase<T>() { }
  287. virtual ~wxAnyValueTypeImpl() { }
  288. virtual bool ConvertValue(const wxAnyValueBuffer& src,
  289. wxAnyValueType* dstType,
  290. wxAnyValueBuffer& dst) const
  291. {
  292. wxUnusedVar(src);
  293. wxUnusedVar(dstType);
  294. wxUnusedVar(dst);
  295. return false;
  296. }
  297. };
  298. template<typename T>
  299. wxAnyValueTypeScopedPtr wxAnyValueTypeImpl<T>::sm_instance = new wxAnyValueTypeImpl<T>();
  300. //
  301. // Helper macro for using same base value type implementation for multiple
  302. // actual C++ data types.
  303. //
  304. #define _WX_ANY_DEFINE_SUB_TYPE(T, CLSTYPE) \
  305. template<> \
  306. class wxAnyValueTypeImpl<T> : public wxAnyValueTypeImpl##CLSTYPE \
  307. { \
  308. typedef wxAnyBase##CLSTYPE##Type UseDataType; \
  309. public: \
  310. wxAnyValueTypeImpl() : wxAnyValueTypeImpl##CLSTYPE() { } \
  311. virtual ~wxAnyValueTypeImpl() { } \
  312. static void SetValue(const T& value, wxAnyValueBuffer& buf) \
  313. { \
  314. void* voidPtr = reinterpret_cast<void*>(&buf.m_buffer[0]); \
  315. UseDataType* dptr = reinterpret_cast<UseDataType*>(voidPtr); \
  316. *dptr = static_cast<UseDataType>(value); \
  317. } \
  318. static T GetValue(const wxAnyValueBuffer& buf) \
  319. { \
  320. const void* voidPtr = \
  321. reinterpret_cast<const void*>(&buf.m_buffer[0]); \
  322. const UseDataType* sptr = \
  323. reinterpret_cast<const UseDataType*>(voidPtr); \
  324. return static_cast<T>(*sptr); \
  325. }
  326. #if wxUSE_EXTENDED_RTTI
  327. #define WX_ANY_DEFINE_SUB_TYPE(T, CLSTYPE) \
  328. _WX_ANY_DEFINE_SUB_TYPE(T, CLSTYPE)\
  329. virtual const wxTypeInfo* GetTypeInfo() const \
  330. { \
  331. return wxGetTypeInfo((T*)NULL); \
  332. } \
  333. };
  334. #else
  335. #define WX_ANY_DEFINE_SUB_TYPE(T, CLSTYPE) \
  336. _WX_ANY_DEFINE_SUB_TYPE(T, CLSTYPE)\
  337. };
  338. #endif
  339. //
  340. // Integer value types
  341. //
  342. #ifdef wxLongLong_t
  343. typedef wxLongLong_t wxAnyBaseIntType;
  344. typedef wxULongLong_t wxAnyBaseUintType;
  345. #else
  346. typedef long wxAnyBaseIntType;
  347. typedef unsigned long wxAnyBaseUintType;
  348. #endif
  349. class WXDLLIMPEXP_BASE wxAnyValueTypeImplInt :
  350. public wxAnyValueTypeImplBase<wxAnyBaseIntType>
  351. {
  352. WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplInt)
  353. public:
  354. wxAnyValueTypeImplInt() :
  355. wxAnyValueTypeImplBase<wxAnyBaseIntType>() { }
  356. virtual ~wxAnyValueTypeImplInt() { }
  357. virtual bool ConvertValue(const wxAnyValueBuffer& src,
  358. wxAnyValueType* dstType,
  359. wxAnyValueBuffer& dst) const;
  360. };
  361. class WXDLLIMPEXP_BASE wxAnyValueTypeImplUint :
  362. public wxAnyValueTypeImplBase<wxAnyBaseUintType>
  363. {
  364. WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplUint)
  365. public:
  366. wxAnyValueTypeImplUint() :
  367. wxAnyValueTypeImplBase<wxAnyBaseUintType>() { }
  368. virtual ~wxAnyValueTypeImplUint() { }
  369. virtual bool ConvertValue(const wxAnyValueBuffer& src,
  370. wxAnyValueType* dstType,
  371. wxAnyValueBuffer& dst) const;
  372. };
  373. WX_ANY_DEFINE_SUB_TYPE(signed long, Int)
  374. WX_ANY_DEFINE_SUB_TYPE(signed int, Int)
  375. WX_ANY_DEFINE_SUB_TYPE(signed short, Int)
  376. WX_ANY_DEFINE_SUB_TYPE(signed char, Int)
  377. #ifdef wxLongLong_t
  378. WX_ANY_DEFINE_SUB_TYPE(wxLongLong_t, Int)
  379. #endif
  380. WX_ANY_DEFINE_SUB_TYPE(unsigned long, Uint)
  381. WX_ANY_DEFINE_SUB_TYPE(unsigned int, Uint)
  382. WX_ANY_DEFINE_SUB_TYPE(unsigned short, Uint)
  383. WX_ANY_DEFINE_SUB_TYPE(unsigned char, Uint)
  384. #ifdef wxLongLong_t
  385. WX_ANY_DEFINE_SUB_TYPE(wxULongLong_t, Uint)
  386. #endif
  387. //
  388. // This macro is used in header, but then in source file we must have:
  389. // WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl##TYPENAME)
  390. //
  391. #define _WX_ANY_DEFINE_CONVERTIBLE_TYPE(T, TYPENAME, CONVFUNC, GV) \
  392. class WXDLLIMPEXP_BASE wxAnyValueTypeImpl##TYPENAME : \
  393. public wxAnyValueTypeImplBase<T> \
  394. { \
  395. WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl##TYPENAME) \
  396. public: \
  397. wxAnyValueTypeImpl##TYPENAME() : \
  398. wxAnyValueTypeImplBase<T>() { } \
  399. virtual ~wxAnyValueTypeImpl##TYPENAME() { } \
  400. virtual bool ConvertValue(const wxAnyValueBuffer& src, \
  401. wxAnyValueType* dstType, \
  402. wxAnyValueBuffer& dst) const \
  403. { \
  404. GV value = GetValue(src); \
  405. return CONVFUNC(value, dstType, dst); \
  406. } \
  407. }; \
  408. template<> \
  409. class wxAnyValueTypeImpl<T> : public wxAnyValueTypeImpl##TYPENAME \
  410. { \
  411. public: \
  412. wxAnyValueTypeImpl() : wxAnyValueTypeImpl##TYPENAME() { } \
  413. virtual ~wxAnyValueTypeImpl() { } \
  414. };
  415. #define WX_ANY_DEFINE_CONVERTIBLE_TYPE(T, TYPENAME, CONVFUNC, BT) \
  416. _WX_ANY_DEFINE_CONVERTIBLE_TYPE(T, TYPENAME, CONVFUNC, BT) \
  417. #define WX_ANY_DEFINE_CONVERTIBLE_TYPE_BASE(T, TYPENAME, CONVFUNC) \
  418. _WX_ANY_DEFINE_CONVERTIBLE_TYPE(T, TYPENAME, \
  419. CONVFUNC, const T&) \
  420. //
  421. // String value type
  422. //
  423. // Convert wxString to destination wxAny value type
  424. extern WXDLLIMPEXP_BASE bool wxAnyConvertString(const wxString& value,
  425. wxAnyValueType* dstType,
  426. wxAnyValueBuffer& dst);
  427. WX_ANY_DEFINE_CONVERTIBLE_TYPE_BASE(wxString, wxString, wxAnyConvertString)
  428. WX_ANY_DEFINE_CONVERTIBLE_TYPE(const char*, ConstCharPtr,
  429. wxAnyConvertString, wxString)
  430. WX_ANY_DEFINE_CONVERTIBLE_TYPE(const wchar_t*, ConstWchar_tPtr,
  431. wxAnyConvertString, wxString)
  432. //
  433. // Bool value type
  434. //
  435. template<>
  436. class WXDLLIMPEXP_BASE wxAnyValueTypeImpl<bool> :
  437. public wxAnyValueTypeImplBase<bool>
  438. {
  439. WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<bool>)
  440. public:
  441. wxAnyValueTypeImpl() :
  442. wxAnyValueTypeImplBase<bool>() { }
  443. virtual ~wxAnyValueTypeImpl() { }
  444. virtual bool ConvertValue(const wxAnyValueBuffer& src,
  445. wxAnyValueType* dstType,
  446. wxAnyValueBuffer& dst) const;
  447. };
  448. //
  449. // Floating point value type
  450. //
  451. class WXDLLIMPEXP_BASE wxAnyValueTypeImplDouble :
  452. public wxAnyValueTypeImplBase<double>
  453. {
  454. WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplDouble)
  455. public:
  456. wxAnyValueTypeImplDouble() :
  457. wxAnyValueTypeImplBase<double>() { }
  458. virtual ~wxAnyValueTypeImplDouble() { }
  459. virtual bool ConvertValue(const wxAnyValueBuffer& src,
  460. wxAnyValueType* dstType,
  461. wxAnyValueBuffer& dst) const;
  462. };
  463. // WX_ANY_DEFINE_SUB_TYPE requires this
  464. typedef double wxAnyBaseDoubleType;
  465. WX_ANY_DEFINE_SUB_TYPE(float, Double)
  466. WX_ANY_DEFINE_SUB_TYPE(double, Double)
  467. //
  468. // Defines a dummy wxAnyValueTypeImpl<> with given export
  469. // declaration. This is needed if a class is used with
  470. // wxAny in both user shared library and application.
  471. //
  472. #define wxDECLARE_ANY_TYPE(CLS, DECL) \
  473. template<> \
  474. class DECL wxAnyValueTypeImpl<CLS> : \
  475. public wxAnyValueTypeImplBase<CLS> \
  476. { \
  477. WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<CLS>) \
  478. public: \
  479. wxAnyValueTypeImpl() : \
  480. wxAnyValueTypeImplBase<CLS>() { } \
  481. virtual ~wxAnyValueTypeImpl() { } \
  482. \
  483. virtual bool ConvertValue(const wxAnyValueBuffer& src, \
  484. wxAnyValueType* dstType, \
  485. wxAnyValueBuffer& dst) const \
  486. { \
  487. wxUnusedVar(src); \
  488. wxUnusedVar(dstType); \
  489. wxUnusedVar(dst); \
  490. return false; \
  491. } \
  492. };
  493. // Make sure some of wx's own types get the right wxAnyValueType export
  494. // (this is needed only for types that are referred to from wxBase.
  495. // currently we may not use any of these types from there, but let's
  496. // use the macro on at least one to make sure it compiles since we can't
  497. // really test it properly in unit tests since a separate DLL would
  498. // be needed).
  499. #if wxUSE_DATETIME
  500. #include "wx/datetime.h"
  501. wxDECLARE_ANY_TYPE(wxDateTime, WXDLLIMPEXP_BASE)
  502. #endif
  503. //#include "wx/object.h"
  504. //wxDECLARE_ANY_TYPE(wxObject*, WXDLLIMPEXP_BASE)
  505. //#include "wx/arrstr.h"
  506. //wxDECLARE_ANY_TYPE(wxArrayString, WXDLLIMPEXP_BASE)
  507. #if wxUSE_VARIANT
  508. class WXDLLIMPEXP_FWD_BASE wxAnyToVariantRegistration;
  509. // Because of header inter-dependencies, cannot include this earlier
  510. #include "wx/variant.h"
  511. //
  512. // wxVariantData* data type implementation. For cases when appropriate
  513. // wxAny<->wxVariant conversion code is missing.
  514. //
  515. class WXDLLIMPEXP_BASE wxAnyValueTypeImplVariantData :
  516. public wxAnyValueTypeImplBase<wxVariantData*>
  517. {
  518. WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplVariantData)
  519. public:
  520. wxAnyValueTypeImplVariantData() :
  521. wxAnyValueTypeImplBase<wxVariantData*>() { }
  522. virtual ~wxAnyValueTypeImplVariantData() { }
  523. virtual void DeleteValue(wxAnyValueBuffer& buf) const
  524. {
  525. wxVariantData* data = static_cast<wxVariantData*>(buf.m_ptr);
  526. if ( data )
  527. data->DecRef();
  528. }
  529. virtual void CopyBuffer(const wxAnyValueBuffer& src,
  530. wxAnyValueBuffer& dst) const
  531. {
  532. wxVariantData* data = static_cast<wxVariantData*>(src.m_ptr);
  533. if ( data )
  534. data->IncRef();
  535. dst.m_ptr = data;
  536. }
  537. static void SetValue(wxVariantData* value,
  538. wxAnyValueBuffer& buf)
  539. {
  540. value->IncRef();
  541. buf.m_ptr = value;
  542. }
  543. static wxVariantData* GetValue(const wxAnyValueBuffer& buf)
  544. {
  545. return static_cast<wxVariantData*>(buf.m_ptr);
  546. }
  547. virtual bool ConvertValue(const wxAnyValueBuffer& src,
  548. wxAnyValueType* dstType,
  549. wxAnyValueBuffer& dst) const
  550. {
  551. wxUnusedVar(src);
  552. wxUnusedVar(dstType);
  553. wxUnusedVar(dst);
  554. return false;
  555. }
  556. };
  557. template<>
  558. class wxAnyValueTypeImpl<wxVariantData*> :
  559. public wxAnyValueTypeImplVariantData
  560. {
  561. public:
  562. wxAnyValueTypeImpl() : wxAnyValueTypeImplVariantData() { }
  563. virtual ~wxAnyValueTypeImpl() { }
  564. };
  565. #endif // wxUSE_VARIANT
  566. #ifdef __VISUALC6__
  567. // Re-enable useless VC6 warnings
  568. #pragma warning (pop)
  569. #endif
  570. /*
  571. Let's define a discrete Null value so we don't have to really
  572. ever check if wxAny.m_type pointer is NULL or not. This is an
  573. optimization, mostly. Implementation of this value type is
  574. "hidden" in the source file.
  575. */
  576. extern WXDLLIMPEXP_DATA_BASE(wxAnyValueType*) wxAnyNullValueType;
  577. //
  578. // We need to implement custom signed/unsigned int equals operators
  579. // for signed/unsigned (eg. wxAny(128UL) == 128L) comparisons to work.
  580. #define WXANY_IMPLEMENT_INT_EQ_OP(TS, TUS) \
  581. bool operator==(TS value) const \
  582. { \
  583. if ( wxAnyValueTypeImpl<TS>::IsSameClass(m_type) ) \
  584. return (value == static_cast<TS> \
  585. (wxAnyValueTypeImpl<TS>::GetValue(m_buffer))); \
  586. if ( wxAnyValueTypeImpl<TUS>::IsSameClass(m_type) ) \
  587. return (value == static_cast<TS> \
  588. (wxAnyValueTypeImpl<TUS>::GetValue(m_buffer))); \
  589. return false; \
  590. } \
  591. bool operator==(TUS value) const \
  592. { \
  593. if ( wxAnyValueTypeImpl<TUS>::IsSameClass(m_type) ) \
  594. return (value == static_cast<TUS> \
  595. (wxAnyValueTypeImpl<TUS>::GetValue(m_buffer))); \
  596. if ( wxAnyValueTypeImpl<TS>::IsSameClass(m_type) ) \
  597. return (value == static_cast<TUS> \
  598. (wxAnyValueTypeImpl<TS>::GetValue(m_buffer))); \
  599. return false; \
  600. }
  601. #if wxUSE_VARIANT
  602. // Note that the following functions are implemented outside wxAny class
  603. // so that it can reside entirely in header and lack the export declaration.
  604. // Helper function used to associate wxAnyValueType with a wxVariantData.
  605. extern WXDLLIMPEXP_BASE void
  606. wxPreRegisterAnyToVariant(wxAnyToVariantRegistration* reg);
  607. // This function performs main wxAny to wxVariant conversion duties.
  608. extern WXDLLIMPEXP_BASE bool
  609. wxConvertAnyToVariant(const wxAny& any, wxVariant* variant);
  610. #endif // wxUSE_VARIANT
  611. //
  612. // The wxAny class represents a container for any type. A variant's value
  613. // can be changed at run time, possibly to a different type of value.
  614. //
  615. // As standard, wxAny can store value of almost any type, in a fairly
  616. // optimal manner even.
  617. //
  618. class wxAny
  619. {
  620. public:
  621. /**
  622. Default constructor.
  623. */
  624. wxAny()
  625. {
  626. m_type = wxAnyNullValueType;
  627. }
  628. /**
  629. Destructor.
  630. */
  631. ~wxAny()
  632. {
  633. m_type->DeleteValue(m_buffer);
  634. }
  635. //@{
  636. /**
  637. Various constructors.
  638. */
  639. template<typename T>
  640. wxAny(const T& value)
  641. {
  642. m_type = wxAnyValueTypeImpl<T>::sm_instance.get();
  643. wxAnyValueTypeImpl<T>::SetValue(value, m_buffer);
  644. }
  645. // These two constructors are needed to deal with string literals
  646. wxAny(const char* value)
  647. {
  648. m_type = wxAnyValueTypeImpl<const char*>::sm_instance.get();
  649. wxAnyValueTypeImpl<const char*>::SetValue(value, m_buffer);
  650. }
  651. wxAny(const wchar_t* value)
  652. {
  653. m_type = wxAnyValueTypeImpl<const wchar_t*>::sm_instance.get();
  654. wxAnyValueTypeImpl<const wchar_t*>::SetValue(value, m_buffer);
  655. }
  656. wxAny(const wxAny& any)
  657. {
  658. m_type = wxAnyNullValueType;
  659. AssignAny(any);
  660. }
  661. #if wxUSE_VARIANT
  662. wxAny(const wxVariant& variant)
  663. {
  664. m_type = wxAnyNullValueType;
  665. AssignVariant(variant);
  666. }
  667. #endif
  668. //@}
  669. /**
  670. Use this template function for checking if this wxAny holds
  671. a specific C++ data type.
  672. @remarks This template function does not work on some older compilers
  673. (such as Visual C++ 6.0). For full compiler ccompatibility
  674. please use wxANY_CHECK_TYPE(any, T) macro instead.
  675. @see wxAnyValueType::CheckType()
  676. */
  677. // FIXME-VC6: remove this hack when VC6 is no longer supported
  678. template <typename T>
  679. bool CheckType(T* = NULL) const
  680. {
  681. return m_type->CheckType<T>();
  682. }
  683. /**
  684. Returns the value type as wxAnyValueType instance.
  685. @remarks You cannot reliably test whether two wxAnys are of
  686. same value type by simply comparing return values
  687. of wxAny::GetType(). Instead, use wxAny::HasSameType().
  688. @see HasSameType()
  689. */
  690. const wxAnyValueType* GetType() const
  691. {
  692. return m_type;
  693. }
  694. /**
  695. Returns @true if this and another wxAny have the same
  696. value type.
  697. */
  698. bool HasSameType(const wxAny& other) const
  699. {
  700. return GetType()->IsSameType(other.GetType());
  701. }
  702. /**
  703. Tests if wxAny is null (that is, whether there is no data).
  704. */
  705. bool IsNull() const
  706. {
  707. return (m_type == wxAnyNullValueType);
  708. }
  709. /**
  710. Makes wxAny null (that is, clears it).
  711. */
  712. void MakeNull()
  713. {
  714. m_type->DeleteValue(m_buffer);
  715. m_type = wxAnyNullValueType;
  716. }
  717. //@{
  718. /**
  719. Assignment operators.
  720. */
  721. template<typename T>
  722. wxAny& operator=(const T &value)
  723. {
  724. m_type->DeleteValue(m_buffer);
  725. m_type = wxAnyValueTypeImpl<T>::sm_instance.get();
  726. wxAnyValueTypeImpl<T>::SetValue(value, m_buffer);
  727. return *this;
  728. }
  729. wxAny& operator=(const wxAny &any)
  730. {
  731. if (this != &any)
  732. AssignAny(any);
  733. return *this;
  734. }
  735. #if wxUSE_VARIANT
  736. wxAny& operator=(const wxVariant &variant)
  737. {
  738. AssignVariant(variant);
  739. return *this;
  740. }
  741. #endif
  742. // These two operators are needed to deal with string literals
  743. wxAny& operator=(const char* value)
  744. {
  745. Assign(value);
  746. return *this;
  747. }
  748. wxAny& operator=(const wchar_t* value)
  749. {
  750. Assign(value);
  751. return *this;
  752. }
  753. //@{
  754. /**
  755. Equality operators.
  756. */
  757. bool operator==(const wxString& value) const
  758. {
  759. wxString value2;
  760. if ( !GetAs(&value2) )
  761. return false;
  762. return value == value2;
  763. }
  764. bool operator==(const char* value) const
  765. { return (*this) == wxString(value); }
  766. bool operator==(const wchar_t* value) const
  767. { return (*this) == wxString(value); }
  768. //
  769. // We need to implement custom signed/unsigned int equals operators
  770. // for signed/unsigned (eg. wxAny(128UL) == 128L) comparisons to work.
  771. WXANY_IMPLEMENT_INT_EQ_OP(signed char, unsigned char)
  772. WXANY_IMPLEMENT_INT_EQ_OP(signed short, unsigned short)
  773. WXANY_IMPLEMENT_INT_EQ_OP(signed int, unsigned int)
  774. WXANY_IMPLEMENT_INT_EQ_OP(signed long, unsigned long)
  775. #ifdef wxLongLong_t
  776. WXANY_IMPLEMENT_INT_EQ_OP(wxLongLong_t, wxULongLong_t)
  777. #endif
  778. wxGCC_WARNING_SUPPRESS(float-equal)
  779. bool operator==(float value) const
  780. {
  781. if ( !wxAnyValueTypeImpl<float>::IsSameClass(m_type) )
  782. return false;
  783. return value ==
  784. static_cast<float>
  785. (wxAnyValueTypeImpl<float>::GetValue(m_buffer));
  786. }
  787. bool operator==(double value) const
  788. {
  789. if ( !wxAnyValueTypeImpl<double>::IsSameClass(m_type) )
  790. return false;
  791. return value ==
  792. static_cast<double>
  793. (wxAnyValueTypeImpl<double>::GetValue(m_buffer));
  794. }
  795. wxGCC_WARNING_RESTORE(float-equal)
  796. bool operator==(bool value) const
  797. {
  798. if ( !wxAnyValueTypeImpl<bool>::IsSameClass(m_type) )
  799. return false;
  800. return value == (wxAnyValueTypeImpl<bool>::GetValue(m_buffer));
  801. }
  802. //@}
  803. //@{
  804. /**
  805. Inequality operators (implement as template).
  806. */
  807. template<typename T>
  808. bool operator!=(const T& value) const
  809. { return !((*this) == value); }
  810. //@}
  811. /**
  812. This template function converts wxAny into given type. In most cases
  813. no type conversion is performed, so if the type is incorrect an
  814. assertion failure will occur.
  815. @remarks For convenience, conversion is done when T is wxString. This
  816. is useful when a string literal (which are treated as
  817. const char* and const wchar_t*) has been assigned to wxAny.
  818. This template function may not work properly with Visual C++
  819. 6. For full compiler compatibility, please use
  820. wxANY_AS(any, T) macro instead.
  821. */
  822. // FIXME-VC6: remove this hack when VC6 is no longer supported
  823. template<typename T>
  824. T As(T* = NULL) const
  825. {
  826. if ( !wxAnyValueTypeImpl<T>::IsSameClass(m_type) )
  827. {
  828. wxFAIL_MSG("Incorrect or non-convertible data type");
  829. }
  830. return static_cast<T>(wxAnyValueTypeImpl<T>::GetValue(m_buffer));
  831. }
  832. // Allow easy conversion from 'const char *' etc. to wxString
  833. // FIXME-VC6: remove this hack when VC6 is no longer supported
  834. //template<>
  835. wxString As(wxString*) const
  836. {
  837. wxString value;
  838. if ( !GetAs(&value) )
  839. {
  840. wxFAIL_MSG("Incorrect or non-convertible data type");
  841. }
  842. return value;
  843. }
  844. #if wxUSE_EXTENDED_RTTI
  845. const wxTypeInfo* GetTypeInfo() const
  846. {
  847. return m_type->GetTypeInfo();
  848. }
  849. #endif
  850. /**
  851. Template function that retrieves and converts the value of this
  852. variant to the type that T* value is.
  853. @return Returns @true if conversion was successful.
  854. */
  855. template<typename T>
  856. bool GetAs(T* value) const
  857. {
  858. if ( !wxAnyValueTypeImpl<T>::IsSameClass(m_type) )
  859. {
  860. wxAnyValueType* otherType =
  861. wxAnyValueTypeImpl<T>::sm_instance.get();
  862. wxAnyValueBuffer temp_buf;
  863. if ( !m_type->ConvertValue(m_buffer, otherType, temp_buf) )
  864. return false;
  865. *value =
  866. static_cast<T>(wxAnyValueTypeImpl<T>::GetValue(temp_buf));
  867. otherType->DeleteValue(temp_buf);
  868. return true;
  869. }
  870. *value = static_cast<T>(wxAnyValueTypeImpl<T>::GetValue(m_buffer));
  871. return true;
  872. }
  873. #if wxUSE_VARIANT
  874. // GetAs() wxVariant specialization
  875. bool GetAs(wxVariant* value) const
  876. {
  877. return wxConvertAnyToVariant(*this, value);
  878. }
  879. #endif
  880. private:
  881. // Assignment functions
  882. void AssignAny(const wxAny& any)
  883. {
  884. // Must delete value - CopyBuffer() never does that
  885. m_type->DeleteValue(m_buffer);
  886. wxAnyValueType* newType = any.m_type;
  887. if ( !newType->IsSameType(m_type) )
  888. m_type = newType;
  889. newType->CopyBuffer(any.m_buffer, m_buffer);
  890. }
  891. #if wxUSE_VARIANT
  892. void AssignVariant(const wxVariant& variant)
  893. {
  894. wxVariantData* data = variant.GetData();
  895. if ( data && data->GetAsAny(this) )
  896. return;
  897. m_type->DeleteValue(m_buffer);
  898. if ( variant.IsNull() )
  899. {
  900. // Init as Null
  901. m_type = wxAnyNullValueType;
  902. }
  903. else
  904. {
  905. // If everything else fails, wrap the whole wxVariantData
  906. m_type = wxAnyValueTypeImpl<wxVariantData*>::sm_instance.get();
  907. wxAnyValueTypeImpl<wxVariantData*>::SetValue(data, m_buffer);
  908. }
  909. }
  910. #endif
  911. template<typename T>
  912. void Assign(const T &value)
  913. {
  914. m_type->DeleteValue(m_buffer);
  915. m_type = wxAnyValueTypeImpl<T>::sm_instance.get();
  916. wxAnyValueTypeImpl<T>::SetValue(value, m_buffer);
  917. }
  918. // Data
  919. wxAnyValueBuffer m_buffer;
  920. wxAnyValueType* m_type;
  921. };
  922. //
  923. // This method of checking the type is compatible with VC6
  924. #define wxANY_CHECK_TYPE(any, T) \
  925. wxANY_VALUE_TYPE_CHECK_TYPE((any).GetType(), T)
  926. //
  927. // This method of getting the value is compatible with VC6
  928. #define wxANY_AS(any, T) \
  929. (any).As(static_cast<T*>(NULL))
  930. template<typename T>
  931. inline bool wxAnyValueType::CheckType(T* reserved) const
  932. {
  933. wxUnusedVar(reserved);
  934. return wxAnyValueTypeImpl<T>::IsSameClass(this);
  935. }
  936. WX_DECLARE_LIST_WITH_DECL(wxAny, wxAnyList, class WXDLLIMPEXP_BASE);
  937. #endif // wxUSE_ANY
  938. #endif // _WX_ANY_H_