Boost循环缓冲区,如何让它在填满时调用一些函数?

发布于 2024-11-30 03:48:11 字数 752 浏览 4 评论 0 原文

我喜欢 Boost 模板化圆形缓冲区容器,但是100%充满时如何获取?

   #include <boost/circular_buffer.hpp>

   int main() {

      // Create a circular buffer with a capacity for 3 integers.
      boost::circular_buffer<int> cb(3);

      // Insert some elements into the buffer.
      cb.push_back(1);
      cb.push_back(2);
      cb.push_back(3);

      // Here I want some function to be called (for example one that would cout all buffer contents)

      int a = cb[0];  // a == 1
      int b = cb[1];  // b == 2
      int c = cb[2];  // c == 3

      return 0;
   }

那么如何在 boost::circular_buffer 中监听此类事件并计算所有缓冲区内容?

I like Boost Templated Circular Buffer Container, but how to get when it was filled 100%?

   #include <boost/circular_buffer.hpp>

   int main() {

      // Create a circular buffer with a capacity for 3 integers.
      boost::circular_buffer<int> cb(3);

      // Insert some elements into the buffer.
      cb.push_back(1);
      cb.push_back(2);
      cb.push_back(3);

      // Here I want some function to be called (for example one that would cout all buffer contents)

      int a = cb[0];  // a == 1
      int b = cb[1];  // b == 2
      int c = cb[2];  // c == 3

      return 0;
   }

So how to listen for such event in boost::circular_buffer and for example cout all buffer contents?

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

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

发布评论

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

评论(2

仅此而已 2024-12-07 03:48:11

您可以将circular_buffer实例包装到它自己的类中,并在其之上实现事件处理。

class MyCircularBuffer
{
public:
    MyCircularBuffer() : buffer(3) {}

    void PushBack(int i)
    {
        buffer.push_back(i);
        if(buffer.size() == 3)
        {
            // buffer is full!
        }
    }
private:
    boost::circular_buffer<int> buffer;
};

如果需要向 MyCircularBuffer 之外的代码通知该事件,您可以实现 观察者模式,使用Boost.Function 或使用其他一些回调机制。

You can wrap the circular_buffer instance into its own class and implement event handling on top of it.

class MyCircularBuffer
{
public:
    MyCircularBuffer() : buffer(3) {}

    void PushBack(int i)
    {
        buffer.push_back(i);
        if(buffer.size() == 3)
        {
            // buffer is full!
        }
    }
private:
    boost::circular_buffer<int> buffer;
};

If code outside of MyCircularBuffer needs to be notified of the event, you can implement some variant of the Observer pattern, use Boost.Function or use some other callback mechanism.

小女人ら 2024-12-07 03:48:11

它不会触发事件。每次添加时都必须手动检查它是否已满,如果已满,则执行一些过程。

这通常是通过将容器包装在用户创建的对象中来完成的,以便您可以监视缓冲区的添加内容。

或者,换句话说,您不需要通用的解决方案;而是需要通用的解决方案。只需编写一个使用容器并自动刷新它的对象即可。

It doesn't fire an event. You must manually check to see if it is full each time you add to it, and if it is, then you execute some process.

This would typically be done by wrapping the container in a user-created object, so that you can monitor additions to the buffer.

Or, to put it another way, you don't need a generic solution; just write an object that uses the container and flushes it automatically.

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