📄 application.h
字号:
/// usually happens as the first action in Application::initialize(),
/// the application's logger is the one specified by the
/// "application.logger" configuration property. If that property
/// is not specified, the logger is "Application".
const OptionSet& options() const;
/// Returns the application's option set.
static Application& instance();
/// Returns a reference to the Application singleton.
///
/// Throws a NullPointerException if no Application instance exists.
const Poco::Timestamp& startTime() const;
/// Returns the application start time (UTC).
Poco::Timespan uptime() const;
/// Returns the application uptime.
void stopOptionsProcessing();
/// If called from an option callback, stops all further
/// options processing.
///
/// If called, the following options on the command line
/// will not be processed, and required options will not
/// be checked.
///
/// This is useful, for example, if an option for displaying
/// help information has been encountered and no other things
/// besides displaying help shall be done.
const char* name() const;
protected:
void initialize(Application& self);
/// Initializes the application and all registered subsystems.
/// Subsystems are always initialized in the exact same order
/// in which they have been registered.
///
/// Overriding implementations must call the base class implementation.
void uninitialize();
/// Uninitializes the application and all registered subsystems.
/// Subsystems are always uninitialized in reverse order in which
/// they have been initialized.
///
/// Overriding implementations must call the base class implementation.
void reinitialize(Application& self);
/// Re-nitializes the application and all registered subsystems.
/// Subsystems are always reinitialized in the exact same order
/// in which they have been registered.
///
/// Overriding implementations must call the base class implementation.
virtual void defineOptions(OptionSet& options);
/// Called before command line processing begins.
/// If a subclass wants to support command line arguments,
/// it must override this method.
/// The default implementation does not define any options itself,
/// but calls defineOptions() on all registered subsystems.
///
/// Overriding implementations should call the base class implementation.
virtual void handleOption(const std::string& name, const std::string& value);
/// Called when the option with the given name is encountered
/// during command line arguments processing.
///
/// The default implementation does option validation, bindings
/// and callback handling.
///
/// Overriding implementations must call the base class implementation.
void setLogger(Poco::Logger& logger);
/// Sets the logger used by the application.
virtual int main(const std::vector<std::string>& args);
/// The application's main logic.
///
/// Unprocessed command line arguments are passed in args.
/// Note that all original command line arguments are available
/// via the properties application.argc and application.argv[<n>].
///
/// Returns an exit code which should be one of the values
/// from the ExitCode enumeration.
bool findFile(Poco::Path& path) const;
/// Searches for the file in path in the application directory.
///
/// If path is absolute, the method immediately returns true and
/// leaves path unchanged.
///
/// If path is relative, searches for the file in the application
/// directory and in all subsequent parent directories.
/// Returns true and stores the absolute path to the file in
/// path if the file could be found. Returns false and leaves path
/// unchanged otherwise.
void init();
/// Common initialization code.
~Application();
/// Destroys the Application and deletes all registered subsystems.
private:
void setup();
void setArgs(int argc, char* argv[]);
void setArgs(const std::vector<std::string>& args);
void getApplicationPath(Poco::Path& path) const;
void processOptions();
bool findAppConfigFile(const std::string& appName, const std::string& extension, Poco::Path& path) const;
typedef Poco::AutoPtr<Subsystem> SubsystemPtr;
typedef std::vector<SubsystemPtr> SubsystemVec;
typedef Poco::AutoPtr<LayeredConfiguration> ConfigPtr;
typedef std::vector<std::string> ArgVec;
ConfigPtr _pConfig;
SubsystemVec _subsystems;
bool _initialized;
std::string _command;
ArgVec _args;
OptionSet _options;
bool _unixOptions;
Poco::Logger* _pLogger;
Poco::Timestamp _startTime;
bool _stopOptionsProcessing;
static Application* _pInstance;
friend class LoggingSubsystem;
Application(const Application&);
Application& operator = (const Application&);
};
//
// inlines
//
template <class C> C& Application::getSubsystem() const
{
for (SubsystemVec::const_iterator it = _subsystems.begin(); it != _subsystems.end(); ++it)
{
const Subsystem* pSS(it->get());
const C* pC = dynamic_cast<const C*>(pSS);
if (pC) return *const_cast<C*>(pC);
}
throw Poco::NotFoundException("The subsystem has not been registered", typeid(C).name());
}
inline bool Application::initialized() const
{
return _initialized;
}
inline LayeredConfiguration& Application::config() const
{
return *const_cast<LayeredConfiguration*>(_pConfig.get());
}
inline Poco::Logger& Application::logger() const
{
poco_check_ptr (_pLogger);
return *_pLogger;
}
inline const OptionSet& Application::options() const
{
return _options;
}
inline Application& Application::instance()
{
poco_check_ptr (_pInstance);
return *_pInstance;
}
inline const Poco::Timestamp& Application::startTime() const
{
return _startTime;
}
inline Poco::Timespan Application::uptime() const
{
Poco::Timestamp now;
Poco::Timespan uptime = now - _startTime;
return uptime;
}
} } // namespace Poco::Util
//
// Macro to implement main()
//
#if defined(_WIN32) && defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
#define POCO_APP_MAIN(App) \
int wmain(int argc, wchar_t** argv) \
{ \
Poco::AutoPtr<App> pApp = new App; \
try \
{ \
pApp->init(argc, argv); \
} \
catch (Poco::Exception& exc) \
{ \
pApp->logger().log(exc); \
return Application::EXIT_CONFIG;\
} \
return pApp->run(); \
}
#else
#define POCO_APP_MAIN(App) \
int main(int argc, char** argv) \
{ \
Poco::AutoPtr<App> pApp = new App; \
try \
{ \
pApp->init(argc, argv); \
} \
catch (Poco::Exception& exc) \
{ \
pApp->logger().log(exc); \
return Application::EXIT_CONFIG;\
} \
return pApp->run(); \
}
#endif
#endif // Util_Application_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -