Intel® RealSense™ Cross Platform API  2.13.0
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 
41  {
42  return _added;
43  }
44 
45  private:
46  device_list _removed;
47  device_list _added;
48  };
49 
50  template<class T>
52  {
53  T _callback;
54 
55  public:
56  explicit devices_changed_callback(T callback) : _callback(callback) {}
57 
58  void on_devices_changed(rs2_device_list* removed, rs2_device_list* added) override
59  {
60  std::shared_ptr<rs2_device_list> old(removed, rs2_delete_device_list);
61  std::shared_ptr<rs2_device_list> news(added, rs2_delete_device_list);
62 
63 
64  event_information info({ device_list(old), device_list(news) });
65  _callback(info);
66  }
67 
68  void release() override { delete this; }
69  };
70 
71  class pipeline;
72  class device_hub;
73 
78  class context
79  {
80  public:
82  {
83  rs2_error* e = nullptr;
84  _context = std::shared_ptr<rs2_context>(
87  error::handle(e);
88  }
89 
95  {
96  rs2_error* e = nullptr;
97  std::shared_ptr<rs2_device_list> list(
98  rs2_query_devices(_context.get(), &e),
100  error::handle(e);
101 
102  return device_list(list);
103  }
104 
109  std::vector<sensor> query_all_sensors() const
110  {
111  std::vector<sensor> results;
112  for (auto&& dev : query_devices())
113  {
114  auto sensors = dev.query_sensors();
115  std::copy(sensors.begin(), sensors.end(), std::back_inserter(results));
116  }
117  return results;
118  }
119 
120 
122  {
123  rs2_error* e = nullptr;
124  std::shared_ptr<rs2_device> dev(
127  error::handle(e);
128  return device{ dev };
129  }
130 
135  template<class T>
137  {
138  rs2_error* e = nullptr;
140  new devices_changed_callback<T>(std::move(callback)), &e);
141  error::handle(e);
142  }
143 
151  playback load_device(const std::string& file)
152  {
153  rs2_error* e = nullptr;
154  auto device = std::shared_ptr<rs2_device>(
155  rs2_context_add_device(_context.get(), file.c_str(), &e),
158 
159  return playback { device };
160  }
161 
162  void unload_device(const std::string& file)
163  {
164  rs2_error* e = nullptr;
165  rs2_context_remove_device(_context.get(), file.c_str(), &e);
167  }
168 
169 protected:
170  friend class rs2::pipeline;
171  friend class rs2::device_hub;
172 
173  context(std::shared_ptr<rs2_context> ctx)
174  : _context(ctx)
175  {}
176  std::shared_ptr<rs2_context> _context;
177  };
178 
183  {
184  public:
185  explicit device_hub(context ctx)
186  : _ctx(std::move(ctx))
187  {
188  rs2_error* e = nullptr;
189  _device_hub = std::shared_ptr<rs2_device_hub>(
190  rs2_create_device_hub(_ctx._context.get(), &e),
192  error::handle(e);
193  }
194 
200  {
201  rs2_error* e = nullptr;
202  std::shared_ptr<rs2_device> dev(
203  rs2_device_hub_wait_for_device(_ctx._context.get(), _device_hub.get(), &e),
205 
206  error::handle(e);
207 
208  return device(dev);
209 
210  }
211 
215  bool is_connected(const device& dev) const
216  {
217  rs2_error* e = nullptr;
218  auto res = rs2_device_hub_is_device_connected(_device_hub.get(), dev._dev.get(), &e);
219  error::handle(e);
220 
221  return res > 0 ? true : false;
222 
223  }
224  private:
225  context _ctx;
226  std::shared_ptr<rs2_device_hub> _device_hub;
227  };
228 
229 }
230 #endif // LIBREALSENSE_RS2_CONTEXT_HPP
Definition: rs_record_playback.hpp:27
Definition: rs_sensor.hpp:232
void release() override
Definition: rs_context.hpp:68
void on_devices_changed(rs2_device_list *removed, rs2_device_list *added) override
Definition: rs_context.hpp:58
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:37
bool is_connected(const device &dev) const
Definition: rs_context.hpp:215
rs2_device_list * rs2_query_devices(const rs2_context *context, rs2_error **error)
void unload_device(const std::string &file)
Definition: rs_context.hpp:162
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:151
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
Definition: rs_context.hpp:78
device_list get_new_devices() const
Definition: rs_context.hpp:40
device_list query_devices() const
Definition: rs_context.hpp:94
device wait_for_device() const
Definition: rs_context.hpp:199
std::shared_ptr< rs2_sensor > _sensor
Definition: rs_sensor.hpp:421
std::shared_ptr< rs2_context > _context
Definition: rs_context.hpp:176
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:173
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:109
std::shared_ptr< rs2_device > _dev
Definition: rs_device.hpp:143
device get_sensor_parent(const sensor &s) const
Definition: rs_context.hpp:121
static void handle(rs2_error *e)
Definition: rs_types.hpp:121
Definition: rs_pipeline.hpp:335
rs2_device * rs2_device_hub_wait_for_device(rs2_context *ctx, const rs2_device_hub *hub, rs2_error **error)
Definition: rs_device.hpp:184
Definition: rs_context.hpp:182
context()
Definition: rs_context.hpp:81
void set_devices_changed_callback(T callback)
Definition: rs_context.hpp:136
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:56
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:156
Definition: rs_context.hpp:51
Definition: rs_types.hpp:53
struct rs2_error rs2_error
Definition: rs_types.h:149
void rs2_delete_device_list(rs2_device_list *info_list)
Definition: rs_device.hpp:18
const rs2_device_list * get_list() const
Definition: rs_device.hpp:281
device_hub(context ctx)
Definition: rs_context.hpp:185