MoochoPack: Miscellaneous Utilities for MOOCHO  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
OptionsFromStreamPack_OptionsFromStream.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef OPTIONS_FROM_STREAM_H
43 #define OPTIONS_FROM_STREAM_H
44 
45 #include "Moocho_ConfigDefs.hpp"
46 
47 // /////////////////////////////////////////////////
48 // Forward declarations.
49 
50 namespace OptionsFromStreamPack {
51  namespace OptionsFromStreamUtilityPack {
52 
53  // Simple class for a boolean variable that is false on construction.
54  class false_bool_t {
55  public:
56  false_bool_t() : val_(false) {}
57  void set(bool val) { val_ = val; }
58  operator bool() const { return val_; }
59  private:
60  bool val_;
61  };
62 
63  // Implementation type for the map for looking up a value given
64  // a an options name.
65  typedef std::map< std::string , std::string > option_to_value_map_t;
66 
67  // Implementation type for an options group's options and
68  // a boolean variable to determine if this option has been visited yet.
69  typedef std::pair< option_to_value_map_t, false_bool_t > options_group_pair_t;
70 
71  // Implementation type of the map for looking up a set of options
72  // given the option groups name.
73  typedef std::map< std::string, options_group_pair_t > options_group_map_t;
74 
75  // The above declarations sets up the data structure:
76  //
77  // map<string,options_group_pair_t> (options_group_map_t)
78  // |
79  // | value_type
80  // `.'
81  // pair<string,options_group_pair_t>
82  // |
83  // -----------------------
84  // | |
85  // | first | second
86  // `.' `.'
87  // string pair<option_to_value_map_t,false_bool_t> (options_group_pair_t)
88  // | |
89  // "solver_options" |
90  // |
91  // ------------------------------------------------
92  // | |
93  // | first | second
94  // `.' `.'
95  // map<string,string> (option_to_value_map_t) false_bool_t
96  // | |
97  // | value_type "true"
98  // `.'
99  // pair<string,string>
100  // |
101  // --------------
102  // | first | second
103  // `.' `.'
104  // string string
105  // | |
106  // "tol" "1e-6"
107  //
108 
109  class OptionsGroup;
110  }
111  class OptionsFromStream;
112 }
113 
114 // //////////////////////////////////////////////////
115 // Class and function declarations
116 
117 namespace OptionsFromStreamPack {
118 
119 /* \defgroup OptionsFromStreamPack_grp Untilities for maintianing a simple options database.
120  * \ingroup Misc_grp
121  *
122  */
123 // @ {
124 
125 namespace OptionsFromStreamUtilityPack {
126 
230 public:
231 
232  // friends
233  friend class OptionsFromStream;
234 
237 
239  typedef option_to_value_map_t::iterator iterator;
241  typedef option_to_value_map_t::const_iterator const_iterator;
242 
244 
251 
253 
260 
262  std::string& option_value( const std::string& option_name );
264  const std::string& option_value( const std::string& option_name ) const;
265 
267 
269  static bool option_exists( const std::string& option_value );
270 
272  bool options_group_exists() const;
273 
276 
278  int num_options() const;
280  iterator begin();
282  iterator end();
284  const_iterator begin() const;
286  const_iterator end() const;
287 
289 
290 private:
291 
292  option_to_value_map_t* option_to_value_map_; // 0 used for no options group.
293  static std::string option_does_not_exist_;
294 
295  // Not defined and not to be called
296  OptionsGroup();
297  OptionsGroup& operator=(const OptionsGroup&);
298 
299 public:
300  // Is given zero by OptionsFromStream to signify the option group does't exist.
301  // It is put here to keep it away from the eyes of the general user.
302  OptionsGroup( option_to_value_map_t* option_to_value_map )
303  : option_to_value_map_(option_to_value_map)
304  {}
305 
306 }; // end class OptionsGroup
307 
308 inline
310 const std::string& option_name( OptionsGroup::const_iterator& itr ) {
311  return (*itr).first;
312 }
313 
314 inline
316 const std::string& option_value( OptionsGroup::const_iterator& itr ) {
317  return (*itr).second;
318 }
319 
320 
321 // @ }
322 
323 } // end namespace OptionsFromStreamUtilityPack
324 
325 
423 public:
424 
427 
429  typedef OptionsFromStreamUtilityPack::options_group_map_t::iterator iterator;
431  typedef OptionsFromStreamUtilityPack::options_group_map_t::const_iterator const_iterator;
432 
435 
437  class InputStreamError : public std::logic_error
438  {public: InputStreamError(const std::string& what_arg) : std::logic_error(what_arg) {}};
439 
441 
448 
451 
457  explicit OptionsFromStream( std::istream& in );
458 
460  void clear_options();
461 
475  void read_options( std::istream& in );
476 
478 
483  void print_options( std::ostream& out ) const;
484 
492 
494  options_group_t options_group( const std::string& options_group_name );
496  const options_group_t options_group( const std::string& options_group_name ) const;
497 
499 
501  static bool options_group_exists( const options_group_t& options_group );
502 
512 
515 
517  void print_unaccessed_options_groups( std::ostream& out ) const;
518 
520 
523 
525  int num_options_groups() const;
527  iterator begin();
529  iterator end();
531  const_iterator begin() const;
533  const_iterator end() const;
534 
536 
537 private:
538  typedef OptionsFromStreamUtilityPack::false_bool_t false_bool_t;
539  typedef OptionsFromStreamUtilityPack::option_to_value_map_t option_to_value_map_t;
540  typedef OptionsFromStreamUtilityPack::options_group_map_t options_group_map_t;
541  options_group_map_t options_group_map_;
542 
543 }; // end class OptionsFromStream
544 
546 inline
547 const std::string&
548 options_group_name( OptionsFromStream::const_iterator& itr )
549 {
550  return (*itr).first;
551 }
552 
554 inline
556 options_group( OptionsFromStream::const_iterator& itr )
557 {
559  const_cast<OptionsFromStreamUtilityPack::option_to_value_map_t*>(&(*itr).second.first) );
560 }
561 
562 
563 // @ }
564 
565 /* @name Return the name or value of an option from an OptionsGroup iterator. */
566 // @ {
567 
568 //
569 using OptionsFromStreamUtilityPack::option_name;
570 //
571 using OptionsFromStreamUtilityPack::option_value;
572 
573 // @ }
574 
575 // end namespace OptionsFromStreamPack
576 // @ }
577 
578 } // end namespace OptionsFromStreamPack
579 
580 // //////////////////////////////////////////////////////////////////////////////////////////
581 // Inline definitions.
582 
583 namespace OptionsFromStreamPack {
584 
585 namespace OptionsFromStreamUtilityPack {
586 
587 // class OptionsGroup.
588 
589 inline
590 std::string& OptionsGroup::option_value( const std::string& option_name ) {
591  option_to_value_map_t::iterator itr = option_to_value_map_->find( option_name );
592  return ( itr != option_to_value_map_->end() ? (*itr).second : option_does_not_exist_ );
593 }
594 
595 inline
596 const std::string& OptionsGroup::option_value( const std::string& option_name ) const {
597  option_to_value_map_t::const_iterator itr = option_to_value_map_->find( option_name );
598  return ( itr != option_to_value_map_->end() ? (*itr).second : option_does_not_exist_ );
599 }
600 
601 inline
602 bool OptionsGroup::option_exists( const std::string& option_value ) {
603  return &option_value != &option_does_not_exist_;
604 }
605 
606 inline
608  return option_to_value_map_ != 0;
609 }
610 
611 inline
613  return option_to_value_map_->size();
614 }
615 
616 inline
618  return option_to_value_map_->begin();
619 }
620 
621 inline
623  return option_to_value_map_->end();
624 }
625 
626 inline
628  return option_to_value_map_->begin();
629 }
630 
631 inline
633  return option_to_value_map_->end();
634 }
635 
636 } // end namespace OptionsFromStreamPack
637 
638 // class OptionsFromStream
639 
640 inline
642 {}
643 
644 inline
646  read_options(in);
647 }
648 
649 inline
651  options_group_map_.clear();
652 }
653 
654 inline
656 {
657  return options_group.options_group_exists();
658 }
659 
660 inline
662  return options_group_map_.size();
663 }
664 
665 inline
667  return options_group_map_.begin();
668 }
669 
670 inline
672  return options_group_map_.end();
673 }
674 
675 inline
677  return options_group_map_.begin();
678 }
679 
680 inline
682  return options_group_map_.end();
683 }
684 
685 } // end namespace OptionsFromStreamPack
686 
687 #endif // OPTIONS_FROM_STREAM_H
Extracts options from a text stream and then allows convenient access to them.
void print_options(std::ostream &out) const
Print the options to an output stream.
static bool options_group_exists(const options_group_t &options_group)
OptionsFromStreamUtilityPack::options_group_map_t::const_iterator const_iterator
non-const iterator through options group access options
OptionsFromStreamUtilityPack::OptionsGroup options_group_t
{OptionsGroup} typedef
void read_options(std::istream &in)
Add / modify options read in from a text stream.
void reset_unaccessed_options_groups()
Reset the flags to false for if the options groups was accessed.
options_group_t options_group(const std::string &options_group_name)
void print_unaccessed_options_groups(std::ostream &out) const
Print a list of options groups never accessed (accessed flag is falsed).
bool options_group_exists() const
Returns true if this options groups exists.
OptionsFromStreamUtilityPack::options_group_map_t::iterator iterator
const iterator through options group access options