WPILibC++  unspecified
UsbCameraBuffer.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef CSCORE_USBCAMERABUFFER_H_
9 #define CSCORE_USBCAMERABUFFER_H_
10 
11 #ifdef __linux__
12 #include <sys/mman.h>
13 #endif
14 
15 #include <utility>
16 
17 namespace cs {
18 
20  public:
21  UsbCameraBuffer() noexcept : m_data{nullptr}, m_length{0} {}
22  UsbCameraBuffer(UsbCameraBuffer&& other) noexcept : UsbCameraBuffer() {
23  swap(*this, other);
24  }
25  UsbCameraBuffer& operator=(UsbCameraBuffer&& other) noexcept {
26  swap(*this, other);
27  return *this;
28  }
29  UsbCameraBuffer(const UsbCameraBuffer&) = delete;
30  UsbCameraBuffer& operator=(const UsbCameraBuffer&) = delete;
31 
32 #ifdef __linux__
33  UsbCameraBuffer(int fd, size_t length, off_t offset) noexcept
34  : m_length{length} {
35  m_data =
36  mmap(nullptr, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
37  if (m_data == MAP_FAILED) {
38  m_data = nullptr;
39  m_length = 0;
40  }
41  }
42 
43  ~UsbCameraBuffer() {
44  if (m_data) munmap(m_data, m_length);
45  }
46 #endif
47 
48  friend void swap(UsbCameraBuffer& first, UsbCameraBuffer& second) noexcept {
49  using std::swap;
50  swap(first.m_data, second.m_data);
51  swap(first.m_length, second.m_length);
52  }
53 
54  void* m_data;
55  size_t m_length;
56 };
57 
58 } // namespace cs
59 
60 #endif // CSCORE_USBCAMERABUFFER_H_
Definition: SinkImpl.h:19
Definition: UsbCameraBuffer.h:19