[Orxonox-commit 592] r3124 - branches/pch/src/core
rgrieder at orxonox.net
rgrieder at orxonox.net
Tue Jun 9 14:42:19 CEST 2009
Author: rgrieder
Date: 2009-06-09 14:42:19 +0200 (Tue, 09 Jun 2009)
New Revision: 3124
Modified:
branches/pch/src/core/Game.cc
branches/pch/src/core/Game.h
Log:
Using smart pointers in Game to manage the little tree. It's merely a test case.
Modified: branches/pch/src/core/Game.cc
===================================================================
--- branches/pch/src/core/Game.cc 2009-06-09 11:31:32 UTC (rev 3123)
+++ branches/pch/src/core/Game.cc 2009-06-09 12:42:19 UTC (rev 3124)
@@ -36,6 +36,7 @@
#include <exception>
#include <cassert>
+#include <boost/weak_ptr.hpp>
#include "util/Debug.h"
#include "util/Exception.h"
@@ -50,15 +51,18 @@
namespace orxonox
{
+ using boost::shared_ptr;
+ using boost::weak_ptr;
+
static void stop_game()
{ Game::getInstance().stop(); }
SetConsoleCommandShortcutExternAlias(stop_game, "exit");
struct _CoreExport GameStateTreeNode
{
- GameState* state_;
- GameStateTreeNode* parent_;
- std::vector<GameStateTreeNode*> children_;
+ GameState* state_;
+ weak_ptr<GameStateTreeNode> parent_;
+ std::vector<shared_ptr<GameStateTreeNode> > children_;
};
std::map<std::string, GameState*> Game::allStates_s;
@@ -73,9 +77,6 @@
assert(singletonRef_s == 0);
singletonRef_s = this;
- this->rootStateNode_ = 0;
- this->activeStateNode_ = 0;
-
this->abort_ = false;
// reset statistics
@@ -105,10 +106,6 @@
// Destroy pretty much everyhting left
delete this->core_;
- // Delete all the created nodes
- for (std::vector<GameStateTreeNode*>::const_iterator it = this->allStateNodes_.begin(); it != this->allStateNodes_.end(); ++it)
- delete *it;
-
delete this->gameClock_;
assert(singletonRef_s);
@@ -171,8 +168,8 @@
while (this->requestedStateNodes_.size() > 1)
{
// Note: this->requestedStateNodes_.front() is the currently active state node
- std::vector<GameStateTreeNode*>::iterator it = this->requestedStateNodes_.begin() + 1;
- if (*it == this->activeStateNode_->parent_)
+ std::vector<shared_ptr<GameStateTreeNode> >::iterator it = this->requestedStateNodes_.begin() + 1;
+ if (*it == this->activeStateNode_->parent_.lock())
this->unloadState(this->activeStateNode_->state_);
else // has to be child
this->loadState((*it)->state_);
@@ -225,7 +222,7 @@
// UNLOAD all remaining states
while (!this->activeStates_.empty())
this->unloadState(this->activeStates_.back());
- this->activeStateNode_ = 0;
+ this->activeStateNode_.reset();
this->requestedStateNodes_.clear();
}
@@ -250,10 +247,10 @@
if (state == NULL || this->activeStateNode_ == NULL)
return;
- GameStateTreeNode* requestedNode = 0;
+ shared_ptr<GameStateTreeNode> requestedNode;
// this->requestedStateNodes_.back() is the currently active state
- GameStateTreeNode* lastRequestedNode = this->requestedStateNodes_.back();
+ shared_ptr<GameStateTreeNode> lastRequestedNode = this->requestedStateNodes_.back();
// Already the active node?
if (state == lastRequestedNode->state_)
@@ -273,12 +270,12 @@
}
// Check parent and all its grand parents
- GameStateTreeNode* currentNode = lastRequestedNode;
+ shared_ptr<GameStateTreeNode> currentNode = lastRequestedNode;
while (requestedNode == NULL && currentNode != NULL)
{
if (currentNode->state_ == state)
requestedNode = currentNode;
- currentNode = currentNode->parent_;
+ currentNode = currentNode->parent_.lock();
}
if (requestedNode == NULL)
@@ -296,8 +293,8 @@
void Game::popState()
{
- if (this->activeStateNode_ != NULL && this->requestedStateNodes_.back()->parent_)
- this->requestState(this->requestedStateNodes_.back()->parent_->state_->getName());
+ if (this->activeStateNode_ != NULL && this->requestedStateNodes_.back()->parent_.lock())
+ this->requestState(this->requestedStateNodes_.back()->parent_.lock()->state_->getName());
else
COUT(2) << "Warning: Could not pop GameState. Ignoring." << std::endl;
}
@@ -332,7 +329,7 @@
str.substr(startPos, pos - startPos), indentation));
}
unsigned int currentLevel = 0;
- GameStateTreeNode* currentNode = 0;
+ shared_ptr<GameStateTreeNode> currentNode;
for (std::vector<std::pair<std::string, unsigned> >::const_iterator it = stateStrings.begin(); it != stateStrings.end(); ++it)
{
std::string newStateName = it->first;
@@ -345,30 +342,27 @@
// root
if (this->rootStateNode_ != NULL)
ThrowException(GameState, "No two root GameStates are allowed!");
- GameStateTreeNode* newNode = new GameStateTreeNode;
- this->allStateNodes_.push_back(newNode);
+ shared_ptr<GameStateTreeNode> newNode(new GameStateTreeNode);
newNode->state_ = newState;
- newNode->parent_ = 0;
this->rootStateNode_ = newNode;
currentNode = this->rootStateNode_;
}
else if (currentNode)
{
- GameStateTreeNode* newNode = new GameStateTreeNode;
- this->allStateNodes_.push_back(newNode);
+ shared_ptr<GameStateTreeNode> newNode(new GameStateTreeNode);
newNode->state_ = newState;
if (newLevel < currentLevel)
{
// Get down the hierarchy
do
- currentNode = currentNode->parent_;
+ currentNode = currentNode->parent_.lock();
while (newLevel < --currentLevel);
}
if (newLevel == currentLevel)
{
// same level
newNode->parent_ = currentNode->parent_;
- newNode->parent_->children_.push_back(newNode);
+ newNode->parent_.lock()->children_.push_back(newNode);
}
else if (newLevel == currentLevel + 1)
{
Modified: branches/pch/src/core/Game.h
===================================================================
--- branches/pch/src/core/Game.h 2009-06-09 11:31:32 UTC (rev 3123)
+++ branches/pch/src/core/Game.h 2009-06-09 12:42:19 UTC (rev 3124)
@@ -40,6 +40,7 @@
#include <list>
#include <map>
#include <vector>
+#include <boost/shared_ptr.hpp>
#include "OrxonoxClass.h"
/**
@@ -105,10 +106,9 @@
void unloadState(GameState* state);
std::vector<GameState*> activeStates_;
- GameStateTreeNode* rootStateNode_;
- GameStateTreeNode* activeStateNode_;
- std::vector<GameStateTreeNode*> requestedStateNodes_;
- std::vector<GameStateTreeNode*> allStateNodes_;
+ boost::shared_ptr<GameStateTreeNode> rootStateNode_;
+ boost::shared_ptr<GameStateTreeNode> activeStateNode_;
+ std::vector<boost::shared_ptr<GameStateTreeNode> > requestedStateNodes_;
Core* core_;
Clock* gameClock_;
More information about the Orxonox-commit
mailing list