Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/SuperOrxoBros_HS18/src/libraries/core/Namespace.cc @ 12177

Last change on this file since 12177 was 12177, checked in by siramesh, 5 years ago

Super Orxo Bros Final (Sidharth Ramesh, Nisa Balta, Jeff Ren)

File size: 5.1 KB
Line 
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"
30
31#include <set>
32
33#include "NamespaceNode.h"
34#include "CoreIncludes.h"
35#include "XMLPort.h"
36#include "util/SubString.h"
37
38namespace orxonox
39{
40    RegisterClass(Namespace);
41
42    Namespace::Namespace(Context* context) : BaseObject(context), Context(context),
43      bAutogeneratedFileRootNamespace_(false),
44      bRoot_(false),
45      operator_("or")
46    {
47        RegisterObject(Namespace);
48
49        this->setNamespace(WeakPtr<Namespace>(this)); // store a weak-pointer to itself (a strong-pointer would create a recursive dependency)
50    }
51
52    Namespace::~Namespace()
53    {
54        if (this->bRoot_)
55            for (NamespaceNode* node : this->representingNamespaces_)
56                delete node;
57    }
58
59    void Namespace::XMLPort(Element& xmlelement, XMLPort::Mode mode)
60    {
61        SUPER(Namespace, XMLPort, xmlelement, mode);
62
63        std::string name = this->getName();
64        size_t pos = 0;
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            {
82                for (NamespaceNode* node : this->getNamespace()->representingNamespaces_)
83                {
84                    std::set<NamespaceNode*> temp = node->getNodeRelative(tokens[i]);
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        {
95            for (NamespaceNode* node : this->representingNamespaces_)
96            {
97                node->setRoot(true);
98                node->setHidden(true);
99            }
100        }
101
102        XMLPortObjectExtended(Namespace, BaseObject, "", loadObjects, saveObjects, xmlelement, mode, true, false);
103    }
104
105    void Namespace::loadObjects(BaseObject* object)
106    {
107    }
108
109    const BaseObject* Namespace::saveObjects(unsigned int index) const
110    {
111        return nullptr; // todo
112    }
113
114    bool Namespace::includes(const Namespace* ns) const
115    {
116        for (NamespaceNode* node1 : this->representingNamespaces_)
117        {
118            for (NamespaceNode* node2 : ns->representingNamespaces_)
119            {
120                if (node1->includes(node2))
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;
151        for (NamespaceNode* node : this->representingNamespaces_)
152        {
153            if (i > 0)
154                output += " / ";
155
156            output += node->toString();
157            i++;
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;
168        for (NamespaceNode* node : this->representingNamespaces_)
169        {
170            if (i > 0)
171                output += '\n';
172
173            output += node->toString(indentation);
174            i++;
175        }
176
177        return output;
178    }
179}
Note: See TracBrowser for help on using the repository browser.