Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3124


Ignore:
Timestamp:
Jun 9, 2009, 2:42:19 PM (15 years ago)
Author:
rgrieder
Message:

Using smart pointers in Game to manage the little tree. It's merely a test case.

Location:
code/branches/pch/src/core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/pch/src/core/Game.cc

    r3084 r3124  
    3737#include <exception>
    3838#include <cassert>
     39#include <boost/weak_ptr.hpp>
    3940
    4041#include "util/Debug.h"
     
    5152namespace orxonox
    5253{
     54    using boost::shared_ptr;
     55    using boost::weak_ptr;
     56
    5357    static void stop_game()
    5458        { Game::getInstance().stop(); }
     
    5761    struct _CoreExport GameStateTreeNode
    5862    {
    59         GameState*                      state_;
    60         GameStateTreeNode*              parent_;
    61         std::vector<GameStateTreeNode*> children_;
     63        GameState* state_;
     64        weak_ptr<GameStateTreeNode> parent_;
     65        std::vector<shared_ptr<GameStateTreeNode> > children_;
    6266    };
    6367
     
    7377        assert(singletonRef_s == 0);
    7478        singletonRef_s = this;
    75 
    76         this->rootStateNode_ = 0;
    77         this->activeStateNode_ = 0;
    7879
    7980        this->abort_ = false;
     
    105106        // Destroy pretty much everyhting left
    106107        delete this->core_;
    107 
    108         // Delete all the created nodes
    109         for (std::vector<GameStateTreeNode*>::const_iterator it = this->allStateNodes_.begin(); it != this->allStateNodes_.end(); ++it)
    110             delete *it;
    111108
    112109        delete this->gameClock_;
     
    172169            {
    173170                // Note: this->requestedStateNodes_.front() is the currently active state node
    174                 std::vector<GameStateTreeNode*>::iterator it = this->requestedStateNodes_.begin() + 1;
    175                 if (*it == this->activeStateNode_->parent_)
     171                std::vector<shared_ptr<GameStateTreeNode> >::iterator it = this->requestedStateNodes_.begin() + 1;
     172                if (*it == this->activeStateNode_->parent_.lock())
    176173                    this->unloadState(this->activeStateNode_->state_);
    177174                else // has to be child
     
    226223        while (!this->activeStates_.empty())
    227224            this->unloadState(this->activeStates_.back());
    228         this->activeStateNode_ = 0;
     225        this->activeStateNode_.reset();
    229226        this->requestedStateNodes_.clear();
    230227    }
     
    251248            return;
    252249
    253         GameStateTreeNode* requestedNode = 0;
     250        shared_ptr<GameStateTreeNode> requestedNode;
    254251
    255252        // this->requestedStateNodes_.back() is the currently active state
    256         GameStateTreeNode* lastRequestedNode = this->requestedStateNodes_.back();
     253        shared_ptr<GameStateTreeNode> lastRequestedNode = this->requestedStateNodes_.back();
    257254
    258255        // Already the active node?
     
    274271
    275272        // Check parent and all its grand parents
    276         GameStateTreeNode* currentNode = lastRequestedNode;
     273        shared_ptr<GameStateTreeNode> currentNode = lastRequestedNode;
    277274        while (requestedNode == NULL && currentNode != NULL)
    278275        {
    279276            if (currentNode->state_ == state)
    280277                requestedNode = currentNode;
    281             currentNode = currentNode->parent_;
     278            currentNode = currentNode->parent_.lock();
    282279        }
    283280
     
    297294    void Game::popState()
    298295    {
    299         if (this->activeStateNode_ != NULL && this->requestedStateNodes_.back()->parent_)
    300             this->requestState(this->requestedStateNodes_.back()->parent_->state_->getName());
     296        if (this->activeStateNode_ != NULL && this->requestedStateNodes_.back()->parent_.lock())
     297            this->requestState(this->requestedStateNodes_.back()->parent_.lock()->state_->getName());
    301298        else
    302299            COUT(2) << "Warning: Could not pop GameState. Ignoring." << std::endl;
     
    333330        }
    334331        unsigned int currentLevel = 0;
    335         GameStateTreeNode* currentNode = 0;
     332        shared_ptr<GameStateTreeNode> currentNode;
    336333        for (std::vector<std::pair<std::string, unsigned> >::const_iterator it = stateStrings.begin(); it != stateStrings.end(); ++it)
    337334        {
     
    346343                if (this->rootStateNode_ != NULL)
    347344                    ThrowException(GameState, "No two root GameStates are allowed!");
    348                 GameStateTreeNode* newNode = new GameStateTreeNode;
    349                 this->allStateNodes_.push_back(newNode);
     345                shared_ptr<GameStateTreeNode> newNode(new GameStateTreeNode);
    350346                newNode->state_ = newState;
    351                 newNode->parent_ = 0;
    352347                this->rootStateNode_ = newNode;
    353348                currentNode = this->rootStateNode_;
     
    355350            else if (currentNode)
    356351            {
    357                 GameStateTreeNode* newNode = new GameStateTreeNode;
    358                 this->allStateNodes_.push_back(newNode);
     352                shared_ptr<GameStateTreeNode> newNode(new GameStateTreeNode);
    359353                newNode->state_ = newState;
    360354                if (newLevel < currentLevel)
     
    362356                    // Get down the hierarchy
    363357                    do
    364                         currentNode = currentNode->parent_;
     358                        currentNode = currentNode->parent_.lock();
    365359                    while (newLevel < --currentLevel);
    366360                }
     
    369363                    // same level
    370364                    newNode->parent_ = currentNode->parent_;
    371                     newNode->parent_->children_.push_back(newNode);
     365                    newNode->parent_.lock()->children_.push_back(newNode);
    372366                }
    373367                else if (newLevel == currentLevel + 1)
  • code/branches/pch/src/core/Game.h

    r3084 r3124  
    4141#include <map>
    4242#include <vector>
     43#include <boost/shared_ptr.hpp>
    4344#include "OrxonoxClass.h"
    4445
     
    106107
    107108        std::vector<GameState*>         activeStates_;
    108         GameStateTreeNode*              rootStateNode_;
    109         GameStateTreeNode*              activeStateNode_;
    110         std::vector<GameStateTreeNode*> requestedStateNodes_;
    111         std::vector<GameStateTreeNode*> allStateNodes_;
     109        boost::shared_ptr<GameStateTreeNode> rootStateNode_;
     110        boost::shared_ptr<GameStateTreeNode> activeStateNode_;
     111        std::vector<boost::shared_ptr<GameStateTreeNode> > requestedStateNodes_;
    112112
    113113        Core*                           core_;
Note: See TracChangeset for help on using the changeset viewer.