Apache Qpid - AMQP Messaging for Java JMS, C++, Python, Ruby, and .NET Apache Qpid Documentation

qpid/Options.h

Go to the documentation of this file.
00001 #ifndef QPID_COMMONOPTIONS_H
00002 #define QPID_COMMONOPTIONS_H
00003 
00004 /*
00005  *
00006  * Licensed to the Apache Software Foundation (ASF) under one
00007  * or more contributor license agreements.  See the NOTICE file
00008  * distributed with this work for additional information
00009  * regarding copyright ownership.  The ASF licenses this file
00010  * to you under the Apache License, Version 2.0 (the
00011  * "License"); you may not use this file except in compliance
00012  * with the License.  You may obtain a copy of the License at
00013  *
00014  *   http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing,
00017  * software distributed under the License is distributed on an
00018  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00019  * KIND, either express or implied.  See the License for the
00020  * specific language governing permissions and limitations
00021  * under the License.
00022  *
00023  */
00024 
00025 #include "qpid/Exception.h"
00026 
00027 // Disable warnings triggered by boost.
00028 #ifdef _MSC_VER
00029 #  pragma warning(push)
00030 #  pragma warning(disable : 4251 4275)
00031 #endif
00032 
00033 #include <boost/program_options.hpp>
00034 #include <boost/format.hpp>
00035 
00036 #ifdef _MSC_VER
00037 #  pragma warning(pop)
00038 #endif
00039 
00040 #include <sstream>
00041 #include <iterator>
00042 #include <algorithm>
00043 #include <string>
00044 #include "qpid/CommonImportExport.h"
00045 
00046 namespace qpid {
00047 namespace po=boost::program_options;
00048 
00049 
00050 
00052 QPID_COMMON_EXTERN std::string prettyArg(const std::string&, const std::string&);
00053 
00055 template <class T>
00056 class OptionValue : public po::typed_value<T> {
00057   public:
00058     OptionValue(T& value, const std::string& arg)
00059         : po::typed_value<T>(&value), argName(arg) {}
00060     std::string name() const { return argName; }
00061 
00062   private:
00063     std::string argName;
00064 };
00065 
00066 
00073 template<class T>
00074 po::value_semantic* optValue(T& value, const char* name) {
00075     std::string valstr(boost::lexical_cast<std::string>(value));
00076     return new OptionValue<T>(value, prettyArg(name, valstr));
00077 }
00078 
00082 template <class T>
00083 po::value_semantic* optValue(std::vector<T>& value, const char* name) {
00084     using namespace std;
00085     ostringstream os;
00086     copy(value.begin(), value.end(), ostream_iterator<T>(os, " "));
00087     string val=os.str();
00088     if (!val.empty())
00089         val.erase(val.end()-1); // Remove trailing " "
00090     return (new OptionValue<vector<T> >(value, prettyArg(name, val)));
00091 }
00092 
00094 inline po::value_semantic* optValue(bool& value) { return po::bool_switch(&value); }
00095 
00136 /*
00137  *  ---------------------------------------------
00138  *  Explanation for Boost 103200 conditional code
00139  *  ---------------------------------------------
00140  *
00141  *  This boost version has an implementation of the program_options library
00142  *  that has no provision for allowing unregistered options to pass by.
00143  *
00144  *  But that means that, if you have a program that loads optional modules
00145  *  after start-up, and those modules each have their own set of options,
00146  *  then if you parse the command line too soon, you will get spurious
00147  *  reports of unrecognized options -- and the program will exit!
00148  *
00149  *  And we must process the command-line before module-loading, because we
00150  *  need to look at the "bootstrap" options.
00151  *
00152  *  This conditional code:
00153  *
00154  *      1. implements it's own functor class, derived from the Boost
00155  *         "options_description_easy_init" class.  This functor is used
00156  *         to process added options and do the functor chaining, so that
00157  *         I can snoop on the arguments before doing an explicit call
00158  *         to its parent.
00159  *
00160  *      2. It implements two static vectors, one to hold long names, and
00161  *         one for short names, so that options declared by modules are
00162  *         not forgotten when their options_description goes out of scope.
00163  *
00164  *  I will be thrilled to personally delete this code if we ever decide
00165  *  that qpid doesn't really need to support this antique version of Boost.
00166  *
00167  */
00168 
00169 #if ( BOOST_VERSION == 103200 )
00170 struct Options;
00171 
00172 
00173 struct
00174 options_description_less_easy_init
00175   : public po::options_description_easy_init
00176 {
00177   options_description_less_easy_init ( Options * my_owner,
00178                                        po::options_description * my_parents_owner
00179                                      )
00180     : po::options_description_easy_init(my_parents_owner)
00181   {
00182     owner = my_owner;
00183   }
00184 
00185 
00186   options_description_less_easy_init&
00187   operator()(char const * name,
00188              char const * description);
00189 
00190 
00191   options_description_less_easy_init&
00192   operator()(char const * name,
00193              const po::value_semantic* s);
00194 
00195 
00196   options_description_less_easy_init&
00197   operator()(const char* name,
00198              const po::value_semantic* s,
00199              const char* description);
00200 
00201 
00202   Options * owner;
00203 };
00204 #endif
00205 
00206 
00207 struct Options : public po::options_description {
00208 
00209     struct Exception : public qpid::Exception {
00210         Exception(const std::string& msg) : qpid::Exception(msg) {}
00211     };
00212 
00213     QPID_COMMON_EXTERN Options(const std::string& name=std::string());
00214 
00220     QPID_COMMON_EXTERN void parse(int argc, char const* const* argv,
00221                const std::string& configfile=std::string(),
00222                bool  allowUnknown = false);
00223 
00224 
00225   #if ( BOOST_VERSION == 103200 )
00226   options_description_less_easy_init m_less_easy;
00227 
00228   options_description_less_easy_init addOptions() {
00229       return m_less_easy;
00230   }
00231 
00232   bool
00233   is_registered_option ( std::string s );
00234 
00235   void
00236   register_names ( std::string s );
00237 
00238   static std::vector<std::string> long_names;
00239   static std::vector<std::string> short_names;
00240   #else
00241   boost::program_options::options_description_easy_init addOptions() {
00242       return add_options();
00243   }
00244   #endif
00245 };
00246 
00247 
00248 
00252 struct CommonOptions : public Options {
00253     QPID_COMMON_EXTERN CommonOptions(const std::string& name=std::string(),
00254                   const std::string& configfile=std::string());
00255     bool help;
00256     bool version;
00257     std::string config;
00258 };
00259 
00260 
00261 
00262 
00263 } // namespace qpid
00264 
00265 #endif  

Qpid C++ API Reference
Generated on Mon Dec 19 10:54:08 2011 for Qpid C++ Client API by doxygen 1.4.7