Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/core/Namespace.cc @ 10555

Last change on this file since 10555 was 10555, checked in by landauf, 9 years ago

renamed SmartPtr to StrongPtr (now we have weak and strong pointers)

  • Property svn:eol-style set to native
File size: 5.6 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(StrongPtr<Namespace>(this, false));
50    }
51
52    Namespace::~Namespace()
53    {
54        if (this->bRoot_)
55            for (std::set<NamespaceNode*>::iterator it = this->representingNamespaces_.begin(); it != this->representingNamespaces_.end(); ++it)
56                delete (*it);
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 (std::set<NamespaceNode*>::iterator it = this->getNamespace()->representingNamespaces_.begin(); it != this->getNamespace()->representingNamespaces_.end(); ++it)
83                {
84                    std::set<NamespaceNode*> temp = (*it)->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 (std::set<NamespaceNode*>::iterator it = this->representingNamespaces_.begin(); it != this->representingNamespaces_.end(); ++it)
96            {
97                (*it)->setRoot(true);
98                (*it)->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        object->setNamespace(this);
108    }
109
110    const BaseObject* Namespace::saveObjects(unsigned int index) const
111    {
112        return 0; // todo
113    }
114
115    bool Namespace::includes(const Namespace* ns) const
116    {
117        for (std::set<NamespaceNode*>::const_iterator it1 = this->representingNamespaces_.begin(); it1 != this->representingNamespaces_.end(); ++it1)
118        {
119            for (std::set<NamespaceNode*>::const_iterator it2 = ns->representingNamespaces_.begin(); it2 != ns->representingNamespaces_.end(); ++it2)
120            {
121                if ((*it1)->includes(*it2))
122                {
123                    if (this->operator_ == "or")
124                        return true;
125
126                    if (this->operator_ == "not")
127                        return false;
128                }
129                else
130                {
131                    if (this->operator_ == "and")
132                        return false;
133                }
134            }
135        }
136
137        if (this->operator_ == "or")
138            return false;
139        else if (this->operator_ == "and")
140            return true;
141        else if (this->operator_ == "not")
142            return true;
143
144        return false;
145    }
146
147    std::string Namespace::toString() const
148    {
149        std::string output;
150
151        int i = 0;
152        for (std::set<NamespaceNode*>::const_iterator it = this->representingNamespaces_.begin(); it != this->representingNamespaces_.end(); i++, ++it)
153        {
154            if (i > 0)
155                output += " / ";
156
157            output += (*it)->toString();
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 (std::set<NamespaceNode*>::const_iterator it = this->representingNamespaces_.begin(); it != this->representingNamespaces_.end(); i++, ++it)
169        {
170            if (i > 0)
171                output += '\n';
172
173            output += (*it)->toString(indentation);
174        }
175
176        return output;
177    }
178}
Note: See TracBrowser for help on using the repository browser.