Astra SDK  v2.1.3
Frame.hpp
1 // This file is part of the Orbbec Astra SDK [https://orbbec3d.com]
2 // Copyright (c) 2015-2017 Orbbec 3D
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // Be excellent to each other.
17 #ifndef ASTRA_FRAME_HPP
18 #define ASTRA_FRAME_HPP
19 
20 #include <memory>
21 #include "capi/astra_core.h"
22 
23 namespace astra {
24 
31  class Frame
32  {
33  public:
34  Frame(astra_reader_frame_t readerFrame, const bool autoCloseFrame)
35  : frameRef_(std::make_shared<FrameRef>(readerFrame, autoCloseFrame))
36  { }
37 
38  Frame(astra_reader_frame_t readerFrame)
39  : Frame(readerFrame, true)
40  { }
41 
42  template<typename T>
43  T get()
44  {
45  return get<T>(DEFAULT_SUBTYPE);
46  }
47 
48  template<typename T>
49  T get(astra_stream_subtype_t subtype)
50  {
51  return T::template acquire<T>(frameRef_->get_frame(), subtype);
52  }
53 
54  bool is_valid()
55  {
56  return frameRef_->get_frame() != nullptr;
57  }
58 
59  operator bool()
60  {
61  return is_valid();
62  }
63 
64  private:
65  class FrameRef
66  {
67  public:
68  FrameRef(astra_reader_frame_t readerFrame, const bool autoCloseFrame)
69  : frame_(readerFrame),
70  autoCloseFrame_(autoCloseFrame)
71  { }
72 
73  ~FrameRef()
74  {
75  if (frame_ != nullptr && autoCloseFrame_)
76  {
77  astra_reader_close_frame(&frame_);
78  }
79  }
80 
81  astra_reader_frame_t get_frame() const { return frame_; }
82 
83  private:
84  astra_reader_frame_t frame_;
85  const bool autoCloseFrame_;
86  };
87 
88  std::shared_ptr<FrameRef> frameRef_;
89  };
90 }
91 
92 #endif // ASTRA_FRAME_HPP
Frame class
Definition: Frame.hpp:32