User:Ccw: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Proposed solution to issue 4377)
Line 4: Line 4:


=Some class constructors and destructors are too complex=
=Some class constructors and destructors are too complex=
===Proposed solution to [issue http://bugs.mtasa.com/view.php?id=4377]===
==Proposed solution to [issue http://bugs.mtasa.com/view.php?id=4377]==
Move code out of constructors and destructors into functions that are called post-construction and pre-destruction respectively.
==Example of solution when applied to CClientEntity and CClientColModel:==
==Example of solution when applied to CClientEntity and CClientColModel:==


Line 31: Line 32:
{
{
protected:
protected:
                                     CClientEntity          ( ElementID ID );
                                     CClientEntity          ( void ) {}
    virtual                        ~CClientEntity         ( void );
                                  ~CClientEntity           ( void ) {}
     virtual void                   PostConstruct          ( void );
     void                           PostConstruct          ( ElementID ID );
     virtual void                    PreDestroy              ( void );
     virtual void                    PreDestroy              ( void );
public:
public:
Line 40: Line 41:




void CClientEntity::PostConstruct ()
void CClientEntity::PostConstruct ( ElementID ID )
{
{
     // Code from CClientEntity constructor moved here
     // Code from CClientEntity constructor moved here
Line 64: Line 65:
class CClientColModel: public CClientEntity
class CClientColModel: public CClientEntity
{
{
public:
protected:
                                     CClientColModel        ( class CClientManager* pManager, ElementID ID );
                                     CClientColModel        ( void ) {}
                                     ~CClientColModel        ( void );
                                     ~CClientColModel        ( void ) {}
     virtual void                   PostConstruct          ( void );
     void                           PostConstruct          ( class CClientManager* pManager, ElementID ID );
     virtual void                    PreDestroy              ( void );
     virtual void                    PreDestroy              ( void );
public:
public:
Line 74: Line 75:




void CClientColModel::PostConstruct ()
void CClientColModel::PostConstruct ( CClientStreamer * pStreamer, ElementID ID )
{
{
     __super::PostConstruct ();
     CClientEntity::PostConstruct ( ID );
     // Code from CClientColModel constructor moved here
     // Code from CClientColModel constructor moved here
}
}
Line 89: Line 90:
{
{
     CClientColModel* pElement = new CClientColModel ( pStreamer, ID );
     CClientColModel* pElement = new CClientColModel ( pStreamer, ID );
     pElement->PostConstruct ()
     pElement->PostConstruct ( pStreamer, ID )
     return pElement;
     return pElement;
}
}
Line 102: Line 103:
delete pElement;
delete pElement;
</syntaxhighlight>
</syntaxhighlight>
is replaced with
should be replaced with
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
CClientColModel::NewCClientColModel ( pStreamer, ID );
CClientColModel::NewCClientColModel ( pStreamer, ID );
pElement->Delete ();
pElement->Delete ();
</syntaxhighlight>
</syntaxhighlight>

Revision as of 14:59, 1 June 2009

Coder.gif This user is an MTA developer


Some class constructors and destructors are too complex

Proposed solution to [issue http://bugs.mtasa.com/view.php?id=4377]

Move code out of constructors and destructors into functions that are called post-construction and pre-destruction respectively.

Example of solution when applied to CClientEntity and CClientColModel:

Current base and concrete class declaration:

class CClientEntity
{
public:
                                    CClientEntity           ( ElementID ID );
    virtual                         ~CClientEntity          ( void );
...


class CClientColModel: public CClientEntity
{
public:
                                    CClientColModel         ( class CClientManager* pManager, ElementID ID );
                                    ~CClientColModel        ( void );
...


Proposed base class declaration and definition:

class CClientEntity
{
protected:
                                    CClientEntity           ( void ) {}
                                   ~CClientEntity           ( void ) {}
    void                            PostConstruct           ( ElementID ID );
    virtual void                    PreDestroy              ( void );
public:
    void                            Delete                  ( void );
...


void CClientEntity::PostConstruct ( ElementID ID )
{
    // Code from CClientEntity constructor moved here
}

void CClientEntity::PreDestroy ()
{
    // Code from CClientEntity destructor moved here
}

void CClientEntity::Delete ( void )
{
    PreDestroy ();
    delete this;
}



Proposed concrete class declaration and definition:

class CClientColModel: public CClientEntity
{
protected:
                                    CClientColModel         ( void ) {}
                                    ~CClientColModel        ( void ) {}
    void                            PostConstruct           ( class CClientManager* pManager, ElementID ID );
    virtual void                    PreDestroy              ( void );
public:
    static CClientColModel*         NewCClientColModel      ( CClientStreamer * pStreamer, ElementID ID );
...


void CClientColModel::PostConstruct ( CClientStreamer * pStreamer, ElementID ID )
{
    CClientEntity::PostConstruct ( ID );
    // Code from CClientColModel constructor moved here
}

void CClientColModel::PreDestroy ()
{
    // Code from CClientColModel destructor moved here
    __super::PreDestroy ();
}

CClientColModel* CClientColModel::NewCClientColModel ( CClientStreamer * pStreamer, ElementID ID )
{
    CClientColModel* pElement = new CClientColModel ( pStreamer, ID );
    pElement->PostConstruct ( pStreamer, ID )
    return pElement;
}


Other changes required

Any occurrence of

new CClientColModel ( pStreamer, ID );
delete pElement;

should be replaced with

CClientColModel::NewCClientColModel ( pStreamer, ID );
pElement->Delete ();