1 #ifndef _REF_COUNTED_FRAME_H
2 #define _REF_COUNTED_FRAME_H 1
4 // A wrapper around FrameAllocator::Frame that is automatically refcounted;
5 // when the refcount goes to zero, the frame is given back to the allocator.
7 // Note that the important point isn't really the pointer to the Frame itself,
8 // it's the resources it's representing that need to go back to the allocator.
12 #include "bmusb/bmusb.h"
14 void release_refcounted_frame(bmusb::FrameAllocator::Frame *frame);
16 typedef std::shared_ptr<bmusb::FrameAllocator::Frame> RefCountedFrameBase;
18 class RefCountedFrame : public RefCountedFrameBase {
22 RefCountedFrame(const bmusb::FrameAllocator::Frame &frame)
23 : RefCountedFrameBase(new bmusb::FrameAllocator::Frame(frame), release_refcounted_frame) {}
26 // Similar to RefCountedFrame, but as unique_ptr instead of shared_ptr.
28 struct Unique_frame_deleter {
29 void operator() (bmusb::FrameAllocator::Frame *frame) const {
30 release_refcounted_frame(frame);
34 typedef std::unique_ptr<bmusb::FrameAllocator::Frame, Unique_frame_deleter>
37 class UniqueFrame : public UniqueFrameBase {
41 UniqueFrame(const bmusb::FrameAllocator::Frame &frame)
42 : UniqueFrameBase(new bmusb::FrameAllocator::Frame(frame)) {}
44 bmusb::FrameAllocator::Frame get_and_release()
46 bmusb::FrameAllocator::Frame *ptr = release();
47 bmusb::FrameAllocator::Frame frame = *ptr;
53 #endif // !defined(_REF_COUNTED_FRAME_H)