Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/core/NamespaceNode.cc @ 895

Last change on this file since 895 was 895, checked in by landauf, 16 years ago
  • added new XMLPortParam template to specify the template arguments / to specify the functor
  • implemented the Namespace object and
  • added a NamespaceNode class that helps keeping track of the existing namespaces in a level

this is not yet tested nor fully included in the loader nor usefull (as we have no scripts at the moment), but i think it's a good start.

File size: 4.0 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28#include "NamespaceNode.h"
29#include "Debug.h"
30
31namespace orxonox
32{
33    NamespaceNode::NamespaceNode(const std::string& name, NamespaceNode* parent)
34    {
35        this->name_ = name;
36        this->parent_ = parent;
37        this->bRoot_ = false;
38        this->bHidden_ = false;
39    }
40
41    NamespaceNode::~NamespaceNode()
42    {
43        for (std::map<std::string, NamespaceNode*>::iterator it = this->subnodes_.begin(); it != this->subnodes_.end(); )
44            delete ((*(it++)).second);
45    }
46
47    std::set<NamespaceNode*> NamespaceNode::getNodeRelative(const std::string& name)
48    {
49        std::set<NamespaceNode*> nodes;
50
51        if (name == "")
52        {
53            nodes.insert(this);
54        }
55        else
56        {
57            unsigned int pos = name.find("::");
58            std::string firstPart = name.substr(0, pos);
59            std::string secondPart = name.substr(pos + 2, std::string::npos);
60
61            if (firstPart == "..")
62            {
63                if (this->bRoot_)
64                {
65                    COUT(2) << "Warning: Can't go to enclosing namespace with '..' operator in namespace " << this->name_ << ", namespace is root." << std::endl;
66                    nodes = this->getNodeRelative(secondPart);
67                }
68                else
69                {
70                    nodes = this->parent_->getNodeRelative(secondPart);
71                }
72            }
73            else if (name.find('@') != 0)
74            {
75                std::map<std::string, NamespaceNode*>::iterator it = this->subnodes_.find(firstPart);
76                if (it == this->subnodes_.end())
77                    it = this->subnodes_.insert(this->subnodes_.begin(), std::pair<std::string, NamespaceNode*>(firstPart, new NamespaceNode(firstPart, this)));
78
79                if ((*it).second->isHidden())
80                    COUT(2) << "Warning: Subnamespace '" << firstPart << "' in namespace '" << this->name_ << "' is hidden and can't be accessed." << std::endl;
81                else
82                    nodes = (*it).second->getNodeRelative(secondPart);
83            }
84            else
85            {
86                for (std::map<std::string, NamespaceNode*>::iterator it = this->subnodes_.begin(); it != this->subnodes_.end(); ++it)
87                {
88                    if ((*it).first.find(firstPart) == ((*it).first.size() - firstPart.size()))
89                    {
90                        std::set<NamespaceNode*> temp2 = (*it).second->getNodeRelative(secondPart);
91                        nodes.insert(temp2.begin(), temp2.end());
92                    }
93                }
94            }
95        }
96
97        return nodes;
98    }
99
100    bool NamespaceNode::includes(const NamespaceNode* ns) const
101    {
102        if (ns == this)
103        {
104            return true;
105        }
106        else
107        {
108            for (std::map<std::string, NamespaceNode*>::const_iterator it = this->subnodes_.begin(); it != this->subnodes_.end(); ++it)
109                if ((*it).second->includes(ns))
110                    return true;
111        }
112
113        return false;
114    }
115}
Note: See TracBrowser for help on using the repository browser.