]> git.saurik.com Git - wxWidgets.git/blob - include/wx/cmdargs.h
3f99cb50eb783363f404dade28fc834866535e6b
[wxWidgets.git] / include / wx / cmdargs.h
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 WXDLLIMPEXP_BASE 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 // and the same for "if ( !argv )" checks
84 bool operator!() const
85 {
86 return m_args.empty();
87 }
88
89 wxString operator[](size_t n) const
90 {
91 return m_args[n];
92 }
93
94 // we must provide this overload for g++ 3.4 which can't choose between
95 // our operator[](size_t) and ::operator[](char**, int) otherwise
96 wxString operator[](int n) const
97 {
98 return m_args[n];
99 }
100
101
102 // convenience methods, i.e. not existing only for backwards compatibility
103
104 // do we have any arguments at all?
105 bool IsEmpty() const { return m_args.empty(); }
106
107 // access the arguments as a convenient array of wxStrings
108 const wxArrayString& GetArguments() const { return m_args; }
109
110 ~wxCmdLineArgsArray()
111 {
112 FreeArgs();
113 }
114
115 private:
116 template <typename T>
117 void Free(T **args)
118 {
119 if ( !args )
120 return;
121
122 const size_t count = m_args.size();
123 for ( size_t n = 0; n < count; n++ )
124 free(args[n]);
125
126 delete [] args;
127 }
128
129 void FreeArgs()
130 {
131 Free(m_argsA);
132 Free(m_argsW);
133 }
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 {
146 return cond && !array.IsEmpty();
147 }
148
149 #endif // wxUSE_UNICODE
150
151 #endif // _WX_CMDARGS_H_
152