Skip to main content

TensorStorageWrapper

TensorStorageWrapper

Sean Parent type-erasure wrapper for tensor storage backends. More…

#include <TensorStorageWrapper.hpp>

Public Functions

Name
template <typename StorageImpl >
TensorStorageWrapper(StorageImpl impl)
Construct from any concrete tensor storage implementation.
TensorStorageWrapper() =default
Default constructor — creates an empty wrapper.
TensorStorageWrapper(TensorStorageWrapper const & ) =default
Copy constructor (shared ownership, O(1)).
TensorStorageWrapper & operator=(TensorStorageWrapper const & ) =default
Copy assignment (shared ownership, O(1)).
TensorStorageWrapper(TensorStorageWrapper && ) =default
Move constructor.
TensorStorageWrapper & operator=(TensorStorageWrapper && ) =default
Move assignment.
~TensorStorageWrapper() =default
bool isValid() const
Check whether the wrapper holds a valid storage backend.
float getValueAt(std::span< std::size_t const > indices) const
Get a single float value by multi-dimensional index.
std::span< float const > flatData() const
Get a span over the flat (row-major) data.
std::vector< float > sliceAlongAxis(std::size_t axis, std::size_t index) const
Extract a sub-tensor by fixing one axis to a single index.
std::vector< float > getColumn(std::size_t col) const
Get a single column (axis 1 slice) across all rows.
std::vector< std::size_t > shape() const
Get the shape as a vector of sizes (one per axis).
std::size_t totalElements() const
Get the total number of elements (product of shape).
bool isContiguous() const
Whether the underlying data is contiguous in memory.
TensorStorageType getStorageType() const
Get the storage type identifier.
TensorStorageCache tryGetCache() const
Try to get cached pointers for fast-path access.
template <typename T >
T const *
tryGetAs() const
Attempt to retrieve the concrete storage implementation.
template <typename T >
T *
tryGetMutableAs()
Mutable version of tryGetAs.
template <typename T >
T const *
getAsChecked(TensorStorageType expected_type) const
Type-safe cast using enum verification instead of dynamic_cast.
template <typename T >
T *
getMutableAsChecked(TensorStorageType expected_type) const
Mutable version of getAsChecked.
std::shared_ptr< void const > sharedStorage() const
Get a shared_ptr to the internal concept (for ViewTensorStorage).
template <typename T >
T const *
tryGetAs() const
template <typename T >
T const *
getAsChecked(TensorStorageType expected_type) const

Detailed Description

class TensorStorageWrapper;

Sean Parent type-erasure wrapper for tensor storage backends.

Wraps any concrete storage that inherits from TensorStorageBase behind a uniform non-templated interface. This allows TensorData to hold any storage backend (Armadillo, Dense, LibTorch, View, Lazy) without exposing the CRTP type parameter to consumers.

Pattern

Uses the same Sean Parent type-erasure pattern as:

  • AnalogTimeSeries::DataStorageWrapper (shared_ptr, copyable)
  • RaggedStorageWrapper (unique_ptr, move-only)

This wrapper uses std::shared_ptr to enable zero-copy view sharing: when a ViewTensorStorage is created from a TensorStorageWrapper, both share ownership of the underlying concept (via the shared_ptr aliasing constructor or direct shared_ptr copy).

Ownership

The wrapper is copyable (shared ownership via shared_ptr).

  • Copies share the same underlying storage (cheap, O(1))
  • This enables ViewTensorStorage to reference the source without worrying about lifetime
  • For exclusive ownership, create a new storage and wrap it

Type Recovery

[tryGetAs](/api_generated/Classes/classTensorStorageWrapper.md#function-trygetas)<ConcreteStorage>() uses dynamic_cast on the internal concept/model hierarchy to recover the concrete type. This is the same approach as RaggedStorageWrapper::tryGet() and enables zero-copy backend-specific access (e.g., Armadillo matrix for mlpack, at::Tensor for model inference).

Thread Safety

Same as the underlying storage. The wrapper itself adds no synchronization. shared_ptr reference counting is thread-safe, but concurrent mutation of the pointee is not.

Public Functions Documentation

function TensorStorageWrapper

template <typename StorageImpl >
explicit TensorStorageWrapper(
    StorageImpl impl
)

Construct from any concrete tensor storage implementation.

Parameters:

  • impl The concrete storage to wrap (moved into the wrapper)

Template Parameters:

function TensorStorageWrapper

TensorStorageWrapper() =default

Default constructor — creates an empty wrapper.

The wrapper is in a “null” state. All accessor methods will throw std::runtime_error if called. Use [isValid()](/api_generated/Classes/classTensorStorageWrapper.md#function-isvalid) to check.

function TensorStorageWrapper

TensorStorageWrapper(
    TensorStorageWrapper const & 
) =default

Copy constructor (shared ownership, O(1)).

function operator=

TensorStorageWrapper & operator=(
    TensorStorageWrapper const & 
) =default

Copy assignment (shared ownership, O(1)).

function TensorStorageWrapper

TensorStorageWrapper(
    TensorStorageWrapper && 
) =default

Move constructor.

function operator=

TensorStorageWrapper & operator=(
    TensorStorageWrapper && 
) =default

Move assignment.

function ~TensorStorageWrapper

~TensorStorageWrapper() =default

function isValid

bool isValid() const

Check whether the wrapper holds a valid storage backend.

Return: true if a storage backend is present, false if default-constructed

function getValueAt

float getValueAt(
    std::span< std::size_t const > indices
) const

Get a single float value by multi-dimensional index.

See: TensorStorageBase::getValueAt

function flatData

std::span< float const > flatData() const

Get a span over the flat (row-major) data.

See: TensorStorageBase::flatData

function sliceAlongAxis

std::vector< float > sliceAlongAxis(
    std::size_t axis,
    std::size_t index
) const

Extract a sub-tensor by fixing one axis to a single index.

See: TensorStorageBase::sliceAlongAxis

function getColumn

std::vector< float > getColumn(
    std::size_t col
) const

Get a single column (axis 1 slice) across all rows.

See: TensorStorageBase::getColumn

function shape

std::vector< std::size_t > shape() const

Get the shape as a vector of sizes (one per axis).

See: TensorStorageBase::shape

function totalElements

std::size_t totalElements() const

Get the total number of elements (product of shape).

See: TensorStorageBase::totalElements

function isContiguous

bool isContiguous() const

Whether the underlying data is contiguous in memory.

See: TensorStorageBase::isContiguous

function getStorageType

TensorStorageType getStorageType() const

Get the storage type identifier.

See: TensorStorageBase::getStorageType

function tryGetCache

TensorStorageCache tryGetCache() const

Try to get cached pointers for fast-path access.

See: TensorStorageBase::tryGetCache

function tryGetAs

template <typename T >
T const * tryGetAs() const

Attempt to retrieve the concrete storage implementation.

Template Parameters:

  • T The concrete storage type to cast to

Return: Pointer to the concrete storage, or nullptr if wrong type

Uses dynamic_cast to test whether the erased storage is of type T. Returns a const pointer to the concrete storage, or nullptr if the backend is a different type.

Usage:

if (auto* arma = wrapper.tryGetAs<ArmadilloTensorStorage>()) {
    arma::fmat const& m = arma->matrix();
    // zero-copy Armadillo access
}

function tryGetMutableAs

template <typename T >
T * tryGetMutableAs()

Mutable version of tryGetAs.

Template Parameters:

  • T The concrete storage type to cast to

Return: Pointer to the concrete storage, or nullptr if wrong type

Returns a non-const pointer to the concrete storage. Use with care — mutation through this pointer bypasses any observer notifications.

function getAsChecked

template <typename T >
T const * getAsChecked(
    TensorStorageType expected_type
) const

Type-safe cast using enum verification instead of dynamic_cast.

Parameters:

Template Parameters:

  • T The concrete storage type to cast to

Return: Pointer to the concrete storage, or nullptr if type doesn’t match

Precondition: expected_type must be the correct TensorStorageType for T. Passing the wrong enum is undefined behavior.

When TensorData (a static library) is absorbed into DataManagerIO (a shared library with hidden visibility), dynamic_cast fails across library boundaries because each module gets its own typeinfo for StorageModel<T>. This method works around that by checking the [TensorStorageType](/api_generated/Files/TensorStorageBase_8hpp.md#enum-tensorstoragetype) enum (which is value-based and works correctly across boundaries), then using static_cast.

function getMutableAsChecked

template <typename T >
T * getMutableAsChecked(
    TensorStorageType expected_type
) const

Mutable version of getAsChecked.

function sharedStorage

std::shared_ptr< void const > sharedStorage() const

Get a shared_ptr to the internal concept (for ViewTensorStorage).

Return: shared_ptr to the internal StorageConcept (may be null)

ViewTensorStorage needs to share ownership of the source storage to prevent dangling references. This method exposes the internal shared_ptr for that purpose.

function tryGetAs

template <typename T >
T const * tryGetAs() const

Template Parameters:

  • T The concrete storage type to cast to

Return: Pointer to the concrete storage, or nullptr if wrong type

Uses dynamic_cast to test whether the erased storage is of type T. Returns a const pointer to the concrete storage, or nullptr if the backend is a different type.

Usage:

if (auto* arma = wrapper.tryGetAs<ArmadilloTensorStorage>()) {
    arma::fmat const& m = arma->matrix();
    // zero-copy Armadillo access
}

function getAsChecked

template <typename T >
T const * getAsChecked(
    TensorStorageType expected_type
) const

Parameters:

Template Parameters:

  • T The concrete storage type to cast to

Return: Pointer to the concrete storage, or nullptr if type doesn’t match

Precondition: expected_type must be the correct TensorStorageType for T. Passing the wrong enum is undefined behavior.

When TensorData (a static library) is absorbed into DataManagerIO (a shared library with hidden visibility), dynamic_cast fails across library boundaries because each module gets its own typeinfo for StorageModel<T>. This method works around that by checking the [TensorStorageType](/api_generated/Files/TensorStorageBase_8hpp.md#enum-tensorstoragetype) enum (which is value-based and works correctly across boundaries), then using static_cast.


Updated on 2026-11-13 at 20:25:39 +0000