views:

67

answers:

3

Here's what I've got:

 switch(argv[0])
 {
  case "-test1":
   AfxBeginThread(method1, 0); break;
  case "-test2":
   AfxBeginThread(method2, 0); break;
  case "-test3":
   AfxBeginThread(method3, 0); break;
  default:
   AfxBeginThread(method1, 0); break;
 }

I'm using windows so the arguments come into the array as TCHAR*'s. What do I need to do to make this work?

Edit:

So I'm trying to do the following now...

if(strcmp(argv[0], "-http") == 0)
    doStuff();

I'm getting the following compile error

error C2664: 'strcmp' : cannot convert parameter 1 from 'TCHAR *' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast.

Help?

+4  A: 

That won't work. In C/C++, switch only works with integer types.

In other languages (VB, C#), you can use any data type in a switch statement, but in C & C++, you can only use integer types (int, long, char etc).

The reason for this is that when using ints, there is an optimization available over using chained if(). switch was created to take advantage of that optimization. Later on, people realized, it made the code neater, and added it to other languages --- but just as a shorthand for chained if()s -- without taking advantage of the optimization.

James Curran
+2  A: 

What do I need to do to make this work?

Something like this:

// Beware, un-compiled code ahead!
namespace {
  typedef std::map<std::basic_string<TCHAR>,AFX_THREADPROC> thread_func_map_type;
  typedef thread_func_map_type::value_type thread_func_entry_type;

  const thread_func_entry_type thread_func_entries[] =
  { thread_func_entry_type(_T("-test1"), method1)
  , thread_func_entry_type(_T("-test2"), method2)
  , thread_func_entry_type(_T("-test3"), method3) };

  const thread_func_map_type thread_func_map( thread_func_entries
                                            , thread_func_entries 
                                            + thread_func_entries
                                            / thread_func_entries[0] );
}

// ...

thread_func_map_type::const_iterator it = thread_func_map.find(argv[1]);
if( it == thread_func_map.end() )
  it = thread_func_map.find("-test1");
assert(it!=thread_func_map.end());
AfxBeginThread(it->second, 0);
sbi
heh - I was just coding up a C variant of this (too much embedded work...)
Michael Burr
This doesn't address the TCHAR issue.
Adrian McCarthy
@Adrian: You're right, I forgot about that! It's now fixed.
sbi
+3  A: 

You can't switch over values that are not constant integral values. But since argument matching isn't time critical, you can put in a couple of if's and strcmp's instead.

This code runs apparently under Windows, you're most likely forced to use TCHAR, which means that you need _tcscmp() instead of strcmp, and the good old _T() macro:

 if      (_tcscmp(_T("-test1"),argv[0])==0) AfxBeginThread(method1, 0);
 else if (_tcscmp(_T("-test2"),argv[0])==0) AfxBeginThread(method2, 0);
 else if (_tcscmp(_T("-test3"),argv[0])==0) AfxBeginThread(method3, 0);
 else AfxBeginThread(method1, 0);

The TCHAR, _tcscmp and _T() thing is explained here:

Short story is, that you can build your program for various character representations (16-bit UCS-2, 8 bit multibyte etc.) from a single source by using these macros, which expand to the right function (_tcscmp->strcmp or wcscmp or mbccmp) depending of the character representation you are building for.

Luther Blissett
This worked, but I had to make it argv[1] instead of argv[0]. I guess the first is reserved. Thanks!Also, I noticed that my app is using wchar's rather than just normal chars. I'm running XP. Shouldn't it be using ASCII by default not UNICODE?
themaestro