]> git.saurik.com Git - wxWidgets.git/blob - include/wx/cmdargs.h
compile fix
[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 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 wxString operator[](size_t n) const
84 {
85 return m_args[n];
86 }
87
88 // we must provide this overload for g++ 3.4 which can't choose between
89 // our operator[](size_t) and ::operator[](char**, int) otherwise
90 wxString operator[](int n) const
91 {
92 return m_args[n];
93 }
94
95 // this is the only method of this class which doesn't exist solely for
96 // compatibility purposes: it allows to access the arguments as a
97 // convenient array of wxStrings
98 const wxArrayString& GetArguments() const { return m_args; }
99
100 ~wxCmdLineArgsArray()
101 {
102 FreeArgs();
103 }
104
105 private:
106 template <typename T>
107 void Free(T **args)
108 {
109 if ( !args )
110 return;
111
112 const size_t count = m_args.size();
113 for ( size_t n = 0; n < count; n++ )
114 free(args[n]);
115
116 delete [] args;
117 }
118
119 void FreeArgs()
120 {
121 Free(m_argsA);
122 Free(m_argsW);
123 }
124
125 wxArrayString m_args;
126 mutable char **m_argsA;
127 mutable wchar_t **m_argsW;
128
129 DECLARE_NO_COPY_CLASS(wxCmdLineArgsArray)
130 };
131
132 #endif // wxUSE_UNICODE
133
134 #endif // _WX_CMDARGS_H_
135