OptionsFromStreamPack_OptionsFromStream.hpp

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 
00291 };  // end class OptionsGroup
00292 
00293 inline
00295 const std::string& option_name( OptionsGroup::const_iterator& itr ) {
00296   return (*itr).first;
00297 }
00298 
00299 inline
00301 const std::string& option_value( OptionsGroup::const_iterator& itr ) {
00302   return (*itr).second;
00303 }
00304 
00305 
00306 // @ }
00307 
00308 } // end namespace OptionsFromStreamUtilityPack 
00309 
00406 class OptionsFromStream {
00407 public:
00408 
00411 
00413   typedef OptionsFromStreamUtilityPack::options_group_map_t::iterator     iterator;
00415   typedef OptionsFromStreamUtilityPack::options_group_map_t::const_iterator const_iterator;
00416 
00418   typedef OptionsFromStreamUtilityPack::OptionsGroup              options_group_t;
00419 
00421   class InputStreamError : public std::logic_error
00422   {public: InputStreamError(const std::string& what_arg) : std::logic_error(what_arg) {}};
00423 
00425 
00432 
00434   OptionsFromStream();
00435 
00441   explicit OptionsFromStream( std::istream& in );
00442 
00444   void clear_options();
00445 
00456   void read_options( std::istream& in );
00457 
00459 
00464   void print_options( std::ostream& out ) const;
00465 
00473 
00475   options_group_t options_group( const std::string& options_group_name );
00477   const options_group_t options_group( const std::string& options_group_name ) const;
00478 
00480 
00482   static bool options_group_exists( const options_group_t& options_group );
00483 
00493 
00495   void reset_unaccessed_options_groups();
00496 
00498   void print_unaccessed_options_groups( std::ostream& out ) const;
00499 
00501 
00504 
00506   int       num_options_groups() const;
00508   iterator    begin();
00510   iterator    end();
00512   const_iterator  begin() const;
00514   const_iterator  end() const;
00515 
00517 
00518 private:
00519   typedef OptionsFromStreamUtilityPack::false_bool_t      false_bool_t;
00520   typedef OptionsFromStreamUtilityPack::option_to_value_map_t option_to_value_map_t;
00521   typedef OptionsFromStreamUtilityPack::options_group_map_t options_group_map_t;
00522   options_group_map_t                     options_group_map_;
00523 
00524 };  // end class OptionsFromStream
00525 
00527 inline
00528 const std::string&
00529 options_group_name( OptionsFromStream::const_iterator& itr )
00530 {
00531   return (*itr).first;
00532 }
00533 
00535 inline
00536 const OptionsFromStream::options_group_t
00537 options_group( OptionsFromStream::const_iterator& itr )
00538 {
00539   return OptionsFromStream::options_group_t(
00540     const_cast<OptionsFromStreamUtilityPack::option_to_value_map_t*>(&(*itr).second.first) );
00541 }
00542 
00543 
00544 // @ }
00545 
00546 /* @name Return the name or value of an option from an OptionsGroup iterator. */
00547 // @ {
00548 
00549 //
00550 using OptionsFromStreamUtilityPack::option_name;
00551 //
00552 using OptionsFromStreamUtilityPack::option_value;
00553 
00554 // @ }
00555 
00556 //  end namespace OptionsFromStreamPack 
00557 // @ }
00558 
00559 } // end namespace OptionsFromStreamPack
00560 
00561 // //////////////////////////////////////////////////////////////////////////////////////////
00562 // Inline definitions.
00563 
00564 namespace OptionsFromStreamPack {
00565 
00566 namespace OptionsFromStreamUtilityPack {
00567 
00568 // class OptionsGroup.
00569 
00570 inline
00571 std::string& OptionsGroup::option_value( const std::string& option_name ) {
00572   option_to_value_map_t::iterator itr = option_to_value_map_->find( option_name );
00573   return ( itr != option_to_value_map_->end() ? (*itr).second : option_does_not_exist_ );
00574 }
00575 
00576 inline
00577 const std::string& OptionsGroup::option_value( const std::string& option_name ) const {
00578   option_to_value_map_t::const_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 bool OptionsGroup::option_exists( const std::string& option_value ) {
00584   return &option_value != &option_does_not_exist_;
00585 }
00586 
00587 inline
00588 bool OptionsGroup::options_group_exists() const {
00589   return option_to_value_map_ != 0;
00590 }
00591 
00592 inline
00593 int OptionsGroup::num_options() const {
00594   return option_to_value_map_->size();
00595 }
00596 
00597 inline
00598 OptionsGroup::iterator    OptionsGroup::begin() {
00599   return option_to_value_map_->begin();
00600 }
00601 
00602 inline
00603 OptionsGroup::iterator    OptionsGroup::end() {
00604   return option_to_value_map_->end();
00605 }
00606 
00607 inline
00608 OptionsGroup::const_iterator  OptionsGroup::begin() const {
00609   return option_to_value_map_->begin();
00610 }
00611 
00612 inline
00613 OptionsGroup::const_iterator  OptionsGroup::end() const {
00614   return option_to_value_map_->end();
00615 }
00616 
00617 inline
00618 OptionsGroup::OptionsGroup( option_to_value_map_t* option_to_value_map )
00619   : option_to_value_map_(option_to_value_map)
00620 {}
00621 
00622 } // end namespace OptionsFromStreamPack
00623 
00624 // class OptionsFromStream
00625 
00626 inline
00627 OptionsFromStream::OptionsFromStream()
00628 {}
00629 
00630 inline
00631 OptionsFromStream::OptionsFromStream( std::istream& in ) {
00632   read_options(in);
00633 }
00634 
00635 inline
00636 void OptionsFromStream::clear_options() {
00637   options_group_map_.clear();
00638 }
00639 
00640 inline
00641 bool OptionsFromStream::options_group_exists( const options_group_t& options_group )
00642 {
00643   return options_group.options_group_exists();
00644 }
00645 
00646 inline
00647 int OptionsFromStream::num_options_groups() const {
00648   return options_group_map_.size();
00649 }
00650 
00651 inline
00652 OptionsFromStream::iterator OptionsFromStream::begin() {
00653   return options_group_map_.begin();
00654 }
00655 
00656 inline
00657 OptionsFromStream::iterator OptionsFromStream::end() {
00658   return options_group_map_.end();
00659 }
00660 
00661 inline
00662 OptionsFromStream::const_iterator OptionsFromStream::begin() const {
00663   return options_group_map_.begin();
00664 }
00665 
00666 inline
00667 OptionsFromStream::const_iterator OptionsFromStream::end() const {
00668   return options_group_map_.end();
00669 }
00670 
00671 } // end namespace OptionsFromStreamPack
00672 
00673 #endif  // OPTIONS_FROM_STREAM_H

Generated on Thu Sep 18 12:33:34 2008 for MoochoPack: Miscellaneous Utilities for MOOCHO by doxygen 1.3.9.1