variant.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: variant.h
  3. // Purpose: interface of wxVariant
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxVariant
  9. The wxVariant class represents a container for any type. A variant's value
  10. can be changed at run time, possibly to a different type of value.
  11. @note As of wxWidgets 2.9.1, wxAny has become the preferred variant class.
  12. While most controls still use wxVariant in their interface, you
  13. can start using wxAny in your code because of an implicit conversion
  14. layer. See below for more information.
  15. As standard, wxVariant can store values of type bool, wxChar, double, long,
  16. string, string list, time, date, void pointer, list of strings, and list of
  17. variants. However, an application can extend wxVariant's capabilities by
  18. deriving from the class wxVariantData and using the wxVariantData form of
  19. the wxVariant constructor or assignment operator to assign this data to a
  20. variant. Actual values for user-defined types will need to be accessed via
  21. the wxVariantData object, unlike the case for basic data types where
  22. convenience functions such as GetLong() can be used.
  23. Under Microsoft Windows, three additional wxVariantData-derived classes --
  24. wxVariantDataCurrency, wxVariantDataErrorCode and wxVariantDataSafeArray --
  25. are available for interoperation with OLE VARIANT when using wxAutomationObject.
  26. Pointers to any wxObject derived class can also easily be stored in a
  27. wxVariant. wxVariant will then use wxWidgets' built-in RTTI system to set
  28. the type name (returned by GetType()) and to perform type-safety checks at
  29. runtime.
  30. This class is useful for reducing the programming for certain tasks, such
  31. as an editor for different data types, or a remote procedure call protocol.
  32. An optional name member is associated with a wxVariant. This might be used,
  33. for example, in CORBA or OLE automation classes, where named parameters are
  34. required.
  35. Note that as of wxWidgets 2.7.1, wxVariant is
  36. @ref overview_refcount "reference counted". Additionally, the convenience
  37. macros DECLARE_VARIANT_OBJECT() and IMPLEMENT_VARIANT_OBJECT() were added
  38. so that adding (limited) support for conversion to and from wxVariant can
  39. be very easily implemented without modifying either wxVariant or the class
  40. to be stored by wxVariant. Since assignment operators cannot be declared
  41. outside the class, the shift left operators are used like this:
  42. @code
  43. // in the header file
  44. DECLARE_VARIANT_OBJECT(MyClass)
  45. // in the implementation file
  46. IMPLEMENT_VARIANT_OBJECT(MyClass)
  47. // in the user code
  48. wxVariant variant;
  49. MyClass value;
  50. variant << value;
  51. // or
  52. value << variant;
  53. @endcode
  54. For this to work, MyClass must derive from wxObject, implement the
  55. @ref overview_rtti "wxWidgets RTTI system" and support the assignment
  56. operator and equality operator for itself. Ideally, it should also be
  57. reference counted to make copying operations cheap and fast. This can be
  58. most easily implemented using the reference counting support offered by
  59. wxObject itself. By default, wxWidgets already implements the shift
  60. operator conversion for a few of its drawing related classes:
  61. @code
  62. IMPLEMENT_VARIANT_OBJECT(wxColour)
  63. IMPLEMENT_VARIANT_OBJECT(wxImage)
  64. IMPLEMENT_VARIANT_OBJECT(wxIcon)
  65. IMPLEMENT_VARIANT_OBJECT(wxBitmap)
  66. @endcode
  67. Note that as of wxWidgets 2.9.0, wxVariantData no longer inherits from
  68. wxObject and wxVariant no longer uses the type-unsafe wxList class for list
  69. operations but the type-safe wxVariantList class. Also, wxVariantData now
  70. supports the wxVariantData::Clone() function for implementing the Unshare()
  71. function. wxVariantData::Clone() is implemented automatically by
  72. IMPLEMENT_VARIANT_OBJECT().
  73. Since wxVariantData no longer derives from wxObject, any code that tests
  74. the type of the data using wxDynamicCast() will require adjustment. You can
  75. use the macro wxDynamicCastVariantData() with the same arguments as
  76. wxDynamicCast(), to use C++ RTTI type information instead of wxWidgets
  77. RTTI.
  78. @section variant_wxanyconversion wxVariant to wxAny Conversion Layer
  79. wxAny is a more modern, template-based variant class. It is not
  80. directly compatible with wxVariant, but there is a transparent conversion
  81. layer.
  82. Following is an example how to use these conversions with wxPropertyGrid's
  83. property class wxPGProperty (which currently uses wxVariants both
  84. internally and in the public API):
  85. @code
  86. // Get property value as wxAny instead of wxVariant
  87. wxAny value = property->GetValue();
  88. // Do something with it
  89. DoSomethingWithString(value.As<wxString>());
  90. // Write back new value to property
  91. value = "New Value";
  92. property->SetValue(value);
  93. @endcode
  94. Some caveats:
  95. @li In wxAny, there are no separate types for handling integers of
  96. different sizes, so converting wxAny with 'long long' value
  97. will yield wxVariant of "long" type when the value is small
  98. enough to fit in without overflow. Otherwise, variant type
  99. "longlong" is used. Also note that wxAny holding unsigned integer
  100. will always be converted to "ulonglong" wxVariant type.
  101. @li Unlike wxVariant, wxAny does not store a (rarely needed) name string.
  102. @li Because of implicit conversion of wxVariant to wxAny, wxAny cannot
  103. usually contain value of type wxVariant. In other words,
  104. any.CheckType<wxVariant>() can never return @true.
  105. Supplied conversion functions will automatically work with all
  106. built-in wxVariant types, and also with all user-specified types generated
  107. using IMPLEMENT_VARIANT_OBJECT(). For hand-built wxVariantData classes,
  108. you will need to use supplied macros in a following manner:
  109. @code
  110. // Declare wxVariantData for data type Foo
  111. class wxVariantDataFoo: public wxVariantData
  112. {
  113. public:
  114. // interface
  115. // ...
  116. DECLARE_WXANY_CONVERSION()
  117. protected:
  118. // data storage etc
  119. // ...
  120. };
  121. IMPLEMENT_TRIVIAL_WXANY_CONVERSION(Foo, wxVariantDataFoo)
  122. @endcode
  123. @library{wxbase}
  124. @category{data}
  125. @see wxVariantData, wxAny
  126. */
  127. class wxVariant : public wxObject
  128. {
  129. public:
  130. /**
  131. Default constructor.
  132. */
  133. wxVariant();
  134. /**
  135. Constructs a variant directly with a wxVariantData object. wxVariant
  136. will take ownership of the wxVariantData and will not increase its
  137. reference count.
  138. */
  139. wxVariant(wxVariantData* data, const wxString& name = wxEmptyString);
  140. /**
  141. Constructs a variant from another variant by increasing the reference
  142. count.
  143. */
  144. wxVariant(const wxVariant& variant);
  145. /**
  146. Constructs a variant by converting it from wxAny.
  147. */
  148. wxVariant(const wxAny& any);
  149. /**
  150. Constructs a variant from a wide string literal.
  151. */
  152. wxVariant(const wxChar* value, const wxString& name = wxEmptyString);
  153. /**
  154. Constructs a variant from a string.
  155. */
  156. wxVariant(const wxString& value, const wxString& name = wxEmptyString);
  157. /**
  158. Constructs a variant from a wide char.
  159. */
  160. wxVariant(wxChar value, const wxString& name = wxEmptyString);
  161. /**
  162. Constructs a variant from a long.
  163. */
  164. wxVariant(long value, const wxString& name = wxEmptyString);
  165. /**
  166. Constructs a variant from a bool.
  167. */
  168. wxVariant(bool value, const wxString& name = wxEmptyString);
  169. /**
  170. Constructs a variant from a double.
  171. */
  172. wxVariant(double value, const wxString& name = wxEmptyString);
  173. /**
  174. Constructs a variant from a wxLongLong.
  175. */
  176. wxVariant(wxLongLong value, const wxString& name = wxEmptyString);
  177. /**
  178. Constructs a variant from a wxULongLong.
  179. */
  180. wxVariant(wxULongLong value, const wxString& name = wxEmptyString);
  181. /**
  182. Constructs a variant from a list of variants
  183. */
  184. wxVariant(const wxVariantList& value, const wxString& name = wxEmptyString);
  185. /**
  186. Constructs a variant from a void pointer.
  187. */
  188. wxVariant(void* value, const wxString& name = wxEmptyString);
  189. /**
  190. Constructs a variant from a pointer to an wxObject
  191. derived class.
  192. */
  193. wxVariant(wxObject* value, const wxString& name = wxEmptyString);
  194. /**
  195. Constructs a variant from a wxDateTime.
  196. */
  197. wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString);
  198. /**
  199. Constructs a variant from a wxArrayString.
  200. */
  201. wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString);
  202. /**
  203. Destructor.
  204. @note wxVariantData's destructor is protected, so wxVariantData cannot
  205. usually be deleted. Instead, wxVariantData::DecRef() should be
  206. called. See @ref overview_refcount_destruct
  207. "reference-counted object destruction" for more info.
  208. */
  209. virtual ~wxVariant();
  210. /**
  211. @name List Functionality
  212. */
  213. //@{
  214. /**
  215. Returns the value at @a idx (zero-based).
  216. */
  217. wxVariant operator [](size_t idx) const;
  218. /**
  219. Returns a reference to the value at @a idx (zero-based). This can be
  220. used to change the value at this index.
  221. */
  222. wxVariant& operator [](size_t idx);
  223. /**
  224. Appends a value to the list.
  225. */
  226. void Append(const wxVariant& value);
  227. /**
  228. Makes the variant null by deleting the internal data and set the name
  229. to wxEmptyString.
  230. */
  231. void Clear();
  232. /**
  233. Deletes the contents of the list.
  234. */
  235. void ClearList();
  236. /**
  237. Deletes the zero-based @a item from the list.
  238. */
  239. bool Delete(size_t item);
  240. /**
  241. Returns the number of elements in the list.
  242. */
  243. size_t GetCount() const;
  244. /**
  245. Returns a reference to the wxVariantList class used by wxVariant if
  246. this wxVariant is currently a list of variants.
  247. */
  248. wxVariantList& GetList() const;
  249. /**
  250. Inserts a value at the front of the list.
  251. */
  252. void Insert(const wxVariant& value);
  253. /**
  254. Makes an empty list. This differs from a null variant which has no
  255. data; a null list is of type list, but the number of elements in the
  256. list is zero.
  257. */
  258. void NullList();
  259. //@}
  260. //@{
  261. /**
  262. Retrieves and converts the value of this variant to the type that
  263. @a value is.
  264. */
  265. bool Convert(long* value) const;
  266. bool Convert(bool* value) const;
  267. bool Convert(double* value) const;
  268. bool Convert(wxString* value) const;
  269. bool Convert(wxChar* value) const;
  270. bool Convert(wxLongLong* value) const;
  271. bool Convert(wxULongLong* value) const;
  272. bool Convert(wxDateTime* value) const;
  273. //@}
  274. /**
  275. Converts wxVariant into wxAny.
  276. */
  277. wxAny GetAny() const;
  278. /**
  279. Returns the string array value.
  280. */
  281. wxArrayString GetArrayString() const;
  282. /**
  283. Returns the boolean value.
  284. */
  285. bool GetBool() const;
  286. /**
  287. Returns the character value.
  288. */
  289. wxUniChar GetChar() const;
  290. /**
  291. Returns a pointer to the internal variant data. To take ownership of
  292. this data, you must call its wxVariantData::IncRef() method. When you
  293. stop using it, wxVariantData::DecRef() must be called as well.
  294. */
  295. wxVariantData* GetData() const;
  296. /**
  297. Returns the date value.
  298. */
  299. wxDateTime GetDateTime() const;
  300. /**
  301. Returns the floating point value.
  302. */
  303. double GetDouble() const;
  304. /**
  305. Returns the integer value.
  306. */
  307. long GetLong() const;
  308. /**
  309. Returns the signed 64-bit integer value.
  310. */
  311. wxLongLong GetLongLong() const;
  312. /**
  313. Returns a constant reference to the variant name.
  314. */
  315. const wxString& GetName() const;
  316. /**
  317. Gets the string value.
  318. */
  319. wxString GetString() const;
  320. /**
  321. Returns the value type as a string.
  322. The built-in types are:
  323. - "bool"
  324. - "char"
  325. - "datetime"
  326. - "double"
  327. - "list"
  328. - "long"
  329. - "longlong"
  330. - "string"
  331. - "ulonglong"
  332. - "arrstring"
  333. - "void*"
  334. If the variant is null, the value type returned is the string "null"
  335. (not the empty string).
  336. */
  337. wxString GetType() const;
  338. /**
  339. Returns the unsigned 64-bit integer value.
  340. */
  341. wxULongLong GetULongLong() const;
  342. /**
  343. Gets the void pointer value.
  344. Notice that this method can be used for null objects (i.e. those for
  345. which IsNull() returns @true) and will return @NULL for them.
  346. */
  347. void* GetVoidPtr() const;
  348. /**
  349. Gets the wxObject pointer value.
  350. */
  351. wxObject* GetWxObjectPtr() const;
  352. /**
  353. Returns @true if there is no data associated with this variant, @false
  354. if there is data.
  355. */
  356. bool IsNull() const;
  357. /**
  358. Returns @true if @a type matches the type of the variant, @false
  359. otherwise.
  360. */
  361. bool IsType(const wxString& type) const;
  362. /**
  363. Returns @true if the data is derived from the class described by
  364. @a type, @false otherwise.
  365. */
  366. bool IsValueKindOf(const wxClassInfo* type) const;
  367. /**
  368. Makes the variant null by deleting the internal data.
  369. */
  370. void MakeNull();
  371. /**
  372. Makes a string representation of the variant value (for any type).
  373. */
  374. wxString MakeString() const;
  375. /**
  376. Returns @true if @a value matches an element in the list.
  377. */
  378. bool Member(const wxVariant& value) const;
  379. /**
  380. Sets the internal variant data, deleting the existing data if there is
  381. any.
  382. */
  383. void SetData(wxVariantData* data);
  384. /**
  385. Makes sure that any data associated with this variant is not shared
  386. with other variants. For this to work, wxVariantData::Clone() must be
  387. implemented for the data types you are working with.
  388. wxVariantData::Clone() is implemented for all the default data types.
  389. */
  390. bool Unshare();
  391. //@{
  392. /**
  393. Inequality test operator.
  394. */
  395. bool operator !=(const wxVariant& value) const;
  396. bool operator !=(const wxString& value) const;
  397. bool operator !=(const wxChar* value) const;
  398. bool operator !=(wxChar value) const;
  399. bool operator !=(long value) const;
  400. bool operator !=(bool value) const;
  401. bool operator !=(double value) const;
  402. bool operator !=(wxLongLong value) const;
  403. bool operator !=(wxULongLong value) const;
  404. bool operator !=(void* value) const;
  405. bool operator !=(wxObject* value) const;
  406. bool operator !=(const wxVariantList& value) const;
  407. bool operator !=(const wxArrayString& value) const;
  408. bool operator !=(const wxDateTime& value) const;
  409. //@}
  410. //@{
  411. /**
  412. Assignment operator, using @ref overview_refcount "reference counting"
  413. if possible.
  414. */
  415. void operator =(const wxVariant& value);
  416. void operator =(wxVariantData* value);
  417. void operator =(const wxString& value);
  418. void operator =(const wxChar* value);
  419. void operator =(wxChar value);
  420. void operator =(long value);
  421. void operator =(bool value);
  422. void operator =(double value);
  423. bool operator =(wxLongLong value) const;
  424. bool operator =(wxULongLong value) const;
  425. void operator =(void* value);
  426. void operator =(wxObject* value);
  427. void operator =(const wxVariantList& value);
  428. void operator =(const wxDateTime& value);
  429. void operator =(const wxArrayString& value);
  430. //@}
  431. //@{
  432. /**
  433. Equality test operator.
  434. */
  435. bool operator ==(const wxVariant& value) const;
  436. bool operator ==(const wxString& value) const;
  437. bool operator ==(const wxChar* value) const;
  438. bool operator ==(wxChar value) const;
  439. bool operator ==(long value) const;
  440. bool operator ==(bool value) const;
  441. bool operator ==(double value) const;
  442. bool operator ==(wxLongLong value) const;
  443. bool operator ==(wxULongLong value) const;
  444. bool operator ==(void* value) const;
  445. bool operator ==(wxObject* value) const;
  446. bool operator ==(const wxVariantList& value) const;
  447. bool operator ==(const wxArrayString& value) const;
  448. bool operator ==(const wxDateTime& value) const;
  449. //@}
  450. //@{
  451. /**
  452. Operators for implicit conversion, using appropriate getter member
  453. function.
  454. */
  455. double operator double() const;
  456. long operator long() const;
  457. wxLongLong operator wxLongLong() const;
  458. wxULongLong operator wxULongLong() const;
  459. //@}
  460. /**
  461. Operator for implicit conversion to a pointer to a void, using
  462. GetVoidPtr().
  463. */
  464. void* operator void*() const;
  465. /**
  466. Operator for implicit conversion to a wxChar, using GetChar().
  467. */
  468. char operator wxChar() const;
  469. /**
  470. Operator for implicit conversion to a pointer to a wxDateTime, using
  471. GetDateTime().
  472. */
  473. void* operator wxDateTime() const;
  474. /**
  475. Operator for implicit conversion to a string, using MakeString().
  476. */
  477. wxString operator wxString() const;
  478. };
  479. /**
  480. @class wxVariantData
  481. The wxVariantData class is used to implement a new type for wxVariant.
  482. Derive from wxVariantData, and override the pure virtual functions.
  483. wxVariantData is @ref overview_refcount "reference counted", but you don't
  484. normally have to care about this, as wxVariant manages the count
  485. automatically. However, in case your application needs to take ownership of
  486. wxVariantData, be aware that the object is created with a reference count
  487. of 1, and passing it to wxVariant will not increase this. In other words,
  488. IncRef() needs to be called only if you both take ownership of
  489. wxVariantData and pass it to a wxVariant. Also note that the destructor is
  490. protected, so you can never explicitly delete a wxVariantData instance.
  491. Instead, DecRef() will delete the object automatically when the reference
  492. count reaches zero.
  493. @library{wxbase}
  494. @category{data}
  495. @see wxVariant, wxGetVariantCast()
  496. */
  497. class wxVariantData : public wxObjectRefData
  498. {
  499. public:
  500. /**
  501. Default constructor.
  502. */
  503. wxVariantData();
  504. /**
  505. This function can be overridden to clone the data. You must implement
  506. this function in order for wxVariant::Unshare() to work for your data.
  507. This function is implemented for all built-in data types.
  508. */
  509. virtual wxVariantData* Clone() const;
  510. /**
  511. Decreases reference count. If the count reaches zero, the object is
  512. automatically deleted.
  513. @note The destructor of wxVariantData is protected, so delete cannot be
  514. used as normal. Instead, DecRef() should be called.
  515. */
  516. void DecRef();
  517. /**
  518. Returns @true if this object is equal to @a data.
  519. */
  520. virtual bool Eq(wxVariantData& data) const = 0;
  521. /**
  522. Converts value to wxAny, if possible. Return @true if successful.
  523. */
  524. virtual bool GetAny(wxAny* any) const;
  525. /**
  526. Returns the string type of the data.
  527. */
  528. virtual wxString GetType() const = 0;
  529. /**
  530. If the data is a wxObject returns a pointer to the objects wxClassInfo
  531. structure, if the data isn't a wxObject the method returns @NULL.
  532. */
  533. virtual wxClassInfo* GetValueClassInfo();
  534. /**
  535. Increases reference count. Note that initially wxVariantData has
  536. reference count of 1.
  537. */
  538. void IncRef();
  539. /**
  540. Reads the data from @a stream.
  541. */
  542. virtual bool Read(istream& stream);
  543. /**
  544. Reads the data from @a string.
  545. */
  546. virtual bool Read(wxString& string);
  547. /**
  548. Writes the data to @a stream.
  549. */
  550. virtual bool Write(ostream& stream) const;
  551. /**
  552. Writes the data to @a string.
  553. */
  554. virtual bool Write(wxString& string) const;
  555. };
  556. // ============================================================================
  557. // Global functions/macros
  558. // ============================================================================
  559. /** @addtogroup group_funcmacro_rtti */
  560. //@{
  561. /**
  562. This macro returns a pointer to the data stored in @a var (wxVariant) cast
  563. to the type @a classname if the data is of this type (the check is done
  564. during the run-time) or @NULL otherwise.
  565. @header{wx/variant.h}
  566. @see @ref overview_rtti, wxDynamicCast()
  567. */
  568. #define wxGetVariantCast(var, classname)
  569. //@}