00001 /*MT* 00002 00003 MediaTomb - http://www.mediatomb.cc/ 00004 00005 object_dictionary.h - this file is part of MediaTomb. 00006 00007 Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>, 00008 Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc> 00009 00010 Copyright (C) 2006-2010 Gena Batyan <bgeradz@mediatomb.cc>, 00011 Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>, 00012 Leonhard Wimmer <leo@mediatomb.cc> 00013 00014 MediaTomb is free software; you can redistribute it and/or modify 00015 it under the terms of the GNU General Public License version 2 00016 as published by the Free Software Foundation. 00017 00018 MediaTomb is distributed in the hope that it will be useful, 00019 but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 GNU General Public License for more details. 00022 00023 You should have received a copy of the GNU General Public License 00024 version 2 along with MediaTomb; if not, write to the Free Software 00025 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 00026 00027 $Id: object_dictionary.h 2081 2010-03-23 20:18:00Z lww $ 00028 */ 00029 00032 #ifndef __OBJECT_DICTIONARY_H__ 00033 #define __OBJECT_DICTIONARY_H__ 00034 00035 #include "zmmf/zmmf.h" 00036 00037 00038 template <class T> 00039 class ObjectDictionaryElement : public zmm::Object 00040 { 00041 public: 00043 ObjectDictionaryElement(zmm::String key, zmm::Ref<T> value) : zmm::Object() 00044 { 00045 this->key = key; 00046 this->value = value; 00047 } 00048 00051 void setKey(zmm::String key) { this->key = key; } 00052 00055 void setValue(zmm::Ref<T> value) { this->value = value; } 00056 00058 zmm::String getKey() { return key; } 00059 00061 zmm::Ref<T> getValue() { return value; } 00062 00063 protected: 00064 zmm::String key; 00065 zmm::Ref<T> value; 00066 }; 00067 00068 00069 template <class T> 00070 class ObjectDictionary : public zmm::Object 00071 { 00072 public: 00074 ObjectDictionary() : zmm::Object() 00075 { 00076 elements = zmm::Ref<zmm::Array<ObjectDictionaryElement<T> > >(new zmm::Array<ObjectDictionaryElement<T> >()); 00077 } 00078 00080 void put(zmm::String key, zmm::Ref<T> value) 00081 { 00082 for (int i = 0; i < elements->size(); i++) 00083 { 00084 zmm::Ref<ObjectDictionaryElement<T> > el = elements->get(i); 00085 if(el->getKey() == key) 00086 { 00087 el->setValue(value); 00088 return; 00089 } 00090 } 00091 zmm::Ref<ObjectDictionaryElement<T> > newEl(new ObjectDictionaryElement<T>(key, value)); 00092 elements->append(newEl); 00093 } 00094 00096 zmm::Ref<T> get(zmm::String key) 00097 { 00098 for (int i = 0; i < elements->size(); i++) 00099 { 00100 zmm::Ref<ObjectDictionaryElement<T> > el = elements->get(i); 00101 if (el->getKey() == key) 00102 { 00103 return el->getValue(); 00104 } 00105 } 00106 return nil; 00107 } 00108 00110 int size() { return elements->size(); } 00111 00113 void remove(zmm::String key) 00114 { 00115 for (int i = 0; i < elements->size(); i++) 00116 { 00117 zmm::Ref<ObjectDictionaryElement<T> > el = elements->get(i); 00118 if (el->getKey() == key) 00119 { 00120 elements->remove(i, 1); 00121 return; 00122 } 00123 } 00124 } 00125 00127 void clear() 00128 { 00129 elements->remove(0, elements->size()); 00130 } 00131 00133 zmm::Ref<ObjectDictionary> clone() 00134 { 00135 zmm::Ref<ObjectDictionary> ret(new ObjectDictionary()); 00136 int len = elements->size(); 00137 for (int i = 0; i < len; i++) 00138 { 00139 zmm::Ref<ObjectDictionaryElement<T> > el = elements->get(i); 00140 ret->put(el->getKey(), el->getValue()); 00141 } 00142 return ret; 00143 } 00144 00145 zmm::Ref<zmm::Array<ObjectDictionaryElement<T> > > getElements() 00146 { 00147 return elements; 00148 } 00149 00151 inline void optimize() { elements->optimize(); } 00152 00153 protected: 00155 zmm::Ref<zmm::Array<ObjectDictionaryElement<T> > > elements; 00156 }; 00157 #endif // __OBJECT_DICTIONARY_H__
1.6.1