开发者

Handling a variable number of arguments at runtime in a struct

I have to classes, an Executer with these methods:

  1. Executer()
  2. struct Execute(string s)
  3. Lookup(string name, int module, int num, ...)

and a Parser:

  1. Parser()
  2. struct Parse(stri开发者_开发技巧ng s)

The Exectuers Execute method calls the Parsers Parse method. The Parser then chucks the string into smaller bits (it explodes the string on the ;-sign) and returns a struct to the Execute method. This struct it uses to call the Lookup method. The struct that the Parse returns holds some standard information:

  1. An command name
  2. A senderId (a username, a mac address and a password)
  3. A variable number of arguments

And that is my problem. The Lookup method take variable arguments, but how do I handle the the hand over of these variable arguments by the struct? Im not an expert in C and C++. Should I mass the two classes togheter? So the Parser method could call the Execute method, sparing the struct away. Or maybe there is a way of parsing an unknown variable of arguments at runtime? By some sort of array?

EDIT I cant use the STL library from C++. I only use the C++ class and virtual feature. Im writing to an compiler where Im restricted to use almost all of the C libraries + the magic skills of C++ (virtual and class). SOory for not telling that right away.

EDIT 2 Im writing code to an embedded system and thereby using avr-gcc to compile my code. Thats why I cant use STL. The avr-gcc doesnt support this.


Use std::vector<> or a simular container that can hold an arbitrary number of entries.

struct {
  std::string commandName;
  sender_t senderId;
  std::vector<arg_t> arguments;
};

Edit: oh, you can't use std::vector. In that case: use an array and store the length:

struct {
  const char* commandName;
  sender_t senderId;
  int argumentCount;
  int maxArgumentCount; // you might not need this
  arg_t* arguments; // pointer to array of (at least) argumentCount elements.
};

Use malloc() or new() to create the array for the arguments.

I would suggest to wrap the argumentCount, maxArgumentCount and arguments in a separate class, which can handle the malloc/new and free/delete as well. This will make it easier to prevent memory leaks.

In the end, you'll have written your own vector_of_arg_t class, so maybe have a look at some basic vector implementation. There must be tutorials on that on the web.


You could declare your Lookup method as follows:

void Lookup(string name, int module, int num, std::vector<std::string> &args);

By storing the variable arguments in an args array, you can have as many as you want.


See

Q: How can I write a function which takes a variable number of arguments and passes them to some other function (which takes a variable number of arguments)?

A: In general, you cannot. Ideally, you should provide a version of that other function which accepts a va_list pointer.

Suppose you want to write a faterror function which will print a fatal error message, then exit. You might like to write it in terms of the error function of question 15.5:

    void faterror(const char *fmt, ...)
    {
        error(fmt, what goes here? );
        exit(EXIT_FAILURE);
    }

but it's not obvious how to hand faterror's arguments off to error.

<snip>

Read on at

http://c-faq.com/varargs/handoff.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜