1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: declaration of wxCmdLineArgsArray helper class
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_CMDARGS_H_
12 #define _WX_CMDARGS_H_
14 #include "wx/arrstr.h"
16 // ----------------------------------------------------------------------------
17 // wxCmdLineArgsArray: helper class used by wxApp::argv
18 // ----------------------------------------------------------------------------
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
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
32 wxCmdLineArgsArray() { m_argsA
= NULL
; m_argsW
= NULL
; }
35 wxCmdLineArgsArray
& operator=(T
**argv
)
44 m_args
.push_back(*argv
++);
50 operator char**() const
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());
63 operator wchar_t**() const
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());
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
80 return !m_args
.empty();
83 // and the same for "if ( !argv )" checks
84 bool operator!() const
86 return m_args
.empty();
89 wxString
operator[](size_t n
) const
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
102 // convenience methods, i.e. not existing only for backwards compatibility
104 // do we have any arguments at all?
105 bool IsEmpty() const { return m_args
.empty(); }
107 // access the arguments as a convenient array of wxStrings
108 const wxArrayString
& GetArguments() const { return m_args
; }
110 ~wxCmdLineArgsArray()
116 template <typename T
>
122 const size_t count
= m_args
.size();
123 for ( size_t n
= 0; n
< count
; n
++ )
135 wxArrayString m_args
;
136 mutable char **m_argsA
;
137 mutable wchar_t **m_argsW
;
139 DECLARE_NO_COPY_CLASS(wxCmdLineArgsArray
)
142 // provide global operator overload for compatibility with the existing code
143 // doing things like "if ( condition && argv )"
144 inline operator&&(bool cond
, const wxCmdLineArgsArray
& array
)
146 return cond
&& !array
.IsEmpty();
149 #endif // wxUSE_UNICODE
151 #endif // _WX_CMDARGS_H_