]> git.sesse.net Git - nageru/blob - ref_counted_frame.h
Do not link kaeru against CEF.
[nageru] / ref_counted_frame.h
1 #ifndef _REF_COUNTED_FRAME_H
2 #define _REF_COUNTED_FRAME_H 1
3
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.
6 //
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.
9
10 #include <memory>
11
12 #include "bmusb/bmusb.h"
13
14 void release_refcounted_frame(bmusb::FrameAllocator::Frame *frame);
15
16 typedef std::shared_ptr<bmusb::FrameAllocator::Frame> RefCountedFrameBase;
17
18 class RefCountedFrame : public RefCountedFrameBase {
19 public:
20         RefCountedFrame() {}
21
22         RefCountedFrame(const bmusb::FrameAllocator::Frame &frame)
23                 : RefCountedFrameBase(new bmusb::FrameAllocator::Frame(frame), release_refcounted_frame) {}
24 };
25
26 // Similar to RefCountedFrame, but as unique_ptr instead of shared_ptr.
27
28 struct Unique_frame_deleter {
29         void operator() (bmusb::FrameAllocator::Frame *frame) const {
30                 release_refcounted_frame(frame);
31         }
32 };
33
34 typedef std::unique_ptr<bmusb::FrameAllocator::Frame, Unique_frame_deleter>
35         UniqueFrameBase;
36
37 class UniqueFrame : public UniqueFrameBase {
38 public:
39         UniqueFrame() {}
40
41         UniqueFrame(const bmusb::FrameAllocator::Frame &frame)
42                 : UniqueFrameBase(new bmusb::FrameAllocator::Frame(frame)) {}
43
44         bmusb::FrameAllocator::Frame get_and_release()
45         {
46                 bmusb::FrameAllocator::Frame *ptr = release();
47                 bmusb::FrameAllocator::Frame frame = *ptr;
48                 delete ptr;
49                 return frame;
50         }
51 };
52
53 #endif  // !defined(_REF_COUNTED_FRAME_H)