object.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2006-2011 by the FIFE team                              *
00003  *   http://www.fifengine.net                                              *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 
00024 // 3rd party library includes
00025 
00026 // FIFE includes
00027 // These includes are split up in two parts, separated by one empty line
00028 // First block: files included from the FIFE root src directory
00029 // Second block: files included from the same folder
00030 #include "util/base/exception.h"
00031 
00032 #include "object.h"
00033 #include "action.h"
00034 #include "ipather.h"
00035 
00036 namespace FIFE {
00037     Object::Object(const std::string& identifier, const std::string& name_space, Object* inherited):
00038         m_id(identifier),
00039         m_namespace(name_space),
00040         m_filename(""),
00041         m_inherited(inherited),
00042         m_actions(NULL),
00043         m_blocking(false),
00044         m_static(false),
00045         m_pather(NULL),
00046         m_visual(NULL),
00047         m_defaultaction(NULL) {
00048     }
00049 
00050     Object::~Object() {
00051         if (m_actions) {
00052             std::map<std::string, Action*>::const_iterator i(m_actions->begin());
00053             while (i != m_actions->end()) {
00054                 delete i->second;
00055                 ++i;
00056             }
00057             delete m_actions;
00058         }
00059         delete m_visual;
00060     }
00061 
00062     Action* Object::createAction(const std::string& identifier, bool is_default) {
00063         if (!m_actions) {
00064             m_actions = new std::map<std::string, Action*>;
00065         }
00066 
00067         std::map<std::string, Action*>::const_iterator it = m_actions->begin();
00068         for(; it != m_actions->end(); ++it) {
00069             if(identifier == it->second->getId()) {
00070                 throw NameClash(identifier);
00071             }
00072         }
00073 
00074         Action* a = getAction(identifier);
00075         if (!a) {
00076             a = new Action(identifier);
00077             (*m_actions)[identifier] = a;
00078             if (is_default || (!m_defaultaction)) {
00079                 m_defaultaction = a;
00080             }
00081         }
00082         return a;
00083     }
00084 
00085     Action* Object::getAction(const std::string& identifier) const {
00086         std::map<std::string, Action*>::const_iterator i;
00087         if (m_actions) {
00088             i = m_actions->find(identifier);
00089         }
00090         if ((!m_actions) || (i == m_actions->end())) {
00091             if (m_inherited) {
00092                 return m_inherited->getAction(identifier);
00093             }
00094             return NULL;
00095         }
00096         return i->second;
00097     }
00098 
00099     std::list<std::string> Object::getActionIds() const {
00100         std::list<std::string> action_ids;
00101         action_ids.clear();
00102         if (m_actions) {
00103             std::map<std::string, Action*>::const_iterator actions_it = m_actions->begin();
00104             for(; actions_it != m_actions->end(); ++actions_it) {
00105                 action_ids.push_back(actions_it->first);
00106             }
00107         }
00108         return action_ids;
00109     }
00110 
00111     void Object::setPather(IPather* pather) {
00112         m_pather = pather;
00113     }
00114 
00115     bool Object::isBlocking() const {
00116         if (m_blocking) {
00117             return true;
00118         }
00119         if (m_inherited) {
00120             return m_inherited->isBlocking();
00121         }
00122         return false;
00123     }
00124 
00125     bool Object::isStatic() const {
00126         if (m_static) {
00127             return true;
00128         }
00129         if (m_inherited) {
00130             return m_inherited->isStatic();
00131         }
00132         return false;
00133     }
00134 
00135     bool Object::operator==(const Object& obj) const {
00136         return m_id == obj.getId() && m_namespace == obj.getNamespace();
00137     }
00138 
00139     bool Object::operator!=(const Object& obj) const {
00140         return m_id != obj.getId() || m_namespace != obj.getNamespace();
00141     }
00142 
00143 }