How to get rid of void pointers

I inherited a large application that was originally written in C (but at the same time a lot of C ++ was added to it). Due to historical reasons, the application contains many pointers to emptiness. Before starting to suffocate, let me explain why this was done.

The application contains many different data structures, but they are stored in "universal" containers. Currently, I would use STL template containers for it, or I would give all data structures a common base class so that the container can store pointers in the base class, but in [good?] Old C-days, the only solution was to draw a struct-pointer to void pointer.

In addition, there is a lot of code that works on these void pointers, and uses very strange C constructs to emulate polymorphism in C.

Now I am processing the application and trying to get rid of void pointers. Adding a common base class to all data structures is not so difficult (several days of work), but the problem is that the code is full of constructs, as shown below.

This is an example of how data is stored:

void storeData (int datatype, void *data); // function prototype ... Customer *myCustomer = ...; storeData (TYPE_CUSTOMER, myCustomer); 

This is an example of how data is retrieved again:

 Customer *myCustomer = (Customer *) fetchData (TYPE_CUSTOMER, key); 

I really want to replace all void pointers with some kind of smart pointer (with reference counting), but I can't find a trick to automate (or at least) help me get rid of all casts and void pointers.

Any tips on how to find, replace, or in any way interact with these conversions?

+6
c ++ c casting void-pointers
source share
3 answers

There seems to be no automated way / trick to convert or search for all uses of void pointers. I will have to use manual labor to find all void pointers in combination with PC-Lint, which will give errors whenever an incorrect conversion occurs.

Case is closed.

0
source share

I really want to replace all void pointers with some smart pointer (reference counting), but I cannot find a trick to automate (or at least) help me to get rid of all throws and void pointers.

Such automatic refactoring carries many risks.

Otherwise, sometimes I like to play tricks, performing such functions void * template functions. It:

 void storeData (int datatype, void *data); 

becomes:

 template <class T> void storeData (int datatype, T *data); 

First implement the template by simply wrapping the original (renamed) function and converting the types. This may allow you to see potential problems - already by simply compiling the code.

+7
source share

You probably don't need to get rid of ghosts to use generic pointers.

 storeData(TYPE_CUSTOMER, myCustomer1->get()); shared_ptr<Customer> myCustomer2(reinterpret_cast<Customer*>fetchData(TYPE_CUSTOMER, "???"); 

Of course, this assumes that you do not expect to split the same pointer to store / fetch calls. In other words, myCustomer1 and myCustomer2 do not use the same pointer.

0
source share

All Articles