--- trunk/OOPSE-4/src/utils/GenericData.hpp 2004/09/24 04:16:43 1490 +++ trunk/OOPSE-4/src/utils/GenericData.hpp 2004/10/21 16:22:01 1625 @@ -1,174 +1,248 @@ -#ifndef __GENERICDATA_H__ -#define __GENERICDATA_H__ +/* + * Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project + * + * Contact: oopse@oopse.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * All we ask is that proper credit is given for our work, which includes + * - but is not limited to - adding the above copyright notice to the beginning + * of your source code files, and to any copyright notice that you may distribute + * with programs based on this work. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ -#include +/** + * @file GenericData.hpp + * @brief + * @author tlin + * @date 09/20/2004 + * @time 9:30am + * @version 1.0 + */ + +#ifndef UTIL_GENERICDATA_HPP +#define UTIL_GENERICDATA_HPP + +#include #include #include +namespace oopse{ -#define ZCONSTIME_ID "ZCONSTIME" -#define ZCONSPARADATA_ID "ZCONSPARA" -#define ZCONSFILENAME_ID "ZCONSFILENAME" -#define ZCONSTOL_ID "ZCONSTOL" -#define ZCONSFORCEPOLICY_ID "ZCONSFORCEPOLICY" -#define ZCONSGAP_ID "ZCONSGAP" -#define ZCONSFIXTIME_ID "ZCONSFIXTIME" -#define ZCONSUSINGSMD_ID "ZCONSUSINGSMD" + /** + * @ class GenericData GenericData.hpp "utils/GenericData.hpp" + * @brief Base class for generic data which is associated with an id + */ + class GenericData{ + public: + GenericData() : id_("UndefinedGenericData"){} -#define CHIVALUE_ID "CHIVALUE" -#define INTEGRALOFCHIDT_ID "INTEGRALOFCHIDT" -#define ETAVALUE_ID "ETAVALUE" + GenericData(const std::string& id) { setID(id); } -using namespace std; -//////////////////////////////////////////////////////////////////////////////// -//Declaration of GenericData -//////////////////////////////////////////////////////////////////////////////// -class GenericData -{ - public: - GenericData(); - GenericData(const GenericData& rhs) { id = rhs.getID(); } - GenericData& operator =(const GenericData& rhs); - virtual ~GenericData() {} + /** virtual destructor */ + virtual ~GenericData() {} - const string& getID() const { return id; } - void setID(const string& rhs) { id = rhs; } - protected: - string id; -}; + /** + * Returns the id of this generic data + * + * @return the id of this generic data + * + * @see #setID + */ + const std::string getID() const { return id_; } -/** - * Something we can improve it here is to use template - */ -//////////////////////////////////////////////////////////////////////////////// -//Declaration of IntData -//////////////////////////////////////////////////////////////////////////////// -class IntData : public GenericData{ + /** + * Sets the id of this generic data + * + * @param id the id to be set + * + * @see #getID + */ + void setID(const std::string& id) { id_ = id; } - public: + + private: + GenericData(const GenericData&); + GenericData& operator=(GenericData&); + std::string id_; - double getData() { return data; } - void setData(int rhs) { data = rhs; } + }; - protected: - int data; -}; + /** + * @class SimpleTypeData + * @brief SimpleTypeData class is a POD repository class + * @warning ElemDataType must be copy constructible, and copy assignable + */ + template class SimpleTypeData : public GenericData{ -//////////////////////////////////////////////////////////////////////////////// -//Declaration of DoubleData -//////////////////////////////////////////////////////////////////////////////// -class DoubleData : public GenericData{ + public: + SimpleTypeData() : GenericData(), data_(ElemDataType()) {} + SimpleTypeData(const std::string& id) : GenericData(id), data_(ElemDataType()) {} - public: + template + SimpleTypeData(const SimpleTypeData& s) { + data_ = s.getData(); + } - double getData() { return data; } - void setData(double rhs) { data = rhs; } + SimpleTypeData& operator =(const SimpleTypeData& s) { + if (this == &s) + return *this; + + data_ = s.getData(); + return *this; + } + + template + SimpleTypeData& operator =(const SimpleTypeData& s) { + data_ = s.getData(); + return *this; + } + + /** Returns POD data */ + const ElemDataType& getData() const {return data_;} + ElemDataType& getData() {return data_;} + /** + * Sets POD data + * @data POD data to be set + */ + void setData(const ElemDataType& data) { data_ = data; } - protected: - double data; -}; + private: + ElemDataType data_; + }; -//////////////////////////////////////////////////////////////////////////////// -//Declaration of StringData -//////////////////////////////////////////////////////////////////////////////// -class StringData : public GenericData{ + /** BoolGenericData is a generic data type contains a bool variable */ + typedef SimpleTypeData BoolGenericData; - public: - const string& getData() const { return data; } - void setData(const string& rhs) { data = rhs; } - protected: - string data; -}; + /** IntGenericData is a generic data type contains an integer variable */ + typedef SimpleTypeData IntGenericData; -//////////////////////////////////////////////////////////////////////////////// -//Declaration of BoolData -//////////////////////////////////////////////////////////////////////////////// -class BoolData : public GenericData{ - public: - bool getData() const { return data; } - void setData(const bool rhs) { data = rhs; } - protected: - bool data; + /** FloatGenericData is a generic data type contains a float variable */ + typedef SimpleTypeData FloatGenericData; -}; + /** DoubleGenericData is a generic data type contains a double variable */ + typedef SimpleTypeData DoubleGenericData; + + /** + * @typedef StringGenericData + * A generic data type contains a string variable + * + * @code + * StringGenericData* s = new StringGenericData("MyStringGenericData"); + * PropertyMap propMap; + * GenericData* gdata; + * + * s->setData("OOPSE"); + * propMap->addProperty(s); + * + * gdata = propMap->getProperty("MyStringGenericData"); + * if (gdata != NULL){ + * s = dynamic_cast(gdata); + * if (s != NULL) + * std::cout << s->getData() << std::endl; + * } + * + * @endcode + */ + typedef SimpleTypeData StringGenericData; -//////////////////////////////////////////////////////////////////////////////// -//Declaration of ZConsParaData -//////////////////////////////////////////////////////////////////////////////// -struct ZConsParaItem { - int zconsIndex; - bool havingZPos; - double zPos; - double kRatio; - bool havingCantVel; - double cantVel; -}; + /** + * @class STLContainerTypeData + * @brief STL container type generic data which is associated with an id + * + * @template ContainerType + * @template ElemDataType + */ + template > class ContainerType, + typename ElemDataType > + class STLContainerTypeData : public GenericData, public ContainerType{ + public: + typedef STLContainerTypeData SelfType; + typedef ContainerType STLContainerType; -class ZConsParaData : public GenericData{ + STLContainerTypeData(const std::string& id) + : GenericData(id), ContainerType () {} + + STLContainerTypeData(const SelfType& s) : SelfType(s){} - public: - ZConsParaData(); - void addItem(ZConsParaItem& item) {data.push_back(item);} - vector* getData() {return &data;} - void setData(vector& theData) {data = theData;} - void sortByIndex(); - bool isIndexUnique(); + SelfType& operator =(const SelfType& s){ + if (this == &s) + return *this; - private: - vector data; - }; + STLContainerType::operator=(s); + return *this; + } + }; -class ZConsParaSortCriterion{ - public: - bool operator ()(const ZConsParaItem& item1, const ZConsParaItem& item2){ - return item1.zconsIndex < item2.zconsIndex; - } + /** + * @typedef IntVectorGenericData + * A generic data type contains a vector variable. + */ + typedef STLContainerTypeData IntVectorGenericData; -}; + /** + * @typedef IntVectorGenericData + * A generic data type contains a vector variable. + */ + typedef STLContainerTypeData FloatVectorGenericData; -//////////////////////////////////////////////////////////////////////////////// -//Declaration of IntData -//////////////////////////////////////////////////////////////////////////////// -class DoubleArrayData : public GenericData{ + /** + * @typedef IntVectorGenericData + * A generic data type contains a vector variable. + */ + typedef STLContainerTypeData DoubleVectorGenericData; - public: - vector getData() const { return data; } - void setData(double* source, int num){ - data.clear(); - for(int i = 0; i < num; i++) - data.push_back(source[i]); - } - protected: - vector data; -}; + /** + * @typedef StringVectorGenericData + * A generic data type contains a vector variable. + * + * @code + * StringVectorGenericData* sv = new StringVectorGenericData("MyStringVector"); + * GenericData* gdata; + * PropertyMap propMap; + * std::vector::iterator iter; + * + * sv->push_back("Hello World"); + * sv->push_back("OOPSE"); + * + * propMap.addProperty(sv); + * + * gdata = propMap.getProperty("MyStringVector"); + * + * if (gdata != NULL){ + * + * sv = dynamic_cast(gdata); + * + * if (sv != NULL){ + * for (iter = sv->begin(); iter != sv->end(); ++ iter) + * std::cout << *iter << std::endl; + * } + * } + * @endcode + */ + typedef STLContainerTypeData StringVectorGenericData; -//////////////////////////////////////////////////////////////////////////////// -//Declaration of AtomData -//////////////////////////////////////////////////////////////////////////////// -struct AtomInfo : public GenericData{ - public: - string AtomType; - double pos[3]; - double dipole[3]; -}; + /** + * @typedef IntVectorGenericData + * A generic data type contains a list > variable. + */ + typedef STLContainerTypeData > IntVectorListGenericData; + +#define CHIVALUE_ID "CHIVALUE" +#define INTEGRALOFCHIDT_ID "INTEGRALOFCHIDT" +#define ETAVALUE_ID "ETAVALUE" -class AtomData : public GenericData{ - public: - ~AtomData(); - void addAtomInfo(AtomInfo* info) {data.push_back(info);} - void clearAllAtomInfo(); - AtomInfo* beginAtomInfo(vector::iterator& i){ - i = data.begin(); - return i != data.end()? *i : NULL; - } - AtomInfo* nextAtomInfo(vector::iterator& i){ - ++i; - return i != data.end()? *i: NULL; - } - vector getData() {return data;} - int getSize() {return data.size();} - protected: - vector data; -}; - -#endif +} // namespace oopse +#endif //UTIL _GENERICDATA_HPP