MOOCHO (Single Doxygen Collection) Version of the Day
OptionsFromStreamPack_OptionsFromStream.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef OPTIONS_FROM_STREAM_H
00030 #define OPTIONS_FROM_STREAM_H
00031 
00032 #include "Moocho_ConfigDefs.hpp"
00033 
00034 // /////////////////////////////////////////////////
00035 // Forward declarations.
00036 
00037 namespace OptionsFromStreamPack {
00038   namespace OptionsFromStreamUtilityPack {
00039 
00040     // Simple class for a boolean variable that is false on construction.
00041     class false_bool_t {
00042     public:
00043       false_bool_t() : val_(false) {}
00044       void set(bool val) { val_ = val; }
00045       operator bool() const { return val_; }
00046     private:
00047       bool val_;
00048     };
00049 
00050     // Implementation type for the map for looking up a value given
00051     // a an options name.
00052     typedef std::map< std::string , std::string >     option_to_value_map_t;
00053 
00054     // Implementation type for an options group's options and
00055     // a boolean variable to determine if this option has been visited yet.
00056     typedef std::pair< option_to_value_map_t, false_bool_t >  options_group_pair_t;
00057 
00058     // Implementation type of the map for looking up a set of options
00059     // given the option groups name.
00060     typedef std::map< std::string, options_group_pair_t > options_group_map_t;
00061 
00062     // The above declarations sets up the data structure:
00063     // 
00064     //       map<string,options_group_pair_t> (options_group_map_t)
00065     //                  |
00066     //                  | value_type
00067     //                 `.'
00068     //       pair<string,options_group_pair_t> 
00069     //                  |
00070     //       -----------------------              
00071     //      |                       |         
00072     //      | first                 | second
00073     //     `.'                     `.'
00074     //    string         pair<option_to_value_map_t,false_bool_t> (options_group_pair_t)
00075     //      |                      |
00076     // "solver_options"            |
00077     //                             |
00078     //        ------------------------------------------------
00079     //       |                                                |
00080     //       | first                                          | second
00081     //      `.'                                              `.'
00082     //    map<string,string> (option_to_value_map_t)       false_bool_t
00083     //       |                                                |
00084     //       | value_type                                   "true"
00085     //      `.'
00086     // pair<string,string>
00087     //       |
00088     //     --------------
00089     //    | first        | second
00090     //   `.'            `.'
00091     //  string         string
00092     //    |              |    
00093     //  "tol"          "1e-6"
00094     //  
00095 
00096     class OptionsGroup;
00097   }
00098   class OptionsFromStream;
00099 }
00100 
00101 // //////////////////////////////////////////////////
00102 // Class and function declarations
00103 
00104 namespace OptionsFromStreamPack {
00105 
00106 /* \defgroup OptionsFromStreamPack_grp Untilities for maintianing a simple options database.
00107  * \ingroup Misc_grp
00108  * 
00109  */
00110 // @ {
00111 
00112 namespace OptionsFromStreamUtilityPack {
00113 
00216 class OptionsGroup {
00217 public:
00218 
00219   // friends
00220   friend class OptionsFromStream;
00221 
00224 
00226   typedef option_to_value_map_t::iterator     iterator;
00228   typedef option_to_value_map_t::const_iterator const_iterator;
00229 
00231 
00238 
00240 
00247 
00249   std::string& option_value( const std::string& option_name );
00251   const std::string& option_value( const std::string& option_name ) const;
00252 
00254 
00256   static bool option_exists( const std::string& option_value );
00257 
00259   bool options_group_exists() const;
00260 
00263 
00265   int       num_options() const;
00267   iterator    begin();
00269   iterator    end();
00271   const_iterator  begin() const;
00273   const_iterator  end() const;
00274 
00276 
00277 private:
00278   
00279   option_to_value_map_t*  option_to_value_map_; // 0 used for no options group.
00280   static std::string    option_does_not_exist_;
00281 
00282   // Not defined and not to be called
00283   OptionsGroup();
00284   OptionsGroup& operator=(const OptionsGroup&);
00285 
00286 public:
00287   // Is given zero by OptionsFromStream to signify the option group does't exist.
00288   // It is put here to keep it away from the eyes of the general user.
00289   OptionsGroup( option_to_value_map_t* option_to_value_map )
00290     : option_to_value_map_(option_to_value_map)
00291   {}
00292 
00293 };  // end class OptionsGroup
00294 
00295 inline
00297 const std::string& option_name( OptionsGroup::const_iterator& itr ) {
00298   return (*itr).first;
00299 }
00300 
00301 inline
00303 const std::string& option_value( OptionsGroup::const_iterator& itr ) {
00304   return (*itr).second;
00305 }
00306 
00307 
00308 // @ }
00309 
00310 } // end namespace OptionsFromStreamUtilityPack 
00311 
00312 
00409 class OptionsFromStream {
00410 public:
00411 
00414 
00416   typedef OptionsFromStreamUtilityPack::options_group_map_t::iterator     iterator;
00418   typedef OptionsFromStreamUtilityPack::options_group_map_t::const_iterator const_iterator;
00419 
00421   typedef OptionsFromStreamUtilityPack::OptionsGroup              options_group_t;
00422 
00424   class InputStreamError : public std::logic_error
00425   {public: InputStreamError(const std::string& what_arg) : std::logic_error(what_arg) {}};
00426 
00428 
00435 
00437   OptionsFromStream();
00438 
00444   explicit OptionsFromStream( std::istream& in );
00445 
00447   void clear_options();
00448 
00462   void read_options( std::istream& in );
00463 
00465 
00470   void print_options( std::ostream& out ) const;
00471 
00479 
00481   options_group_t options_group( const std::string& options_group_name );
00483   const options_group_t options_group( const std::string& options_group_name ) const;
00484 
00486 
00488   static bool options_group_exists( const options_group_t& options_group );
00489 
00499 
00501   void reset_unaccessed_options_groups();
00502 
00504   void print_unaccessed_options_groups( std::ostream& out ) const;
00505 
00507 
00510 
00512   int       num_options_groups() const;
00514   iterator    begin();
00516   iterator    end();
00518   const_iterator  begin() const;
00520   const_iterator  end() const;
00521 
00523 
00524 private:
00525   typedef OptionsFromStreamUtilityPack::false_bool_t      false_bool_t;
00526   typedef OptionsFromStreamUtilityPack::option_to_value_map_t option_to_value_map_t;
00527   typedef OptionsFromStreamUtilityPack::options_group_map_t options_group_map_t;
00528   options_group_map_t                     options_group_map_;
00529 
00530 };  // end class OptionsFromStream
00531 
00533 inline
00534 const std::string&
00535 options_group_name( OptionsFromStream::const_iterator& itr )
00536 {
00537   return (*itr).first;
00538 }
00539 
00541 inline
00542 const OptionsFromStream::options_group_t
00543 options_group( OptionsFromStream::const_iterator& itr )
00544 {
00545   return OptionsFromStream::options_group_t(
00546     const_cast<OptionsFromStreamUtilityPack::option_to_value_map_t*>(&(*itr).second.first) );
00547 }
00548 
00549 
00550 // @ }
00551 
00552 /* @name Return the name or value of an option from an OptionsGroup iterator. */
00553 // @ {
00554 
00555 //
00556 using OptionsFromStreamUtilityPack::option_name;
00557 //
00558 using OptionsFromStreamUtilityPack::option_value;
00559 
00560 // @ }
00561 
00562 //  end namespace OptionsFromStreamPack 
00563 // @ }
00564 
00565 } // end namespace OptionsFromStreamPack
00566 
00567 // //////////////////////////////////////////////////////////////////////////////////////////
00568 // Inline definitions.
00569 
00570 namespace OptionsFromStreamPack {
00571 
00572 namespace OptionsFromStreamUtilityPack {
00573 
00574 // class OptionsGroup.
00575 
00576 inline
00577 std::string& OptionsGroup::option_value( const std::string& option_name ) {
00578   option_to_value_map_t::iterator itr = option_to_value_map_->find( option_name );
00579   return ( itr != option_to_value_map_->end() ? (*itr).second : option_does_not_exist_ );
00580 }
00581 
00582 inline
00583 const std::string& OptionsGroup::option_value( const std::string& option_name ) const {
00584   option_to_value_map_t::const_iterator itr = option_to_value_map_->find( option_name );
00585   return ( itr != option_to_value_map_->end() ? (*itr).second : option_does_not_exist_ );
00586 }
00587 
00588 inline
00589 bool OptionsGroup::option_exists( const std::string& option_value ) {
00590   return &option_value != &option_does_not_exist_;
00591 }
00592 
00593 inline
00594 bool OptionsGroup::options_group_exists() const {
00595   return option_to_value_map_ != 0;
00596 }
00597 
00598 inline
00599 int OptionsGroup::num_options() const {
00600   return option_to_value_map_->size();
00601 }
00602 
00603 inline
00604 OptionsGroup::iterator    OptionsGroup::begin() {
00605   return option_to_value_map_->begin();
00606 }
00607 
00608 inline
00609 OptionsGroup::iterator    OptionsGroup::end() {
00610   return option_to_value_map_->end();
00611 }
00612 
00613 inline
00614 OptionsGroup::const_iterator  OptionsGroup::begin() const {
00615   return option_to_value_map_->begin();
00616 }
00617 
00618 inline
00619 OptionsGroup::const_iterator  OptionsGroup::end() const {
00620   return option_to_value_map_->end();
00621 }
00622 
00623 } // end namespace OptionsFromStreamPack
00624 
00625 // class OptionsFromStream
00626 
00627 inline
00628 OptionsFromStream::OptionsFromStream()
00629 {}
00630 
00631 inline
00632 OptionsFromStream::OptionsFromStream( std::istream& in ) {
00633   read_options(in);
00634 }
00635 
00636 inline
00637 void OptionsFromStream::clear_options() {
00638   options_group_map_.clear();
00639 }
00640 
00641 inline
00642 bool OptionsFromStream::options_group_exists( const options_group_t& options_group )
00643 {
00644   return options_group.options_group_exists();
00645 }
00646 
00647 inline
00648 int OptionsFromStream::num_options_groups() const {
00649   return options_group_map_.size();
00650 }
00651 
00652 inline
00653 OptionsFromStream::iterator OptionsFromStream::begin() {
00654   return options_group_map_.begin();
00655 }
00656 
00657 inline
00658 OptionsFromStream::iterator OptionsFromStream::end() {
00659   return options_group_map_.end();
00660 }
00661 
00662 inline
00663 OptionsFromStream::const_iterator OptionsFromStream::begin() const {
00664   return options_group_map_.begin();
00665 }
00666 
00667 inline
00668 OptionsFromStream::const_iterator OptionsFromStream::end() const {
00669   return options_group_map_.end();
00670 }
00671 
00672 } // end namespace OptionsFromStreamPack
00673 
00674 #endif  // OPTIONS_FROM_STREAM_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines