Application.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    ---------------------------------------------------------------------------
00021 */
00022 
00023 #ifndef __ccxx_Application_hxx
00024 #define __ccxx_Application_hxx
00025 
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/String.h++>
00028 
00029 #include <cstddef>
00030 
00031 namespace ccxx {
00032 
00043 class COMMONCPP_API Application
00044 {
00045   private:
00046 
00047   class CommandOption
00048   {
00049     friend class Application;
00050 
00051     protected:
00052 
00053     char _opt;
00054     String _longOpt;
00055     bool _hasArg;
00056     String _argName;
00057     String _argDesc;
00058 
00059     CommandOption(char opt, const String &longOpt, const String &argName,
00060                   const String &argDesc);
00061 
00062     inline bool hasOpt() const
00063     { return(_opt != NUL); }
00064 
00065     inline bool hasLongOpt() const
00066     { return(! _longOpt.isNull()); }
00067 
00068     inline char getOpt() const
00069     { return (_opt); }
00070 
00071     inline String getLongOpt() const
00072     { return(_longOpt); }
00073 
00074     inline bool hasArg() const { return(_hasArg); }
00075 
00076     inline const String& getArgDesc() const { return(_argDesc); }
00077     inline const String& getArgName() const { return(_argName); }
00078   };
00079 
00080   class CommandOptionList; // fwd decl
00081 
00082   public:
00083 
00096   Application(int argc, char **argv, const String &version = "0.0",
00097               const String &compileDate = "",
00098               const String &compileTime = "");
00099 
00102   virtual ~Application();
00103 
00104   protected:
00105 
00117   virtual void shutdown();
00118 
00145   bool registerOption(char opt, const String &longOpt,
00146                       const String &argName, const String &desc);
00147 
00161   bool parseOptions(int argc, char **argv, int& index);
00162 
00183   virtual bool processOption(char opt, const String &longOpt,
00184                              const char *arg = NULL);
00185 
00188   virtual void printUsage();
00189 
00194   void printError(const char *message, ...);
00195 
00197   String _version;
00198   String _compileDate;
00199   String _compileTime;
00200   CommandOptionList *_options;
00201   int _numLongOpts;
00204   private:
00205 
00206   void buildUsageText();
00207   void doShutdown();
00208 
00209   bool _argsParsed;
00210   bool _useStyles;
00211   String _helpText;
00212   String _optString;
00213 
00214   static Application *_instance;
00215 
00216   static void _sighandler(int sig);
00217 };
00218 
00219 }; // namespace ccxx
00220 
00221 #endif // __ccxx_Application_hxx
00222 
00223 /* end of header file */
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3