rs_context.hpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 #ifndef LIBREALSENSE_RS2_CONTEXT_HPP
5 #define LIBREALSENSE_RS2_CONTEXT_HPP
6 
7 #include "rs_types.hpp"
8 #include "rs_record_playback.hpp"
9 #include "rs_processing.hpp"
10 
11 namespace rs2
12 {
14  {
15  public:
17  :_removed(removed), _added(added) {}
18 
23  bool was_removed(const rs2::device& dev) const
24  {
25  rs2_error* e = nullptr;
26 
27  if (!dev)
28  return false;
29 
30  auto res = rs2_device_list_contains(_removed.get_list(), dev.get().get(), &e);
31  error::handle(e);
32 
33  return res > 0;
34  }
35 
40  bool was_added(const rs2::device& dev) const
41  {
42  rs2_error* e = nullptr;
43 
44  if (!dev)
45  return false;
46 
47  auto res = rs2_device_list_contains(_added.get_list(), dev.get().get(), &e);
48  error::handle(e);
49 
50  return res > 0;
51  }
52 
58  {
59  return _added;
60  }
61 
62  private:
65  };
66 
67  template<class T>
69  {
71 
72  public:
73  explicit devices_changed_callback(T callback) : _callback(callback) {}
74 
75  void on_devices_changed(rs2_device_list* removed, rs2_device_list* added) override
76  {
77  std::shared_ptr<rs2_device_list> old(removed, rs2_delete_device_list);
78  std::shared_ptr<rs2_device_list> news(added, rs2_delete_device_list);
79 
80 
82  _callback(info);
83  }
84 
85  void release() override { delete this; }
86  };
87 
88  class pipeline;
89  class device_hub;
90  class software_device;
91 
96  class context
97  {
98  public:
100  {
101  rs2_error* e = nullptr;
102  _context = std::shared_ptr<rs2_context>(
105  error::handle(e);
106  }
107 
113  {
114  rs2_error* e = nullptr;
115  std::shared_ptr<rs2_device_list> list(
116  rs2_query_devices(_context.get(), &e),
118  error::handle(e);
119 
120  return device_list(list);
121  }
122 
128  {
129  rs2_error* e = nullptr;
130  std::shared_ptr<rs2_device_list> list(
133  error::handle(e);
134 
135  return device_list(list);
136  }
137 
142  std::vector<sensor> query_all_sensors() const
143  {
144  std::vector<sensor> results;
145  for (auto&& dev : query_devices())
146  {
147  auto sensors = dev.query_sensors();
148  std::copy(sensors.begin(), sensors.end(), std::back_inserter(results));
149  }
150  return results;
151  }
152 
153 
155  {
156  rs2_error* e = nullptr;
157  std::shared_ptr<rs2_device> dev(
160  error::handle(e);
161  return device{ dev };
162  }
163 
168  template<class T>
170  {
171  rs2_error* e = nullptr;
173  new devices_changed_callback<T>(std::move(callback)), &e);
174  error::handle(e);
175  }
176 
185  {
186  rs2_error* e = nullptr;
187  auto device = std::shared_ptr<rs2_device>(
188  rs2_context_add_device(_context.get(), file.c_str(), &e),
191 
192  return playback { device };
193  }
194 
195  void unload_device(const std::string& file)
196  {
197  rs2_error* e = nullptr;
198  rs2_context_remove_device(_context.get(), file.c_str(), &e);
200  }
201 
203  {
204  rs2_error* e = nullptr;
207  }
208 
209  context(std::shared_ptr<rs2_context> ctx)
210  : _context(ctx)
211  {}
212  explicit operator std::shared_ptr<rs2_context>() { return _context; };
213  protected:
214  friend class rs2::pipeline;
215  friend class rs2::device_hub;
216  friend class rs2::software_device;
217 
218  std::shared_ptr<rs2_context> _context;
219  };
220 
225  {
226  public:
228  {
229  rs2_error* e = nullptr;
230  _device_hub = std::shared_ptr<rs2_device_hub>(
231  rs2_create_device_hub(ctx._context.get(), &e),
233  error::handle(e);
234  }
235 
241  {
242  rs2_error* e = nullptr;
243  std::shared_ptr<rs2_device> dev(
244  rs2_device_hub_wait_for_device(_device_hub.get(), &e),
246 
247  error::handle(e);
248 
249  return device(dev);
250 
251  }
252 
256  bool is_connected(const device& dev) const
257  {
258  rs2_error* e = nullptr;
259  auto res = rs2_device_hub_is_device_connected(_device_hub.get(), dev._dev.get(), &e);
260  error::handle(e);
261 
262  return res > 0 ? true : false;
263 
264  }
265 
266  explicit operator std::shared_ptr<rs2_device_hub>() { return _device_hub; }
267  explicit device_hub(std::shared_ptr<rs2_device_hub> hub) : _device_hub(std::move(hub)) {}
268  private:
269  std::shared_ptr<rs2_device_hub> _device_hub;
270  };
271 
272 }
273 #endif // LIBREALSENSE_RS2_CONTEXT_HPP
void on_devices_changed(rs2_device_list *removed, rs2_device_list *added) override
Definition: rs_context.hpp:75
int rs2_device_list_contains(const rs2_device_list *info_list, const rs2_device *device, rs2_error **error)
Definition: rs.cpp:890
#define RS2_API_VERSION
Definition: rs.h:41
bool is_connected(const device &dev) const
Definition: rs_context.hpp:256
std::shared_ptr< rs2_device_hub > _device_hub
Definition: rs_context.hpp:269
std::vector< sensor > query_all_sensors() const
Generate a flat list of all available sensors from all RealSense devices.
Definition: rs_context.hpp:142
rs2_device_list * rs2_query_devices(const rs2_context *context, rs2_error **error)
Definition: rs.cpp:208
const std::shared_ptr< rs2_device > & get() const
Definition: rs_device.hpp:116
void unload_device(const std::string &file)
Definition: rs_context.hpp:195
GLdouble s
rs2_device_hub * rs2_create_device_hub(const rs2_context *context, rs2_error **error)
Creates RealSense device_hub .
Definition: rs.cpp:178
device get_sensor_parent(const sensor &s) const
Definition: rs_context.hpp:154
bool was_removed(const rs2::device &dev) const
Definition: rs_context.hpp:23
playback load_device(const std::string &file)
Definition: rs_context.hpp:184
rs2_device * rs2_create_device_from_sensor(const rs2_sensor *sensor, rs2_error **error)
Definition: rs.cpp:2338
void rs2_delete_device(rs2_device *device)
Definition: rs.cpp:301
rs2_context * rs2_create_context(int api_version, rs2_error **error)
Creates RealSense context that is required for the rest of the API.
Definition: rs.cpp:163
GLint GLuint mask
device_list query_devices() const
Definition: rs_context.hpp:112
void rs2_delete_context(rs2_context *context)
Frees the relevant context object.
Definition: rs.cpp:171
rs2_device * rs2_context_add_device(rs2_context *ctx, const char *file, rs2_error **error)
Definition: rs.cpp:1519
const rs2_device_list * get_list() const
Definition: rs_device.hpp:767
Definition: cah-model.h:10
GLsizei const GLchar *const * string
void unload_tracking_module()
Definition: rs_context.hpp:202
e
Definition: rmse.py:177
def info(name, value, persistent=False)
Definition: test.py:301
bool was_added(const rs2::device &dev) const
Definition: rs_context.hpp:40
std::shared_ptr< rs2_sensor > _sensor
Definition: rs_sensor.hpp:352
rs2_device_list * rs2_query_devices_ex(const rs2_context *context, int product_mask, rs2_error **error)
Definition: rs.cpp:214
std::shared_ptr< rs2_context > _context
Definition: rs_context.hpp:218
int rs2_device_hub_is_device_connected(const rs2_device_hub *hub, const rs2_device *device, rs2_error **error)
Definition: rs.cpp:199
event_information(device_list removed, device_list added)
Definition: rs_context.hpp:16
context(std::shared_ptr< rs2_context > ctx)
Definition: rs_context.hpp:209
void rs2_delete_device_hub(const rs2_device_hub *hub)
Frees the relevant device hub object.
Definition: rs.cpp:184
std::shared_ptr< rs2_device > _dev
Definition: rs_device.hpp:146
def callback(frame)
Definition: t265_stereo.py:91
device_hub(std::shared_ptr< rs2_device_hub > hub)
Definition: rs_context.hpp:267
device_list query_devices(int mask) const
Definition: rs_context.hpp:127
device_list get_new_devices() const
Definition: rs_context.hpp:57
static void handle(rs2_error *e)
Definition: rs_types.hpp:144
void rs2_context_unload_tracking_module(rs2_context *ctx, rs2_error **error)
Definition: rs.cpp:1547
typename::boost::move_detail::remove_reference< T >::type && move(T &&t) BOOST_NOEXCEPT
void set_devices_changed_callback(T callback)
Definition: rs_context.hpp:169
void rs2_set_devices_changed_callback_cpp(rs2_context *context, rs2_devices_changed_callback *callback, rs2_error **error)
Definition: rs.cpp:823
devices_changed_callback(T callback)
Definition: rs_context.hpp:73
GLuint res
Definition: glext.h:8856
void rs2_context_remove_device(rs2_context *ctx, const char *file, rs2_error **error)
Definition: rs.cpp:1539
device wait_for_device() const
Definition: rs_context.hpp:240
void rs2_delete_device_list(rs2_device_list *info_list)
Definition: rs.cpp:275
rs2_device * rs2_device_hub_wait_for_device(const rs2_device_hub *hub, rs2_error **error)
Definition: rs.cpp:191
auto device
Definition: pyrs_net.cpp:17
void copy(void *dst, void const *src, size_t size)
Definition: types.cpp:836
device_hub(context ctx)
Definition: rs_context.hpp:227


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:40