STK++ 0.9.13
STK_Option.cpp
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2010 Serge Iovleff
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23 */
24
25/*
26 * Project: stkpp::Dmanager
27 * created on: 18 oct. 2010
28 * Purpose: .
29 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
30 *
31 **/
32
37#include <Sdk.h>
38
39#include "../include/STK_IPage.h"
40
41namespace STK
42{
43
44/* defaut constructor
45 * @param name the name of the Option
46 **/
47Option::Option( String const& name, TypeOption type, bool isOptional)
48 : name_(name)
49 , sep_(CHAR_SEP)
50 , type_(type)
51 , isOptional_(isOptional)
52 , isValued_(false)
53 , p_String_(0)
54{
57}
58
59/* Special constructor. This will construct an option as a sub-page. The
60 * name of the option will be the name of the page.
61 * @param page the page to set as option
62 **/
64 : name_(page.name())
65 , sep_(CHAR_SEP)
66 , type_(page_)
67 , isOptional_(page.isOptional())
68 , isValued_(true)
69 , p_Page_(page.clone())
70{
72}
73
74/* Copy constructor.
75 * @param opt the Option to copy
76 **/
78 : name_(opt.name_)
79 , sep_(opt.sep_)
80 , type_(opt.type_)
81 , isOptional_(opt.isOptional_)
82 , isValued_(opt.isOptional_)
83 , p_String_(0)
84{
85 // copy rhs value
86 switch (type_)
87 {
88 case string_:
89 if (opt.p_String_) set(*(opt.p_String_));
90 else p_String_ = 0;
91 break;
92 case real_:
93 if (opt.p_Real_) set(*(opt.p_Real_));
94 else p_Real_ = 0;
95 break;
96 case integer_:
97 if (opt.p_int_) set(*(opt.p_int_));
98 else p_int_ = 0;
99 break;
100 case range_:
101 if (opt.p_Range_) set(*(opt.p_Range_));
102 else p_Range_ = 0;
103 break;
104 case lstring_:
105 if (opt.p_lString_) set(*(opt.p_lString_));
106 else p_lString_ = 0;
107 break;
108 case lreal_:
109 if (opt.p_lReal_) set(*(opt.p_lReal_));
110 else p_lReal_ = 0;
111 break;
112 case linteger_:
113 if (opt.p_lint_) set(*(opt.p_lint_));
114 else p_lint_ = 0;
115 break;
116 case lrange_:
117 if (opt.p_lRange_) set(*(opt.p_lRange_));
118 else p_lRange_ = 0;
119 break;
120 case page_:
121 if (opt.p_Page_) setPage(*(opt.p_Page_));
122 else p_Page_ = 0;
123 break;
124 case unknown_:
126 break; // avoid warning
127 };
128}
129
130/* destructor.n*/
132
133/* copy */
135{
136 // Do the assignment operation of the members
137 name_ = opt.name_;
138 sep_ = opt.sep_;
139 type_ = opt.type_;
140 isOptional_ = opt.isOptional_;
141 isValued_ = opt.isValued_;
142 // copy rhs value
143 switch (type_)
144 {
145 case string_:
146 if (opt.p_String_) set(*(opt.p_String_));
147 else p_String_ = 0;
148 break;
149 case real_:
150 if (opt.p_Real_) set(*(opt.p_Real_));
151 else p_Real_ = 0;
152 break;
153 case integer_:
154 if (opt.p_int_) set(*(opt.p_int_));
155 else p_int_ = 0;
156 break;
157 case range_:
158 if (opt.p_Range_) set(*(opt.p_Range_));
159 else p_Range_ = 0;
160 break;
161 case lstring_:
162 if (opt.p_lString_) set(*(opt.p_lString_));
163 else p_lString_ = 0;
164 break;
165 case lreal_:
166 if (opt.p_lReal_) set(*(opt.p_lReal_));
167 else p_lReal_ = 0;
168 break;
169 case linteger_:
170 if (opt.p_lint_) set(*(opt.p_lint_));
171 else p_lint_ = 0;
172 break;
173 case lrange_:
174 if (opt.p_lRange_) set(*(opt.p_lRange_));
175 else p_lRange_ = 0;
176 break;
177 case page_:
178 if (opt.p_Page_) setPage(*(opt.p_Page_));
179 else p_Page_ = 0;
180 break;
181 case unknown_:
182 STKRUNTIME_ERROR_NO_ARG(Option::operator=,Unknown type option.);
183 break; // avoid warning
184 };
185 // return this
186 return *this; // Return a reference to myself.
187}
188/* Convert a string in a value
189 * @param str the string to convert
190 * @return @c true if the conversion success, @c false otherwise
191 */
193{
194 // choose type
195 switch (type_)
196 {
197 case string_:
198 set(str);
199 break;
200 case real_:
201 {
205 }
206 break;
207 case integer_:
208 {
209 int integerValue;
212 }
213 break;
214 case range_:
215 {
219 }
220 break;
221 case lstring_:
222 {
223 std::list<String> lStringValue;
226 }
227 break;
228 case lreal_:
229 {
230 std::list<Real> lRealValue;
233 }
234 break;
235 case linteger_:
236 {
237 std::list<int> lintValue;
239 set(lintValue);
240 }
241 break;
242 case lrange_:
243 {
244 std::list<Range> lRangeValue;
247 }
248 break;
249 case page_:
251 break;
252 case unknown_:
254 break; // avoid warning
255 };
256 // error if an error occur in readList ?
257 return true;
258}
259
260/* set a value from a Page.
261 * @param value the Page to set
262 **/
263void Option::setPage( IPage const& value )
264{
265 deleteValue();
266 p_Page_ = value.clone();
267 isValued_ = true;
268 type_ = page_;
269}
270
271
272/* @brief write out the option in the output stream
273 * @param os output stream
274 */
276{
277 // write option name if it's not a page
278 if (type_ != page_)
279 { // write name and " = "
280 os << name_ << _T(" ") << CHAR_EQUAL << _T(" ");
281 }
282 // write option value
283 switch (type_)
284 {
285 case string_:
286 if (p_String_)
287 os << *p_String_;
288 break;
289 case real_:
290 if (p_Real_)
291 os << *p_Real_;
292 break;
293 case integer_:
294 if (p_int_)
295 os << *p_int_;
296 break;
297 case range_:
298 if (p_Range_)
299 os << *p_Range_;
300 break;
301 case lstring_:
302 if (p_lString_)
304 break;
305 case lreal_:
306 if (p_lReal_)
308 break;
309 case linteger_:
310 if (p_lint_)
312 break;
313 case lrange_:
314 if (p_lRange_)
316 break;
317 case page_:
318 if (p_Page_)
319 p_Page_->write(os);
320 break;
321 case unknown_:
323 break; // avoid warning
324 };
325}
326
327/* @brief read out the option from the input stream
328 * @param is input stream
329 */
331{
332 // read option value
333 if (type_ == page_)
334 {
335 if (p_Page_)
336 {
337 p_Page_->read(is);
338 if (!p_Page_->validate())
340 }
341 };
342}
343
344/* set a value from string.
345 * @param value the string value to set
346 **/
347void Option::set( String const& value )
348{
349 deleteValue();
350 p_String_ = new String(value);
351 isValued_ = true;
352}
353
354/* set a value from a Real.
355 * @param value the real value to set
356 **/
357void Option::set( Real const& value )
358{
359 deleteValue();
360 p_Real_ = new Real(value);
361 isValued_ = true;
362}
363
364/* set a value from an int.
365 * @param value the integer value to set
366 **/
367void Option::set( int const& value )
368{
369 deleteValue();
370 p_int_ = new int(value);
371 isValued_ = true;
372}
373
374/* set a value from a Range.
375 * @param value the Range value to set
376 **/
377void Option::set( Range const& value )
378{
379 deleteValue();
380 p_Range_ = new Range(value);
381 isValued_ = true;
382}
383
384/* set a value from a list of Real.
385 * @param value the list real values to set
386 **/
387void Option::set( std::list<String> const& value )
388{
389 deleteValue();
390 p_lString_ = new std::list<String>(value);
391 isValued_ = true;
392}
393
394/* set a value from a list of Real.
395 * @param value the list real values to set
396 **/
397void Option::set( std::list<Real> const& value )
398{
399 deleteValue();
400 p_lReal_ = new std::list<Real>(value);
401 isValued_ = true;
402}
403
404/* set a value from a list of int.
405 * @param value the list of integer value to set
406 **/
407void Option::set( std::list<int> const& value )
408{
409 deleteValue();
410 p_lint_ = new std::list<int>(value);
411 isValued_ = true;
412}
413/* set a value from a list of Range.
414 * @param value the list of Range to set
415 **/
416void Option::set( std::list<Range> const& value )
417{
418 deleteValue();
419 p_lRange_ = new std::list<Range>(value);
420 isValued_ = true;
421}
422
423// delete allocated value
425{
426 switch (type_)
427 {
428 case string_:
429 if (p_String_) delete p_String_;
430 p_String_ = 0;
431 break;
432 case real_:
433 if (p_Real_) delete p_Real_;
434 p_Real_ = 0;
435 break;
436 case integer_:
437 if (p_int_) delete p_int_;
438 p_int_ = 0;
439 break;
440 case range_:
441 if (p_Range_) delete p_Range_;
442 p_Range_ = 0;
443 break;
444 case lstring_:
445 if (p_lString_) delete p_lString_;
446 p_lString_ = 0;
447 break;
448 case lreal_:
449 if (p_lReal_) delete p_lReal_;
450 p_lReal_ = 0;
451 break;
452 case linteger_:
453 if (p_lint_) delete p_lint_;
454 p_lint_ = 0;
455 break;
456 case lrange_:
457 if (p_lRange_) delete p_lRange_;
458 p_lRange_ = 0;
459 break;
460 case page_:
461 if (p_Page_) delete p_Page_;
462 p_Page_ = 0;
463 break;
464 default:
465 break;
466 };
467 isValued_ = false;
468}
469
470/*set A default value to the option.
471 */
473{
474 //deleteValue();
475 switch (type_)
476 {
477 case string_:
478 p_String_ = new String;
480 break;
481 case real_:
482 p_Real_ = new Real;
484 break;
485 case integer_:
486 p_int_ = new int;
488 break;
489 case range_:
490 p_Range_ = new Range;
491 break;
492 case lstring_:
493 p_lString_ = new std::list<String>;
494 break;
495 case lreal_:
496 p_lReal_ = new std::list<Real>;
497 break;
498 case linteger_:
499 p_lint_ = new std::list<int>;
500 break;
501 case lrange_:
502 p_lRange_ = new std::list<Range>;
503 break;
504 case page_:
505 p_Page_ = 0;
506 break;
507 default:
508 break;
509 };
510}
511
512} // namespace STK
#define STKRUNTIME_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:129
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
#define _T(x)
Let x unmodified.
This file include all the other header files of the project Sdk.
A IPage is an interface base class for reading and/or writing a page of option in a file.
Definition STK_IPage.h:94
void write(ostream &os) const
write out options in a stream
virtual bool validate()
validate the page.
Definition STK_IPage.h:193
virtual IPage * clone() const
Definition STK_IPage.h:195
String const & msg_error() const
name of the IPage.
Definition STK_IPage.h:132
void read(istream &is)
read in options from an input steam stream
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
the Option class allow to store the value of an option from a IPage.
Definition STK_Option.h:56
void read(istream &is)
read in the options from the input stream.
Range * p_Range_
Definition STK_Option.h:255
Option & operator=(const Option &opt)
Assignment operator.
void setPage(IPage const &value)
set a value from a Page.
void setDefaultValue()
set a default value of the option.
IPage * p_Page_
Definition STK_Option.h:260
bool setValue(String const &str)
Convert a string in a value.
std::list< int > * p_lint_
Definition STK_Option.h:258
String * p_String_
Definition STK_Option.h:252
Real * p_Real_
Definition STK_Option.h:253
void write(ostream &os) const
write out the options in the output stream
Char sep_
Char used for the option list.
Definition STK_Option.h:241
TypeOption type_
type of the option
Definition STK_Option.h:243
std::list< String > * p_lString_
Definition STK_Option.h:256
~Option()
Destructor.
bool isOptional_
true if the option is optional, false otherwise
Definition STK_Option.h:245
bool isValued_
true if the option is valued, false otherwise.
Definition STK_Option.h:248
Option(String const &name, TypeOption type=string_, bool isOptional=true)
constructor.
TypeOption type() const
type of the option.
Definition STK_Option.h:120
std::list< Range > * p_lRange_
Definition STK_Option.h:259
std::list< Real > * p_lReal_
Definition STK_Option.h:257
String name_
name of the option
Definition STK_Option.h:239
void deleteValue()
Remove the value of the option.
void set(String const &value)
set a value from string.
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
bool stringToType(Type &t, String const &s, std::ios_base &(*f)(std::ios_base &)=std::dec)
convert a String to Type
Definition STK_String.h:195
String const & toUpperString(String &s)
convert the characters of the String to upper case
Definition STK_String.h:134
String stringNa
Representation of a Not Available value.
void readList(String const &strBuffer, std::list< TYPE > &lst, Char sep=CHAR_SEP)
Read a list of value of type TYPE stored in a line.
TypeOption
A TypeOption is the kind of options that can read or write a program in a file.
Definition STK_Option.h:72
void writeList(ostream &os, std::list< TYPE > const &lst, Char sep=CHAR_SEP)
Write a list of value of type TYPE stored in a line.
@ linteger_
list of discrete numbers separated by commas
Definition STK_Option.h:80
@ page_
a page option
Definition STK_Option.h:82
@ range_
range of number
Definition STK_Option.h:77
@ lstring_
list of strings separated by commas
Definition STK_Option.h:78
@ lreal_
list of floating numbers separated by commas
Definition STK_Option.h:79
@ integer_
discrete number
Definition STK_Option.h:76
@ lrange_
list of range of number separated by commas
Definition STK_Option.h:81
@ string_
characters
Definition STK_Option.h:74
@ real_
floating
Definition STK_Option.h:75
std::basic_string< Char > String
STK fundamental type of a String.
double Real
STK fundamental type of Real values.
std::basic_ostream< Char > ostream
ostream for Char
Definition STK_Stream.h:57
std::basic_istream< Char > istream
istream for Char
Definition STK_Stream.h:55
The namespace STK is the main domain space of the Statistical ToolKit project.
TRange< UnknownSize > Range
Definition STK_Range.h:59
static Type NA()
Adding a Non Available (NA) special number.