Well, I've got this really HUGE database to configure on a DSP. The idea struck me that I may want to keep the test app that I make for use in the field (so someone doesn't have to write a program to do it, just make a config file). Well, there are ~100 configurable items in about a dozen structures. So, my idea was to make a VERY generic processor that would, after the "filter" is defined, take the data from the command line / configuration file / stdin and process it into the various structure elements. The way that I've come up with is to enumerate my data types

Hmm I think the closest thing in C to what you want is a union but that doesn't seem to ease things for you. However, you would still have to add all the possible types as members to that union.

1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

That still doesn't get around the having to have the enumeration, however. So, I'm still going about it the best way possible (I think).

It is VERY LONG coding, though. It turned out to be 11 arrays of structures, 6 or so elements in each struct and about 8 elements in each array. Though, some of it could not be avoided (like the usage string for each -- that just has to be typed out). It took a long time to do all that -- like 11 hours. The rest of it was pretty easy: Two different loops to process the input stream (one for the command line interface and the other for the the input stream (file, stdin -- what's the difference?).

Still, though, I think it is FORTRAN or maybe lisp that I've used before that enable me to pass the data type as a parameter (maybe prolog???) so that one could truly create a generic assign from a pointer.

hmm... if you have a C++ cross compiler at hand, couldn't you use a class template and function templates?

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

Given a packed C structure (I HAVE TO USE -- This is the interface to the driver), how do I make a C++ class that will wrapper around the structure so that I can complete the structure from the command line arguments.

The user space parameters would be named what you'd expect (for this case) "CHANNEL", "TYPE", "CHANGES", and "RATE".

My approach is to have a structure that defines the user space content, and maps it directly to the element of the structure via a void *. Then, I have another var that gives me what type it really is so that I can deference the pointer correctly.

I'm all ears (eyes, actaully) laserlight, Elysia, and anyone else who can show me.

Now, the question is how you would get the type? C++ allows for very easy passing of the type, assuming you can get it statically (not dynamically).
But not knowing exactly how it's going to be done, or any other information, this is pretty much all I can say for now.

Originally Posted by Adak

io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.

Originally Posted by Salem

You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

Some time back I posted the C-ish source for a variant which sounds like what you want/need. It carried its own RTTI (run time type identification) member (an enumeration if I recall) and a void *ptr and a size. Then it could be a char, string, int, double, MyStruct, whatever. Variants without really good compiler support can be tough but sometimes they are the answer, particularly if you want something like the C-equivalent of a class-factory...

If you can do it in C++, then it's certainly doable in C. I would do it with callbacks and strings, you register a callback and the arguments it takes. Then make everything "stringisable", and call the callback with the usual "int argc, char * argv[]" flavour. Parsing the type is left to whoever supplies the callback.

If you can do it in C++, then it's certainly doable in C. I would do it with callbacks and strings, you register a callback and the arguments it takes. Then make everything "stringisable", and call the callback with the usual "int argc, char * argv[]" flavour. Parsing the type is left to whoever supplies the callback.

Sure, never meant to imply otherwise; just that using overloaded functions on a struct allowed for code like:

Code:

CVariant A, B, C;
A = 42;
B = "Hello";
C = MyClass;
// etc

Where it gets fun is making a variant that can also be a container like an array and make it know its own type (CVariant in this case). That way you could have containers of containers of .. etc. All with a single variable type. Made DB and socket IO pretty easy even if the class (this was in C++) was somewhat complex. Once you can stringify the data it becomes easy to write over a socket, etc. and reconstruct on the distant end.

Given a packed C structure (I HAVE TO USE -- This is the interface to the driver), how do I make a C++ class that will wrapper around the structure so that I can complete the structure from the command line arguments.

The user space parameters would be named what you'd expect (for this case) "CHANNEL", "TYPE", "CHANGES", and "RATE".

My approach is to have a structure that defines the user space content, and maps it directly to the element of the structure via a void *. Then, I have another var that gives me what type it really is so that I can deference the pointer correctly.

I'm all ears (eyes, actaully) laserlight, Elysia, and anyone else who can show me.

Basically, what you need are templates. But templates only provide compile-time functionality - you need to generate them at run-time, and for that, virtual functions are needed. Unfortunately, to be practical, dynamic allocation (managed by a 'smart-pointer', preferably) with deep-copying functionality is required. I'll provide a working example in just a moment, but first, let's start by defining a template function that can be used to convert one type to another, using C++ iostreams:

Okay, now for the more complicated bit. We have a class parameterized by the type that it converts *from* (the "source", typically an std::string), that stores a reference to some arbitrary type (the "destination").