通用未来/承诺的容器

发布于 2024-09-29 17:11:37 字数 994 浏览 0 评论 0原文

我们正在尝试生成一个将执行异步调用的库组件。 由于各种原因,这需要有一个类似 C 的接口,因此我们提供了大量静态函数作为库的接口。

我们还需要用户能够控制内存。所以大多数函数看起来像

int myLibFunction(void* data, size_t data_size);

我试图用更智能的 Future 对象替换它,这样我们就不会使用 void 指针,这样访问就可以了。线程之间的数据是同步的。理想情况下,调用看起来像这样:

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);
}

这是一个相当干净的接口,并且该接口在编译时强制类型安全。然而,我遇到的问题是我正在创建一个要在内部执行的作业队列。然而,由于 Future 的大小不同,我无法创建它们的 std::queue,我希望能够创建一个 std::queue,其中 Job 包含 Future* 但这是无效的。

我还尝试让 Job 包含 Future,其中所有数据类都从 ParentData 派生,但也无济于事。

这个问题与拥有智能指针容器的问题非常相似。由于我工作的团队的性质,我将无法在库之外公开任何增强对象,如果我使 Future 具有多态性,我将被板球棒追赶。

库的用户端代码能够控制数据的实际位置,这一点很重要。

问候, 伊恩

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤带着心事 2024-10-06 17:11:37

您要么使用 C++ 来实现多态性,要么使用 void* 重新实现多态性。解决问题的最简单方法是为 Future 提供一个不依赖于 T 的基类。通常我会这样做:

class Future {};

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

然后,您可以创建 Future 的容器* 并具有一定的类型安全性。

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.

池木 2024-10-06 17:11:37

目前还不完全清楚你想做什么。不过,如果我理解正确的话,您有一堆遗留函数,并且您试图将每个函数包装在(模板)类中,并为其提供特定于类型的数据。像下面这样的东西可以做你想要的吗?

#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;
}

这可以使用智能指针更干净地完成,但这个想法应该成立。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文