]>
Commit | Line | Data |
---|---|---|
541ea80f VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/cmdargs.h | |
3 | // Purpose: declaration of wxCmdLineArgsArray helper class | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2007-11-12 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org> | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef _WX_CMDARGS_H_ | |
12 | #define _WX_CMDARGS_H_ | |
13 | ||
14 | #include "wx/arrstr.h" | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // wxCmdLineArgsArray: helper class used by wxApp::argv | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #if wxUSE_UNICODE | |
21 | ||
22 | // this class is used instead of either "char **" or "wchar_t **" (neither of | |
23 | // which would be backwards compatible with all the existing code) for argv | |
24 | // field of wxApp | |
25 | // | |
26 | // as it's used for compatibility, it tries to look as much as traditional | |
27 | // (char **) argv as possible, in particular it provides implicit conversions | |
28 | // to "char **" and also array-like operator[] | |
29 | class wxCmdLineArgsArray | |
30 | { | |
31 | public: | |
32 | wxCmdLineArgsArray() { m_argsA = NULL; m_argsW = NULL; } | |
33 | ||
34 | template <typename T> | |
35 | wxCmdLineArgsArray& operator=(T **argv) | |
36 | { | |
37 | FreeArgs(); | |
38 | ||
39 | m_args.clear(); | |
40 | ||
41 | if ( argv ) | |
42 | { | |
43 | while ( *argv ) | |
44 | m_args.push_back(*argv++); | |
45 | } | |
46 | ||
47 | return *this; | |
48 | } | |
49 | ||
50 | operator char**() const | |
51 | { | |
52 | if ( !m_argsA ) | |
53 | { | |
54 | const size_t count = m_args.size(); | |
55 | m_argsA = new char *[count]; | |
56 | for ( size_t n = 0; n < count; n++ ) | |
57 | m_argsA[n] = wxStrdup(m_args[n].ToAscii()); | |
58 | } | |
59 | ||
60 | return m_argsA; | |
61 | } | |
62 | ||
63 | operator wchar_t**() const | |
64 | { | |
65 | if ( !m_argsW ) | |
66 | { | |
67 | const size_t count = m_args.size(); | |
68 | m_argsW = new wchar_t *[count]; | |
69 | for ( size_t n = 0; n < count; n++ ) | |
70 | m_argsW[n] = wxStrdup(m_args[n].wc_str()); | |
71 | } | |
72 | ||
73 | return m_argsW; | |
74 | } | |
75 | ||
76 | // existing code does checks like "if ( argv )" and we want it to continue | |
77 | // to compile, so provide this conversion even if it is pretty dangerous | |
78 | operator bool() const | |
79 | { | |
80 | return !m_args.empty(); | |
81 | } | |
82 | ||
83 | wxString operator[](size_t n) const | |
84 | { | |
85 | return m_args[n]; | |
86 | } | |
87 | ||
88 | // this is the only method of this class which doesn't exist solely for | |
89 | // compatibility purposes: it allows to access the arguments as a | |
90 | // convenient array of wxStrings | |
91 | const wxArrayString& GetArguments() const { return m_args; } | |
92 | ||
93 | ~wxCmdLineArgsArray() | |
94 | { | |
95 | FreeArgs(); | |
96 | } | |
97 | ||
98 | private: | |
99 | template <typename T> | |
100 | void Free(T **args) | |
101 | { | |
102 | if ( !args ) | |
103 | return; | |
104 | ||
105 | const size_t count = m_args.size(); | |
106 | for ( size_t n = 0; n < count; n++ ) | |
107 | free(args[n]); | |
108 | ||
109 | delete [] args; | |
110 | } | |
111 | ||
112 | void FreeArgs() | |
113 | { | |
114 | Free(m_argsA); | |
115 | Free(m_argsW); | |
116 | } | |
117 | ||
118 | wxArrayString m_args; | |
119 | mutable char **m_argsA; | |
120 | mutable wchar_t **m_argsW; | |
121 | ||
122 | DECLARE_NO_COPY_CLASS(wxCmdLineArgsArray) | |
123 | }; | |
124 | ||
125 | #endif // wxUSE_UNICODE | |
126 | ||
127 | #endif // _WX_CMDARGS_H_ | |
128 |