]> git.saurik.com Git - wxWidgets.git/blob - include/wx/cmdargs.h
Add wxActivateEvent::GetActivationReason().
[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 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.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 #if wxUSE_UNICODE
20
21 // this class is used instead of either "char **" or "wchar_t **" (neither of
22 // which would be backwards compatible with all the existing code) for argv
23 // field of wxApp
24 //
25 // as it's used for compatibility, it tries to look as much as traditional
26 // (char **) argv as possible, in particular it provides implicit conversions
27 // to "char **" and also array-like operator[]
28 class WXDLLIMPEXP_BASE wxCmdLineArgsArray
29 {
30 public:
31 wxCmdLineArgsArray() { m_argsA = NULL; m_argsW = NULL; }
32
33 template <typename T>
34 wxCmdLineArgsArray& operator=(T **argv)
35 {
36 FreeArgs();
37
38 m_args.clear();
39
40 if ( argv )
41 {
42 while ( *argv )
43 m_args.push_back(*argv++);
44 }
45
46 return *this;
47 }
48
49 operator char**() const
50 {
51 if ( !m_argsA )
52 {
53 const size_t count = m_args.size();
54 m_argsA = new char *[count];
55 for ( size_t n = 0; n < count; n++ )
56 m_argsA[n] = wxStrdup(m_args[n].ToAscii());
57 }
58
59 return m_argsA;
60 }
61
62 operator wchar_t**() const
63 {
64 if ( !m_argsW )
65 {
66 const size_t count = m_args.size();
67 m_argsW = new wchar_t *[count];
68 for ( size_t n = 0; n < count; n++ )
69 m_argsW[n] = wxStrdup(m_args[n].wc_str());
70 }
71
72 return m_argsW;
73 }
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 {
79 return !m_args.empty();
80 }
81
82 // and the same for "if ( !argv )" checks
83 bool operator!() const
84 {
85 return m_args.empty();
86 }
87
88 wxString operator[](size_t n) const
89 {
90 return m_args[n];
91 }
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 {
97 return m_args[n];
98 }
99
100
101 // convenience methods, i.e. not existing only for backwards compatibility
102
103 // do we have any arguments at all?
104 bool IsEmpty() const { return m_args.empty(); }
105
106 // access the arguments as a convenient array of wxStrings
107 const wxArrayString& GetArguments() const { return m_args; }
108
109 ~wxCmdLineArgsArray()
110 {
111 FreeArgs();
112 }
113
114 private:
115 template <typename T>
116 void Free(T **args)
117 {
118 if ( !args )
119 return;
120
121 const size_t count = m_args.size();
122 for ( size_t n = 0; n < count; n++ )
123 free(args[n]);
124
125 delete [] args;
126 }
127
128 void FreeArgs()
129 {
130 Free(m_argsA);
131 Free(m_argsW);
132 }
133
134 wxArrayString m_args;
135 mutable char **m_argsA;
136 mutable wchar_t **m_argsW;
137
138 wxDECLARE_NO_COPY_CLASS(wxCmdLineArgsArray);
139 };
140
141 // provide global operator overload for compatibility with the existing code
142 // doing things like "if ( condition && argv )"
143 inline bool operator&&(bool cond, const wxCmdLineArgsArray& array)
144 {
145 return cond && !array.IsEmpty();
146 }
147
148 #endif // wxUSE_UNICODE
149
150 #endif // _WX_CMDARGS_H_
151