Skip to content

Commit

Permalink
Avoid to call BufferedPort::prepare every time VectorsCollectionServe…
Browse files Browse the repository at this point in the history
…r::VectorsCollectionServer is called
  • Loading branch information
GiulioRomualdi committed Jan 8, 2024
1 parent a405c4b commit 66f87c4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
3 changes: 2 additions & 1 deletion bindings/python/YarpUtilities/src/VectorsCollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ void CreateVectorsCollectionServer(pybind11::module& module)
const std::string& key,
Eigen::Ref<const Eigen::VectorXd> data) -> bool {
return impl.populateData(key, data);
});
})
.def("prepare_data", &VectorsCollectionServer::prepareData);
}
} // namespace YarpUtilities
} // namespace bindings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace YarpUtilities
* server.populateMetadata("key2", {"metadata4", "metadata5", "metadata6"});
* server.finalizeMetadata();
*
* // prepare the data
* server.prepareData();
* server.clearData(); // optional
* server.populateData("key1", {1.0, 2.0, 3.0});
* server.populateData("key2", {4.0, 5.0, 6.0});
Expand Down Expand Up @@ -94,7 +96,8 @@ class VectorsCollectionServer : public VectorsCollectionMetadataService
* @param key key of the data.
* @param data data.
* @return true if the data has been set successfully, false otherwise.
* @note this function should be called after the metadata has been finalized.
* @note this function should be called after the metadata has been finalized and after the
* prepareData function has been called.
*/
bool populateData(const std::string& key, const iDynTree::Span<const double>& data);

Expand All @@ -111,6 +114,12 @@ class VectorsCollectionServer : public VectorsCollectionMetadataService
*/
bool areMetadataReady() override;

/**
* Prepare the data.
* @note this function should be called before the data is populated.
*/
void prepareData();

/**
* Send the data filled with populateData
* @param forceStrict If this is true, wait until any previous sends are complete. If false, the
Expand All @@ -124,8 +133,9 @@ class VectorsCollectionServer : public VectorsCollectionMetadataService
* to reuse it without reallocating memory, you may skip calling this function. Otherwise, use
* VectorsCollection::clearData to free the memory allocated in the internal buffer.
* @note Note that this function only clears the data and does not affect the metadata.
* @return true if the data has been cleared successfully, false otherwise.
*/
void clearData();
bool clearData();

private:
struct Impl;
Expand Down
45 changes: 40 additions & 5 deletions src/YarpUtilities/src/VectorsCollectionServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <yarp/os/BufferedPort.h>
#include <yarp/os/Port.h>

#include <functional>
#include <optional>
#include <unordered_set>

using namespace BipedalLocomotion::YarpUtilities;
Expand All @@ -25,8 +27,21 @@ struct VectorsCollectionServer::Impl

std::atomic<bool> isMetadataFinalized{false}; /**< True if the metadata has been finalized. */
std::unordered_set<std::string> setOfKeys; /**< Set of keys. */
std::optional<std::reference_wrapper<VectorsCollection>> collection; /**< Reference to the
collection. */

/**
* Check if the collection is valid.
* @return True if the collection is valid.
*/
[[nodiscard]] bool isCollectionValid() const;
};

bool VectorsCollectionServer::Impl::isCollectionValid() const
{
return collection.has_value();
}

VectorsCollectionServer::VectorsCollectionServer()
{
m_pimpl = std::make_unique<Impl>();
Expand Down Expand Up @@ -129,6 +144,11 @@ bool VectorsCollectionServer::finalizeMetadata()
return true;
}

void VectorsCollectionServer::prepareData()
{
m_pimpl->collection = m_pimpl->port.prepare();
}

bool VectorsCollectionServer::populateData(const std::string& key,
const iDynTree::Span<const double>& data)
{
Expand All @@ -148,9 +168,15 @@ bool VectorsCollectionServer::populateData(const std::string& key,
return false;
}

// prepare the data
BipedalLocomotion::YarpUtilities::VectorsCollection& collection = m_pimpl->port.prepare();
collection.vectors[key].assign(data.begin(), data.end());
if (!m_pimpl->isCollectionValid())
{
log()->error("{} The data collection is not valid. Please call prepareData before "
"calling this function.",
logPrefix);
return false;
}

m_pimpl->collection.value().get().vectors[key].assign(data.begin(), data.end());

return true;
}
Expand All @@ -160,9 +186,18 @@ void VectorsCollectionServer::sendData(bool forceStrict /*= false */)
m_pimpl->port.write(forceStrict);
}

void VectorsCollectionServer::clearData()
bool VectorsCollectionServer::clearData()
{
m_pimpl->port.prepare().vectors.clear();
// check if the reference to the collection is valid
if (!m_pimpl->isCollectionValid())
{
log()->error("[VectorsCollectionServer::clearData] The reference to the collection is "
"invalid. Please call prepareData before calling this function.");
return false;
}

m_pimpl->collection.value().get().vectors.clear();
return true;
}

bool VectorsCollectionServer::areMetadataReady()
Expand Down

0 comments on commit 66f87c4

Please sign in to comment.