location.cpp
Go to the documentation of this file.
00001 /*************************************************************************** 00002 file : $URL: http://svn.code.sf.net/p/frepple/code/trunk/src/model/location.cpp $ 00003 version : $LastChangedRevision: 1718 $ $LastChangedBy: jdetaeye $ 00004 date : $LastChangedDate: 2012-07-21 10:05:34 +0200 (Sat, 21 Jul 2012) $ 00005 ***************************************************************************/ 00006 00007 /*************************************************************************** 00008 * * 00009 * Copyright (C) 2007-2012 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 Affero General Public License as published * 00013 * by the Free Software Foundation; either version 3 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 * 00019 * GNU Affero General Public License for more details. * 00020 * * 00021 * You should have received a copy of the GNU Affero General Public * 00022 * License along with this program. * 00023 * If not, see <http://www.gnu.org/licenses/>. * 00024 * * 00025 ***************************************************************************/ 00026 00027 #define FREPPLE_CORE 00028 #include "frepple/model.h" 00029 00030 namespace frepple 00031 { 00032 00033 template<class Location> DECLARE_EXPORT Tree utils::HasName<Location>::st; 00034 DECLARE_EXPORT const MetaCategory* Location::metadata; 00035 DECLARE_EXPORT const MetaClass* LocationDefault::metadata; 00036 00037 00038 int Location::initialize() 00039 { 00040 // Initialize the metadata 00041 metadata = new MetaCategory("location", "locations", reader, writer); 00042 00043 // Initialize the Python class 00044 return FreppleCategory<Location>::initialize(); 00045 } 00046 00047 00048 int LocationDefault::initialize() 00049 { 00050 // Initialize the metadata 00051 LocationDefault::metadata = new MetaClass("location", "location_default", 00052 Object::createString<LocationDefault>, true); 00053 00054 // Initialize the Python class 00055 return FreppleClass<LocationDefault,Location>::initialize(); 00056 } 00057 00058 00059 DECLARE_EXPORT void Location::writeElement(XMLOutput* o, const Keyword& tag, mode m) const 00060 { 00061 // Writing a reference 00062 if (m == REFERENCE) 00063 { 00064 o->writeElement(tag, Tags::tag_name, getName()); 00065 return; 00066 } 00067 00068 // Write the complete object 00069 if (m != NOHEADER) o->BeginObject(tag, Tags::tag_name, XMLEscape(getName())); 00070 00071 // Write the fields 00072 HasDescription::writeElement(o, tag); 00073 HasHierarchy<Location>::writeElement(o, tag); 00074 o->writeElement(Tags::tag_available, available); 00075 o->EndObject(tag); 00076 } 00077 00078 00079 DECLARE_EXPORT void Location::beginElement(XMLInput& pIn, const Attribute& pAttr) 00080 { 00081 if (pAttr.isA(Tags::tag_available) || pAttr.isA(Tags::tag_maximum)) 00082 pIn.readto( Calendar::reader(Calendar::metadata,pIn.getAttributes()) ); 00083 else 00084 HasHierarchy<Location>::beginElement(pIn, pAttr); 00085 } 00086 00087 00088 DECLARE_EXPORT void Location::endElement(XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement) 00089 { 00090 if (pAttr.isA(Tags::tag_available)) 00091 { 00092 CalendarDouble *cal = dynamic_cast<CalendarDouble*>(pIn.getPreviousObject()); 00093 if (cal) 00094 setAvailable(cal); 00095 else 00096 { 00097 Calendar *c = dynamic_cast<Calendar*>(pIn.getPreviousObject()); 00098 if (!c) 00099 throw LogicException("Incorrect object type during read operation"); 00100 throw DataException("Calendar '" + c->getName() + 00101 "' has invalid type for use as location calendar"); 00102 } 00103 } 00104 else 00105 { 00106 HasDescription::endElement(pIn, pAttr, pElement); 00107 HasHierarchy<Location>::endElement(pIn, pAttr, pElement); 00108 } 00109 } 00110 00111 00112 DECLARE_EXPORT Location::~Location() 00113 { 00114 // Remove all references from buffers to this location 00115 for (Buffer::iterator buf = Buffer::begin(); 00116 buf != Buffer::end(); ++buf) 00117 if (buf->getLocation() == this) buf->setLocation(NULL); 00118 00119 // Remove all references from resources to this location 00120 for (Resource::iterator res = Resource::begin(); 00121 res != Resource::end(); ++res) 00122 if (res->getLocation() == this) res->setLocation(NULL); 00123 00124 // Remove all references from operations to this location 00125 for (Operation::iterator oper = Operation::begin(); 00126 oper != Operation::end(); ++oper) 00127 if (oper->getLocation() == this) oper->setLocation(NULL); 00128 } 00129 00130 00131 DECLARE_EXPORT PyObject* Location::getattro(const Attribute& attr) 00132 { 00133 if (attr.isA(Tags::tag_name)) 00134 return PythonObject(getName()); 00135 if (attr.isA(Tags::tag_description)) 00136 return PythonObject(getDescription()); 00137 if (attr.isA(Tags::tag_category)) 00138 return PythonObject(getCategory()); 00139 if (attr.isA(Tags::tag_subcategory)) 00140 return PythonObject(getSubCategory()); 00141 if (attr.isA(Tags::tag_owner)) 00142 return PythonObject(getOwner()); 00143 if (attr.isA(Tags::tag_available)) 00144 return PythonObject(getAvailable()); 00145 if (attr.isA(Tags::tag_hidden)) 00146 return PythonObject(getHidden()); 00147 if (attr.isA(Tags::tag_members)) 00148 return new LocationIterator(this); 00149 return NULL; 00150 } 00151 00152 00153 DECLARE_EXPORT int Location::setattro(const Attribute& attr, const PythonObject& field) 00154 { 00155 if (attr.isA(Tags::tag_name)) 00156 setName(field.getString()); 00157 else if (attr.isA(Tags::tag_description)) 00158 setDescription(field.getString()); 00159 else if (attr.isA(Tags::tag_category)) 00160 setCategory(field.getString()); 00161 else if (attr.isA(Tags::tag_subcategory)) 00162 setSubCategory(field.getString()); 00163 else if (attr.isA(Tags::tag_owner)) 00164 { 00165 if (!field.check(Location::metadata)) 00166 { 00167 PyErr_SetString(PythonDataException, "location owner must be of type location"); 00168 return -1; 00169 } 00170 Location* y = static_cast<Location*>(static_cast<PyObject*>(field)); 00171 setOwner(y); 00172 } 00173 else if (attr.isA(Tags::tag_available)) 00174 { 00175 if (!field.check(CalendarDouble::metadata)) 00176 { 00177 PyErr_SetString(PythonDataException, "location availability must be of type double calendar"); 00178 return -1; 00179 } 00180 CalendarDouble* y = static_cast<CalendarDouble*>(static_cast<PyObject*>(field)); 00181 setAvailable(y); 00182 } 00183 else if (attr.isA(Tags::tag_hidden)) 00184 setHidden(field.getBool()); 00185 else 00186 return -1; 00187 return 0; 00188 } 00189 00190 } // end namespace