location.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002   file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/tags/0.8.0/src/model/location.cpp $
00003   version : $LastChangedRevision: 1108 $  $LastChangedBy: jdetaeye $
00004   date : $LastChangedDate: 2009-12-06 18:54:18 +0100 (Sun, 06 Dec 2009) $
00005  ***************************************************************************/
00006 
00007 /***************************************************************************
00008  *                                                                         *
00009  * Copyright (C) 2007 by Johan De Taeye                                    *
00010  *                                                                         *
00011  * This library is free software; you can redistribute it and/or modify it *
00012  * under the terms of the GNU Lesser General Public License as published   *
00013  * by the Free Software Foundation; either version 2.1 of the License, or  *
00014  * (at your option) any later version.                                     *
00015  *                                                                         *
00016  * This library is distributed in the hope that it will be useful,         *
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
00019  * General Public License for more details.                                *
00020  *                                                                         *
00021  * You should have received a copy of the GNU Lesser General Public        *
00022  * License along with this library; if not, write to the Free Software     *
00023  * Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,*
00024  * USA                                                                     *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00028 #define FREPPLE_CORE
00029 #include "frepple/model.h"
00030 
00031 namespace frepple
00032 {
00033 
00034 template<class Location> DECLARE_EXPORT Tree utils::HasName<Location>::st;
00035 DECLARE_EXPORT const MetaCategory* Location::metadata;
00036 DECLARE_EXPORT const MetaClass* LocationDefault::metadata;
00037 
00038 
00039 int Location::initialize()
00040 {
00041   // Initialize the metadata
00042   metadata = new MetaCategory("location", "locations", reader, writer);
00043 
00044   // Initialize the Python class
00045   return FreppleCategory<Location>::initialize();
00046 }
00047 
00048 
00049 int LocationDefault::initialize()
00050 {
00051   // Initialize the metadata
00052   LocationDefault::metadata = new MetaClass("location", "location_default",
00053     Object::createString<LocationDefault>, true);
00054 
00055   // Initialize the Python class
00056   return FreppleClass<LocationDefault,Location>::initialize();
00057 }
00058 
00059 
00060 DECLARE_EXPORT void Location::writeElement(XMLOutput* o, const Keyword& tag, mode m) const
00061 {
00062   // Writing a reference
00063   if (m == REFERENCE)
00064   {
00065     o->writeElement(tag, Tags::tag_name, getName());
00066     return;
00067   }
00068 
00069   // Write the complete object
00070   if (m != NOHEADER) o->BeginObject(tag, Tags::tag_name, getName());
00071 
00072   // Write the fields
00073   HasDescription::writeElement(o, tag);
00074   HasHierarchy<Location>::writeElement(o, tag);
00075   o->writeElement(Tags::tag_available, available);
00076   o->EndObject(tag);
00077 }
00078 
00079 
00080 DECLARE_EXPORT void Location::beginElement(XMLInput& pIn, const Attribute& pAttr)
00081 {
00082   if (pAttr.isA(Tags::tag_available) || pAttr.isA(Tags::tag_maximum))
00083     pIn.readto( Calendar::reader(Calendar::metadata,pIn.getAttributes()) );
00084   else
00085     HasHierarchy<Location>::beginElement(pIn, pAttr);
00086 }
00087 
00088 
00089 DECLARE_EXPORT void Location::endElement(XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement)
00090 {
00091   if (pAttr.isA(Tags::tag_available))
00092   {
00093     CalendarBool *cal = dynamic_cast<CalendarBool*>(pIn.getPreviousObject());
00094     if (cal)
00095       setAvailable(cal);
00096     else
00097     {
00098       Calendar *c = dynamic_cast<Calendar*>(pIn.getPreviousObject());
00099       if (!c)
00100         throw LogicException("Incorrect object type during read operation");
00101       throw DataException("Calendar '" + c->getName() +
00102           "' has invalid type for use as location calendar");
00103     }
00104   }
00105   else
00106   {
00107     HasDescription::endElement(pIn, pAttr, pElement);
00108     HasHierarchy<Location>::endElement(pIn, pAttr, pElement);
00109   }
00110 }
00111 
00112 
00113 DECLARE_EXPORT Location::~Location()
00114 {
00115   // Remove all references from buffers to this location
00116   for (Buffer::iterator buf = Buffer::begin();
00117       buf != Buffer::end(); ++buf)
00118     if (buf->getLocation() == this) buf->setLocation(NULL);
00119 
00120   // Remove all references from resources to this location
00121   for (Resource::iterator res = Resource::begin();
00122       res != Resource::end(); ++res)
00123     if (res->getLocation() == this) res->setLocation(NULL);
00124 
00125   // Remove all references from operations to this location
00126   for (Operation::iterator oper = Operation::begin();
00127       oper != Operation::end(); ++oper)
00128     if (oper->getLocation() == this) oper->setLocation(NULL);
00129 }
00130 
00131 
00132 DECLARE_EXPORT PyObject* Location::getattro(const Attribute& attr)
00133 {
00134   if (attr.isA(Tags::tag_name))
00135     return PythonObject(getName());
00136   if (attr.isA(Tags::tag_description))
00137     return PythonObject(getDescription());
00138   if (attr.isA(Tags::tag_category))
00139     return PythonObject(getCategory());
00140   if (attr.isA(Tags::tag_subcategory))
00141     return PythonObject(getSubCategory());
00142   if (attr.isA(Tags::tag_owner))
00143     return PythonObject(getOwner());
00144   if (attr.isA(Tags::tag_available))
00145     return PythonObject(getAvailable());
00146   if (attr.isA(Tags::tag_hidden))
00147     return PythonObject(getHidden());
00148   return NULL;
00149 }
00150 
00151 
00152 DECLARE_EXPORT int Location::setattro(const Attribute& attr, const PythonObject& field)
00153 {
00154   if (attr.isA(Tags::tag_name))
00155     setName(field.getString());
00156   else if (attr.isA(Tags::tag_description))
00157     setDescription(field.getString());
00158   else if (attr.isA(Tags::tag_category))
00159     setCategory(field.getString());
00160   else if (attr.isA(Tags::tag_subcategory))
00161     setSubCategory(field.getString());
00162   else if (attr.isA(Tags::tag_owner))
00163   {
00164     if (!field.check(Location::metadata))
00165     {
00166       PyErr_SetString(PythonDataException, "location owner must be of type location");
00167       return -1;
00168     }
00169     Location* y = static_cast<Location*>(static_cast<PyObject*>(field));
00170     setOwner(y);
00171   }
00172   else if (attr.isA(Tags::tag_available))
00173   {
00174     if (!field.check(CalendarBool::metadata))
00175     {
00176       PyErr_SetString(PythonDataException, "location calendar must be of type calendar_bool");
00177       return -1;
00178     }
00179     CalendarBool* y = static_cast<CalendarBool*>(static_cast<PyObject*>(field));
00180     setAvailable(y);
00181   }
00182   else if (attr.isA(Tags::tag_hidden))
00183     setHidden(field.getBool());
00184   else
00185     return -1;
00186   return 0;
00187 }
00188 
00189 } // end namespace
Generated by  doxygen 1.6.2-20100208