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

Comparing trunk/OOPSE-4/src/io/Globals.hpp (file contents):
Revision 2425 by chrisfen, Fri Nov 11 15:22:11 2005 UTC vs.
Revision 2447 by chuckv, Wed Nov 16 21:37:45 2005 UTC

# Line 53 | Line 53
53   #include "types/Component.hpp"
54   #include "types/MakeStamps.hpp"
55   #include "types/ZconStamp.hpp"
56 #include "utils/CaseConversion.hpp"
56  
57 < template<typename T>
59 < struct ParameterTraits;
57 > #include "utils/ParameterManager.hpp"
58  
61 //string
62 template<>                    
63 struct ParameterTraits<std::string>{
64  typedef std::string RepType;       // Representation type of the value
65
66 template<typename T> static bool    convert(T v, RepType& r){return false;} // !NB everything is ok
67 template<typename T> static RepType convert(T v)            {RepType tmp; convert(v,tmp);return tmp;}
68  static bool convert(RepType v, RepType& r) { r = v; return true;}
69  static std::string getParamType() { return "string";}  
70 };
71 //bool
72 template<>                    
73 struct ParameterTraits<bool>{
74  typedef bool RepType;
75  template<typename T> static bool    convert(T, RepType&){return false;}
76  template<typename T> static RepType convert(T v)        {RepType tmp; convert(v,tmp);return tmp;}
77  static bool convert(std::string v, RepType& r) {
78    oopse::toLower(v);
79    bool result = false;
80    if (v == "true") {
81        r = true;
82        result = true;
83    } else if (v == "false") {
84        r = false;
85        result = true;
86    }
87    
88     return result;
89  }
90  static std::string getParamType() { return "bool";}  
91 };
92
93 //int  
94 template<>
95 struct ParameterTraits<int>{
96    typedef int RepType;
97    template<typename T> static bool    convert(T, RepType&){return false;}
98    template<typename T> static RepType convert(T v)        {RepType tmp; convert(v,tmp);return tmp;}
99    static bool convert(RepType v, RepType& r)            { r=v; return true;}
100    static std::string getParamType() { return "int";}  
101 };
102
103 //double
104 template<>                    
105 struct ParameterTraits<double>{
106    typedef double RepType;
107    template<typename T> static bool    convert(T, RepType&){return false;}
108    template<typename T> static RepType convert(T v)        {RepType tmp; convert(v,tmp);return tmp;}
109    static bool convert(RepType v, RepType& r)            {r=v; return true;}
110    static bool convert(int v, RepType& r)                {r = static_cast<double>(v); return true;}
111    static std::string getParamType() { return "double";}    
112 };
113
114
115 class ParameterBase {
116  public:    
117    ParameterBase() : keyword_(), optional_(false), defaultValue_(false), empty_(true) {}
118    virtual ~ParameterBase() {}
119    bool isOptional() {return optional_;}
120    void setOptional(bool optional) {optional_ = optional;}
121    bool hasDefaultValue() {return defaultValue_;}
122    virtual bool isValid() { return true;}
123    const std::string& getKeyword() {return keyword_;}
124    void setKeyword(const std::string& keyword) { keyword_ = keyword;}
125    bool empty() {return empty_;}
126    virtual bool setData(std::string) = 0;
127    virtual bool setData(int) = 0;
128    virtual bool setData(double) = 0;
129    virtual std::string getParamType() = 0;
130  protected:
131    std::string keyword_;
132    bool optional_;
133    bool defaultValue_;
134    bool empty_;
135 };
136
137 template<class ParamType>
138 class Parameter : public ParameterBase{
139   public:    
140    typedef ParameterTraits<ParamType> ValueType;
141     void setDefaultValue(const ParamType& value) {data_ = value; defaultValue_ = true;}
142     ParamType getData() { return data_;}
143
144    virtual bool setData(std::string sval) {
145        return internalSetData<std::string>(sval);
146    }
147    virtual bool setData(int ival) {
148        return internalSetData<int>(ival);
149    }
150    
151    virtual bool setData(double dval) {
152        return internalSetData<double>(dval);
153    }
154
155    virtual std::string getParamType() { return ParameterTraits<ParamType>::getParamType();}
156   private:
157     template<class T> bool internalSetData(T data) {
158        ParamType tmp;
159        bool result = ValueType::convert(data, tmp);
160        if (result) {
161            empty_ = false;
162            data_ = tmp;
163        }
164        return result;
165     }
166    
167   private:
168     ParamType data_;
169      
170 };
171
172 #define DeclareParameter(NAME, TYPE)         \
173    private:                                                   \
174      Parameter<TYPE> NAME;                                     \
175    public:                                                      \
176      bool have##NAME() { return !NAME.empty();}  \
177      TYPE get##NAME() { return NAME.getData();}
178
179
59   class Globals {
60    public:
61      Globals();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines