location.cpp
Go to the documentation of this file.
00001 /*************************************************************************** 00002 file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/trunk/src/model/location.cpp $ 00003 version : $LastChangedRevision: 1505 $ $LastChangedBy: jdetaeye $ 00004 date : $LastChangedDate: 2011-08-26 18:55:08 +0200 (Fri, 26 Aug 2011) $ 00005 ***************************************************************************/ 00006 00007 /*************************************************************************** 00008 * * 00009 * Copyright (C) 2007-2011 by Johan De Taeye, frePPLe bvba * 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 if (attr.isA(Tags::tag_members)) 00149 return new LocationIterator(this); 00150 return NULL; 00151 } 00152 00153 00154 DECLARE_EXPORT int Location::setattro(const Attribute& attr, const PythonObject& field) 00155 { 00156 if (attr.isA(Tags::tag_name)) 00157 setName(field.getString()); 00158 else if (attr.isA(Tags::tag_description)) 00159 setDescription(field.getString()); 00160 else if (attr.isA(Tags::tag_category)) 00161 setCategory(field.getString()); 00162 else if (attr.isA(Tags::tag_subcategory)) 00163 setSubCategory(field.getString()); 00164 else if (attr.isA(Tags::tag_owner)) 00165 { 00166 if (!field.check(Location::metadata)) 00167 { 00168 PyErr_SetString(PythonDataException, "location owner must be of type location"); 00169 return -1; 00170 } 00171 Location* y = static_cast<Location*>(static_cast<PyObject*>(field)); 00172 setOwner(y); 00173 } 00174 else if (attr.isA(Tags::tag_available)) 00175 { 00176 if (!field.check(CalendarBool::metadata)) 00177 { 00178 PyErr_SetString(PythonDataException, "location calendar must be of type calendar_bool"); 00179 return -1; 00180 } 00181 CalendarBool* y = static_cast<CalendarBool*>(static_cast<PyObject*>(field)); 00182 setAvailable(y); 00183 } 00184 else if (attr.isA(Tags::tag_hidden)) 00185 setHidden(field.getBool()); 00186 else 00187 return -1; 00188 return 0; 00189 } 00190 00191 } // end namespace