application.hpp revision 240116
1240116Smarcel//
2240116Smarcel// Automated Testing Framework (atf)
3240116Smarcel//
4240116Smarcel// Copyright (c) 2007 The NetBSD Foundation, Inc.
5240116Smarcel// All rights reserved.
6240116Smarcel//
7240116Smarcel// Redistribution and use in source and binary forms, with or without
8240116Smarcel// modification, are permitted provided that the following conditions
9240116Smarcel// are met:
10240116Smarcel// 1. Redistributions of source code must retain the above copyright
11240116Smarcel//    notice, this list of conditions and the following disclaimer.
12240116Smarcel// 2. Redistributions in binary form must reproduce the above copyright
13240116Smarcel//    notice, this list of conditions and the following disclaimer in the
14240116Smarcel//    documentation and/or other materials provided with the distribution.
15240116Smarcel//
16240116Smarcel// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17240116Smarcel// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18240116Smarcel// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19240116Smarcel// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20240116Smarcel// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21240116Smarcel// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22240116Smarcel// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23240116Smarcel// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24240116Smarcel// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25240116Smarcel// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26240116Smarcel// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27240116Smarcel// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28240116Smarcel//
29240116Smarcel
30240116Smarcel#if !defined(_ATF_CXX_APPLICATION_HPP_)
31240116Smarcel#define _ATF_CXX_APPLICATION_HPP_
32240116Smarcel
33240116Smarcel#include <ostream>
34240116Smarcel#include <set>
35240116Smarcel#include <stdexcept>
36240116Smarcel#include <string>
37240116Smarcel
38240116Smarcelnamespace atf {
39240116Smarcelnamespace application {
40240116Smarcel
41240116Smarcel// ------------------------------------------------------------------------
42240116Smarcel// The "usage_error" class.
43240116Smarcel// ------------------------------------------------------------------------
44240116Smarcel
45240116Smarcelclass usage_error : public std::runtime_error {
46240116Smarcel    char m_text[4096];
47240116Smarcel
48240116Smarcelpublic:
49240116Smarcel    usage_error(const char*, ...) throw();
50240116Smarcel    ~usage_error(void) throw();
51240116Smarcel
52240116Smarcel    const char* what(void) const throw();
53240116Smarcel};
54240116Smarcel
55240116Smarcel// ------------------------------------------------------------------------
56240116Smarcel// The "option" class.
57240116Smarcel// ------------------------------------------------------------------------
58240116Smarcel
59240116Smarcelclass option {
60240116Smarcel    char m_character;
61240116Smarcel    std::string m_argument;
62240116Smarcel    std::string m_description;
63240116Smarcel
64240116Smarcel    friend class app;
65240116Smarcel
66240116Smarcelpublic:
67240116Smarcel    option(char, const std::string&, const std::string&);
68240116Smarcel
69240116Smarcel    bool operator<(const option&) const;
70240116Smarcel};
71240116Smarcel
72240116Smarcel// ------------------------------------------------------------------------
73240116Smarcel// The "app" class.
74240116Smarcel// ------------------------------------------------------------------------
75240116Smarcel
76240116Smarcelclass app {
77240116Smarcel    bool m_hflag;
78240116Smarcel
79240116Smarcel    void process_options(void);
80240116Smarcel    void usage(std::ostream&);
81240116Smarcel
82240116Smarcel    bool inited(void);
83240116Smarcel
84240116Smarcelprotected:
85240116Smarcel    typedef std::set< option > options_set;
86240116Smarcel
87240116Smarcel    int m_argc;
88240116Smarcel    char* const* m_argv;
89240116Smarcel
90240116Smarcel    const char* m_argv0;
91240116Smarcel    const char* m_prog_name;
92240116Smarcel    std::string m_description;
93240116Smarcel    std::string m_manpage, m_global_manpage;
94240116Smarcel    const bool m_use_ui;
95240116Smarcel
96240116Smarcel    options_set options(void);
97240116Smarcel
98240116Smarcel    // To be redefined.
99240116Smarcel    virtual std::string specific_args(void) const;
100240116Smarcel    virtual options_set specific_options(void) const;
101240116Smarcel    virtual void process_option(int, const char*);
102240116Smarcel    virtual int main(void) = 0;
103240116Smarcel
104240116Smarcelpublic:
105240116Smarcel    app(const std::string&, const std::string&, const std::string&,
106240116Smarcel        bool = true);
107240116Smarcel    virtual ~app(void);
108240116Smarcel
109240116Smarcel    int run(int, char* const*);
110240116Smarcel};
111240116Smarcel
112240116Smarcel} // namespace application
113240116Smarcel} // namespace atf
114240116Smarcel
115240116Smarcel#endif // !defined(_ATF_CXX_APPLICATION_HPP_)
116