/src/wxwidgets/include/wx/cmdargs.h
Line | Count | Source (jump to first uncovered line) |
1 | | /////////////////////////////////////////////////////////////////////////////// |
2 | | // Name: wx/cmdargs.h |
3 | | // Purpose: declaration of wxCmdLineArgsArray helper class |
4 | | // Author: Vadim Zeitlin |
5 | | // Created: 2007-11-12 |
6 | | // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org> |
7 | | // Licence: wxWindows licence |
8 | | /////////////////////////////////////////////////////////////////////////////// |
9 | | |
10 | | #ifndef _WX_CMDARGS_H_ |
11 | | #define _WX_CMDARGS_H_ |
12 | | |
13 | | #include "wx/arrstr.h" |
14 | | |
15 | | // ---------------------------------------------------------------------------- |
16 | | // wxCmdLineArgsArray: helper class used by wxApp::argv |
17 | | // ---------------------------------------------------------------------------- |
18 | | |
19 | | // this class is used instead of either "char **" or "wchar_t **" (neither of |
20 | | // which would be backwards compatible with all the existing code) for argv |
21 | | // field of wxApp |
22 | | // |
23 | | // as it's used for compatibility, it tries to look as much as traditional |
24 | | // (char **) argv as possible, in particular it provides implicit conversions |
25 | | // to "char **" and also array-like operator[] |
26 | | class WXDLLIMPEXP_BASE wxCmdLineArgsArray |
27 | | { |
28 | | public: |
29 | 0 | wxCmdLineArgsArray() { m_argsA = nullptr; m_argsW = nullptr; } |
30 | | |
31 | | template <typename T> |
32 | | void Init(int argc, T **argv) |
33 | | { |
34 | | FreeArgs(); |
35 | | |
36 | | m_args.clear(); |
37 | | m_args.reserve(argc); |
38 | | |
39 | | for ( int i = 0; i < argc; i++ ) |
40 | | { |
41 | | m_args.push_back(argv[i]); |
42 | | } |
43 | | } |
44 | | |
45 | | operator char**() const |
46 | 0 | { |
47 | 0 | if ( !m_argsA ) |
48 | 0 | { |
49 | 0 | const size_t count = m_args.size(); |
50 | 0 | m_argsA = new char *[count + 1]; |
51 | 0 | for ( size_t n = 0; n < count; n++ ) |
52 | 0 | m_argsA[n] = wxStrdup(m_args[n].ToAscii()); |
53 | 0 |
|
54 | 0 | m_argsA[count] = nullptr; |
55 | 0 | } |
56 | 0 |
|
57 | 0 | return m_argsA; |
58 | 0 | } |
59 | | |
60 | | operator wchar_t**() const |
61 | 0 | { |
62 | 0 | if ( !m_argsW ) |
63 | 0 | { |
64 | 0 | const size_t count = m_args.size(); |
65 | 0 | m_argsW = new wchar_t *[count + 1]; |
66 | 0 | for ( size_t n = 0; n < count; n++ ) |
67 | 0 | m_argsW[n] = wxStrdup(m_args[n].wc_str()); |
68 | 0 |
|
69 | 0 | m_argsW[count] = nullptr; |
70 | 0 | } |
71 | 0 |
|
72 | 0 | return m_argsW; |
73 | 0 | } |
74 | | |
75 | | // existing code does checks like "if ( argv )" and we want it to continue |
76 | | // to compile, so provide this conversion even if it is pretty dangerous |
77 | | operator bool() const |
78 | 0 | { |
79 | 0 | return !m_args.empty(); |
80 | 0 | } |
81 | | |
82 | | // and the same for "if ( !argv )" checks |
83 | | bool operator!() const |
84 | 0 | { |
85 | 0 | return m_args.empty(); |
86 | 0 | } |
87 | | |
88 | | wxString operator[](size_t n) const |
89 | 0 | { |
90 | 0 | return m_args[n]; |
91 | 0 | } |
92 | | |
93 | | // we must provide this overload for g++ 3.4 which can't choose between |
94 | | // our operator[](size_t) and ::operator[](char**, int) otherwise |
95 | | wxString operator[](int n) const |
96 | 0 | { |
97 | 0 | return m_args[n]; |
98 | 0 | } |
99 | | |
100 | | |
101 | | // convenience methods, i.e. not existing only for backwards compatibility |
102 | | |
103 | | // do we have any arguments at all? |
104 | 0 | bool IsEmpty() const { return m_args.empty(); } |
105 | | |
106 | | // access the arguments as a convenient array of wxStrings |
107 | 0 | const wxArrayString& GetArguments() const { return m_args; } |
108 | | |
109 | | ~wxCmdLineArgsArray() |
110 | 0 | { |
111 | 0 | FreeArgs(); |
112 | 0 | } |
113 | | |
114 | | private: |
115 | | template <typename T> |
116 | | void Free(T**& args) |
117 | 0 | { |
118 | 0 | if ( !args ) |
119 | 0 | return; |
120 | | |
121 | 0 | const size_t count = m_args.size(); |
122 | 0 | for ( size_t n = 0; n < count; n++ ) |
123 | 0 | free(args[n]); |
124 | |
|
125 | 0 | delete [] args; |
126 | 0 | args = nullptr; |
127 | 0 | } Unexecuted instantiation: void wxCmdLineArgsArray::Free<char>(char**&) Unexecuted instantiation: void wxCmdLineArgsArray::Free<wchar_t>(wchar_t**&) |
128 | | |
129 | | void FreeArgs() |
130 | 0 | { |
131 | 0 | Free(m_argsA); |
132 | 0 | Free(m_argsW); |
133 | 0 | } |
134 | | |
135 | | wxArrayString m_args; |
136 | | mutable char **m_argsA; |
137 | | mutable wchar_t **m_argsW; |
138 | | |
139 | | wxDECLARE_NO_COPY_CLASS(wxCmdLineArgsArray); |
140 | | }; |
141 | | |
142 | | // provide global operator overload for compatibility with the existing code |
143 | | // doing things like "if ( condition && argv )" |
144 | | inline bool operator&&(bool cond, const wxCmdLineArgsArray& array) |
145 | 0 | { |
146 | 0 | return cond && !array.IsEmpty(); |
147 | 0 | } |
148 | | |
149 | | #endif // _WX_CMDARGS_H_ |
150 | | |