Boost循环缓冲区,如何让它在填满时调用一些函数?
我喜欢 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 中监听此类事件并计算所有缓冲区内容?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
circular_buffer
实例包装到它自己的类中,并在其之上实现事件处理。如果需要向
MyCircularBuffer
之外的代码通知该事件,您可以实现 观察者模式,使用Boost.Function 或使用其他一些回调机制。You can wrap the
circular_buffer
instance into its own class and implement event handling on top of it.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.它不会触发事件。每次添加时都必须手动检查它是否已满,如果已满,则执行一些过程。
这通常是通过将容器包装在用户创建的对象中来完成的,以便您可以监视缓冲区的添加内容。
或者,换句话说,您不需要通用的解决方案;而是需要通用的解决方案。只需编写一个使用容器并自动刷新它的对象即可。
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.