Skip to content

Commit

Permalink
options_container now tracks option registration order
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Jul 11, 2024
1 parent b3f8502 commit 370c21f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
42 changes: 36 additions & 6 deletions src/core/options-container.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.
// Copyright(c) 2023-4 Intel Corporation. All Rights Reserved.

#include "options-container.h"
#include "enum-helpers.h"
Expand All @@ -12,8 +12,8 @@ namespace librealsense {

const option & options_container::get_option( rs2_option id ) const
{
auto it = _options.find( id );
if( it == _options.end() )
auto it = _options_by_id.find( id );
if( it == _options_by_id.end() )
{
throw invalid_value_exception( rsutils::string::from()
<< "option '" << get_option_name( id ) << "' not supported" );
Expand All @@ -27,17 +27,47 @@ void options_container::update( std::shared_ptr< extension_snapshot > ext )
auto ctr = As< options_container >( ext );
if( ! ctr )
return;
for( auto && opt : ctr->_options )
for( auto id : ctr->_ordered_options )
{
_options[opt.first] = opt.second;
auto & opt = _options_by_id[id];
if( ! opt )
_ordered_options.push_back( id );
opt = ctr->_options_by_id[id];
}
}


void options_container::register_option( rs2_option id, std::shared_ptr< option > option )
{
auto & opt = _options_by_id[id];
if( ! opt )
_ordered_options.push_back( id );
opt = option;
_recording_function( *this );
}


void options_container::unregister_option( rs2_option id )
{
for( auto it = _ordered_options.begin(); it != _ordered_options.end(); ++it )
{
if( *it == id )
{
_ordered_options.erase( it );
break;
}
}
_options_by_id.erase( id );
}


std::vector< rs2_option > options_container::get_supported_options() const
{
// Return options ordered by their ID values! This means custom options will be first!
// Kept for backwards compatibility with existing devices; software sensors will return the _ordered_options

std::vector< rs2_option > options;
for( auto option : _options )
for( auto option : _options_by_id )
options.push_back( option.first );

return options;
Expand Down
30 changes: 13 additions & 17 deletions src/core/options-container.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015 Intel Corporation. All Rights Reserved.
// Copyright(c) 2015-24 Intel Corporation. All Rights Reserved.
#pragma once

#include "options-interface.h"
Expand All @@ -24,8 +24,9 @@ class LRS_EXTENSION_API options_container : public virtual options_interface, pu
public:
bool supports_option(rs2_option id) const override
{
auto it = _options.find(id);
if (it == _options.end()) return false;
auto it = _options_by_id.find( id );
if( it == _options_by_id.end() )
return false;
return it->second->is_enabled();
}

Expand All @@ -41,22 +42,16 @@ class LRS_EXTENSION_API options_container : public virtual options_interface, pu
return (const_cast<const options_container*>(this)->get_option_handler(id));
}

std::shared_ptr<option> get_option_handler(rs2_option id) const
std::shared_ptr<option> get_option_handler( rs2_option id ) const
{
auto it = _options.find(id);
return (it == _options.end() ? std::shared_ptr<option>(nullptr) : it->second);
auto it = _options_by_id.find( id );
if( it == _options_by_id.end() )
return {};
return it->second;
}

void register_option(rs2_option id, std::shared_ptr<option> option)
{
_options[id] = option;
_recording_function(*this);
}

void unregister_option(rs2_option id)
{
_options.erase(id);
}
void register_option( rs2_option id, std::shared_ptr< option > option );
void unregister_option( rs2_option id );

void create_snapshot(std::shared_ptr<options_interface>& snapshot) const override
{
Expand All @@ -75,7 +70,8 @@ class LRS_EXTENSION_API options_container : public virtual options_interface, pu
std::string const & get_option_name( rs2_option option ) const override;

protected:
std::map<rs2_option, std::shared_ptr<option>> _options;
std::vector< rs2_option > _ordered_options;
std::map< rs2_option, std::shared_ptr< option > > _options_by_id;
std::function<void(const options_interface&)> _recording_function = [](const options_interface&) {};
};

Expand Down

0 comments on commit 370c21f

Please sign in to comment.