item.cpp
Go to the documentation of this file.
00001 /*************************************************************************** 00002 file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/trunk/src/model/item.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 Item> DECLARE_EXPORT Tree utils::HasName<Item>::st; 00035 DECLARE_EXPORT const MetaCategory* Item::metadata; 00036 DECLARE_EXPORT const MetaClass* ItemDefault::metadata; 00037 00038 00039 int Item::initialize() 00040 { 00041 // Initialize the metadata 00042 metadata = new MetaCategory("item", "items", reader, writer); 00043 00044 // Initialize the Python class 00045 return FreppleCategory<Item>::initialize(); 00046 } 00047 00048 00049 int ItemDefault::initialize() 00050 { 00051 // Initialize the metadata 00052 ItemDefault::metadata = new MetaClass("item", "item_default", 00053 Object::createString<ItemDefault>, true); 00054 00055 // Initialize the Python class 00056 return FreppleClass<ItemDefault,Item>::initialize(); 00057 } 00058 00059 00060 DECLARE_EXPORT Item::~Item() 00061 { 00062 // Remove references from the buffers 00063 for (Buffer::iterator buf = Buffer::begin(); buf != Buffer::end(); ++buf) 00064 if (buf->getItem() == this) buf->setItem(NULL); 00065 00066 // Remove references from the demands 00067 for (Demand::iterator l = Demand::begin(); l != Demand::end(); ++l) 00068 if (l->getItem() == this) l->setItem(NULL); 00069 } 00070 00071 00072 DECLARE_EXPORT void Item::writeElement(XMLOutput *o, const Keyword& tag, mode m) const 00073 { 00074 // Writing a reference 00075 if (m == REFERENCE) 00076 { 00077 o->writeElement(tag, Tags::tag_name, getName()); 00078 return; 00079 } 00080 00081 // Write the complete item 00082 if (m != NOHEADER) o->BeginObject(tag, Tags::tag_name, getName()); 00083 00084 // Write the fields 00085 HasDescription::writeElement(o, tag); 00086 HasHierarchy<Item>::writeElement(o, tag); 00087 o->writeElement(Tags::tag_operation, deliveryOperation); 00088 if (getPrice() != 0.0) o->writeElement(Tags::tag_price, getPrice()); 00089 o->EndObject(tag); 00090 } 00091 00092 00093 DECLARE_EXPORT void Item::beginElement(XMLInput& pIn, const Attribute& pAttr) 00094 { 00095 if (pAttr.isA (Tags::tag_operation)) 00096 pIn.readto( Operation::reader(Operation::metadata,pIn.getAttributes()) ); 00097 else 00098 HasHierarchy<Item>::beginElement(pIn, pAttr); 00099 } 00100 00101 00102 DECLARE_EXPORT void Item::endElement(XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement) 00103 { 00104 if (pAttr.isA(Tags::tag_operation)) 00105 { 00106 Operation *o = dynamic_cast<Operation*>(pIn.getPreviousObject()); 00107 if (o) setOperation(o); 00108 else throw LogicException("Incorrect object type during read operation"); 00109 } 00110 else if (pAttr.isA(Tags::tag_price)) 00111 setPrice(pElement.getDouble()); 00112 else 00113 { 00114 HasDescription::endElement(pIn, pAttr, pElement); 00115 HasHierarchy<Item>::endElement(pIn, pAttr, pElement); 00116 } 00117 } 00118 00119 00120 DECLARE_EXPORT PyObject* Item::getattro(const Attribute& attr) 00121 { 00122 if (attr.isA(Tags::tag_name)) 00123 return PythonObject(getName()); 00124 if (attr.isA(Tags::tag_description)) 00125 return PythonObject(getDescription()); 00126 if (attr.isA(Tags::tag_category)) 00127 return PythonObject(getCategory()); 00128 if (attr.isA(Tags::tag_subcategory)) 00129 return PythonObject(getSubCategory()); 00130 if (attr.isA(Tags::tag_price)) 00131 return PythonObject(getPrice()); 00132 if (attr.isA(Tags::tag_owner)) 00133 return PythonObject(getOwner()); 00134 if (attr.isA(Tags::tag_operation)) 00135 return PythonObject(getOperation()); 00136 if (attr.isA(Tags::tag_hidden)) 00137 return PythonObject(getHidden()); 00138 if (attr.isA(Tags::tag_members)) 00139 return new ItemIterator(this); 00140 // @todo support member iteration for res, item, ... 00141 return NULL; 00142 } 00143 00144 00145 DECLARE_EXPORT int Item::setattro(const Attribute& attr, const PythonObject& field) 00146 { 00147 if (attr.isA(Tags::tag_name)) 00148 setName(field.getString()); 00149 else if (attr.isA(Tags::tag_description)) 00150 setDescription(field.getString()); 00151 else if (attr.isA(Tags::tag_category)) 00152 setCategory(field.getString()); 00153 else if (attr.isA(Tags::tag_subcategory)) 00154 setSubCategory(field.getString()); 00155 else if (attr.isA(Tags::tag_price)) 00156 setPrice(field.getDouble()); 00157 else if (attr.isA(Tags::tag_owner)) 00158 { 00159 if (!field.check(Item::metadata)) 00160 { 00161 PyErr_SetString(PythonDataException, "item owner must be of type item"); 00162 return -1; 00163 } 00164 Item* y = static_cast<Item*>(static_cast<PyObject*>(field)); 00165 setOwner(y); 00166 } 00167 else if (attr.isA(Tags::tag_operation)) 00168 { 00169 if (!field.check(Operation::metadata)) 00170 { 00171 PyErr_SetString(PythonDataException, "item operation must be of type operation"); 00172 return -1; 00173 } 00174 Operation* y = static_cast<Operation*>(static_cast<PyObject*>(field)); 00175 setOperation(y); 00176 } 00177 else if (attr.isA(Tags::tag_hidden)) 00178 setHidden(field.getBool()); 00179 else 00180 return -1; 00181 return 0; 00182 } 00183 00184 00185 } // end namespace