]> git.saurik.com Git - wxWidgets.git/blame - include/wx/cmdargs.h
Don't define __STRICT_ANSI__, we should build both with and without it.
[wxWidgets.git] / include / wx / cmdargs.h
CommitLineData
541ea80f
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: wx/cmdargs.h
3// Purpose: declaration of wxCmdLineArgsArray helper class
4// Author: Vadim Zeitlin
5// Created: 2007-11-12
541ea80f
VZ
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[]
d154ab3d 28class WXDLLIMPEXP_BASE wxCmdLineArgsArray
541ea80f
VZ
29{
30public:
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
d154ab3d
VZ
82 // and the same for "if ( !argv )" checks
83 bool operator!() const
84 {
85 return m_args.empty();
86 }
87
541ea80f
VZ
88 wxString operator[](size_t n) const
89 {
90 return m_args[n];
91 }
92
9b894c06
VZ
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
d154ab3d
VZ
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
541ea80f
VZ
107 const wxArrayString& GetArguments() const { return m_args; }
108
109 ~wxCmdLineArgsArray()
110 {
111 FreeArgs();
112 }
113
114private:
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
c0c133e1 138 wxDECLARE_NO_COPY_CLASS(wxCmdLineArgsArray);
541ea80f
VZ
139};
140
d154ab3d
VZ
141// provide global operator overload for compatibility with the existing code
142// doing things like "if ( condition && argv )"
a790886d 143inline bool operator&&(bool cond, const wxCmdLineArgsArray& array)
d154ab3d
VZ
144{
145 return cond && !array.IsEmpty();
146}
147
541ea80f
VZ
148#endif // wxUSE_UNICODE
149
150#endif // _WX_CMDARGS_H_
151