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
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
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:
- StorageImpl A type inheriting from TensorStorageBase
function TensorStorageWrapper
TensorStorageWrapper() =defaultDefault 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 &
) =defaultCopy constructor (shared ownership, O(1)).
function operator=
TensorStorageWrapper & operator=(
TensorStorageWrapper const &
) =defaultCopy assignment (shared ownership, O(1)).
function TensorStorageWrapper
TensorStorageWrapper(
TensorStorageWrapper &&
) =defaultMove constructor.
function operator=
TensorStorageWrapper & operator=(
TensorStorageWrapper &&
) =defaultMove assignment.
function ~TensorStorageWrapper
~TensorStorageWrapper() =defaultfunction isValid
bool isValid() constCheck 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
) constGet a single float value by multi-dimensional index.
function flatData
std::span< float const > flatData() constGet a span over the flat (row-major) data.
function sliceAlongAxis
std::vector< float > sliceAlongAxis(
std::size_t axis,
std::size_t index
) constExtract a sub-tensor by fixing one axis to a single index.
function getColumn
std::vector< float > getColumn(
std::size_t col
) constGet a single column (axis 1 slice) across all rows.
function shape
std::vector< std::size_t > shape() constGet the shape as a vector of sizes (one per axis).
function totalElements
std::size_t totalElements() constGet the total number of elements (product of shape).
function isContiguous
bool isContiguous() constWhether the underlying data is contiguous in memory.
function getStorageType
TensorStorageType getStorageType() constGet the storage type identifier.
function tryGetCache
TensorStorageCache tryGetCache() constTry to get cached pointers for fast-path access.
function tryGetAs
template <typename T >
T const * tryGetAs() constAttempt 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
) constType-safe cast using enum verification instead of dynamic_cast.
Parameters:
- expected_type The TensorStorageType enum value corresponding to T
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
) constMutable version of getAsChecked.
function tryGetAs
template <typename T >
T const * tryGetAs() constTemplate 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
) constParameters:
- expected_type The TensorStorageType enum value corresponding to T
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