Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
Loading...
Searching...
No Matches
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"
9#include "rs_processing.hpp"
10
11namespace 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);
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);
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:
101 {
102 }
103 context( char const * json_settings = nullptr )
104 {
105 rs2_error* e = nullptr;
106 _context = std::shared_ptr<rs2_context>(
107 rs2_create_context_ex( RS2_API_VERSION, json_settings, &e ),
109 error::handle(e);
110 }
111 context( std::string const & json_settings )
112 : context( json_settings.c_str() )
113 {
114 }
115
116 operator bool() const { return !! _context; }
117
123 {
124 rs2_error* e = nullptr;
125 std::shared_ptr<rs2_device_list> list(
126 rs2_query_devices(_context.get(), &e),
128 error::handle(e);
129
130 return device_list(list);
131 }
132
138 {
139 rs2_error* e = nullptr;
140 std::shared_ptr<rs2_device_list> list(
141 rs2_query_devices_ex(_context.get(), mask, &e),
143 error::handle(e);
144
145 return device_list(list);
146 }
147
152 std::vector<sensor> query_all_sensors() const
153 {
154 std::vector<sensor> results;
155 for (auto&& dev : query_devices())
156 {
157 auto sensors = dev.query_sensors();
158 std::copy(sensors.begin(), sensors.end(), std::back_inserter(results));
159 }
160 return results;
161 }
162
163
165 {
166 rs2_error* e = nullptr;
167 std::shared_ptr<rs2_device> dev(
170 error::handle(e);
171 return device{ dev };
172 }
173
178 template<class T>
180 {
181 rs2_error* e = nullptr;
183 new devices_changed_callback<T>(std::move(callback)), &e);
184 error::handle(e);
185 }
186
194 playback load_device(const std::string& file)
195 {
196 rs2_error* e = nullptr;
197 auto device = std::shared_ptr<rs2_device>(
198 rs2_context_add_device(_context.get(), file.c_str(), &e),
201
202 return playback { device };
203 }
204
205 void unload_device(const std::string& file)
206 {
207 rs2_error* e = nullptr;
208 rs2_context_remove_device(_context.get(), file.c_str(), &e);
210 }
211
213 {
214 rs2_error* e = nullptr;
217 }
218
219 context(std::shared_ptr<rs2_context> ctx)
220 : _context(ctx)
221 {}
222 explicit operator std::shared_ptr<rs2_context>() { return _context; };
223 protected:
224 friend class rs2::pipeline;
225 friend class rs2::device_hub;
227
228 std::shared_ptr<rs2_context> _context;
229 };
230
235 {
236 public:
237 explicit device_hub(context ctx)
238 {
239 rs2_error* e = nullptr;
240 _device_hub = std::shared_ptr<rs2_device_hub>(
241 rs2_create_device_hub(ctx._context.get(), &e),
243 error::handle(e);
244 }
245
251 {
252 rs2_error* e = nullptr;
253 std::shared_ptr<rs2_device> dev(
254 rs2_device_hub_wait_for_device(_device_hub.get(), &e),
256
257 error::handle(e);
258
259 return device(dev);
260
261 }
262
266 bool is_connected(const device& dev) const
267 {
268 rs2_error* e = nullptr;
269 auto res = rs2_device_hub_is_device_connected(_device_hub.get(), dev._dev.get(), &e);
270 error::handle(e);
271
272 return res > 0 ? true : false;
273
274 }
275
276 explicit operator std::shared_ptr<rs2_device_hub>() { return _device_hub; }
277 explicit device_hub(std::shared_ptr<rs2_device_hub> hub) : _device_hub(std::move(hub)) {}
278 private:
279 std::shared_ptr<rs2_device_hub> _device_hub;
280 };
281
282}
283#endif // LIBREALSENSE_RS2_CONTEXT_HPP
Definition rs_context.hpp:97
void set_devices_changed_callback(T callback)
Definition rs_context.hpp:179
context(std::string const &json_settings)
Definition rs_context.hpp:111
device_list query_devices() const
Definition rs_context.hpp:122
context(uninitialized_t)
Definition rs_context.hpp:100
context(char const *json_settings=nullptr)
Definition rs_context.hpp:103
uninitialized_t
Definition rs_context.hpp:99
@ uninitialized
Definition rs_context.hpp:99
void unload_device(const std::string &file)
Definition rs_context.hpp:205
device_list query_devices(int mask) const
Definition rs_context.hpp:137
void unload_tracking_module()
Definition rs_context.hpp:212
std::vector< sensor > query_all_sensors() const
Generate a flat list of all available sensors from all RealSense devices.
Definition rs_context.hpp:152
std::shared_ptr< rs2_context > _context
Definition rs_context.hpp:228
playback load_device(const std::string &file)
Definition rs_context.hpp:194
device get_sensor_parent(const sensor &s) const
Definition rs_context.hpp:164
context(std::shared_ptr< rs2_context > ctx)
Definition rs_context.hpp:219
Definition rs_context.hpp:235
device wait_for_device() const
Definition rs_context.hpp:250
bool is_connected(const device &dev) const
Definition rs_context.hpp:266
device_hub(std::shared_ptr< rs2_device_hub > hub)
Definition rs_context.hpp:277
device_hub(context ctx)
Definition rs_context.hpp:237
Definition rs_device.hpp:1056
Definition rs_device.hpp:20
const std::shared_ptr< rs2_device > & get() const
Definition rs_device.hpp:162
std::shared_ptr< rs2_device > _dev
Definition rs_device.hpp:207
Definition rs_context.hpp:69
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
devices_changed_callback(T callback)
Definition rs_context.hpp:73
static void handle(rs2_error *e)
Definition rs_types.hpp:162
Definition rs_context.hpp:14
event_information(device_list removed, device_list added)
Definition rs_context.hpp:16
bool was_added(const rs2::device &dev) const
Definition rs_context.hpp:40
bool was_removed(const rs2::device &dev) const
Definition rs_context.hpp:23
device_list get_new_devices() const
Definition rs_context.hpp:57
Definition rs_pipeline.hpp:363
Definition rs_record_playback.hpp:28
Definition rs_sensor.hpp:103
std::shared_ptr< rs2_sensor > _sensor
Definition rs_sensor.hpp:352
Definition rs_internal.hpp:218
Definition rs_processing_gl.hpp:13
#define RS2_API_VERSION
Definition rs.h:42
void rs2_delete_context(rs2_context *context)
Frees the relevant context object.
rs2_device * rs2_device_hub_wait_for_device(const rs2_device_hub *hub, rs2_error **error)
void rs2_delete_device_hub(const rs2_device_hub *hub)
Frees the relevant device hub object.
int rs2_device_hub_is_device_connected(const rs2_device_hub *hub, const rs2_device *device, rs2_error **error)
rs2_context * rs2_create_context_ex(int api_version, const char *json_settings, rs2_error **error)
Creates RealSense context that is required for the rest of the API, and accepts a settings JSON.
void rs2_context_unload_tracking_module(rs2_context *ctx, rs2_error **error)
rs2_device_hub * rs2_create_device_hub(const rs2_context *context, rs2_error **error)
Creates RealSense device_hub .
void rs2_context_remove_device(rs2_context *ctx, const char *file, rs2_error **error)
rs2_device * rs2_context_add_device(rs2_context *ctx, const char *file, rs2_error **error)
void rs2_set_devices_changed_callback_cpp(rs2_context *context, rs2_devices_changed_callback *callback, rs2_error **error)
rs2_device_list * rs2_query_devices_ex(const rs2_context *context, int product_mask, rs2_error **error)
rs2_device_list * rs2_query_devices(const rs2_context *context, rs2_error **error)
void rs2_delete_device(rs2_device *device)
int rs2_device_list_contains(const rs2_device_list *info_list, const rs2_device *device, rs2_error **error)
void rs2_delete_device_list(rs2_device_list *info_list)
rs2_device * rs2_create_device_from_sensor(const rs2_sensor *sensor, rs2_error **error)
struct rs2_device_list rs2_device_list
Definition rs_types.h:238
struct rs2_error rs2_error
Definition rs_types.h:230
Definition rs_types.hpp:77