I have read this code snippet somewhere but I am not able to understand its meaning.
/** Use strong typing for ODP types */
#define odp_handle_t struct {} *
What is significance of above code snippet?
I have read this code snippet somewhere but I am not able to understand its meaning.
/** Use strong typing for ODP types */
#define odp_handle_t struct {} *
What is significance of above code snippet?
This code snippet defines the symbol odp_handle_t which represents an opaque handle to a resource. It's opaque because it's a pointer to an empty struct. The thing that it's pointing to is not exposed to the user. It's called a handle because it does not point directly to the resource, it just identifies it. The internal implementation knows how to use this handle to access the required resource. This helps maintain independence between the client code and the implementation of the API. Finally, the strong typing part comes from the fact that it's a pointer to a type (the empty struct) as opposed to a void pointer.
That is just some nonsense code not really related to strong typing. It appears to be some non-standard way of declaring a pointer to incomplete type, but it is not valid C.
#define odp_handle_t struct {} *
odp_handle_t x; // will not compile, struct has no members
I believe this is yet another pointless gcc extension. Also, hiding away pointers behind typedefs is always a very bad idea.
There is no reason why you can't declare your pointer to incomplete/opaque type with pure standard C, and you can do so without hiding pointers behind typedefs:
h file
typedef struct odp_handle_t odp_handle_t;
c file
struct odp_handle_t
{
// have to put struct members in here
};
caller c file
odp_handle_t *pointer_to_incomplete_type;