tsp-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tsp-devel] Use of opaque types


From: Robert de Vries
Subject: [Tsp-devel] Use of opaque types
Date: Sat, 31 Mar 2007 14:03:01 +0200

I see that the developers have used opaque types to hide the implementation of the structures from the end users.
A typical example is this:

/** Opaque handle type for provider */
typedef  void* TSP_provider_t;

This approach however has to disadvantages:
1. The type checking of the compiler is disabled, you can basically put any pointer as an argument to a function taking a TSP_provider_t as an argument
2. Debugging is made more complicated than necessary, when you try to do print *provider in gdb, it says "$20 = (TSP_provider_t) 0x8d52990" instead of

$21 = {server = 0x8d50128, server_info = {info = "rpc://localhost/StubbedServer:1", '\0' <repeats 225 times>},
  channel_id = 3, information = {version_id = 0, channel_id = 0, provider_timeout = 0, provider_group_number = 0,
    symbols = {TSP_sample_symbol_info_list_t_len = 1013, TSP_sample_symbol_info_list_t_val = 0x8d6e6b8},
    base_frequency = 100, max_period = 100000, max_client_number = 100, current_client_number = 4,
    status = TSP_STATUS_OK}, extended_informations = {TSP_sample_symbol_extended_info_list_t_len = 0,
    TSP_sample_symbol_extended_info_list_t_val = 0x0}, requested_sym = {TSP_sample_symbol_info_list_t_len = 1,
    TSP_sample_symbol_info_list_t_val = 0x8d7c500}, groups = 0x8d7c580, receiver = 0x0, sample_fifo = 0x0,
  thread_receiver = 0, data_link_broken = 0}

which is more useful.

Now you may ask, how do I do this?
As follows:
In the public header you make a forward declaration:

typedef struct TSP_otsp_t TSP_provider_t;

In an internal header or in the source code you provide the real struct TSP_otsp_t.
You may note that I did not put a * in the type definition above. This is on purpose.
If you want to do a prototype as follows:
int tsp_func(const TSP_provider_t *prov, ...);
You tell the compiler that tsp_func is not going to touch prov. However if you made a typedef that includes the *, the only thing you can say is that the pointer is const and that we already know.

The problem I currently see as that with the current scheme of opaque types you not only hide the type, but also the type information, and that is not so good.

     Robert



reply via email to

[Prev in Thread] Current Thread [Next in Thread]