SophiaFramework UNIVERSE 5.3 |
This class has the reference counting mechanism which is similar to the mechanism that the SFYResponder class and the SFBBase class have.
This class object works as a smart pointer by becoming a parameter of the SFXRefObjectPointer class.
The SFMTYPEDEFREFOBJECT macro defines class name aliases (ClassNameSmp etc.) of SFXRefObjectPointer<ClassName>, and help you use the smart pointer feature easily.
Example 780. Example code of a user-defined reference counting object class which inherits the SFORefObject class.
// Type alias macro of the reference counting object class name. SFMTYPEDEFREFOBJECT(MyRefObject) class MyRefObject: public SFORefObject { // Macro to prohibit the developer from copying this instance SFMSEALCOPY(MyRefObject) // Macro to specify the inheritance relation from the SFORefObject class to this class SFMREFOBJECTINSTANTIATEONE(MyRefObject, SFORefObject) public: // Generate a MyRefObject class smart pointer. static MyRefObjectSmp NewInstance (SFCErrorPtr exception = null); protected: explicit MyRefObject (Void); virtual ~MyRefObject (Void); // Initialization function which has the possiblity to raise an error virtual SFCError Initialize (Void); }; // Constructor MyRefObject::MyRefObject(Void) { } // Destructor MyRefObject::~MyRefObject(Void) { } // Generate a MyRefObject class smart pointer MyRefObjectSmp MyRefObject::NewInstance(SFCErrorPtr exception) { return static_pointer_cast<MyRefObject>(Factory(::new MyRefObject, exception)); } // Initialization function which has the possiblity to raise an error // This function is called by the Factory() function SFCError MyRefObject::Initialize(Void) { SFCError error; // Call the parent's initialization function if ((error = SFORefObject::Initialize()) == SFERR_NO_ERROR) { // MyRefObject specific initialization error = // (..ommited..); } return error; }
Constructor/Destructor |
---|
SFORefObject( Void ) Constructor of the SFORefObject class.
|
~SFORefObject( Void ) Destructor of the SFORefObject class.
|
Protected Functions | |
---|---|
static SFORefObjectSmp |
Factory(
SFORefObjectPtr object
, SFCErrorPtr exception
) This function is used to implement the NewInstance function.
|
SFCError |
Initialize( Void ) Make the initialization which may raise an error.
|
[ protected, explicit ] SFORefObject(Void);
[ protected, virtual ] ~SFORefObject(Void);
[ protected, static ] SFORefObjectSmp Factory( SFORefObjectPtr object // instance SFCErrorPtr exception // error value );
Set the instance created newly using the new operator.
Return the error value generated internally.
This function is used to implement the NewInstance function which is necessary to define the concrete class of a object class newly.
With this function, the NewInstance function for the concrete class of a object class can be implemented easily.
Note | |
---|---|
The SFORefObject::Factory() function makes the initialization by internal calling the SFORefObject::Initialize function. Initialization code which may raise an error should be written in the SFORefObject::Initialize function. |
static_pointer_cast operator | |
---|---|
Since the SFORefObject::Factory function returns the value of the SFORefObjectSmp type, it is necessary to downcast it into the type of the newly created object class with the static_pointer_cast operator. |
The following is the code to implement the NewInstance() function and the Initialize() function of the MyTableCellReactor class.
/*public */MyTableCellReactor MyTableCellReactor::NewInstance(SFCErrorPtr exception) { return static_pointer_cast<MyTableCellReactor>(Factory(::new MyTableCellReactor, exception)); }// MyTableCellReactor::NewInstance // /*protected virtual */SFCError MyTableCellReactor::Initialize(Void) { SFCError error; // Call the parent's initialize function if ((error = SFOTableCellReactor::Initialize()) == SFERR_NO_ERROR) { // Initialization code which may raise an error // ..(ommited).. } return error; }// MyTableCellReactor::Initialize //
Internal implementation of the SFORefObject::Factory function is as follows:
/*protected static */SFORefObjectSmp SFORefObject::Factory(SFORefObjectPtr object, SFCErrorPtr exception) { SFCError error(SFERR_NO_ERROR); SFORefObjectSmp result; if (object != null) { if ((error = object->Initialize()) == SFERR_NO_ERROR) { result.Set(object, false); } } else { error = SFERR_NO_MEMORY; } if (exception != null) { *exception = error; } return result; }// SFORefObject::Factory //
[ protected, virtual ] SFCError Initialize(Void);
This function makes the initialization which may raise an error.
This function is called by the SFORefObject::Factory function.
If a concrete class specific initialization code may raise an error, you must override this function. Usually, the parent's Initialize() function should be called first in the implementation.
See the example of the SFORefObject::Factory function.
Copyright(c) 2002 - 2024 Sophia Cradle Incorporated All Rights Reserved. |