ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/utils/GenericData.hpp
(Generate patch)

Comparing trunk/OOPSE-4/src/utils/GenericData.hpp (file contents):
Revision 1490 by gezelter, Fri Sep 24 04:16:43 2004 UTC vs.
Revision 1625 by tim, Thu Oct 21 16:22:01 2004 UTC

# Line 1 | Line 1
1 < #ifndef __GENERICDATA_H__
2 < #define __GENERICDATA_H__
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 < #include <algorithm>
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 < #define ZCONSTIME_ID            "ZCONSTIME"
44 < #define ZCONSPARADATA_ID    "ZCONSPARA"
45 < #define ZCONSFILENAME_ID     "ZCONSFILENAME"
46 < #define ZCONSTOL_ID              "ZCONSTOL"
47 < #define ZCONSFORCEPOLICY_ID "ZCONSFORCEPOLICY"
48 < #define ZCONSGAP_ID "ZCONSGAP"
49 < #define ZCONSFIXTIME_ID "ZCONSFIXTIME"
15 < #define ZCONSUSINGSMD_ID "ZCONSUSINGSMD"
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 < #define CHIVALUE_ID "CHIVALUE"
18 < #define INTEGRALOFCHIDT_ID "INTEGRALOFCHIDT"
19 < #define ETAVALUE_ID "ETAVALUE"
51 >            GenericData(const std::string& id) {  setID(id);  }
52  
53 < using namespace std;
54 < ////////////////////////////////////////////////////////////////////////////////
23 < //Declaration of GenericData
24 < ////////////////////////////////////////////////////////////////////////////////
25 < class GenericData
26 < {
27 <  public:
28 <    GenericData();
29 <    GenericData(const GenericData& rhs)  {  id = rhs.getID(); }
30 <    GenericData& operator =(const GenericData& rhs);
31 <    virtual ~GenericData() {}
53 >            /** virtual destructor */
54 >            virtual ~GenericData() {}
55  
33    const string& getID() const {  return id; }
34    void setID(const string& rhs)     {  id = rhs;  }
56  
57 <  protected:
58 <    string id;
59 < };
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 < * Something we can improve it here is to use template
68 < */
69 < ////////////////////////////////////////////////////////////////////////////////
70 < //Declaration of IntData
71 < ////////////////////////////////////////////////////////////////////////////////
72 < class IntData : public GenericData{
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 <  public:
75 >            
76 >        private:
77 >            GenericData(const GenericData&);
78 >            GenericData& operator=(GenericData&);
79 >            std::string id_;
80  
81 <    double getData()         { return data; }
51 <    void setData(int rhs) { data = rhs;  }
81 >    };
82  
83 <  protected:
84 <    int data;
85 < };
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 < ////////////////////////////////////////////////////////////////////////////////
91 < //Declaration of DoubleData
92 < ////////////////////////////////////////////////////////////////////////////////
60 < class DoubleData : public GenericData{
90 >        public:
91 >            SimpleTypeData() :  GenericData(), data_(ElemDataType()) {}
92 >            SimpleTypeData(const std::string& id) : GenericData(id), data_(ElemDataType()) {}
93  
94 <  public:
94 >            template<typename T>
95 >            SimpleTypeData(const SimpleTypeData<T>& s) {
96 >                data_ = s.getData();
97 >            }
98  
99 <    double getData()         { return data; }
100 <    void setData(double rhs) { data = rhs;  }
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 <  protected:
123 <    double data;
124 < };
122 >        private:
123 >            ElemDataType data_;
124 >    };
125  
126 < ////////////////////////////////////////////////////////////////////////////////
127 < //Declaration of StringData
73 < ////////////////////////////////////////////////////////////////////////////////
74 < class StringData : public GenericData{
126 >    /** BoolGenericData is a generic data type contains a bool variable */
127 >    typedef SimpleTypeData<bool> BoolGenericData;
128  
129 <  public:
130 <    const string& getData() const  {  return data; }
78 <    void setData(const string& rhs) {  data = rhs;  }
79 <  protected:
80 <    string data;
81 < };
129 >    /** IntGenericData is a generic data type contains an integer variable */
130 >    typedef SimpleTypeData<int> IntGenericData;
131  
132 < ////////////////////////////////////////////////////////////////////////////////
133 < //Declaration of BoolData
85 < ////////////////////////////////////////////////////////////////////////////////
86 < class BoolData : public GenericData{
87 <  public:
88 <    bool getData() const  {  return data; }
89 <    void setData(const bool rhs) {  data = rhs;  }
90 <  protected:
91 <    bool data;
132 >    /** FloatGenericData is a generic data type contains a float variable */
133 >    typedef SimpleTypeData<float> FloatGenericData;
134  
135 < };
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 < //Declaration of ZConsParaData
163 < ////////////////////////////////////////////////////////////////////////////////
164 < struct ZConsParaItem {
165 <  int zconsIndex;
166 <  bool havingZPos;
167 <  double zPos;
168 <  double kRatio;
169 <  bool havingCantVel;
170 <  double cantVel;
171 < };
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 < class ZConsParaData : public GenericData{
175 >            STLContainerTypeData(const std::string& id)
176 >                : GenericData(id),  ContainerType<ElemDataType> () {}
177 >            
178 >            STLContainerTypeData(const SelfType& s) : SelfType(s){}
179  
180 <  public:
181 <    ZConsParaData();
182 <    void addItem(ZConsParaItem& item) {data.push_back(item);}
112 <    vector<ZConsParaItem>* getData() {return &data;}
113 <    void setData(vector<ZConsParaItem>& theData) {data = theData;}
114 <    void sortByIndex();
115 <    bool isIndexUnique();
180 >            SelfType& operator =(const SelfType& s){
181 >                if (this == &s)
182 >                    return *this;
183  
184 <  private:
185 <    vector<ZConsParaItem> data;
186 <  };
184 >                STLContainerType::operator=(s);
185 >                return *this;
186 >            }
187 >    };
188  
189 < class ZConsParaSortCriterion{
190 <  public:
191 <    bool operator ()(const ZConsParaItem& item1, const ZConsParaItem& item2){
192 <      return item1.zconsIndex < item2.zconsIndex;
193 <    }
189 >    /**
190 >     * @typedef IntVectorGenericData
191 >     * A generic data type contains a vector<int> variable.
192 >     */  
193 >    typedef STLContainerTypeData<std::vector, int> IntVectorGenericData;
194  
195 < };
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 < //Declaration of IntData
203 < ////////////////////////////////////////////////////////////////////////////////
204 < class DoubleArrayData : public GenericData{
201 >    /**
202 >     * @typedef IntVectorGenericData
203 >     * A generic data type contains a vector<double> variable.
204 >     */  
205 >    typedef STLContainerTypeData<std::vector, double> DoubleVectorGenericData;
206  
207 < public:
208 <   vector<double> getData() const  {  return data; }
209 <   void setData(double* source, int num){
210 <    data.clear();
211 <    for(int i = 0; i < num; i++)
212 <     data.push_back(source[i]);
213 <   }
214 < protected:
215 <   vector<double> data;
216 < };
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 < //Declaration of AtomData
239 < ////////////////////////////////////////////////////////////////////////////////
240 < struct AtomInfo : public GenericData{
241 <  public:
242 <    string AtomType;
243 <    double pos[3];
244 <    double dipole[3];  
245 < };
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 < class AtomData : public GenericData{
248 <  public:
157 <    ~AtomData();
158 <    void addAtomInfo(AtomInfo* info) {data.push_back(info);}
159 <    void clearAllAtomInfo();
160 <    AtomInfo* beginAtomInfo(vector<AtomInfo*>::iterator& i){
161 <      i = data.begin();
162 <      return i != data.end()? *i : NULL;
163 <    }
164 <    AtomInfo* nextAtomInfo(vector<AtomInfo*>::iterator& i){
165 <      ++i;
166 <      return i != data.end()? *i: NULL;
167 <    }
168 <    vector<AtomInfo*> getData() {return data;}
169 <    int getSize() {return data.size();}
170 <  protected:
171 <    vector<AtomInfo*> data;
172 < };
173 <
174 < #endif
247 > } // namespace oopse
248 > #endif //UTIL _GENERICDATA_HPP

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines