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 00310 00407 class OptionsFromStream { 00408 public: 00409 00412 00414 typedef OptionsFromStreamUtilityPack::options_group_map_t::iterator iterator; 00416 typedef OptionsFromStreamUtilityPack::options_group_map_t::const_iterator const_iterator; 00417 00419 typedef OptionsFromStreamUtilityPack::OptionsGroup options_group_t; 00420 00422 class InputStreamError : public std::logic_error 00423 {public: InputStreamError(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00424 00426 00433 00435 OptionsFromStream(); 00436 00442 explicit OptionsFromStream( std::istream& in ); 00443 00445 void clear_options(); 00446 00460 void read_options( std::istream& in ); 00461 00463 00468 void print_options( std::ostream& out ) const; 00469 00477 00479 options_group_t options_group( const std::string& options_group_name ); 00481 const options_group_t options_group( const std::string& options_group_name ) const; 00482 00484 00486 static bool options_group_exists( const options_group_t& options_group ); 00487 00497 00499 void reset_unaccessed_options_groups(); 00500 00502 void print_unaccessed_options_groups( std::ostream& out ) const; 00503 00505 00508 00510 int num_options_groups() const; 00512 iterator begin(); 00514 iterator end(); 00516 const_iterator begin() const; 00518 const_iterator end() const; 00519 00521 00522 private: 00523 typedef OptionsFromStreamUtilityPack::false_bool_t false_bool_t; 00524 typedef OptionsFromStreamUtilityPack::option_to_value_map_t option_to_value_map_t; 00525 typedef OptionsFromStreamUtilityPack::options_group_map_t options_group_map_t; 00526 options_group_map_t options_group_map_; 00527 00528 }; // end class OptionsFromStream 00529 00531 inline 00532 const std::string& 00533 options_group_name( OptionsFromStream::const_iterator& itr ) 00534 { 00535 return (*itr).first; 00536 } 00537 00539 inline 00540 const OptionsFromStream::options_group_t 00541 options_group( OptionsFromStream::const_iterator& itr ) 00542 { 00543 return OptionsFromStream::options_group_t( 00544 const_cast<OptionsFromStreamUtilityPack::option_to_value_map_t*>(&(*itr).second.first) ); 00545 } 00546 00547 00548 // @ } 00549 00550 /* @name Return the name or value of an option from an OptionsGroup iterator. */ 00551 // @ { 00552 00553 // 00554 using OptionsFromStreamUtilityPack::option_name; 00555 // 00556 using OptionsFromStreamUtilityPack::option_value; 00557 00558 // @ } 00559 00560 // end namespace OptionsFromStreamPack 00561 // @ } 00562 00563 } // end namespace OptionsFromStreamPack 00564 00565 // ////////////////////////////////////////////////////////////////////////////////////////// 00566 // Inline definitions. 00567 00568 namespace OptionsFromStreamPack { 00569 00570 namespace OptionsFromStreamUtilityPack { 00571 00572 // class OptionsGroup. 00573 00574 inline 00575 std::string& OptionsGroup::option_value( const std::string& option_name ) { 00576 option_to_value_map_t::iterator itr = option_to_value_map_->find( option_name ); 00577 return ( itr != option_to_value_map_->end() ? (*itr).second : option_does_not_exist_ ); 00578 } 00579 00580 inline 00581 const std::string& OptionsGroup::option_value( const std::string& option_name ) const { 00582 option_to_value_map_t::const_iterator itr = option_to_value_map_->find( option_name ); 00583 return ( itr != option_to_value_map_->end() ? (*itr).second : option_does_not_exist_ ); 00584 } 00585 00586 inline 00587 bool OptionsGroup::option_exists( const std::string& option_value ) { 00588 return &option_value != &option_does_not_exist_; 00589 } 00590 00591 inline 00592 bool OptionsGroup::options_group_exists() const { 00593 return option_to_value_map_ != 0; 00594 } 00595 00596 inline 00597 int OptionsGroup::num_options() const { 00598 return option_to_value_map_->size(); 00599 } 00600 00601 inline 00602 OptionsGroup::iterator OptionsGroup::begin() { 00603 return option_to_value_map_->begin(); 00604 } 00605 00606 inline 00607 OptionsGroup::iterator OptionsGroup::end() { 00608 return option_to_value_map_->end(); 00609 } 00610 00611 inline 00612 OptionsGroup::const_iterator OptionsGroup::begin() const { 00613 return option_to_value_map_->begin(); 00614 } 00615 00616 inline 00617 OptionsGroup::const_iterator OptionsGroup::end() const { 00618 return option_to_value_map_->end(); 00619 } 00620 00621 inline 00622 OptionsGroup::OptionsGroup( option_to_value_map_t* option_to_value_map ) 00623 : option_to_value_map_(option_to_value_map) 00624 {} 00625 00626 } // end namespace OptionsFromStreamPack 00627 00628 // class OptionsFromStream 00629 00630 inline 00631 OptionsFromStream::OptionsFromStream() 00632 {} 00633 00634 inline 00635 OptionsFromStream::OptionsFromStream( std::istream& in ) { 00636 read_options(in); 00637 } 00638 00639 inline 00640 void OptionsFromStream::clear_options() { 00641 options_group_map_.clear(); 00642 } 00643 00644 inline 00645 bool OptionsFromStream::options_group_exists( const options_group_t& options_group ) 00646 { 00647 return options_group.options_group_exists(); 00648 } 00649 00650 inline 00651 int OptionsFromStream::num_options_groups() const { 00652 return options_group_map_.size(); 00653 } 00654 00655 inline 00656 OptionsFromStream::iterator OptionsFromStream::begin() { 00657 return options_group_map_.begin(); 00658 } 00659 00660 inline 00661 OptionsFromStream::iterator OptionsFromStream::end() { 00662 return options_group_map_.end(); 00663 } 00664 00665 inline 00666 OptionsFromStream::const_iterator OptionsFromStream::begin() const { 00667 return options_group_map_.begin(); 00668 } 00669 00670 inline 00671 OptionsFromStream::const_iterator OptionsFromStream::end() const { 00672 return options_group_map_.end(); 00673 } 00674 00675 } // end namespace OptionsFromStreamPack 00676 00677 #endif // OPTIONS_FROM_STREAM_H
1.4.7