[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "NamespaceNode.h" |
---|
| 30 | #include "Debug.h" |
---|
| 31 | |
---|
| 32 | namespace orxonox |
---|
| 33 | { |
---|
| 34 | NamespaceNode::NamespaceNode(const std::string& name, NamespaceNode* parent) |
---|
| 35 | { |
---|
| 36 | this->name_ = name; |
---|
| 37 | this->parent_ = parent; |
---|
| 38 | this->bRoot_ = false; |
---|
| 39 | this->bHidden_ = false; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | NamespaceNode::~NamespaceNode() |
---|
| 43 | { |
---|
| 44 | for (std::map<std::string, NamespaceNode*>::iterator it = this->subnodes_.begin(); it != this->subnodes_.end(); ) |
---|
| 45 | delete ((*(it++)).second); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | std::set<NamespaceNode*> NamespaceNode::getNodeRelative(const std::string& name) |
---|
| 49 | { |
---|
| 50 | std::set<NamespaceNode*> nodes; |
---|
| 51 | |
---|
| 52 | if ((name.size() == 0) || (name == "")) |
---|
| 53 | { |
---|
| 54 | nodes.insert(this); |
---|
| 55 | } |
---|
| 56 | else |
---|
| 57 | { |
---|
| 58 | unsigned int pos = name.find("::"); |
---|
| 59 | std::string firstPart = name; |
---|
| 60 | std::string secondPart; |
---|
| 61 | |
---|
| 62 | if (pos != std::string::npos) |
---|
| 63 | { |
---|
| 64 | firstPart = name.substr(0, pos); |
---|
| 65 | secondPart = name.substr(pos + 2, std::string::npos); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | if (firstPart == "..") |
---|
| 69 | { |
---|
| 70 | if (this->bRoot_) |
---|
| 71 | { |
---|
| 72 | COUT(2) << "Warning: Can't go to enclosing namespace with '..' operator in namespace " << this->name_ << ", namespace is root." << std::endl; |
---|
| 73 | nodes = this->getNodeRelative(secondPart); |
---|
| 74 | } |
---|
| 75 | else if (!this->parent_) |
---|
| 76 | { |
---|
| 77 | COUT(2) << "Warning: Can't go to enclosing namespace with '..' operator in namespace " << this->name_ << ", no parent namespace set." << std::endl; |
---|
| 78 | nodes = this->getNodeRelative(secondPart); |
---|
| 79 | } |
---|
| 80 | else |
---|
| 81 | { |
---|
| 82 | nodes = this->parent_->getNodeRelative(secondPart); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | else if (name.find('@') != 0) |
---|
| 86 | { |
---|
| 87 | std::map<std::string, NamespaceNode*>::iterator it = this->subnodes_.find(firstPart); |
---|
| 88 | if (it == this->subnodes_.end()) |
---|
| 89 | it = this->subnodes_.insert(this->subnodes_.begin(), std::pair<std::string, NamespaceNode*>(firstPart, new NamespaceNode(firstPart, this))); |
---|
| 90 | |
---|
| 91 | if ((*it).second->isHidden()) |
---|
| 92 | { |
---|
| 93 | COUT(2) << "Warning: Subnamespace '" << firstPart << "' in namespace '" << this->name_ << "' is hidden and can't be accessed." << std::endl; |
---|
| 94 | nodes.insert(this); |
---|
| 95 | } |
---|
| 96 | else |
---|
| 97 | { |
---|
| 98 | nodes = (*it).second->getNodeRelative(secondPart); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | else |
---|
| 102 | { |
---|
| 103 | bool bFoundMatchingNamespace = false; |
---|
| 104 | |
---|
| 105 | for (std::map<std::string, NamespaceNode*>::iterator it = this->subnodes_.begin(); it != this->subnodes_.end(); ++it) |
---|
| 106 | { |
---|
| 107 | if ((*it).first.find(firstPart) == ((*it).first.size() - firstPart.size())) |
---|
| 108 | { |
---|
| 109 | std::set<NamespaceNode*> temp2 = (*it).second->getNodeRelative(secondPart); |
---|
| 110 | nodes.insert(temp2.begin(), temp2.end()); |
---|
| 111 | bFoundMatchingNamespace = true; |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | if (!bFoundMatchingNamespace) |
---|
| 116 | { |
---|
| 117 | COUT(2) << "Warning: No file included with name '" << firstPart.substr(1, std::string::npos) << "' at this part of the level file, using parent namespace instead." << std::endl; |
---|
| 118 | nodes = this->getNodeRelative(secondPart); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | return nodes; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | bool NamespaceNode::includes(const NamespaceNode* ns) const |
---|
| 127 | { |
---|
| 128 | if (ns == this) |
---|
| 129 | { |
---|
| 130 | return true; |
---|
| 131 | } |
---|
| 132 | else |
---|
| 133 | { |
---|
| 134 | for (std::map<std::string, NamespaceNode*>::const_iterator it = this->subnodes_.begin(); it != this->subnodes_.end(); ++it) |
---|
| 135 | if ((*it).second->includes(ns)) |
---|
| 136 | return true; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | return false; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | std::string NamespaceNode::toString() const |
---|
| 143 | { |
---|
| 144 | std::string output = this->name_; |
---|
| 145 | |
---|
| 146 | if (this->subnodes_.size() > 0) |
---|
| 147 | { |
---|
| 148 | output += " ("; |
---|
| 149 | |
---|
| 150 | int i = 0; |
---|
| 151 | for (std::map<std::string, NamespaceNode*>::const_iterator it = this->subnodes_.begin(); it != this->subnodes_.end(); i++, ++it) |
---|
| 152 | { |
---|
| 153 | if (i > 0) |
---|
| 154 | output += ", "; |
---|
| 155 | |
---|
| 156 | output += (*it).second->toString(); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | output += ")"; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | return output; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | std::string NamespaceNode::toString(const std::string& indentation) const |
---|
| 166 | { |
---|
| 167 | std::string output = (indentation + this->name_ + "\n"); |
---|
| 168 | |
---|
| 169 | for (std::map<std::string, NamespaceNode*>::const_iterator it = this->subnodes_.begin(); it != this->subnodes_.end(); ++it) |
---|
| 170 | output += (*it).second->toString(indentation + " "); |
---|
| 171 | |
---|
| 172 | return output; |
---|
| 173 | } |
---|
| 174 | } |
---|