reference counting
why you shouldn't care about it
why you shouldn't care about itmany wxwidgets objects use a technique known as reference counting, also known as copy on write (cow). this means that when an object is assigned to another, no copying really takes place: only the reference count on the shared object data is incremented and both objects share the same data (a very fast operation). but as soon as one of the two (or more) objects is modified, the data has to be copied because the changes to one of the objects shouldn't be seen in the others. as data copying only happens when the object is written to, this is known as cow. what is important to understand is that all this happens absolutely transparently to the class users and that whether an object is shared or not is not seen from the outside of the class - in any case, the result of any operation on it is the same.
object comparisonthe == and != operators of wxwidgets cow objects always do a deep comparison. this means that the equality operator will return true if two objects are identic and not only if they share the same data. note that wxwidgets follows the stl philosophy: when a comparison operator cannot be implemented efficiently (like for e.g. wximage's == operator which would need to compare pixel-by-pixel the entire image's data), it's not implemented at all. that's why not all reference-counted wxwidgets classes provide comparison operators. also note that if you only need to do a shallow comparison between two wxobject-derived classes, you should not use the == and != operators but rather the wxobject::issameas function.
object destructionwhen a cow object destructor is called, it may not delete the data: if it's shared, the destructor will just decrement the shared data's reference count without destroying it. only when the destructor of the last object owning the data is called, the data is really destroyed. as for all other cow-things, this happens transparently to the class users so that you shouldn't care about it.
list of reference-counted wxwidgets classesthe following classes in wxwidgets have efficient (i.e. fast) assignment operators and copy constructors since they are reference-counted:
wxacceleratortable note that the list above reports the objects which are reference-counted in all ports of wxwidgets; some ports may use this tecnique also for other classes. make your own reference-counted classreference counting can be implemented easily using wxobject and wxobjectrefdata classes. first, derive a new class from wxobjectrefdata and put there the memory-consuming data. then derive a new class from wxobject and implement there the public interface which will be seen by the user of your class. you'll probably want to add a function to your class which does the cast from wxobjectrefdata to your class-specific shared data; e.g.:
myclassrefdata *getdata() const { return wx_static_cast(myclassrefdata*, m_refdata); }in fact, all times you'll need to read the data from your wxobject-derived class, you'll need to call such function. very important, all times you need to actually modify the data placed inside your wxobject-derived class, you must first call the wxobject::unshare function to be sure that the modifications won't affect other instances which are eventually sharing your object's data.
|