From 9af2a35872b8a2844edf8b5b522fa0fed3c364c8 Mon Sep 17 00:00:00 2001
From: ls232920 <laurent.soulier@cea.fr>
Date: Thu, 19 Oct 2023 14:24:28 +0000
Subject: [PATCH] [WIP][IMPR] working on extraction Expliciting resource
 management in Tensor (a bit overkill so far as we're using standard
 smart-pointers but it paves the way for other possible implementation)

---
 include/aidge/backend/TensorImpl.hpp | 11 +++++++++++
 include/aidge/data/Tensor.hpp        | 17 +++++++++++++++++
 src/data/Tensor.cpp                  |  5 ++---
 3 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/include/aidge/backend/TensorImpl.hpp b/include/aidge/backend/TensorImpl.hpp
index 600f0badc..510e243eb 100644
--- a/include/aidge/backend/TensorImpl.hpp
+++ b/include/aidge/backend/TensorImpl.hpp
@@ -54,6 +54,17 @@ public:
     /// @details Copy all characteristics of calling TensorImpl and its data (deep copy).
     /// @return Pointer to a copy of the TensorImpl object
     virtual detail::pimpl::ImplPtr_t Clone() const = 0;
+    /// @brief Creates a new TensorImpl with same properties as self but restricted to a
+    /// given area
+    /// @param i_FirstDataCoordinates Logical coordinates of the data at null natural
+    /// coordinates
+    /// @param i_Dimensions Tensor dimensions
+    /// @details Copy all characteristics of calling TensorImpl and its data (deep copy),
+    /// restricting data to the given area.
+    /// @return Pointer to an extract of the TensorImpl object
+    virtual detail::pimpl::ImplPtr_t Extract(
+        std::vector<Coord_t> const &i_FirstDataCoordinates,
+        std::vector<DimSize_t> const &i_Dimensions) const = 0;
     /// @param src pointer to the raw host buffer from which the data will be copied
     /// @param length Nb of element to copy from the buffer
     virtual void copyFromHost(Byte_t const *const src, NbElts_t length) = 0;
diff --git a/include/aidge/data/Tensor.hpp b/include/aidge/data/Tensor.hpp
index 345586b5a..baa33d8e9 100644
--- a/include/aidge/data/Tensor.hpp
+++ b/include/aidge/data/Tensor.hpp
@@ -38,7 +38,16 @@ namespace detail
 {
 namespace pimpl
 {
+/// @brief Pointer type used to reference TensorImpl
 using ImplPtr_t = std::shared_ptr<TensorImpl>;
+/// @brief Mandatory function used to release TensorImpl ressource when a Tensor is
+/// destroyed.
+/// @param PImpl Referenced TensorImpl to be released
+inline void releasePImpl(ImplPtr_t &PImpl)
+{
+    // for a shared_ptr, merely remove a ref
+    PImpl = nullptr;
+}
 } // namespace pimpl
 
 /// @brief Check if a valid area is included in another one.
@@ -123,6 +132,14 @@ private:
 public:
     static constexpr const char *Type = "Tensor";
 
+    /// @brief Tensor destructor
+    /// @details Release any associated TensorImpl (but not necessarily deallocate it as
+    /// it may be shared)
+    ~Tensor()
+    {
+        detail::pimpl::releasePImpl(mImpl);
+    }
+
     /**
      * @brief Construct a new empty Tensor object.
      * @param dataType Sets the type of inserted data.
diff --git a/src/data/Tensor.cpp b/src/data/Tensor.cpp
index f2364ce31..b9ec6389b 100644
--- a/src/data/Tensor.cpp
+++ b/src/data/Tensor.cpp
@@ -246,9 +246,8 @@ Tensor::Tensor(
     }
     else
     {
-        /// @todo WIP
-        mImpl = Registrar<Tensor>::create({otherTensor.mImpl->backend(), mDataType})(
-            mDataType, mvActiveAreaOrigin, mDims);
+        // forming an extract
+        mImpl = otherTensor.mImpl->Extract(i_FirstDataLogicalCoordinates, i_Dimensions);
     }
 }
 
-- 
GitLab