Note: Goby version 1 (shown here) is now considered obsolete. Please use version 2 for new projects, and consider upgrading old projects.

Goby Underwater Autonomy Project  Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
core/libcore/configuration_reader.h
00001 // copyright 2010 t. schneider tes@mit.edu
00002 // 
00003 //
00004 // This program is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // This software is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this software.  If not, see <http://www.gnu.org/licenses/>.
00016 #ifndef CONFIGURATIONREADER20100929H
00017 #define CONFIGURATIONREADER20100929H
00018 
00019 #include <sstream>
00020 #include <iostream>
00021 #include <fstream>
00022 #include <vector>
00023 
00024 
00025 #include <boost/foreach.hpp>
00026 #include <boost/program_options.hpp>
00027 #include <boost/cstdint.hpp>
00028 #include <boost/filesystem.hpp>
00029 
00030 #include <google/protobuf/message.h>
00031 #include <google/protobuf/descriptor.h>
00032 #include <google/protobuf/descriptor.pb.h>
00033 #include <google/protobuf/io/zero_copy_stream.h>
00034 #include <google/protobuf/io/zero_copy_stream_impl.h>
00035 #include <google/protobuf/text_format.h>
00036 
00037 #include "goby/util/string.h"
00038 
00039 class AppBaseConfig;
00040 
00041 namespace goby
00042 {
00043     namespace core
00044     {        
00045         class ConfigReader
00046         {
00047           public:
00048             static void read_cfg(int argc,
00049                                  char* argv[],
00050                                  google::protobuf::Message* message,
00051                                  std::string* application_name,
00052                                  boost::program_options::options_description* od_all,
00053                                  boost::program_options::variables_map* var_map);
00054             
00055             static void merge_app_base_cfg(AppBaseConfig* base_cfg,
00056                                            const boost::program_options::variables_map& var_map);
00057             
00058             static void get_protobuf_program_options(
00059                 boost::program_options::options_description& po_desc,
00060                 const google::protobuf::Descriptor* desc);
00061             
00062             static void set_protobuf_program_option(
00063                 const boost::program_options::variables_map& vm,
00064                 google::protobuf::Message& message,
00065                 const std::string& full_name,
00066                 const boost::program_options::variable_value& value);
00067 
00068             static void get_example_cfg_file(google::protobuf::Message* message,
00069                                              std::ostream* human_desc_ss,
00070                                              const std::string& indent = "");
00071 
00072             
00073           private:
00074 
00075             enum { MAX_CHAR_PER_LINE = 66 };
00076             enum { MIN_CHAR = 20 };
00077             
00078             static void build_description(const google::protobuf::Descriptor* desc,
00079                                           std::ostream& human_desc,
00080                                           const std::string& indent = "",
00081                                           bool use_color = true);
00082 
00083             static void build_description_field(const google::protobuf::FieldDescriptor* desc,
00084                                                 std::ostream& human_desc,
00085                                                 const std::string& indent,
00086                                                 bool use_color);
00087 
00088             static void wrap_description(std::string* description,
00089                                          int num_blanks);
00090 
00091             
00092             static std::string label(const google::protobuf::FieldDescriptor* field_desc);
00093 
00094 
00095             static std::string word_wrap(std::string s, unsigned width,
00096                                          const std::string & delim);
00097             
00098             template<typename T>
00099                 static void set_single_option(
00100                     boost::program_options::options_description& po_desc,
00101                     const google::protobuf::FieldDescriptor* field_desc,
00102                     const T& default_value,
00103                     const std::string& name,
00104                     const std::string& description)
00105             {
00106                 if(!field_desc->is_repeated())
00107                 {
00108                     if(field_desc->has_default_value())
00109                     {
00110                         po_desc.add_options()
00111                             (name.c_str(),
00112                              boost::program_options::value<T>()->default_value(default_value),
00113                              description.c_str());
00114                     }
00115                     else
00116                     {
00117                         po_desc.add_options()
00118                             (name.c_str(),
00119                              boost::program_options::value<T>(),
00120                              description.c_str());
00121                     }
00122                 }
00123                 else
00124                 {                    
00125                     if(field_desc->has_default_value())
00126                     {
00127                         po_desc.add_options()
00128                             (name.c_str(),
00129                              boost::program_options::value<std::vector<T> >()->default_value(
00130                                  std::vector<T>(1, default_value),
00131                                  goby::util::as<std::string>(default_value)),
00132                              description.c_str());
00133                     }
00134                     else
00135                     {
00136                         po_desc.add_options()
00137                             (name.c_str(),
00138                              boost::program_options::value<std::vector<T> >(),
00139                              description.c_str());
00140                     }
00141                 }
00142                 
00143             }
00144         };        
00145     }
00146 }
00147 
00148 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends