📄 ncbiapp.hpp
字号:
/* * =========================================================================== * PRODUCTION $Log: ncbiapp.hpp,v $ * PRODUCTION Revision 1000.1 2004/02/12 21:44:20 gouriano * PRODUCTION PRODUCTION: UPGRADED [CORE_001] Dev-tree R1.40 * PRODUCTION * =========================================================================== */#ifndef CORELIB___NCBIAPP__HPP#define CORELIB___NCBIAPP__HPP/* $Id: ncbiapp.hpp,v 1000.1 2004/02/12 21:44:20 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors: Denis Vakatov, Vsevolod Sandomirskiy * * *//// @file ncbiapp.hpp/// Defines the CNcbiApplication and CAppException classes for creating/// NCBI applications.////// The CNcbiApplication class defines the application framework and the high/// high level behavior of an application, and the CAppException class is used/// for the exceptions generated by CNcbiApplication.#include <corelib/ncbistd.hpp>#include <corelib/ncbienv.hpp>#include <corelib/ncbiargs.hpp>#include <corelib/ncbireg.hpp>#include <corelib/version.hpp>#include <memory>/// Avoid preprocessor name clash with the NCBI C Toolkit.#if !defined(NCBI_OS_UNIX) || defined(HAVE_NCBI_C)# if defined(GetArgs)# undef GetArgs# endif# define GetArgs GetArgs#endif/** @addtogroup AppFramework * * @{ */BEGIN_NCBI_SCOPE/////////////////////////////////////////////////////////////////////////////////// CAppException --////// Define exceptions generated by CNcbiApplication.////// CAppException inherits its basic functionality from CCoreException/// and defines additional error codes for applications.class NCBI_XNCBI_EXPORT CAppException : public CCoreException{public: /// Error types that an application can generate. /// /// These error conditions are checked for and caught inside AppMain(). enum EErrCode { eUnsetArgs, ///< Command-line argument description not found eSetupDiag, ///< Application diagnostic stream setup failed eLoadConfig, ///< Registry data failed to load from config file eSecond, ///< Second instance of CNcbiApplication is prohibited eNoRegistry ///< Registry file cannot be opened }; /// Translate from the error code value to its string representation. virtual const char* GetErrCodeString(void) const { switch (GetErrCode()) { case eUnsetArgs: return "eUnsetArgs"; case eSetupDiag: return "eSetupDiag"; case eLoadConfig: return "eLoadConfig"; case eSecond: return "eSecond"; case eNoRegistry: return "eNoRegistry"; default: return CException::GetErrCodeString(); } } // Standard exception boilerplate code. NCBI_EXCEPTION_DEFAULT(CAppException, CCoreException);};///////////////////////////////////////////////////////// CNcbiApplication///// Where to write the application's diagnostics to.enum EAppDiagStream { eDS_ToStdout, ///< To standard output stream eDS_ToStderr, ///< To standard error stream eDS_ToStdlog, ///< Add to standard log file (app.name + ".log") eDS_ToMemory, ///< Keep in a temp.memory buffer, see FlushDiag() eDS_Disable, ///< Dont write it anywhere eDS_User, ///< Leave as was previously set (or not set) by user eDS_AppSpecific, ///< Depends on the application type eDS_Default ///< "eDS_User" if set, else "eDS_AppSpecific"};/////////////////////////////////////////////////////////////////////////////////// CNcbiApplication --////// Basic (abstract) NCBI application class.////// Defines the high level behavior of an NCBI application./// A new application is written by deriving a class from the CNcbiApplication/// and writing an implementation of the Run() and maybe some other (like/// Init(), Exit(), etc.) methods.class NCBI_XNCBI_EXPORT CNcbiApplication{public: /// Singleton method. /// /// Track the instance of CNcbiApplication, and throw an exception /// if an attempt is made to create another instance of the application. /// @return /// Current application instance. static CNcbiApplication* Instance(void); /// Constructor. /// /// Register the application instance, and reset important /// application-specific settings to empty values that will /// be set later. CNcbiApplication(void); /// Destructor. /// /// Clean up the application settings, flush the diagnostic stream. virtual ~CNcbiApplication(void); /// Main function (entry point) for the NCBI application. /// /// You can specify where to write the diagnostics to (EAppDiagStream), /// and where to get the configuration file (LoadConfig()) to load /// to the application registry (accessible via GetConfig()). /// /// Throw exception if: /// - not-only instance /// - cannot load explicitly specified config.file /// - SetupDiag() throws an exception /// /// If application name is not specified a default of "ncbi" is used. /// Certain flags such as -logfile, -conffile and -version are special so /// AppMain() processes them separately. /// @return /// Exit code from Run(). Can also return non-zero value if application /// threw an exception. /// @sa /// LoadConfig(), Init(), Run(), Exit() int AppMain (int argc, ///< argc in a regular main(argc, argv, envp) const char* const* argv, ///< argv in a regular main(argc, argv, envp) const char* const* envp = 0, ///< argv in a regular main(argc, argv, envp) EAppDiagStream diag = eDS_Default, ///< Specify diagnostic stream const char* conf = NcbiEmptyCStr, ///< Specify registry to load const string& name = NcbiEmptyString ///< Specify application name ); /// Initialize the application. /// /// The default behavior of this is "do nothing". If you have special /// initialization logic that needs to be peformed, then you must override /// this method with your own logic. virtual void Init(void); /// Run the application. /// /// It is defined as a pure virtual method -- so you must(!) supply the /// Run() method to implement the application-specific logic. /// @return /// Exit code. virtual int Run(void) = 0; /// Cleanup on application exit. /// /// Perform cleanup before exiting. The default behavior of this is /// "do nothing". If you have special cleanup logic that needs to be /// performed, then you must override this method with your own logic. virtual void Exit(void); /// Get the application's cached unprocessed command-line arguments. const CNcbiArguments& GetArguments(void) const; /// Get parsed command line arguments. /// /// Get command line arguments parsed according to the arg descriptions /// set by SetArgDescriptions(). Throw exception if no descriptions /// have been set. /// @return /// The CArgs object containing parsed cmd.-line arguments. /// @sa /// SetArgDescriptions(). const CArgs& GetArgs(void) const; /// Get the application's cached environment. const CNcbiEnvironment& GetEnvironment(void) const; /// Get a non-const copy of the application's cached environment. CNcbiEnvironment& SetEnvironment(void); /// Set a specified environment variable by name void SetEnvironment(const string& name, const string& value); /// Get the application's cached configuration parameters. const CNcbiRegistry& GetConfig(void) const; CNcbiRegistry& GetConfig(void); /// Flush the in-memory diagnostic stream (for "eDS_ToMemory" case only). /// /// In case of "eDS_ToMemory", the diagnostics is stored in /// the internal application memory buffer ("m_DiagStream"). /// Call this function to dump all the diagnostics to stream "os" and /// purge the buffer. /// @param os /// Output stream to dump diagnostics to. If it is NULL, then /// nothing will be written to it (but the buffer will still be purged). /// @param close_diag /// If "close_diag" is TRUE, then also destroy "m_DiagStream". /// @return /// Total number of bytes actually written to "os". SIZE_TYPE FlushDiag(CNcbiOstream* os, bool close_diag = false); /// Get the application's "display" name. /// /// Get name of this application, suitable for displaying /// or for using as the base name for other files. /// Will be the 'name' argument of AppMain if given. /// Otherwise will be taken from the actual name of the application file /// or argv[0]. string GetProgramDisplayName(void) const;protected: /// Disable argument descriptions. /// /// Call if you do not want cmd.line args to be parsed at all. /// Note that by default ArgDescriptions are enabled (i.e. required). void DisableArgDescriptions(void); /// Which standard flag's descriptions should not be displayed in /// the usage message. /// /// Do not display descriptions of the standard flags such as the /// -h, -logfile, -conffile, -version /// flags in the usage message. Note that you still can pass them in /// the command line. enum EHideStdArgs { fHideHelp = 0x01, ///< Hide help description fHideLogfile = 0x02, ///< Hide log file description fHideConffile = 0x04, ///< Hide configuration file description fHideVersion = 0x08 ///< Hide version description }; typedef int THideStdArgs; ///< Binary OR of "EHideStdArgs" /// Set the hide mask for the Hide Std Flags. void HideStdArgs(THideStdArgs hide_mask); /// Flags to adjust standard I/O streams' behaviour. /// /// Set these flags if you insist on using compiler-specific defaults /// for standard "C++" I/O streams (Cin/Cout/Cerr). enum EStdioSetup { fDefault_SyncWithStdio = 0x01, ///< Use compiler-specific default as pertains to the synchronizing ///< of "C++" Cin/Cout/Cerr streams with their "C" counterparts. fDefault_CinBufferSize = 0x02 ///< Use compiler-specific default of Cin buffer size. }; typedef int TStdioSetupFlags; ///< Binary OR of "EStdioSetup" /// Adjust the behavior of standard I/O streams. /// /// Unless this function is called, the behaviour of "C++" Cin/Cout/Cerr /// streams will be the same regardless of the compiler used. /// /// IMPLEMENTATION NOTE: Do not call this function more than once /// and from places other than App's constructor. void SetStdioFlags(TStdioSetupFlags stdio_flags); /// Set the version number for the program. ///
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -