开发者

Containers of generic Future/Promises

We are trying 开发者_如何学Goto 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 give Future<T> a base class that doesn't depend on T. Usually I do it like this:

class Future {};

template<class T>
class FutureOf : public Future {};

Then, you can create containers of Future* and have some type-safety.


It's not entirely clear what you're trying to do. If I understand you correctly, though, you've got a bunch of legacy functions, and you're trying to wrap each one in a (template) class, and provide it with type-specific data. Could something like the following do what you want?

#include <iostream>
#include <list>

class AbstractFuture
{
public:
    virtual int compute() const = 0;
};

// Encapsulates function and data
template <typename F, typename T>
class Future : public AbstractFuture
{
public:
    Future(F func, T x) : func_(func), x_(x) {}
    virtual int compute() const { return func_(x_); };
private:
    const F func_;
    const T x_;
};

// Helper function template, to save all the explicit <> nonsense
// you'd need otherwise
template <typename F, typename T>
AbstractFuture *createFuture(F func, T x) { return new Future<F,T>(func, x); }

// Some data types
typedef float Func1Data;
typedef double Func2Data;

// Dummy function implementations
static int myLibDoJob1(const Func1Data& data)     { return 5; }
static int myLibDoJob2(const Func2Data& data)     { return 7; }


int main()
{
    // Data for each Future
    Func1Data x1 = 0;
    Func2Data x2 = 0;

    // Create some Futures (note, the function template makes this type-safe)
    AbstractFuture *p1 = createFuture(myLibDoJob1, x1);
    AbstractFuture *p2 = createFuture(myLibDoJob2, x2);

    // Put into a container (of pointers to base class)
    std::list<AbstractFuture *> futures;
    futures.push_back(p1);
    futures.push_back(p2);

    // Demonstrate polymorphism works
    for (std::list<AbstractFuture *>::const_iterator it = futures.begin();
         it != futures.end(); ++it)
    {
        std::cout << (*it)->compute() << std::endl;
    }

    delete p1;
    delete p2;

    return 0;
}

This could be done more cleanly with smart pointers, but the idea should hold.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜