We are trying to produce a library component which will perform asynchronous calls.
For various reasons this needs to have a C like interface, so we are providing a large set of static functions as the interface to the library.
We also need the user to have control over the memory. So most functions look something like
int myLibFunction(void* data, size_t data_size);
I’m trying to replace this with a smarter Future object so that we don’t use void pointers and so that the access to the data is synchronised between threads. Ideally the calls would look something like:
Future<T> {
T m_data;
}
static int myLibDoJob1(Future<Func1Data>& data);
static int myLibDoJob2(Future<Func2Data>& data);
main()
{
Func1Data m_data;
Func2Data m_data2;
Future<Func1Data> future1(m_data);
Future<Func2Data> future2(m_data2);
int ret=0;
ret = myLibDoJob1(future1);
ret = myLibDoJob2(future2);
}
This is a fairly clean interface and the interface forces type safety at compile time. However the problem I have is that I’m creating a queue of jobs to perform internally. However due to the Futures being different sizes I can’t create a std:queue of them, I had hoped to be able to create a std::queue, with Job containing a Future* however this isn’t valid.
I’ve also tried having Job contain Future where all the Data classes derive from ParentData also to no avail.
The problem is very similar to the one of having containers of smart pointers. Due to the nature of the team I work in I won’t be able to expose any boost objects outside the library and I will be chased around with a cricket bat if I make Future polymorphic.
Its important that the user side code to the library has control over where the data actually is.
Regards,
Iain
Either you will use C++ to do polymorphism or you will reimplement polymorphism with
void*. The easiest way to solve your problem is to giveFuture<T>a base class that doesn’t depend on T. Usually I do it like this:Then, you can create containers of
Future*and have some type-safety.