[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 "Namespace.h" |
---|
[3196] | 30 | |
---|
| 31 | #include <set> |
---|
| 32 | |
---|
[1505] | 33 | #include "NamespaceNode.h" |
---|
| 34 | #include "CoreIncludes.h" |
---|
| 35 | #include "XMLPort.h" |
---|
| 36 | #include "util/SubString.h" |
---|
| 37 | |
---|
| 38 | namespace orxonox |
---|
| 39 | { |
---|
[9667] | 40 | RegisterClass(Namespace); |
---|
[1505] | 41 | |
---|
[9667] | 42 | Namespace::Namespace(Context* context) : BaseObject(context), Context(context), |
---|
[1505] | 43 | bAutogeneratedFileRootNamespace_(false), |
---|
| 44 | bRoot_(false), |
---|
| 45 | operator_("or") |
---|
| 46 | { |
---|
| 47 | RegisterObject(Namespace); |
---|
[10298] | 48 | |
---|
[10624] | 49 | this->setNamespace(WeakPtr<Namespace>(this)); // store a weak-pointer to itself (a strong-pointer would create a recursive dependency) |
---|
[1505] | 50 | } |
---|
| 51 | |
---|
| 52 | Namespace::~Namespace() |
---|
| 53 | { |
---|
| 54 | if (this->bRoot_) |
---|
[11071] | 55 | for (NamespaceNode* node : this->representingNamespaces_) |
---|
| 56 | delete node; |
---|
[1505] | 57 | } |
---|
| 58 | |
---|
| 59 | void Namespace::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 60 | { |
---|
[1747] | 61 | SUPER(Namespace, XMLPort, xmlelement, mode); |
---|
[1505] | 62 | |
---|
| 63 | std::string name = this->getName(); |
---|
[1889] | 64 | size_t pos = 0; |
---|
[1505] | 65 | while ((pos = name.find(',')) != std::string::npos) |
---|
| 66 | name.replace(pos, 1, " "); |
---|
| 67 | while ((pos = name.find(';')) != std::string::npos) |
---|
| 68 | name.replace(pos, 1, " "); |
---|
| 69 | while ((pos = name.find('\n')) != std::string::npos) |
---|
| 70 | name.replace(pos, 1, " "); |
---|
| 71 | while ((pos = name.find('\t')) != std::string::npos) |
---|
| 72 | name.replace(pos, 1, " "); |
---|
| 73 | SubString tokens(name, " ", "", false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
---|
| 74 | if (this->bRoot_) |
---|
| 75 | { |
---|
| 76 | this->representingNamespaces_.insert(new NamespaceNode(this->getName())); |
---|
| 77 | } |
---|
| 78 | else |
---|
| 79 | { |
---|
| 80 | for (unsigned int i = 0; i < tokens.size(); i++) |
---|
| 81 | { |
---|
[11071] | 82 | for (NamespaceNode* node : this->getNamespace()->representingNamespaces_) |
---|
[1505] | 83 | { |
---|
[11071] | 84 | std::set<NamespaceNode*> temp = node->getNodeRelative(tokens[i]); |
---|
[1505] | 85 | this->representingNamespaces_.insert(temp.begin(), temp.end()); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | XMLPortParam(Namespace, "operator", setOperator, getOperator, xmlelement, mode); |
---|
| 91 | XMLPortParam(Namespace, "bAutogenerated", setAutogenerated, isAutogenerated, xmlelement, mode); |
---|
| 92 | |
---|
| 93 | if (this->bAutogeneratedFileRootNamespace_) |
---|
| 94 | { |
---|
[11071] | 95 | for (NamespaceNode* node : this->representingNamespaces_) |
---|
[1505] | 96 | { |
---|
[11071] | 97 | node->setRoot(true); |
---|
| 98 | node->setHidden(true); |
---|
[1505] | 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
[1854] | 102 | XMLPortObjectExtended(Namespace, BaseObject, "", loadObjects, saveObjects, xmlelement, mode, true, false); |
---|
[1505] | 103 | } |
---|
| 104 | |
---|
| 105 | void Namespace::loadObjects(BaseObject* object) |
---|
| 106 | { |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | const BaseObject* Namespace::saveObjects(unsigned int index) const |
---|
| 110 | { |
---|
[11071] | 111 | return nullptr; // todo |
---|
[1505] | 112 | } |
---|
| 113 | |
---|
| 114 | bool Namespace::includes(const Namespace* ns) const |
---|
| 115 | { |
---|
[11071] | 116 | for (NamespaceNode* node1 : this->representingNamespaces_) |
---|
[1505] | 117 | { |
---|
[11071] | 118 | for (NamespaceNode* node2 : ns->representingNamespaces_) |
---|
[1505] | 119 | { |
---|
[11071] | 120 | if (node1->includes(node2)) |
---|
[1505] | 121 | { |
---|
| 122 | if (this->operator_ == "or") |
---|
| 123 | return true; |
---|
| 124 | |
---|
| 125 | if (this->operator_ == "not") |
---|
| 126 | return false; |
---|
| 127 | } |
---|
| 128 | else |
---|
| 129 | { |
---|
| 130 | if (this->operator_ == "and") |
---|
| 131 | return false; |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if (this->operator_ == "or") |
---|
| 137 | return false; |
---|
| 138 | else if (this->operator_ == "and") |
---|
| 139 | return true; |
---|
| 140 | else if (this->operator_ == "not") |
---|
| 141 | return true; |
---|
| 142 | |
---|
| 143 | return false; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | std::string Namespace::toString() const |
---|
| 147 | { |
---|
| 148 | std::string output; |
---|
| 149 | |
---|
| 150 | int i = 0; |
---|
[11071] | 151 | for (NamespaceNode* node : this->representingNamespaces_) |
---|
[1505] | 152 | { |
---|
| 153 | if (i > 0) |
---|
| 154 | output += " / "; |
---|
| 155 | |
---|
[11071] | 156 | output += node->toString(); |
---|
| 157 | i++; |
---|
[1505] | 158 | } |
---|
| 159 | |
---|
| 160 | return output; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | std::string Namespace::toString(const std::string& indentation) const |
---|
| 164 | { |
---|
| 165 | std::string output; |
---|
| 166 | |
---|
| 167 | int i = 0; |
---|
[11071] | 168 | for (NamespaceNode* node : this->representingNamespaces_) |
---|
[1505] | 169 | { |
---|
| 170 | if (i > 0) |
---|
[6417] | 171 | output += '\n'; |
---|
[1505] | 172 | |
---|
[11071] | 173 | output += node->toString(indentation); |
---|
| 174 | i++; |
---|
[1505] | 175 | } |
---|
| 176 | |
---|
| 177 | return output; |
---|
| 178 | } |
---|
| 179 | } |
---|