C12Adapter Opensource C++ Interface
MObjectPropertySavior Class Reference

Class that helps preserve a property value outside a local C++ scope. More...

Public Member Functions

 MObjectPropertySavior (MObject *obj, const MStdString &propertyName)
 Constructor that accepts the object and the name of the property that has to be saved. More...
 
 MObjectPropertySavior (MObject *obj, const MStdString &propertyName, const MVariant &value)
 Constructor that accepts the object, the property name, and the new value of the property. More...
 
 ~MObjectPropertySavior ()
 Destructor that restores the value of the property to its original value. More...
 

Detailed Description

Class that helps preserve a property value outside a local C++ scope.

The constructor saves the given property by name, and the destructor restores it. The type of the variable to manipulate shall be assignable, or a compile error will result from the usage attempt. Typical use case:

// assume Baud here is equal to 19200
{
MObjectPropertySavior baudSavior(channel, "Baud", 9600); // here the previous Baud is saved, and set to a new value 9600
... at this point the baud can be manipulated freely, if necessary
}
// here the value of Baud will be restored to the previous value 19200 no matter how the above scope was exited

Exception behavior has these specifics:

  • If an exception takes place in the constructor at getting the property value, no attempt will be made at restoration of such value in the destructor, as obviously, it is assumed the value need not be restored.
  • If an exception takes place in the constructor later, at setting of property to the new value, the restoration attempt will be attempted at destructor, as it is assumed it is not known whether the property is consistent.
  • All exceptions in destructor are silenced, this is when the restoration attempt is made. Therefore, strictly speaking, the preservation of the value is not guaranteed.

Constructor & Destructor Documentation

MObjectPropertySavior::MObjectPropertySavior ( MObject obj,
const MStdString propertyName 
)

Constructor that accepts the object and the name of the property that has to be saved.

The constructor will fetch the property value by name and store it for later restoration in destructor.

Parameters
objObject for which the property has to be preserve.
propertyNameName of the property to preserve. Such property should exist in object, and be read/write.
MObjectPropertySavior::MObjectPropertySavior ( MObject obj,
const MStdString propertyName,
const MVariant value 
)

Constructor that accepts the object, the property name, and the new value of the property.

The constructor will store the address of the value and the value itself, so it can be restored in the destructor. Then it will assign a new value given as parameter. This constructor is a convenient shortcut of the following:

MValueSavior<MStdString> savior(myString);
myString = "new value";

which can be written in a new line:

MValueSavior<MStdString> savior(myString, "new value");

The constructor will fetch the property value by name and store it for later restoration in destructor.

Parameters
objObject for which the property has to be preserve.
propertyNameName of the property to preserve. Such property should exist in object, and be read/write.
valueNew value to be assigned to object property, should satisfy the requirements for such property.
MObjectPropertySavior::~MObjectPropertySavior ( )

Destructor that restores the value of the property to its original value.

As value savior is often created on the stack there is no necessity to call its destructor directly or by means of operator delete.