11
11
#ifndef PARAMETER_h
12
12
#define PARAMETER_h
13
13
#include " VParameterNode.hh"
14
+ #include " ParameterIOimpl.hh"
14
15
#include " Message.hh"
16
+ #include " phrase.hh"
17
+
15
18
#include < stdexcept>
16
19
#include < string>
17
20
#include < cstdlib>
18
21
#include < typeinfo>
19
22
#include < ctime>
20
23
#include < cstdio>
21
24
25
+
26
+
22
27
/* * @class Parameter
23
28
@brief Template implementation of VParamterNode, allows any variable to be
24
29
set/read via stl iostreams
25
30
26
31
@ingroup ConfigHandler
27
32
*/
33
+
34
+ class ParameterList ;
35
+
28
36
template <class T > class Parameter : public VParameterNode {
29
37
public:
30
38
// / DefaultConstructor
@@ -38,51 +46,60 @@ public:
38
46
// / Assignment operator
39
47
Parameter& operator =(const Parameter& right){ _val=right._val ; return *this ;}
40
48
// / Return the underlying variable by reference
41
- const T& GetValue (){ return _val; }
49
+ const T& GetValue () const { return _val; }
50
+ // /Return the underlying variable by pointer
51
+ const T* GetPointer () const { return &_val; }
42
52
// / Print information about this parameter
43
53
virtual int PrintHelp (const std::string& myname=" " ) const ;
54
+
55
+ // /Clone to new ParameterList
56
+ virtual Parameter<T>* Clone (const void * from, void * to) const ;
57
+
58
+ // /access to the type of parameter for copy usage
59
+ typedef T param_type;
44
60
protected:
45
61
// / Read the underlying variable from an istream
46
62
virtual std::istream& ReadFrom ( std::istream& in , bool single=false );
47
63
// / Write the underlying variable to an ostream
48
- virtual std::ostream& WriteTo ( std::ostream& out , bool , int );
49
- // / Read unsigned integers from istreams possibly in hex format
50
- virtual unsigned long ReadUnsignedInt (std::istream& in){
51
- std::string temp;
52
- in>>temp;
53
- if (temp[ 0 ] == ' 0 ' && (temp[ 1 ] == ' x ' || temp[ 1 ] == ' X ' ))
54
- return std::strtoul (temp. c_str (), 0 , 16 );
55
- else
56
- return std::strtoul (temp. c_str (), 0 , 10 );
57
- }
64
+ virtual std::ostream& WriteTo ( std::ostream& out , bool showhelp= false ,
65
+ int indent= 0 ) const ;
66
+
67
+ // implementation of read/write is done with impls so we can overload
68
+ /* std::istream& read_impl(std::istream& in, T& t){ return in>>t; }
69
+ std::ostream& write_impl(std::ostream& out, T& t,
70
+ bool showhelp=false, int indent=0){ return out<<t; }
71
+ */
72
+ // specific impl overload
73
+
58
74
private:
59
75
T& _val; // /< reference to the wrapped underyling variable
60
76
};
61
-
62
- // template members must go here so they'll actually get compiled
63
- template <class T >
77
+
78
+ template <class T >
64
79
inline std::istream& Parameter<T>::ReadFrom(std::istream& in, bool )
65
80
{
66
- if ( !( in >> _val ) && !in.eof ()){
81
+ if ( !ParameterIOimpl::read (in, _val) && !in.eof ()){
67
82
Message e (EXCEPTION);
68
83
e<<" Error trying to read parameter with default key " <<_default_key<<" !\n " ;
69
84
throw std::invalid_argument (e.str ());
70
85
}
71
86
return in;
72
87
}
73
88
74
- template <class T >
75
- inline std::ostream& Parameter<T>::WriteTo(std::ostream& out, bool , int )
89
+ template <class T >
90
+ inline std::ostream& Parameter<T>::WriteTo(std::ostream& out, bool showhelp , int indent) const
76
91
{
77
- return out<< _val;
92
+ return ParameterIOimpl::write ( out, _val, showhelp, indent) ;
78
93
}
79
94
80
95
template <class T >
81
96
inline int Parameter<T>::PrintHelp(const std::string& myname) const
82
97
{
83
98
VParameterNode::PrintHelp (myname);
84
99
std::cout<<" Parameter type: " <<typeid (_val).name ()<<" \n "
85
- <<" Current Value: " <<_val<<std::endl;
100
+ <<" Current Value: " ;
101
+ ParameterIOimpl::write (std::cout, _val, true );
102
+ std::cout<<std::endl;
86
103
std::string dummy;
87
104
std::cout<<" \n Hit <enter> to continue." ;
88
105
std::getline (std::cin, dummy);
@@ -91,107 +108,12 @@ inline int Parameter<T>::PrintHelp(const std::string& myname) const
91
108
return 0 ;
92
109
}
93
110
94
- // / Specific ostream overload for booleans
95
- template <> inline std::ostream& Parameter<bool >::WriteTo(std::ostream& out, bool , int )
111
+ template < class T > inline
112
+ Parameter<T>* Parameter<T >::Clone( const void * from, void * to) const
96
113
{
97
- return out<<std::boolalpha<<_val<<std::noboolalpha;
114
+ unsigned diff = (const char *)(GetPointer ()) - (const char *)from;
115
+ return new Parameter<T>((T&)(*( (char *)to+diff )), _default_key, _helptext);
98
116
}
99
117
100
- // / specific istream overload for booleans
101
- template <> inline std::istream& Parameter<bool >::ReadFrom(std::istream& in, bool )
102
- {
103
- std::string temp;
104
- in>>temp;
105
- if (temp == " 1" || temp == " true" || temp == " TRUE" )
106
- _val = true ;
107
- else if ( temp == " 0" || temp == " false" || temp == " FALSE" )
108
- _val = false ;
109
- else {
110
- Message e (EXCEPTION);
111
- e<<" Expected boolean value, got " <<temp<<std::endl;
112
- throw std::invalid_argument (e.str ());
113
- }
114
- return in;
115
- }
116
-
117
- // / Write the 0x prefix on unsigned integers
118
- template <> inline std::ostream& Parameter<unsigned >::WriteTo(std::ostream& out, bool , int )
119
- {
120
- return out<<std::hex<<std::showbase<<_val<<std::noshowbase<<std::dec;
121
- }
122
-
123
- // / Write the 0x prefix on unsigned integers
124
- template <> inline std::ostream& Parameter<unsigned char >::WriteTo(std::ostream& out, bool ,int )
125
- {
126
- return out<<std::hex<<std::showbase<<_val<<std::noshowbase<<std::dec;
127
- }
128
-
129
- // / Write the 0x prefix on unsigned integers
130
- template <> inline std::ostream& Parameter<unsigned short >::WriteTo(std::ostream& out, bool , int )
131
- {
132
- return out<<std::hex<<std::showbase<<_val<<std::noshowbase<<std::dec;
133
- }
134
118
135
- // / Write the 0x prefix on unsigned integers
136
- template <> inline std::ostream& Parameter<unsigned long >::WriteTo(std::ostream& out, bool , int )
137
- {
138
- return out<<std::hex<<std::showbase<<_val<<std::noshowbase<<std::dec;
139
- }
140
-
141
- // / Write the 0x prefix on unsigned integers
142
- template <> inline std::ostream& Parameter<unsigned long long >::WriteTo(std::ostream& out, bool , int )
143
- {
144
- return out<<std::hex<<std::showbase<<_val<<std::noshowbase<<std::dec;
145
- }
146
-
147
- // / Read unsigned ints either in decimal or hex format
148
- template <> inline std::istream& Parameter<unsigned >::ReadFrom(std::istream& in, bool )
149
- {
150
- _val = ReadUnsignedInt (in); return in;
151
- }
152
-
153
- // / Read unsigned ints either in decimal or hex format
154
- template <> inline std::istream& Parameter<unsigned char >::ReadFrom(std::istream& in, bool )
155
- {
156
- _val = ReadUnsignedInt (in); return in;
157
- }
158
-
159
- // / Read unsigned ints either in decimal or hex format
160
- template <> inline std::istream& Parameter<unsigned short >::ReadFrom(std::istream& in, bool )
161
- {
162
- _val = ReadUnsignedInt (in); return in;
163
- }
164
-
165
- // / Read unsigned ints either in decimal or hex format
166
- template <> inline std::istream& Parameter<unsigned long >::ReadFrom(std::istream& in, bool )
167
- {
168
- _val = ReadUnsignedInt (in); return in;
169
- }
170
-
171
- // / Read unsigned ints either in decimal or hex format
172
- template <> inline std::istream& Parameter<unsigned long long >::ReadFrom(std::istream& in, bool )
173
- {
174
- _val = ReadUnsignedInt (in); return in;
175
- }
176
-
177
- // /Override std::string's to let "" be an empty string
178
- template <> inline std::istream& Parameter<std::string>::ReadFrom(std::istream& in, bool )
179
- {
180
- std::string temp;
181
- if (in>>temp){
182
- if (temp == " \"\" " )
183
- _val = " " ;
184
- else
185
- _val = temp;
186
- }
187
- return in;
188
- }
189
-
190
- // /Override std::string's to let "" be an empty string
191
- template <> inline std::ostream& Parameter<std::string>::WriteTo(std::ostream& out, bool , int )
192
- {
193
- if (_val == " " )
194
- return out<<" \"\" " ;
195
- return out<<_val;
196
- }
197
119
#endif
0 commit comments