A mobile holds a pointer that ensures (as much as possible in C++) that only one mobile can hold that pointer at any one time. A pointer can be transferred between two mobiles using the copy constructor or assignment. This is a transfer, not a copy; the source of the transfer becomes blank after the operation (its pointer is set to NULL). If you assign to a mobile that is already holding a non-NULL pointer, the object is deleted, and then the transfer takes place as described. If the mobile holds a non-NULL pointer when it is destroyed, the object will be deleted.
Communicating mobiles via channels will maintain these semantics (as channels use an object's assignment operator) of mobiles and hence mobiles provide a simple means of safely passing (pointers to) large objects through channels, while maintaining the CREW principle (by making it EREW! Exclusive Read and Exclusive Write).
The behaviour of csp::Mobile is very similar to std::auto_ptr. There is one important difference, however, which allows csp::Mobile to be communicated over channels, whereas std::auto_ptr could not. csp::Mobile's assignment operator takes a const csp::Mobile as its argument, whereas std::auto_ptr takes a [non-const] std::auto_ptr. Because the items written to a channel must be const, std::auto_ptr could not be sent over a channel, but csp::Mobile can. csp::Mobile uses the mutable keyword to break the const semantics -- it's not pretty, but it's effective.
Mobiles are covered in the guide.
Public Member Functions | |
void | blank () |
Blanks the mobile by deleting the data and setting pointer to NULL. | |
Mobile< DATA_TYPE > | clone () const |
Clones the mobile. | |
DATA_TYPE * | get () const |
Provides a way to get the data pointer inside the mobile. | |
Mobile (const Mobile< DATA_TYPE > &mob) | |
Copy constructor. | |
Mobile (DATA_TYPE *const _data=NULL) | |
Constructs a mobile with the given data. | |
DATA_TYPE & | operator * () const |
Allows this class to be used in place of a pointer. | |
operator bool () const | |
Easy and neat way of checking for the mobile being empty. | |
DATA_TYPE * | operator-> () const |
Allows this class to be used in place of a pointer. | |
bool | operator< (const Mobile< DATA_TYPE > &mob) const |
See the documentation for operator==. | |
void | operator= (const Mobile< DATA_TYPE > &mob) |
Mobile assignment operator. | |
bool | operator== (const Mobile< DATA_TYPE > &mob) const |
Tests whether the objects inside two mobiles are equal. | |
~Mobile () | |
Destructor. |
csp::Mobile< DATA_TYPE >::Mobile | ( | DATA_TYPE *const | _data = NULL |
) | [inline, explicit] |
Constructs a mobile with the given data.
The data should be on the heap (allocated using new), and should not be passed to two mobiles. The data will be deleted by this class, hence it must be allocated using new. Because it is deleted by this class, it should never be used by two mobiles (they would both try to delete it) and for good practice should be passed to the mobile at its point of allocation, and used via the mobile forever after.
_data | The data for the mobile to point to |
csp::Mobile< DATA_TYPE >::Mobile | ( | const Mobile< DATA_TYPE > & | mob | ) | [inline] |
Copy constructor.
Takes the pointer of the passed mobile, and then blanks the source mobile.
mob | The mobile to assign from |
csp::Mobile< DATA_TYPE >::~Mobile | ( | ) | [inline] |
void csp::Mobile< DATA_TYPE >::blank | ( | ) | [inline] |
Blanks the mobile by deleting the data and setting pointer to NULL.
csp::Mobile< DATA_TYPE >::operator bool | ( | ) | const [inline] |
Easy and neat way of checking for the mobile being empty.
A mobile "is" true if its pointer is not NULL
void csp::Mobile< DATA_TYPE >::operator= | ( | const Mobile< DATA_TYPE > & | mob | ) | [inline] |
Mobile assignment operator.
This assignment takes the data pointer of the source mobile into this mobile, and then blanks the source mobile.
If this mobile already has some data in it, the data will be deleted during the assignment.
DATA_TYPE* csp::Mobile< DATA_TYPE >::operator-> | ( | ) | const [inline] |
Allows this class to be used in place of a pointer.
DATA_TYPE& csp::Mobile< DATA_TYPE >::operator * | ( | ) | const [inline] |
Allows this class to be used in place of a pointer.
DATA_TYPE* csp::Mobile< DATA_TYPE >::get | ( | ) | const [inline] |
Provides a way to get the data pointer inside the mobile.
This method should be used sparingly if at all. Usually, the -> operator of mobile will be sufficient (and an easier way) to use the data inside the mobile. However, sometimes you may need the pointer to the object inside the class, for example to pass to a function expected DATA_TYPE* rather than Mobile<DATA_TYPE>. You should not use this method as a way to pass the pointer around - the object inside will get deleted if this object is assigned to, so the pointer may not be valid for as long as you hope.
Mobile<DATA_TYPE> csp::Mobile< DATA_TYPE >::clone | ( | ) | const [inline] |
Clones the mobile.
This function will cause an error upon compilation unless the class DATA_TYPE is either: primitive, uses a default copy constructor, or has a copy constructor that is publicly accessible. The copy constructor defines the behaviour of the clone; the new Mobile will be a separate object, pointing to a new object copy constructed from the current data.
If the Mobile is blank (NULL), then a new blank Mobile will be returned, without error.
This mobile will not be altered by this operation.
bool csp::Mobile< DATA_TYPE >::operator== | ( | const Mobile< DATA_TYPE > & | mob | ) | const [inline] |
Tests whether the objects inside two mobiles are equal.
A first intuition about the equality of mobiles might be that it should be based on whether the pointers inside the mobiles are equal. However, the pointers should always be different, so if they were equal it would mean you are going to crash your application!
Therefore comparison of mobiles is based on a comparison of the inner types. If you wish to use comparison operators of Mobile<DATA_TYPE>, make sure those same operators are defined for DATA_TYPE.
If DATA_TYPE inherits from boost::noncopyable (or for other reasons does not have a public copy constructor) be sure to use references when implementing DATA_TYPE::operator==, to avoid the compiler trying to copy DATA_TYPE for the function call
bool csp::Mobile< DATA_TYPE >::operator< | ( | const Mobile< DATA_TYPE > & | mob | ) | const [inline] |
See the documentation for operator==.