1 /*
2  *  tDataEntry.h
3  *  Avida
4  *
5  *  Called "tDataEntry.hh" prior to 12/7/05.
6  *  Copyright 1999-2011 Michigan State University. All rights reserved.
7  *  Copyright 1993-2003 California Institute of Technology.
8  *
9  *
10  *  This file is part of Avida.
11  *
12  *  Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
13  *  as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
14  *
15  *  Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public License along with Avida.
19  *  If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef tDataEntry_h
24 #define tDataEntry_h
25 
26 #include <iostream>
27 
28 #ifndef cFlexVar_h
29 #include "cFlexVar.h"
30 #endif
31 #ifndef cString_h
32 #include "cString.h"
33 #endif
34 #ifndef cStringList_h
35 #include "cStringList.h"
36 #endif
37 #ifndef cStringUtil_h
38 #include "cStringUtil.h"
39 #endif
40 
41 
42 template <class TargetType> class tDataEntry
43 {
44 private:
45   cString m_name;            // Short Name
46   cString m_desc;            // Descriptive Name
47   int m_compare_type;        // ID to indicate how values should be compared.
48   cString m_null_value;      // Value when "off", such as "0", "Inf.", or "N/A"
49   cString m_html_table_flags; // String to include in <td> entry in html mode.
50   cStringList m_default_args;
51 
52 public:
53   tDataEntry(const cString& name, const cString& desc, int compare_type = 0,
54              const cString& null = "0", const cString& html_cell = "align=center")
m_name(name)55   : m_name(name), m_desc(desc), m_compare_type(compare_type), m_null_value(null), m_html_table_flags(html_cell) { ; }
~tDataEntry()56   virtual ~tDataEntry() { ; }
57 
GetName()58   const cString& GetName() const { return m_name; }
GetDesc(const TargetType * target,const cFlexVar & idx)59   virtual cString GetDesc(const TargetType* target, const cFlexVar& idx) const { return m_desc; }
GetCompareType()60   int GetCompareType() const { return m_compare_type; }
GetNull()61   const cString& GetNull() const { return m_null_value; }
GetHtmlCellFlags()62   const cString& GetHtmlCellFlags() const { return m_html_table_flags; }
63 
Set(TargetType * target,const cFlexVar &,const cStringList &,const cString &)64   virtual bool Set(TargetType* target, const cFlexVar&, const cStringList&, const cString&) const { return false; }
65   virtual cFlexVar Get(const TargetType* target, const cFlexVar& idx, const cStringList& args) const = 0;
Get(const TargetType * target)66   virtual cFlexVar Get(const TargetType* target) const { return Get(target, 0, m_default_args); }
67 };
68 
69 template <class TargetType, class EntryType> class tDataEntryOfType;
70 
71 template <class TargetType, class EntryType>
72   class tDataEntryOfType<TargetType, EntryType ()> : public tDataEntry<TargetType>
73 {
74 protected:
75   EntryType  (TargetType::*DataGet)() const;
76   void (TargetType::*DataSet)(EntryType);
77 
78 public:
79   tDataEntryOfType(const cString& name, const cString& desc,
80                    EntryType (TargetType::*_funR)() const, void (TargetType::*_funS)(EntryType _val) = NULL,
81                    int compare_type = 0, const cString& null = "0", const cString& html_cell = "align=center")
82   : tDataEntry<TargetType>(name, desc, compare_type, null, html_cell), DataGet(_funR), DataSet(_funS) { ; }
83 
Set(TargetType * target,const cFlexVar &,const cStringList &,const cString & value)84   bool Set(TargetType* target, const cFlexVar&, const cStringList&, const cString& value) const
85   {
86     assert(target != NULL);
87     EntryType new_value(0);
88     if (DataSet == 0) return false;
89     (target->*DataSet)(cStringUtil::Convert(value, new_value));
90     return true;
91   }
92 
Get(const TargetType * target,const cFlexVar &,const cStringList &)93   cFlexVar Get(const TargetType* target, const cFlexVar&, const cStringList&) const
94   {
95     assert(target != NULL);
96     return cFlexVar((target->*DataGet)());
97   }
98 };
99 
100 
101 template <class TargetType, class EntryType, class IdxType>
102   class tDataEntryOfType<TargetType, EntryType (IdxType)> : public tDataEntry<TargetType>
103 {
104 protected:
105   EntryType (TargetType::*DataRetrieval)(IdxType) const;
106   cString (TargetType::*DescFunction)(IdxType) const;
107 
108 public:
109   tDataEntryOfType(const cString& name,
110                    cString (TargetType::*_funD)(IdxType) const,
111                    EntryType (TargetType::*_funR)(IdxType) const,
112                    int compare_type = 0, const cString& null = "0", const cString& html_cell = "align=center")
113   : tDataEntry<TargetType>(name, name, compare_type, null, html_cell), DataRetrieval(_funR), DescFunction(_funD) { ; }
114 
GetDesc(const TargetType * target,const cFlexVar & idx)115   cString GetDesc(const TargetType* target, const cFlexVar& idx) const
116   {
117     return (target->*DescFunction)(idx.As<IdxType>());
118   }
119 
120 
Get(const TargetType * target,const cFlexVar & idx,const cStringList &)121   cFlexVar Get(const TargetType* target, const cFlexVar& idx, const cStringList&) const
122   {
123     assert(target != NULL);
124     return cFlexVar((target->*DataRetrieval)(idx.As<IdxType>()));
125   }
126 };
127 
128 
129 template <class TargetType, class EntryType, class IdxType>
130   class tDataEntryOfType<TargetType, EntryType (IdxType, const cStringList&)> : public tDataEntry<TargetType>
131 {
132 protected:
133   EntryType (TargetType::*DataRetrieval)(IdxType, const cStringList&) const;
134   cString (TargetType::*DescFunction)(IdxType) const;
135 
136 public:
137   tDataEntryOfType(const cString& name,
138                    cString (TargetType::*_funD)(IdxType) const,
139                    EntryType (TargetType::*_funR)(IdxType, const cStringList&) const,
140                    int compare_type = 0, const cString& null = "0", const cString& html_cell = "align=center")
141   : tDataEntry<TargetType>(name, name, compare_type, null, html_cell), DataRetrieval(_funR), DescFunction(_funD) { ; }
142 
GetDesc(const TargetType * target,const cFlexVar & idx)143   cString GetDesc(const TargetType* target, const cFlexVar& idx) const
144   {
145     return (target->*DescFunction)(idx.As<IdxType>());
146   }
147 
Get(const TargetType * target,const cFlexVar & idx,const cStringList & args)148   cFlexVar Get(const TargetType* target, const cFlexVar& idx, const cStringList& args) const
149   {
150     assert(target != NULL);
151     return cFlexVar((target->*DataRetrieval)(idx.As<IdxType>(), args));
152   }
153 };
154 
155 
156 template <class TargetType, class IdxType> class tDataEntryProxy;
157 
158 template <class TargetType>
159 class tDataEntryProxy<TargetType, cFlexVar ()> : public tDataEntry<TargetType>
160 {
161 protected:
162   cFlexVar (*DataRetrieval)(const TargetType*);
163 
164 public:
165   tDataEntryProxy(const cString& name, const cString& desc,
166                   cFlexVar (*_funR)(const TargetType*),
167                   int compare_type = 0, const cString& null = "0", const cString& html_cell = "align=center")
168   : tDataEntry<TargetType>(name, desc, compare_type, null, html_cell), DataRetrieval(_funR) { ; }
169 
Get(const TargetType * target,const cFlexVar & idx,const cStringList & args)170   cFlexVar Get(const TargetType* target, const cFlexVar& idx, const cStringList& args) const
171   {
172     assert(target != NULL);
173     return (*DataRetrieval)(target);
174   }
175 };
176 
177 template <class TargetType, class IdxType>
178 class tDataEntryProxy<TargetType, cFlexVar (IdxType)> : public tDataEntry<TargetType>
179 {
180 protected:
181   cFlexVar (*DataRetrieval)(const TargetType*, IdxType);
182   cString (*DescFunction)(const TargetType*, IdxType);
183 
184 public:
185   tDataEntryProxy(const cString& name,
186                   cString (*_funD)(const TargetType*, IdxType),
187                   cFlexVar (*_funR)(const TargetType*, IdxType),
188                   int compare_type = 0, const cString& null = "0", const cString& html_cell = "align=center")
189   : tDataEntry<TargetType>(name, name, compare_type, null, html_cell), DataRetrieval(_funR), DescFunction(_funD) { ; }
190 
GetDesc(const TargetType * target,const cFlexVar & idx)191   cString GetDesc(const TargetType* target, const cFlexVar& idx) const
192   {
193     return (*DescFunction)(target, idx.As<IdxType>());
194   }
195 
Get(const TargetType * target,const cFlexVar & idx,const cStringList & args)196   cFlexVar Get(const TargetType* target, const cFlexVar& idx, const cStringList& args) const
197   {
198     assert(target != NULL);
199     return (*DataRetrieval)(target, idx.As<IdxType>());
200   }
201 };
202 
203 template <class TargetType, class IdxType>
204 class tDataEntryProxy<TargetType, cFlexVar (IdxType, const cStringList&)> : public tDataEntry<TargetType>
205 {
206 protected:
207   cFlexVar (*DataRetrieval)(const TargetType*, IdxType, const cStringList&);
208   cString (*DescFunction)(const TargetType*, IdxType);
209 
210 public:
211   tDataEntryProxy(const cString& name,
212                   cString (*_funD)(const TargetType*, IdxType),
213                   cFlexVar (*_funR)(const TargetType*, IdxType, const cStringList&),
214                   int compare_type = 0, const cString& null = "0", const cString& html_cell = "align=center")
215   : tDataEntry<TargetType>(name, name, compare_type, null, html_cell), DataRetrieval(_funR), DescFunction(_funD) { ; }
216 
GetDesc(const TargetType * target,const cFlexVar & idx)217   cString GetDesc(const TargetType* target, const cFlexVar& idx) const
218   {
219     return (*DescFunction)(target, idx.As<IdxType>());
220   }
221 
Get(const TargetType * target,const cFlexVar & idx,const cStringList & args)222   cFlexVar Get(const TargetType* target, const cFlexVar& idx, const cStringList& args) const
223   {
224     assert(target != NULL);
225     return (*DataRetrieval)(target, idx.As<IdxType>(), args);
226   }
227 };
228 
229 
230 #endif
231