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