/usr/include/trilinos/OptionsFromStreamPack_OptionsFromStream.hpp is in libtrilinos-dev 10.4.0.dfsg-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | // @HEADER
// ***********************************************************************
//
// Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
// Copyright (2003) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
// Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
//
// ***********************************************************************
// @HEADER
#ifndef OPTIONS_FROM_STREAM_H
#define OPTIONS_FROM_STREAM_H
#include "Moocho_ConfigDefs.hpp"
// /////////////////////////////////////////////////
// Forward declarations.
namespace OptionsFromStreamPack {
namespace OptionsFromStreamUtilityPack {
// Simple class for a boolean variable that is false on construction.
class false_bool_t {
public:
false_bool_t() : val_(false) {}
void set(bool val) { val_ = val; }
operator bool() const { return val_; }
private:
bool val_;
};
// Implementation type for the map for looking up a value given
// a an options name.
typedef std::map< std::string , std::string > option_to_value_map_t;
// Implementation type for an options group's options and
// a boolean variable to determine if this option has been visited yet.
typedef std::pair< option_to_value_map_t, false_bool_t > options_group_pair_t;
// Implementation type of the map for looking up a set of options
// given the option groups name.
typedef std::map< std::string, options_group_pair_t > options_group_map_t;
// The above declarations sets up the data structure:
//
// map<string,options_group_pair_t> (options_group_map_t)
// |
// | value_type
// `.'
// pair<string,options_group_pair_t>
// |
// -----------------------
// | |
// | first | second
// `.' `.'
// string pair<option_to_value_map_t,false_bool_t> (options_group_pair_t)
// | |
// "solver_options" |
// |
// ------------------------------------------------
// | |
// | first | second
// `.' `.'
// map<string,string> (option_to_value_map_t) false_bool_t
// | |
// | value_type "true"
// `.'
// pair<string,string>
// |
// --------------
// | first | second
// `.' `.'
// string string
// | |
// "tol" "1e-6"
//
class OptionsGroup;
}
class OptionsFromStream;
}
// //////////////////////////////////////////////////
// Class and function declarations
namespace OptionsFromStreamPack {
/* \defgroup OptionsFromStreamPack_grp Untilities for maintianing a simple options database.
* \ingroup Misc_grp
*
*/
// @ {
namespace OptionsFromStreamUtilityPack {
/** \brief Class used to encapsulate options belonging an options group.
*
* This class offers two ways to access the options and their values in
* an options group. These two methods differ in their convience,
* the cost of performing them and the level of validation possible.
* In the following discussion, n_total
* is the total number of options being checked for and n_set is the
* number of options set.
*
* 1) Looking up the values directly.
*
* Here the software client just looks up the value directly. If the option with the given
* name does not exist then the function <tt>option_exists( ... )</tt> will return <tt>false</tt>.
*
* In the following example, the lookup of the option named "tol" is attempted
* on the <tt>OptionsGroup</tt> object <tt>optgrp</tt>.
\verbatim
const std::string& val = optgrp.option_value( "tol" );
if( OptionsGroup::option_exists( val ) )
std::cout << "\ntol = " << val;
\endverbatim
* The total cost of this way of looking up option values is:
*
* O(n_total*log(n_set)).
*
* The disadvantage of using this method is that if the user misspelled the
* name of the option then the option will not be found or set. In the
* above example, if the user misspelled the option "tol" as "tal" then
* <tt>OptionsGroup::option_exists( val )</tt> would return false and the option
* value would not be accessed.
*
* 2) Setting options by iterating through options present.
*
* In this method the software client must keep a map to translate from an options
* name to an id representing the option. Then a switch can be used to take
* action.
*
* In the following example (see the example from \Ref{OptionsFromStream}
* , three possible options are looked for.
* Here the client has defined an enum for the options and initialized
* a \Ref{StringToIntMap}.
*
\verbatim
const char optgrp_name[] = "MySolverOptions";
const int num_opt = 3;
enum EOptions {
TOL
,MAX_ITER
,PROB_TYPE
};
const char* SOptions[num_opt] = {
"tol"
,"max_iter"
,"prob_type"
};
StringToIntMap opt_map( optgrp_name, num_opt, SOptions );
OptionsGroup::const_iterator
itr = optgrp.begin(),
itr_end = optgrp.end();
for( ; itr != itr_end; ++itr ) {
switch( (EOptions)opt_map(option_name(itr)) ) {
case TOL:
std::cout << "\noption tol = " << option_value(itr);
break;
case MAX_ITER:
std::cout << "\noption max_iter = " << option_value(itr);
break;
case PROB_TYPE:
std::cout << "\noption prob_type = " << option_value(itr);
break;
default:
std::cout << "\nThe option " << option_name(itr) << " is not valid";
exit(-1);
}
}
\endverbatim
*
* The above code terminates the program in case the option with
* the name returned from <tt>option_name(itr)</tt> is not expected. This option
* could be ignored (as would have been in method 1) or some other action
* could be taken. It is a good idea to use the above method to validate
* that all of the options that the user sets for an options_group are
* known by the client software so that misspellings are caught.
* For example, if the user misspelled "tol" as "tal" then the
* "opt_map" object above would not find this option name an the
* <tt>default:</tt> case above would be executed.
*
* The total cost of this way off looking up option values is:
*
* O(n_set*log(n_total))
*
* As it can be clearly seen, method 2 will be faster since n_total >= n_set.
* Method 1 takes less overhead for the client (no map or enum needed)
* and is easier to maintain. The real advantage of
* Method 2 though is that if a user misspells an option then Method 1
* will not catch this while Method 2 will. For this reason alone Method
* 2 is to be preferred.
*/
class OptionsGroup {
public:
// friends
friend class OptionsFromStream;
/** @name Public Types */
//@{
/** \brief . */
typedef option_to_value_map_t::iterator iterator;
/** \brief . */
typedef option_to_value_map_t::const_iterator const_iterator;
//@}
/** @name Constructors.
*
* Default constructor and assignment operator are not publicly allowed.
* Default copy constructor is allowed.
*/
//@{
//@}
/** @name Lookup an an value of an option given the option's name.
*
* If the option does not exist then <tt>option_exists( option_value( option_name ) )</tt>
* will return <tt>false</tt> otherwise the option exists and can be read.
*/
//@{
/** \brief . */
std::string& option_value( const std::string& option_name );
/** \brief . */
const std::string& option_value( const std::string& option_name ) const;
//@}
/** \brief . */
static bool option_exists( const std::string& option_value );
/// Returns true if this options groups exists.
bool options_group_exists() const;
/** @name Iterator access */
//@{
/** \brief . */
int num_options() const;
/** \brief . */
iterator begin();
/** \brief . */
iterator end();
/** \brief . */
const_iterator begin() const;
/** \brief . */
const_iterator end() const;
//@}
private:
option_to_value_map_t* option_to_value_map_; // 0 used for no options group.
static std::string option_does_not_exist_;
// Not defined and not to be called
OptionsGroup();
OptionsGroup& operator=(const OptionsGroup&);
public:
// Is given zero by OptionsFromStream to signify the option group does't exist.
// It is put here to keep it away from the eyes of the general user.
OptionsGroup( option_to_value_map_t* option_to_value_map );
}; // end class OptionsGroup
inline
/** \brief . */
const std::string& option_name( OptionsGroup::const_iterator& itr ) {
return (*itr).first;
}
inline
/** \brief . */
const std::string& option_value( OptionsGroup::const_iterator& itr ) {
return (*itr).second;
}
// @ }
} // end namespace OptionsFromStreamUtilityPack
/** \brief Extracts options from a text stream and then allows
* convenient access to them.
*
* The basic idea is that options are read in from a stream (which can be
* a file, C++ string etc.) and then parsed and stored in a format so
* that options can be efficiently looked up by client software.
*
* The syntax for the file (or any C++ istream) is as follows:
\verbatim
begin_options
*** These are my solver options
options_group MySolverOptions {
tol = 1e-5; *** Convergence tolerance
max_iter = 100; *** Maximum number of iterations
prob_type = LINEAR;
*prob_type = NON_LINEAR; *** Comment this line out
}
*** Options for another solver
options_group YourSolverOptions {
tol = 1e-4;
*** These options determine the type of problem solved
* type_prob = LP;
type_prob = QP;
}
*** Reset the tolerance
options_group MySolverOptions {
tol = 1e-8; *** Reset to a tighter tolerance
}
end_options
\endverbatim
* The text stream will be read up to the <tt>end_options</tt> line.
* Options groups will be read starting with the <tt>begin_options</tt> line.
* Options groups can not be nested. The names for the option groups
* or the option names themselves can not contain any white space. The
* text for the option values however can contain white space. The <tt>=</tt> must
* separate each option from its value. The value for an option begins
* with the first non-whitespace character after the <tt>=</tt> and ends with the
* last non-whitespace character before the <tt>;</tt>. For the option and value
* pair "tol = + 1e-5 ;" the option value would be
* stored as "+ 1e-5". Comment lines starting with
* <tt>*</tt> can be placed anywhere in the stream and will be ignored.
* Also comments starting with <tt>*</tt> after the <tt>;</tt> for an option and value
* pair can occur on the same line as shown above.
*
* <b>Warning!</b> Do not use the char '}' in any comment within an options
* group! This will break the parser.
*
* The options groups are also reentrant which means that they may be included
* more than once as shown above. Therefore options may be set and reset in the
* same or another declaration of the options groups.
* In the above example, the second declaration of the options group declaration
* for <tt>OptionsGroup1</tt> resets the value of <tt>OptionsGroup1::option1</tt> from
* <tt>value1</tt> to <tt>another_value</tt>. This feature provides much more flexibility
* in letting options be changed easily without having to edit the text stream
* before it is read in.
*
* Now, what if the user misspells an options group name? One strategy is
* to require that the options group exist and then for the client to throw
* exceptions if the options group does not exist. However, where will be
* occasions where and options group may be "optional" and you don't want the user
* to have to specify every options group. Therefore we don't want to make all
* of the options groups mandatory. However, what if a user thinks he/she is
* setting options in an "optional" options group but misspells the name of the
* options group? In this case the options group would never be read by the
* client software and the user may be perplexed as to why nothing changed.
* To help deal with this problem, after all of the option groups have been
* accessed, the client can call the function <tt>print_unaccessed_options_groups(...)</tt>
* to print the list of options groups that have not been accessed. This way
* the user can see if they may have not spelled and "optional" options group
* correctly.
*
* Careful use of this simple class for specifying and setting options has the
* following advantages/features:
* <ul>
* <li> Options are read from a stream, not a file, and can therefore represent
* any kind of source (C++ strings, files, console etc.).
* <li> Options are inclosed in namespaces (called options_group) and therefore
* can handle options from many different sources.
* <li> An OptionsFromStream object can be passed around in a software application
* and interested sub-objects can search for 'their' options group and can pull
* out options that apply to them.
* <li> Reasonable mechanisms are available to validate that users have spelled the
* names of options groups and their options names or the allowable value of
* the option values properly.
* <li> The implementation is very light weight and uses very little code.
* Most of the major functionality comes from the ISO standard C++ library
* and therefore should be portable to any up to date C++ compiler.
*</ul>
*/
class OptionsFromStream {
public:
/** @name Public Types */
//@{
/// const iterator through options group access options
typedef OptionsFromStreamUtilityPack::options_group_map_t::iterator iterator;
/// non-const iterator through options group access options
typedef OptionsFromStreamUtilityPack::options_group_map_t::const_iterator const_iterator;
/// \Ref{OptionsGroup} typedef
typedef OptionsFromStreamUtilityPack::OptionsGroup options_group_t;
/// Thrown if there is an input error
class InputStreamError : public std::logic_error
{public: InputStreamError(const std::string& what_arg) : std::logic_error(what_arg) {}};
//@}
/** @name Constructors / Initializes.
*
* The default constructor, copy constructor and assignment operator functions
* are allowed.
*/
//@{
/// Construct with no options set.
OptionsFromStream();
/** \brief Construct initialized from a text stream.
*
* This is equivalent to calling the default constructor and then calling
* <tt>read_options(in)</tt>.
*/
explicit OptionsFromStream( std::istream& in );
/// Clear all the options
void clear_options();
/** \brief Add / modify options read in from a text stream.
*
* The format of the text stream is described in the introduction.
*
* The options read in from <tt>in</tt> will either be added anew or will
* overwrite options already present.
*
* If the format of the stream is not correct then a
* <tt>InputStreamError</tt> exception will be thrown.
*
* <b>Warning!</b> Do not use the char '}' in any comment within an options
* group! This will break the parser.
*/
void read_options( std::istream& in );
//@}
/** \brief Print the options to an output stream.
*
* This is useful for debugging and also to record exactly what options have been set.
*/
void print_options( std::ostream& out ) const;
/** @name Get an options group access object given its name.
*
* If the option group does not exist then\\
* <tt>options_group_exists( this->options_group( options_group_name ) ) == false</tt>\\
* where <tt>options_group_name</tt> is the string name of the option group.
*/
//@{
/** \brief . */
options_group_t options_group( const std::string& options_group_name );
/** \brief . */
const options_group_t options_group( const std::string& options_group_name ) const;
//@}
/** \brief . */
static bool options_group_exists( const options_group_t& options_group );
/** @name Determine what options groups where not accessed.
*
* The only the options groups accessed through the this->options_group(...)
* functions are maked as accessed. When the options groups are
* accessed through the iterator access, it is assumed that the client
* will not need this other information. Note that all of the
* flags are false by default.
*/
//@{
/// Reset the flags to false for if the options groups was accessed.
void reset_unaccessed_options_groups();
/// Print a list of options groups never accessed (accessed flag is falsed).
void print_unaccessed_options_groups( std::ostream& out ) const;
//@}
/** @name Iterator access to options groups. */
//@{
/** \brief . */
int num_options_groups() const;
/** \brief . */
iterator begin();
/** \brief . */
iterator end();
/** \brief . */
const_iterator begin() const;
/** \brief . */
const_iterator end() const;
//@}
private:
typedef OptionsFromStreamUtilityPack::false_bool_t false_bool_t;
typedef OptionsFromStreamUtilityPack::option_to_value_map_t option_to_value_map_t;
typedef OptionsFromStreamUtilityPack::options_group_map_t options_group_map_t;
options_group_map_t options_group_map_;
}; // end class OptionsFromStream
/** \brief . */
inline
const std::string&
options_group_name( OptionsFromStream::const_iterator& itr )
{
return (*itr).first;
}
/** \brief . */
inline
const OptionsFromStream::options_group_t
options_group( OptionsFromStream::const_iterator& itr )
{
return OptionsFromStream::options_group_t(
const_cast<OptionsFromStreamUtilityPack::option_to_value_map_t*>(&(*itr).second.first) );
}
// @ }
/* @name Return the name or value of an option from an OptionsGroup iterator. */
// @ {
//
using OptionsFromStreamUtilityPack::option_name;
//
using OptionsFromStreamUtilityPack::option_value;
// @ }
// end namespace OptionsFromStreamPack
// @ }
} // end namespace OptionsFromStreamPack
// //////////////////////////////////////////////////////////////////////////////////////////
// Inline definitions.
namespace OptionsFromStreamPack {
namespace OptionsFromStreamUtilityPack {
// class OptionsGroup.
inline
std::string& OptionsGroup::option_value( const std::string& option_name ) {
option_to_value_map_t::iterator itr = option_to_value_map_->find( option_name );
return ( itr != option_to_value_map_->end() ? (*itr).second : option_does_not_exist_ );
}
inline
const std::string& OptionsGroup::option_value( const std::string& option_name ) const {
option_to_value_map_t::const_iterator itr = option_to_value_map_->find( option_name );
return ( itr != option_to_value_map_->end() ? (*itr).second : option_does_not_exist_ );
}
inline
bool OptionsGroup::option_exists( const std::string& option_value ) {
return &option_value != &option_does_not_exist_;
}
inline
bool OptionsGroup::options_group_exists() const {
return option_to_value_map_ != 0;
}
inline
int OptionsGroup::num_options() const {
return option_to_value_map_->size();
}
inline
OptionsGroup::iterator OptionsGroup::begin() {
return option_to_value_map_->begin();
}
inline
OptionsGroup::iterator OptionsGroup::end() {
return option_to_value_map_->end();
}
inline
OptionsGroup::const_iterator OptionsGroup::begin() const {
return option_to_value_map_->begin();
}
inline
OptionsGroup::const_iterator OptionsGroup::end() const {
return option_to_value_map_->end();
}
inline
OptionsGroup::OptionsGroup( option_to_value_map_t* option_to_value_map )
: option_to_value_map_(option_to_value_map)
{}
} // end namespace OptionsFromStreamPack
// class OptionsFromStream
inline
OptionsFromStream::OptionsFromStream()
{}
inline
OptionsFromStream::OptionsFromStream( std::istream& in ) {
read_options(in);
}
inline
void OptionsFromStream::clear_options() {
options_group_map_.clear();
}
inline
bool OptionsFromStream::options_group_exists( const options_group_t& options_group )
{
return options_group.options_group_exists();
}
inline
int OptionsFromStream::num_options_groups() const {
return options_group_map_.size();
}
inline
OptionsFromStream::iterator OptionsFromStream::begin() {
return options_group_map_.begin();
}
inline
OptionsFromStream::iterator OptionsFromStream::end() {
return options_group_map_.end();
}
inline
OptionsFromStream::const_iterator OptionsFromStream::begin() const {
return options_group_map_.begin();
}
inline
OptionsFromStream::const_iterator OptionsFromStream::end() const {
return options_group_map_.end();
}
} // end namespace OptionsFromStreamPack
#endif // OPTIONS_FROM_STREAM_H
|