(io): remove usage of deprecated getInput getOutput getInOut
Description
Since #398 (closed) we should use the RAII mechanism when dealing with input/output/inout.
Since #626 (closed), we should use sight::data::ptr
and sight::data::ptr_vector
to declare, register and manipulate data in services.
Proposal
- Replace all references of
getLockedInput/Inout
andgetWeakInput/Inout
in favor of the use adata::ptr
(easy) - Replace all references of
getInput/Inout
andgetInput/Inout
in favor of the use adata::ptr
(harder, beware of potential dead locks !!!)- If not applicable (very rare case, maybe outside a service), use
getWeakInput/Inout
instead
- If not applicable (very rare case, maybe outside a service), use
- Check that everything works fine.
- Change
getInput/Inout
methods with the same code asgetWeakInput/Inout
and remove its deprecation. - Remove
getLockedInput/Inout
andgetWeakInput/Inout
Instructions to migrate to data::ptr
On top of this new pointer, it is good to remember that we now use std::string_view
for key names. This allows initializing everything in the header.
Here are the typical steps when introducing a data::ptr
in a service:
- Add the necessary
sight::data::ptr
andsight::data::ptr_vector
members - If the name (key) of the data is only used in the header, do not add a constant variable or remove it if it exists (both in the hpp and the cpp)
- If the name (key) of the data is used in the header and in the implementation (
getAutoConnections()
for instance), add astatic constexpr std::stringview
or convert the existing variable (IService::KeyType
) to this type (both in the hpp and the cpp, only present the header at the end). - In the implementation, change every call to getters, i.e.
auto outputMatrix = this->getInOut<data::Matrix4>(s_OUTPUT);
SIGHT_ASSERT("inout '" + s_OUTPUT + "' is not defined", outputMatrix);
{
data::mt::ObjectWriteLock outputMatrixLock(outputMatrix);
sight::geometry::data::identity(outputMatrix);
becomes
{
auto outputMatrix = m_output.lock();
SIGHT_ASSERT("inout '" + s_OUTPUT + "' is not defined", outputMatrix);
sight::geometry::data::identity(*outputMatrix); // if the function takes a & as parameter
sight::geometry::data::identity(outputMatrix.get_shared()); // if the function takes a `sptr&/csptr&` (not recommended) or a `sptr` as parameter
Beware of potential deadlocks, when you call lock()
, this returns a RAII object which locks (of course) in the current scope. You may need to add a scope or move the call to lock() like above. And as much as possible, limit the usage of get_shared()
, it might indicate that your API uses shared_ptr
abusively (see https://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/)
- And finally test!