| 1 | /* | 
| 2 | * Copyright (C) 2000-2004  Object Oriented Parallel Simulation Engine (OOPSE) project | 
| 3 | * | 
| 4 | * Contact: oopse@oopse.org | 
| 5 | * | 
| 6 | * This program is free software; you can redistribute it and/or | 
| 7 | * modify it under the terms of the GNU Lesser General Public License | 
| 8 | * as published by the Free Software Foundation; either version 2.1 | 
| 9 | * of the License, or (at your option) any later version. | 
| 10 | * All we ask is that proper credit is given for our work, which includes | 
| 11 | * - but is not limited to - adding the above copyright notice to the beginning | 
| 12 | * of your source code files, and to any copyright notice that you may distribute | 
| 13 | * with programs based on this work. | 
| 14 | * | 
| 15 | * This program is distributed in the hope that it will be useful, | 
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 18 | * GNU Lesser General Public License for more details. | 
| 19 | * | 
| 20 | * You should have received a copy of the GNU Lesser General Public License | 
| 21 | * along with this program; if not, write to the Free Software | 
| 22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. | 
| 23 | * | 
| 24 | */ | 
| 25 |  | 
| 26 | /** | 
| 27 | * @file GenericData.hpp | 
| 28 | * @brief | 
| 29 | * @author tlin | 
| 30 | * @date 09/20/2004 | 
| 31 | * @time 9:30am | 
| 32 | * @version 1.0 | 
| 33 | */ | 
| 34 |  | 
| 35 | #ifndef UTIL_GENERICDATA_HPP | 
| 36 | #define UTIL_GENERICDATA_HPP | 
| 37 |  | 
| 38 | #include <list> | 
| 39 | #include <string> | 
| 40 | #include <vector> | 
| 41 | namespace oopse{ | 
| 42 |  | 
| 43 | /** | 
| 44 | * @ class GenericData GenericData.hpp "utils/GenericData.hpp" | 
| 45 | * @brief Base class for generic data which is associated with an id | 
| 46 | */ | 
| 47 | class GenericData{ | 
| 48 | public: | 
| 49 | GenericData() :  id_("UndefinedGenericData"){} | 
| 50 |  | 
| 51 | GenericData(const std::string& id) {  setID(id);  } | 
| 52 |  | 
| 53 | /** virtual destructor */ | 
| 54 | virtual ~GenericData() {} | 
| 55 |  | 
| 56 |  | 
| 57 | /** | 
| 58 | *  Returns the id of this generic data | 
| 59 | * | 
| 60 | * @return the id of this generic data | 
| 61 | * | 
| 62 | * @see #setID | 
| 63 | */ | 
| 64 | const std::string getID() const { return id_;  } | 
| 65 |  | 
| 66 | /** | 
| 67 | *  Sets the id of this generic data | 
| 68 | * | 
| 69 | * @param id the id to be set | 
| 70 | * | 
| 71 | * @see #getID | 
| 72 | */ | 
| 73 | void setID(const std::string& id) { id_ = id;  } | 
| 74 |  | 
| 75 |  | 
| 76 | private: | 
| 77 | GenericData(const GenericData&); | 
| 78 | GenericData& operator=(GenericData&); | 
| 79 | std::string id_; | 
| 80 |  | 
| 81 | }; | 
| 82 |  | 
| 83 | /** | 
| 84 | * @class SimpleTypeData | 
| 85 | * @brief SimpleTypeData class is a POD  repository class | 
| 86 | * @warning ElemDataType must be copy constructible, and copy assignable | 
| 87 | */ | 
| 88 | template<typename ElemDataType> class SimpleTypeData : public GenericData{ | 
| 89 |  | 
| 90 | public: | 
| 91 | SimpleTypeData() :  GenericData(), data_(ElemDataType()) {} | 
| 92 | SimpleTypeData(const std::string& id) : GenericData(id), data_(ElemDataType()) {} | 
| 93 |  | 
| 94 | template<typename T> | 
| 95 | SimpleTypeData(const SimpleTypeData<T>& s) { | 
| 96 | data_ = s.getData(); | 
| 97 | } | 
| 98 |  | 
| 99 | SimpleTypeData<ElemDataType>& operator =(const SimpleTypeData<ElemDataType>& s) { | 
| 100 | if (this == &s) | 
| 101 | return *this; | 
| 102 |  | 
| 103 | data_ = s.getData(); | 
| 104 | return *this; | 
| 105 | } | 
| 106 |  | 
| 107 | template<typename T> | 
| 108 | SimpleTypeData<ElemDataType>& operator =(const SimpleTypeData<T>& s) { | 
| 109 | data_ = s.getData(); | 
| 110 | return *this; | 
| 111 | } | 
| 112 |  | 
| 113 | /** Returns POD data */ | 
| 114 | const ElemDataType& getData() const {return data_;} | 
| 115 | ElemDataType& getData()  {return data_;} | 
| 116 | /** | 
| 117 | * Sets POD data | 
| 118 | * @data POD data to be set | 
| 119 | */ | 
| 120 | void setData(const ElemDataType& data) { data_ = data;  } | 
| 121 |  | 
| 122 | private: | 
| 123 | ElemDataType data_; | 
| 124 | }; | 
| 125 |  | 
| 126 | /** BoolGenericData is a generic data type contains a bool variable */ | 
| 127 | typedef SimpleTypeData<bool> BoolGenericData; | 
| 128 |  | 
| 129 | /** IntGenericData is a generic data type contains an integer variable */ | 
| 130 | typedef SimpleTypeData<int> IntGenericData; | 
| 131 |  | 
| 132 | /** FloatGenericData is a generic data type contains a float variable */ | 
| 133 | typedef SimpleTypeData<float> FloatGenericData; | 
| 134 |  | 
| 135 | /** DoubleGenericData is a generic data type contains a double variable */ | 
| 136 | typedef SimpleTypeData<double> DoubleGenericData; | 
| 137 |  | 
| 138 | /** | 
| 139 | * @typedef StringGenericData | 
| 140 | * A generic data type contains a string variable | 
| 141 | * | 
| 142 | * @code | 
| 143 | *   StringGenericData* s = new StringGenericData("MyStringGenericData"); | 
| 144 | *   PropertyMap propMap; | 
| 145 | *   GenericData* gdata; | 
| 146 | * | 
| 147 | *   s->setData("OOPSE"); | 
| 148 | *   propMap->addProperty(s); | 
| 149 | * | 
| 150 | *   gdata = propMap->getProperty("MyStringGenericData"); | 
| 151 | *   if (gdata != NULL){ | 
| 152 | *     s = dynamic_cast<StringGenericData*>(gdata); | 
| 153 | *     if (s != NULL) | 
| 154 | *       std::cout << s->getData() << std::endl; | 
| 155 | *   } | 
| 156 | * | 
| 157 | * @endcode | 
| 158 | */ | 
| 159 | typedef SimpleTypeData<std::string> StringGenericData; | 
| 160 |  | 
| 161 | /** | 
| 162 | * @class STLContainerTypeData | 
| 163 | * @brief STL container type generic data which is associated with an id | 
| 164 | * | 
| 165 | * @template ContainerType | 
| 166 | * @template ElemDataType | 
| 167 | */ | 
| 168 | template <template<typename ELEM, typename = std::allocator<ELEM> > class ContainerType, | 
| 169 | typename ElemDataType > | 
| 170 | class STLContainerTypeData : public GenericData, public ContainerType<ElemDataType>{ | 
| 171 | public: | 
| 172 | typedef STLContainerTypeData<ContainerType, ElemDataType> SelfType; | 
| 173 | typedef ContainerType<ElemDataType> STLContainerType; | 
| 174 |  | 
| 175 | STLContainerTypeData(const std::string& id) | 
| 176 | : GenericData(id),  ContainerType<ElemDataType> () {} | 
| 177 |  | 
| 178 | STLContainerTypeData(const SelfType& s) : SelfType(s){} | 
| 179 |  | 
| 180 | SelfType& operator =(const SelfType& s){ | 
| 181 | if (this == &s) | 
| 182 | return *this; | 
| 183 |  | 
| 184 | STLContainerType::operator=(s); | 
| 185 | return *this; | 
| 186 | } | 
| 187 | }; | 
| 188 |  | 
| 189 | /** | 
| 190 | * @typedef IntVectorGenericData | 
| 191 | * A generic data type contains a vector<int> variable. | 
| 192 | */ | 
| 193 | typedef STLContainerTypeData<std::vector, int> IntVectorGenericData; | 
| 194 |  | 
| 195 | /** | 
| 196 | * @typedef IntVectorGenericData | 
| 197 | * A generic data type contains a vector<float> variable. | 
| 198 | */ | 
| 199 | typedef STLContainerTypeData<std::vector, float> FloatVectorGenericData; | 
| 200 |  | 
| 201 | /** | 
| 202 | * @typedef IntVectorGenericData | 
| 203 | * A generic data type contains a vector<double> variable. | 
| 204 | */ | 
| 205 | typedef STLContainerTypeData<std::vector, double> DoubleVectorGenericData; | 
| 206 |  | 
| 207 | /** | 
| 208 | * @typedef StringVectorGenericData | 
| 209 | *  A generic data type contains a vector<string> variable. | 
| 210 | * | 
| 211 | * @code | 
| 212 | *  StringVectorGenericData* sv = new StringVectorGenericData("MyStringVector"); | 
| 213 | *  GenericData* gdata; | 
| 214 | *  PropertyMap propMap; | 
| 215 | *  std::vector<std::string>::iterator iter; | 
| 216 | * | 
| 217 | *  sv->push_back("Hello World"); | 
| 218 | *  sv->push_back("OOPSE"); | 
| 219 | * | 
| 220 | *  propMap.addProperty(sv); | 
| 221 | * | 
| 222 | *  gdata = propMap.getProperty("MyStringVector"); | 
| 223 | * | 
| 224 | *  if (gdata != NULL){ | 
| 225 | * | 
| 226 | *    sv = dynamic_cast<StringVectorGenericData*>(gdata); | 
| 227 | * | 
| 228 | *    if (sv != NULL){ | 
| 229 | *      for (iter = sv->begin(); iter != sv->end(); ++ iter) | 
| 230 | *        std::cout << *iter << std::endl; | 
| 231 | *    } | 
| 232 | *  } | 
| 233 | * @endcode | 
| 234 | */ | 
| 235 | typedef STLContainerTypeData<std::vector, std::string> StringVectorGenericData; | 
| 236 |  | 
| 237 | /** | 
| 238 | * @typedef IntVectorGenericData | 
| 239 | * A generic data type contains a  list<vector<string> >  variable. | 
| 240 | */ | 
| 241 | typedef STLContainerTypeData<std::list, std::vector<int> > IntVectorListGenericData; | 
| 242 |  | 
| 243 | #define CHIVALUE_ID "CHIVALUE" | 
| 244 | #define INTEGRALOFCHIDT_ID "INTEGRALOFCHIDT" | 
| 245 | #define ETAVALUE_ID "ETAVALUE" | 
| 246 |  | 
| 247 | } // namespace oopse | 
| 248 | #endif //UTIL _GENERICDATA_HPP |