Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
rs_options.hpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2019 Intel Corporation. All Rights Reserved.
3 
4 #ifndef LIBREALSENSE_RS2_OPTIONS_HPP
5 #define LIBREALSENSE_RS2_OPTIONS_HPP
6 
7 #include "rs_types.hpp"
8 #include "../h/rs_types.h"
9 
10 #include <memory>
11 
12 
13 namespace rs2
14 {
16  {
17  std::shared_ptr< const rs2_option_value > _value;
18 
19  public:
20  explicit option_value( rs2_option_value const * handle )
21  : _value( handle, rs2_delete_option_value )
22  {
23  }
24  option_value( option_value const & ) = default;
25  option_value( option_value && ) = default;
26  option_value() = default;
27 
28  enum invalid_t { invalid };
30  : _value( new rs2_option_value{ option_id, false, RS2_OPTION_TYPE_COUNT } ) {}
31 
32  option_value( rs2_option option_id, int64_t as_integer )
33  : _value( new rs2_option_value{ option_id, true, RS2_OPTION_TYPE_INTEGER } )
34  {
35  const_cast< rs2_option_value * >( _value.get() )->as_integer = as_integer;
36  }
37  option_value( rs2_option option_id, float as_float )
38  : _value( new rs2_option_value{ option_id, true, RS2_OPTION_TYPE_FLOAT } )
39  {
40  const_cast< rs2_option_value * >( _value.get() )->as_float = as_float;
41  }
42  option_value( rs2_option option_id, char const * as_string )
43  : _value( new rs2_option_value{ option_id, true, RS2_OPTION_TYPE_STRING } )
44  {
45  const_cast< rs2_option_value * >( _value.get() )->as_string = as_string;
46  }
47  option_value( rs2_option option_id, bool as_boolean )
48  : _value( new rs2_option_value{ option_id, true, RS2_OPTION_TYPE_BOOLEAN } )
49  {
50  const_cast<rs2_option_value *>(_value.get())->as_integer = as_boolean;
51  }
52 
53  option_value & operator=( option_value const & ) = default;
54  option_value & operator=( option_value && ) = default;
55 
56  rs2_option_value const * operator->() const { return _value.get(); }
57  operator rs2_option_value const *() const { return _value.get(); }
58  };
59 
61  {
62  public:
63  options_list( options_list const & ) = default;
64  options_list( options_list && ) = default;
65 
66  explicit options_list( std::shared_ptr< rs2_options_list > list )
67  : _list( std::move( list ) )
68  {
69  rs2_error * e = nullptr;
70  _size = rs2_get_options_list_size( _list.get(), &e );
71  error::handle( e );
72  }
73 
75  : _list( nullptr )
76  , _size( 0 )
77  {
78  }
79 
80  option_value operator[]( size_t index ) const
81  {
82  rs2_error * e = nullptr;
83  auto value = rs2_get_option_value_from_list( _list.get(), static_cast< int >( index ), &e );
84  error::handle( e );
85  return option_value( value );
86  }
87 
88  size_t size() const { return _size; }
89 
90  option_value front() const { return ( *this )[0]; }
91  option_value back() const { return ( *this )[size() - 1]; }
92 
93  class iterator
94  {
95  iterator( const options_list & list, size_t index )
96  : _list( list )
97  , _index( index )
98  {
99  }
100 
101  public:
102  option_value operator*() const { return _list[_index]; }
103 
104  bool operator!=( const iterator & other ) const
105  {
106  return other._index != _index || &other._list != &_list;
107  }
108  bool operator==( const iterator & other ) const
109  {
110  return ! ( *this != other );
111  }
112 
114  {
115  _index++;
116  return *this;
117  }
118 
119  private:
120  friend options_list;
121  const options_list & _list;
122  size_t _index;
123  };
124 
125  iterator begin() const { return iterator( *this, 0 ); }
126  iterator end() const { return iterator( *this, size() ); }
127 
128  std::shared_ptr< rs2_options_list > get() const { return _list; };
129 
130  private:
131  std::shared_ptr< rs2_options_list > _list;
132  size_t _size;
133  };
134 
136  {
137  std::function< void( const options_list & ) > _callback;
138 
139  public:
140  explicit options_changed_callback( const std::function< void( const options_list & ) > & callback )
141  : _callback( callback )
142  {
143  }
144 
145  void on_value_changed( rs2_options_list * list ) override
146  {
147  std::shared_ptr< rs2_options_list > sptr( list, rs2_delete_options_list );
148  options_list opt_list( sptr );
149  _callback( opt_list );
150  }
151 
152  void release() override { delete this; }
153  };
154 
155  class options
156  {
157  public:
163  bool supports(rs2_option option) const
164  {
165  rs2_error* e = nullptr;
166  auto res = rs2_supports_option(_options, option, &e);
167  error::handle(e);
168  return res > 0;
169  }
170 
176  const char* get_option_description(rs2_option option) const
177  {
178  rs2_error* e = nullptr;
179  auto res = rs2_get_option_description(_options, option, &e);
180  error::handle(e);
181  return res;
182  }
183 
189  const char* get_option_name(rs2_option option) const
190  {
191  rs2_error* e = nullptr;
192  auto res = rs2_get_option_name(_options, option, &e);
193  error::handle(e);
194  return res;
195  }
196 
203  const char* get_option_value_description(rs2_option option, float val) const
204  {
205  rs2_error* e = nullptr;
206  auto res = rs2_get_option_value_description(_options, option, val, &e);
207  error::handle(e);
208  return res;
209  }
210 
216  float get_option(rs2_option option) const
217  {
218  rs2_error* e = nullptr;
219  auto res = rs2_get_option(_options, option, &e);
220  error::handle(e);
221  return res;
222  }
223 
230  {
231  rs2_error * e = nullptr;
232  auto value = rs2_get_option_value( _options, option_id, &e );
233  error::handle( e );
234  return option_value( value );
235  }
236 
242  {
243  option_range result;
244  rs2_error* e = nullptr;
245  rs2_get_option_range(_options, option,
246  &result.min, &result.max, &result.step, &result.def, &e);
247  error::handle(e);
248  return result;
249  }
250 
256  void set_option(rs2_option option, float value) const
257  {
258  rs2_error* e = nullptr;
259  rs2_set_option(_options, option, value, &e);
260  error::handle(e);
261  }
262 
268  void set_option_value( option_value const & value ) const
269  {
270  rs2_error * e = nullptr;
271  rs2_set_option_value( _options, value, &e );
272  error::handle( e );
273  }
274 
280  bool is_option_read_only(rs2_option option) const
281  {
282  rs2_error* e = nullptr;
283  auto res = rs2_is_option_read_only(_options, option, &e);
284  error::handle(e);
285  return res > 0;
286  }
287 
292  void on_options_changed( std::function< void( const options_list & ) > callback ) const
293  {
294  rs2_error * e = nullptr;
295  rs2_set_options_changed_callback_cpp( _options, new options_changed_callback( callback ), &e );
296  error::handle( e );
297  }
298 
299  std::vector<rs2_option> get_supported_options()
300  {
301  std::vector<rs2_option> res;
302  rs2_error* e = nullptr;
303  std::shared_ptr< rs2_options_list > options_list( rs2_get_options_list(_options, &e), rs2_delete_options_list);
304  error::handle( e );
305 
306  for (auto opt = 0; opt < rs2_get_options_list_size(options_list.get(), &e);opt++)
307  {
308  res.push_back(rs2_get_option_from_list(options_list.get(), opt, &e));
309  }
310  return res;
311  };
312 
314  {
315  rs2_error * e = nullptr;
316  std::shared_ptr< rs2_options_list > sptr(
317  rs2_get_options_list( _options, &e ),
319  error::handle( e );
320  return options_list( sptr );
321  };
322 
323  options& operator=(const options& other)
324  {
325  _options = other._options;
326  return *this;
327  }
328  // if operator= is ok, this should be ok too
329  options(const options& other) : _options(other._options) {}
330 
331  virtual ~options() = default;
332 
333  protected:
334  explicit options(rs2_options* o = nullptr) : _options(o)
335  {
336  }
337 
338  template<class T>
339  options& operator=(const T& dev)
340  {
341  _options = (rs2_options*)(dev.get());
342  return *this;
343  }
344 
345  private:
346  rs2_options* _options;
347  };
348 
349 
350 }
351 #endif // LIBREALSENSE_RS2_OIPTIONS_HPP
options_list get_supported_option_values()
Definition: rs_options.hpp:313
void rs2_set_option_value(rs2_options const *options, rs2_option_value const *option_value, rs2_error **error)
rs2_option_value const * rs2_get_option_value(const rs2_options *options, rs2_option option_id, rs2_error **error)
Definition: rs_types.hpp:100
float rs2_get_option(const rs2_options *options, rs2_option option, rs2_error **error)
bool supports(rs2_option option) const
Definition: rs_options.hpp:163
Definition: rs_option.h:149
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:26
option_value operator[](size_t index) const
Definition: rs_options.hpp:80
virtual ~options()=default
const char * get_option_name(rs2_option option) const
Definition: rs_options.hpp:189
Definition: rs_options.hpp:93
void on_options_changed(std::function< void(const options_list &) > callback) const
Definition: rs_options.hpp:292
size_t size() const
Definition: rs_options.hpp:88
float min
Definition: rs_types.hpp:196
options & operator=(const T &dev)
Definition: rs_options.hpp:339
Definition: rs_context.hpp:11
rs2_option_value const * rs2_get_option_value_from_list(const rs2_options_list *options, int i, rs2_error **error)
options_list(std::shared_ptr< rs2_options_list > list)
Definition: rs_options.hpp:66
option_value()=default
const char * get_option_description(rs2_option option) const
Definition: rs_options.hpp:176
void rs2_set_options_changed_callback_cpp(rs2_options *options, rs2_options_changed_callback *callback, rs2_error **error)
The value of an option, in a known option type.
Definition: rs_option.h:177
option_value(rs2_option option_id, invalid_t)
Definition: rs_options.hpp:29
bool operator==(const iterator &other) const
Definition: rs_options.hpp:108
options(const options &other)
Definition: rs_options.hpp:329
float max
Definition: rs_types.hpp:197
float step
Definition: rs_types.hpp:199
option_value(rs2_option option_id, float as_float)
Definition: rs_options.hpp:37
void rs2_delete_option_value(rs2_option_value const *handle)
void release() override
Definition: rs_options.hpp:152
float get_option(rs2_option option) const
Definition: rs_options.hpp:216
std::vector< rs2_option > get_supported_options()
Definition: rs_options.hpp:299
option_value(rs2_option option_id, char const *as_string)
Definition: rs_options.hpp:42
int rs2_supports_option(const rs2_options *options, rs2_option option, rs2_error **error)
const char * rs2_get_option_name(const rs2_options *options, rs2_option option, rs2_error **error)
Definition: rs_option.h:150
iterator begin() const
Definition: rs_options.hpp:125
const char * rs2_get_option_value_description(const rs2_options *options, rs2_option option, float value, rs2_error **error)
Definition: rs_options.hpp:15
Definition: rs_types.hpp:194
Definition: rs_options.hpp:60
invalid_t
Definition: rs_options.hpp:28
options_list()
Definition: rs_options.hpp:74
Definition: rs_options.hpp:155
const char * get_option_value_description(rs2_option option, float val) const
Definition: rs_options.hpp:203
void rs2_get_option_range(const rs2_options *sensor, rs2_option option, float *min, float *max, float *step, float *def, rs2_error **error)
void on_value_changed(rs2_options_list *list) override
Definition: rs_options.hpp:145
rs2_options_list * rs2_get_options_list(const rs2_options *options, rs2_error **error)
struct rs2_options rs2_options
Definition: rs_types.h:254
Definition: rs_option.h:152
Definition: rs_option.h:155
option_value operator*() const
Definition: rs_options.hpp:102
static void handle(rs2_error *e)
Definition: rs_types.hpp:162
options(rs2_options *o=nullptr)
Definition: rs_options.hpp:334
Definition: rs_options.hpp:135
iterator & operator++()
Definition: rs_options.hpp:113
Definition: rs_options.hpp:28
rs2_option_value const * operator->() const
Definition: rs_options.hpp:56
bool is_option_read_only(rs2_option option) const
Definition: rs_options.hpp:280
Definition: rs_option.h:151
bool operator!=(const iterator &other) const
Definition: rs_options.hpp:104
option_range get_option_range(rs2_option option) const
Definition: rs_options.hpp:241
void set_option(rs2_option option, float value) const
Definition: rs_options.hpp:256
float def
Definition: rs_types.hpp:198
option_value(rs2_option option_id, int64_t as_integer)
Definition: rs_options.hpp:32
const char * rs2_get_option_description(const rs2_options *options, rs2_option option, rs2_error **error)
options & operator=(const options &other)
Definition: rs_options.hpp:323
std::shared_ptr< rs2_options_list > get() const
Definition: rs_options.hpp:128
int rs2_is_option_read_only(const rs2_options *options, rs2_option option, rs2_error **error)
option_value get_option_value(rs2_option option_id) const
Definition: rs_options.hpp:229
void rs2_set_option(const rs2_options *options, rs2_option option, float value, rs2_error **error)
struct rs2_error rs2_error
Definition: rs_types.h:229
option_value front() const
Definition: rs_options.hpp:90
void rs2_delete_options_list(rs2_options_list *list)
struct rs2_options_list rs2_options_list
Definition: rs_types.h:255
option_value(rs2_option option_id, bool as_boolean)
Definition: rs_options.hpp:47
option_value(rs2_option_value const *handle)
Definition: rs_options.hpp:20
rs2_option rs2_get_option_from_list(const rs2_options_list *options, int i, rs2_error **error)
void set_option_value(option_value const &value) const
Definition: rs_options.hpp:268
int rs2_get_options_list_size(const rs2_options_list *options, rs2_error **error)
iterator end() const
Definition: rs_options.hpp:126
option_value & operator=(option_value const &)=default
options_changed_callback(const std::function< void(const options_list &) > &callback)
Definition: rs_options.hpp:140
option_value back() const
Definition: rs_options.hpp:91