Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
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:
63  device_list _removed;
64  device_list _added;
65  };
66 
67  template<class T>
69  {
70  T _callback;
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 
81  event_information info({ device_list(old), device_list(news) });
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 
127  device_list query_devices(int mask) const
128  {
129  rs2_error* e = nullptr;
130  std::shared_ptr<rs2_device_list> list(
131  rs2_query_devices_ex(_context.get(), mask, &e),
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 
184  playback load_device(const std::string& file)
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:
227  explicit device_hub(context ctx)
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
Definition: rs_record_playback.hpp:27
Definition: rs_sensor.hpp:103
void release() override
Definition: rs_context.hpp:85
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_context.hpp:13
#define RS2_API_VERSION
Definition: rs.h:41
bool is_connected(const device &dev) const
Definition: rs_context.hpp:256
rs2_device_list * rs2_query_devices(const rs2_context *context, rs2_error **error)
void unload_device(const std::string &file)
Definition: rs_context.hpp:195
rs2_device_hub * rs2_create_device_hub(const rs2_context *context, rs2_error **error)
Creates RealSense device_hub .
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)
void rs2_delete_device(rs2_device *device)
rs2_context * rs2_create_context(int api_version, rs2_error **error)
Creates RealSense context that is required for the rest of the API.
void rs2_delete_context(rs2_context *context)
Frees the relevant context object.
rs2_device * rs2_context_add_device(rs2_context *ctx, const char *file, rs2_error **error)
Definition: rs_context.hpp:11
void unload_tracking_module()
Definition: rs_context.hpp:202
Definition: rs_context.hpp:96
device_list get_new_devices() const
Definition: rs_context.hpp:57
bool was_added(const rs2::device &dev) const
Definition: rs_context.hpp:40
device_list query_devices() const
Definition: rs_context.hpp:112
device wait_for_device() const
Definition: rs_context.hpp:240
std::shared_ptr< rs2_sensor > _sensor
Definition: rs_sensor.hpp:354
rs2_device_list * rs2_query_devices_ex(const rs2_context *context, int product_mask, rs2_error **error)
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)
bool was_removed(const rs2::device &dev) const
Definition: rs_context.hpp:23
const std::shared_ptr< rs2_device > & get() const
Definition: rs_device.hpp:116
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.
std::vector< sensor > query_all_sensors() const
Generate a flat list of all available sensors from all RealSense devices.
Definition: rs_context.hpp:142
std::shared_ptr< rs2_device > _dev
Definition: rs_device.hpp:146
device_hub(std::shared_ptr< rs2_device_hub > hub)
Definition: rs_context.hpp:267
device get_sensor_parent(const sensor &s) const
Definition: rs_context.hpp:154
Definition: rs_internal.hpp:260
device_list query_devices(int mask) const
Definition: rs_context.hpp:127
static void handle(rs2_error *e)
Definition: rs_types.hpp:137
Definition: rs_pipeline.hpp:362
void rs2_context_unload_tracking_module(rs2_context *ctx, rs2_error **error)
Definition: rs_device.hpp:565
Definition: rs_context.hpp:224
context()
Definition: rs_context.hpp:99
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)
devices_changed_callback(T callback)
Definition: rs_context.hpp:73
void rs2_context_remove_device(rs2_context *ctx, const char *file, rs2_error **error)
struct rs2_device_list rs2_device_list
Definition: rs_types.h:225
Definition: rs_context.hpp:68
Definition: rs_types.hpp:62
struct rs2_error rs2_error
Definition: rs_types.h:217
void rs2_delete_device_list(rs2_device_list *info_list)
rs2_device * rs2_device_hub_wait_for_device(const rs2_device_hub *hub, rs2_error **error)
Definition: rs_device.hpp:18
const rs2_device_list * get_list() const
Definition: rs_device.hpp:662
device_hub(context ctx)
Definition: rs_context.hpp:227