cmdargs.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #ifndef _WX_CMDARGS_H_
  10. #define _WX_CMDARGS_H_
  11. #include "wx/arrstr.h"
  12. // ----------------------------------------------------------------------------
  13. // wxCmdLineArgsArray: helper class used by wxApp::argv
  14. // ----------------------------------------------------------------------------
  15. #if wxUSE_UNICODE
  16. // this class is used instead of either "char **" or "wchar_t **" (neither of
  17. // which would be backwards compatible with all the existing code) for argv
  18. // field of wxApp
  19. //
  20. // as it's used for compatibility, it tries to look as much as traditional
  21. // (char **) argv as possible, in particular it provides implicit conversions
  22. // to "char **" and also array-like operator[]
  23. class WXDLLIMPEXP_BASE wxCmdLineArgsArray
  24. {
  25. public:
  26. wxCmdLineArgsArray() { m_argsA = NULL; m_argsW = NULL; }
  27. template <typename T>
  28. wxCmdLineArgsArray& operator=(T **argv)
  29. {
  30. FreeArgs();
  31. m_args.clear();
  32. if ( argv )
  33. {
  34. while ( *argv )
  35. m_args.push_back(*argv++);
  36. }
  37. return *this;
  38. }
  39. operator char**() const
  40. {
  41. if ( !m_argsA )
  42. {
  43. const size_t count = m_args.size();
  44. m_argsA = new char *[count];
  45. for ( size_t n = 0; n < count; n++ )
  46. m_argsA[n] = wxStrdup(m_args[n].ToAscii());
  47. }
  48. return m_argsA;
  49. }
  50. operator wchar_t**() const
  51. {
  52. if ( !m_argsW )
  53. {
  54. const size_t count = m_args.size();
  55. m_argsW = new wchar_t *[count];
  56. for ( size_t n = 0; n < count; n++ )
  57. m_argsW[n] = wxStrdup(m_args[n].wc_str());
  58. }
  59. return m_argsW;
  60. }
  61. // existing code does checks like "if ( argv )" and we want it to continue
  62. // to compile, so provide this conversion even if it is pretty dangerous
  63. operator bool() const
  64. {
  65. return !m_args.empty();
  66. }
  67. // and the same for "if ( !argv )" checks
  68. bool operator!() const
  69. {
  70. return m_args.empty();
  71. }
  72. wxString operator[](size_t n) const
  73. {
  74. return m_args[n];
  75. }
  76. // we must provide this overload for g++ 3.4 which can't choose between
  77. // our operator[](size_t) and ::operator[](char**, int) otherwise
  78. wxString operator[](int n) const
  79. {
  80. return m_args[n];
  81. }
  82. // convenience methods, i.e. not existing only for backwards compatibility
  83. // do we have any arguments at all?
  84. bool IsEmpty() const { return m_args.empty(); }
  85. // access the arguments as a convenient array of wxStrings
  86. const wxArrayString& GetArguments() const { return m_args; }
  87. ~wxCmdLineArgsArray()
  88. {
  89. FreeArgs();
  90. }
  91. private:
  92. template <typename T>
  93. void Free(T **args)
  94. {
  95. if ( !args )
  96. return;
  97. const size_t count = m_args.size();
  98. for ( size_t n = 0; n < count; n++ )
  99. free(args[n]);
  100. delete [] args;
  101. }
  102. void FreeArgs()
  103. {
  104. Free(m_argsA);
  105. Free(m_argsW);
  106. }
  107. wxArrayString m_args;
  108. mutable char **m_argsA;
  109. mutable wchar_t **m_argsW;
  110. wxDECLARE_NO_COPY_CLASS(wxCmdLineArgsArray);
  111. };
  112. // provide global operator overload for compatibility with the existing code
  113. // doing things like "if ( condition && argv )"
  114. inline bool operator&&(bool cond, const wxCmdLineArgsArray& array)
  115. {
  116. return cond && !array.IsEmpty();
  117. }
  118. #endif // wxUSE_UNICODE
  119. #endif // _WX_CMDARGS_H_