I have a different objects, like this:
typedef struct { int32_t info ; } item1_properties_t ;
and
typedef struct { int32_t info ; } item2_properties_t ;
the objects are held in containers. I want to introduce a function that returns these objects (i.e. a const pointer) with minimum casts (working in C). I thought of having the function return the type and a void pointer, expecting the caller to cast correctly but I don't really like it. Do you have better ideas?
I don't see a direct connection with PC software here - it's just a requirement that can be more easily satisfied using another programming model (in this case, object oriented programming).
object oriented (OO) design pattern is generally used in pc software development.
Is widely used in many fields of application!
What the OP can do is store the data of the containers as an array of bytes alongside the type of the data. Then, when pointer/copying that data to a coitainer, a cast can be done based on the stored type.
You mean something like this
enum { char_type, int_type, long_type } ; switch( type-id ) { case char_type: char_result = (char)input; break; case int_type: int_result = (int)input; break; case long_type: long_result = (long)input; break; }
Yes. Exactly.
I already said something similar, but the importany thing here is that such a byte-array must contain an exact image of the structure stored in it. If so, getting data out of the generic container is as simple as copying it/pointing to it, and performing one cast.
Isn't this meant to be Keil/ARM forum? It seems to be turning into a 'C' programmers forum. There are plenty of these on the Web so why not go to one of these and keep this forum for what it's intended.
I don't think it is a good idea to restrict question to only address the tool chain and/or uv4. That will rule out many important questions concerning USB, hardware, correct programming techniques etc. True - this is a Keil forum, but this is a classical case where the sum of the parts supersedes/engulfs the whole!