1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/************************************************************************
*
* Copyright (C) 2022-2024 IRCAD France
*
* This file is part of Sight.
*
* Sight is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sight. If not, see <https://www.gnu.org/licenses/>.
*
***********************************************************************/
#pragma once
#include <sight/data/config.hpp>
#include <data/mt/weak_ptr.hpp>
#include <data/object.hpp>
#include <data/ptr_access.hpp>
#include <optional>
namespace sight::data
{
class base_ptr;
//------------------------------------------------------------------------------
/**
* @brief Interface that defines a owner of data::ptr and data::ptr_vector.
*
* It allows to register the pointers at instantiation time and provides generic getters and setters of the pointers.
*/
class SIGHT_DATA_CLASS_API has_data
{
public:
SIGHT_DATA_API has_data() = default;
SIGHT_DATA_API virtual ~has_data() = default;
/**
* @brief Return a weak data pointer of the input object at the given key and index.
* @param _key key of data to retrieve.
* @param _index of the data to retrieve.
* @return weak data pointer in the right type, expired pointer if not found.
*/
template<class DATATYPE = sight::data::object, typename CDATATYPE = std::add_const_t<DATATYPE> >
inline data::mt::weak_ptr<CDATATYPE> input(std::string_view _key, std::optional<std::size_t> _index = {}) const;
/**
* @brief Return a weak data pointer of the in/out object at the given key and index.
* @param _key key of data to retrieve.
* @param _index of the data to retrieve.
* @return weak data pointer in the right type, expired pointer if not found.
*/
template<class DATATYPE = sight::data::object>
inline data::mt::weak_ptr<DATATYPE> inout(std::string_view _key, std::optional<std::size_t> _index = {}) const;
/**
* @brief Return a weak data pointer of the out object at the given key and index.
* @param _key key of data to retrieve.
* @param _index of the data to retrieve.
* @return weak data pointer in the right type, expired pointer if not found.
*/
template<class DATATYPE = sight::data::object>
inline data::mt::weak_ptr<DATATYPE> output(std::string_view _key, std::optional<std::size_t> _index = {}) const;
/**
* @brief Return the input, inout or output object at the given key.
* @param _key key of data to retrieve.
* @param[in] _access access to the object (in/inout/out)
* @param _index optional index of the data to retrieve.
* @return data object, nullptr if not found.
*/
SIGHT_DATA_API data::object::csptr object(
std::string_view _key,
data::access _access,
std::optional<std::size_t> _index = {}) const;
/**
* @brief Set an input object, and overrides the default auto_connect and optional settings.
*
* @param[in] _obj input object used by the service
* @param[in] _key key of the object
* @param[in] _auto_connect if true, the service will be connected to the object's signals
* @param[in] _optional if true, the service can be started even if the objet is not present
* @param[in] _index if specified, indicates the object is part of a group and gives its index
*/
SIGHT_DATA_API void set_input(
data::object::csptr _obj,
std::string_view _key,
std::optional<bool> _auto_connect = {},
std::optional<bool> _optional = {},
std::optional<std::size_t> _index = {});
/**
* @brief Set an in/outs object, and overrides the default auto_connect and optional settings.
*
* @param[in] _obj in/out object used by the service
* @param[in] _key key of the object
* @param[in] _auto_connect if true, the service will be connected to the object's signals
* @param[in] _optional if true, the service can be started even if the objet is not present
* @param[in] _index if specified, indicates the object is part of a group and gives its index
*/
SIGHT_DATA_API void set_inout(
data::object::sptr _obj,
std::string_view _key,
std::optional<bool> _auto_connect = {},
std::optional<bool> _optional = {},
std::optional<std::size_t> _index = {});
/**
* @brief Register an output object, replacing it if it already exists.
* @param _key name of the data or the group to register.
* @param _object pointer to the object to register.
* @param _index optional index of the key in the case of a member of a group of keys.
* @warning The service manages the output object lifetime: if it creates a new object, it will be the only
* maintainer of this object, when calling set_output, it allows to share the object with other services. But these
* services will not maintain a reference to this object (only weak_ptr). When the service stops, it should remove
* its outputs by calling set_output(key, nullptr). Otherwise, a service may work on an expired object.
*/
SIGHT_DATA_API void set_output(
data::object::sptr _object,
std::string_view _key,
std::optional<std::size_t> _index = {});
protected:
/**
* @brief Set a registered object for this service
*
* @param[in] _obj object used by the service
* @param[in] _key key of the object
* @param[in] _index index of the data in the group
* @param[in] _access access to the object (in/inout/out)
* @param[in] _auto_connect if true, the service will be connected to the object's signals
* @param[in] _optional if true, the service can be started even if the objet is not present
*/
SIGHT_DATA_API void set_object(
data::object::sptr _obj,
std::string_view _key,
std::optional<std::size_t> _index,
data::access _access,
std::optional<bool> _auto_connect,
bool _optional
);
/**
* @brief Unset a registered object for this service
*
* @param[in] _key key of the object
* @param[in] _index index of the data in the group
*/
SIGHT_DATA_API void reset_object(std::string_view _key, std::optional<std::size_t> _index);
/**
* @brief Set the deferred identifier of a key. This is useful to declare an object that is not present at start of
* the application and that will be created later.
* @param[in] _key key of the object
* @param[in] _id label of the object
* @param[in] _index index of the data in the group
*/
SIGHT_DATA_API void set_deferred_id(
std::string_view _key,
const std::string& _id,
std::optional<std::size_t> _index = {});
/**
* @brief Reset all output objects
*/
SIGHT_DATA_API void reset_all_out();
using container_t = std::map<std::pair<std::string_view, std::optional<std::size_t> >, base_ptr*>;
SIGHT_DATA_API const container_t& container() const;
private:
friend class base_ptr;
template<class DATATYPE, data::access ACCESS>
friend class ptr;
template<class DATATYPE, data::access ACCESS>
friend class ptr_vector;
/// Registers a pointer
SIGHT_DATA_API void register_ptr(std::string_view _key, base_ptr* _data, std::optional<std::size_t> _index = 0);
/// Unregisters a pointer
SIGHT_DATA_API void unregister_ptr(base_ptr* _data);
/// Notifies that a new object has been created and available
SIGHT_DATA_API virtual void notify_register_out(data::object::sptr, const std::string&) = 0;
/// Notifies that a new object is being destroyed and no longer available
SIGHT_DATA_API virtual void notify_unregister_out(data::object::sptr, const std::string&) = 0;
/**
* @brief Map of data pointers, data::ptr and data::ptr_vector.
* Each pointer registers itself in this map from its constructor.
* They always register with {} as index.
* Then, data::ptr_vector registers a new data::ptr each time a new data is inserted with a real index this time.
* This means that a data::ptr_vector of size 4, is actually registered 5 times, once for the data::ptr_vector, and
* 4 times for each contained data::ptr.
* Accessing elements with {} index is used to get initial properties (autoconnect, optional).
*/
container_t m_data_container;
};
//------------------------------------------------------------------------------
template<class DATATYPE, typename CDATATYPE>
inline data::mt::weak_ptr<CDATATYPE> has_data::input(std::string_view _key, std::optional<std::size_t> _index) const
{
data::mt::weak_ptr<CDATATYPE> input;
input = std::dynamic_pointer_cast<CDATATYPE>(this->object(_key, data::access::in, _index));
return input;
}
//------------------------------------------------------------------------------
template<class DATATYPE>
inline data::mt::weak_ptr<DATATYPE> has_data::inout(std::string_view _key, std::optional<std::size_t> _index) const
{
data::mt::weak_ptr<DATATYPE> inout;
inout =
std::dynamic_pointer_cast<DATATYPE>(
std::const_pointer_cast<data::object>(
this->object(
_key,
data::access::inout,
_index
)
)
);
return inout;
}
//------------------------------------------------------------------------------
template<class DATATYPE>
inline data::mt::weak_ptr<DATATYPE> has_data::output(std::string_view _key, std::optional<std::size_t> _index) const
{
data::mt::weak_ptr<DATATYPE> out;
out =
std::dynamic_pointer_cast<DATATYPE>(
std::const_pointer_cast<data::object>(
this->object(
_key,
data::access::out,
_index
)
)
);
return out;
}
//------------------------------------------------------------------------------
} // namespace sight::data
|