1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: declaration of wxCmdLineArgsArray helper class
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_CMDARGS_H_
11 #define _WX_CMDARGS_H_
13 #include "wx/arrstr.h"
15 // ----------------------------------------------------------------------------
16 // wxCmdLineArgsArray: helper class used by wxApp::argv
17 // ----------------------------------------------------------------------------
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
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
31 wxCmdLineArgsArray() { m_argsA
= NULL
; m_argsW
= NULL
; }
34 wxCmdLineArgsArray
& operator=(T
**argv
)
43 m_args
.push_back(*argv
++);
49 operator char**() const
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());
62 operator wchar_t**() const
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());
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
79 return !m_args
.empty();
82 // and the same for "if ( !argv )" checks
83 bool operator!() const
85 return m_args
.empty();
88 wxString
operator[](size_t n
) const
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
101 // convenience methods, i.e. not existing only for backwards compatibility
103 // do we have any arguments at all?
104 bool IsEmpty() const { return m_args
.empty(); }
106 // access the arguments as a convenient array of wxStrings
107 const wxArrayString
& GetArguments() const { return m_args
; }
109 ~wxCmdLineArgsArray()
115 template <typename T
>
121 const size_t count
= m_args
.size();
122 for ( size_t n
= 0; n
< count
; n
++ )
134 wxArrayString m_args
;
135 mutable char **m_argsA
;
136 mutable wchar_t **m_argsW
;
138 wxDECLARE_NO_COPY_CLASS(wxCmdLineArgsArray
);
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
)
145 return cond
&& !array
.IsEmpty();
148 #endif // wxUSE_UNICODE
150 #endif // _WX_CMDARGS_H_