Lagrange
Attribute< ValueType_ > Class Template Reference

Derived attribute class that stores the actual information. More...

#include <lagrange/Attribute.h>

Inherits AttributeBase.

Public Types

using ValueType = ValueType_
 Attribute value type.
 

Public Member Functions

template<typename SourceValueType >
Attribute< TargetValueType > cast_copy (const Attribute< SourceValueType > &source)
 
template<typename SourceValueType >
Attribute< TargetValueType > & cast_assign (const Attribute< SourceValueType > &source)
 
Attribute growth
void set_default_value (ValueType value)
 Sets the default value to use when growing the attribute. More...
 
ValueType get_default_value () const
 Gets the default value to use when growing the attribute.
 
void set_growth_policy (AttributeGrowthPolicy policy)
 Sets the growth policy for external buffers. More...
 
AttributeGrowthPolicy get_growth_policy () const
 Gets the growth policy for external buffers. More...
 
void set_shrink_policy (AttributeShrinkPolicy policy)
 Sets the shrink policy for external buffers. More...
 
AttributeShrinkPolicy get_shrink_policy () const
 Gets the shrink policy for external buffers. More...
 
void set_write_policy (AttributeWritePolicy policy)
 Sets the write policy for read-only external buffers. More...
 
AttributeWritePolicy get_write_policy () const
 Gets the write policy for read-only external buffers. More...
 
void set_copy_policy (AttributeCopyPolicy policy)
 Set copy policy for external buffers. More...
 
AttributeCopyPolicy get_copy_policy () const
 Get the copy policy for external buffers. More...
 
void create_internal_copy ()
 Creates an internal copy of the attribute data. More...
 
void clear ()
 Clears the attribute buffer (new number of elements is 0).
 
void shrink_to_fit ()
 Shrink attribute buffer to fit the current number of entries. More...
 
void reserve_entries (size_t new_cap)
 Reserve enough memory for new_cap entries. More...
 
void resize_elements (size_t num_elements)
 Resize the buffer to contain num_elements elements. More...
 
void insert_elements (span< const ValueType > values)
 Inserts values for new elements. More...
 
void insert_elements (std::initializer_list< const ValueType > values)
 Inserts values for new elements. More...
 
void insert_elements (size_t count)
 Inserts new elements. More...
 
Attribute access
bool empty () const
 Test whether the attribute is empty (its size is 0). More...
 
size_t get_num_elements () const
 Gets the number of elements. More...
 
bool is_external () const
 Checks whether an attribute buffer is external or internally managed. More...
 
bool is_managed () const
 Checks whether the attribute is managing the lifetime of the underlying buffer. More...
 
bool is_read_only () const
 Checks whether the attribute is external and pointing to a const buffer. More...
 
ValueType get (size_t i, size_t c) const
 Gets an entry for element i, at channel p. More...
 
ValueTyperef (size_t i, size_t c)
 Gets a writable reference to the entry for element i, at channel p. More...
 
ValueType get (size_t i) const
 Gets an entry for a scalar element i. More...
 
ValueTyperef (size_t i)
 Gets a writable reference to a scalar element i, at channel p. More...
 
lagrange::span< const ValueTypeget_all () const
 Returns a read-only view of the buffer spanning num elements x num channels. More...
 
lagrange::span< ValueTyperef_all ()
 Returns a writable view of the buffer spanning num elements x num channels. More...
 
lagrange::span< const ValueTypeget_first (size_t num_elements) const
 Returns a read-only view of the attribute values for the first elements in the buffer. More...
 
lagrange::span< ValueTyperef_first (size_t num_elements)
 Returns a writable view of the attribute values for the first elements in the buffer. More...
 
lagrange::span< const ValueTypeget_last (size_t num_elements) const
 Returns a read-only view of the attribute values for the last elements in the buffer. More...
 
lagrange::span< ValueTyperef_last (size_t num_elements)
 Returns a writable view of the attribute values for the last elements in the buffer. More...
 
lagrange::span< const ValueTypeget_middle (size_t first_element, size_t num_elements) const
 Returns a read-only view of the attribute values for the middle elements in the buffer. More...
 
lagrange::span< ValueTyperef_middle (size_t first_element, size_t num_elements)
 Returns a writable view of the attribute values for the middle elements in the buffer. More...
 
lagrange::span< const ValueTypeget_row (size_t element) const
 Returns a read-only view of the attribute values for the element in the buffer. More...
 
lagrange::span< ValueTyperef_row (size_t element)
 Returns a writable view of the attribute values for the element in the buffer. More...
 
- Public Member Functions inherited from AttributeBase
 AttributeBase (AttributeElement element, AttributeUsage usage, size_t num_channels)
 Constructs a new instance. More...
 
virtual ~AttributeBase ()
 Destroys the object.
 
 AttributeBase (AttributeBase &&other)=default
 Move constructor. More...
 
AttributeBaseoperator= (AttributeBase &&other)=default
 Assignment move operator. More...
 
 AttributeBase (const AttributeBase &other)=default
 Copy constructor. More...
 
AttributeBaseoperator= (const AttributeBase &other)=default
 Assignment copy operator. More...
 
virtual AttributeValueType get_value_type () const =0
 Gets the attribute value type. More...
 
AttributeElement get_element_type () const
 Gets the attribute element type. More...
 
AttributeUsage get_usage () const
 Gets the attribute usage tag. More...
 
size_t get_num_channels () const
 Gets the number of channels for the attribute. More...
 
void unsafe_set_usage (AttributeUsage usage)
 Sets the attribute usage tag. More...
 
void unsafe_set_element_type (AttributeElement element)
 Sets the attribute element type. More...
 

Static Public Attributes

static constexpr bool IsIndexed = false
 Whether this attribute type is indexed.
 

Protected Attributes

std::vector< ValueTypem_data
 Internal buffer storing the data (when the attribute is not external).
 
std::shared_ptr< const ValueTypem_owner
 Optional aliased ptr to extend the lifetime of memory owner object of external buffer.
 
ValueType m_default_value = ValueType(0)
 Default values used to populate buffer when the attribute grows.
 
lagrange::span< ValueTypem_view
 Writable pointer to the buffer storing the attribute data. More...
 
lagrange::span< const ValueTypem_const_view
 Read-only pointer to the buffer storing the attribute data. More...
 
AttributeGrowthPolicy m_growth_policy = AttributeGrowthPolicy::ErrorIfExternal
 Growth policy for external buffers.
 
AttributeShrinkPolicy m_shrink_policy = AttributeShrinkPolicy::ErrorIfExternal
 Shrink policy for external buffers.
 
AttributeWritePolicy m_write_policy = AttributeWritePolicy::ErrorIfReadOnly
 Policy for write access to read-only external buffers.
 
AttributeCopyPolicy m_copy_policy = AttributeCopyPolicy::CopyIfExternal
 Copy policy for external buffers.
 
bool m_is_external = false
 Flag to determine whether an attribute is using an external or internal buffer. More...
 
bool m_is_read_only = false
 Flag to determine whether an external attribute is read-only or writable. More...
 
size_t m_num_elements = 0
 Number of elements associated with the attribute.
 
- Protected Attributes inherited from AttributeBase
AttributeElement m_element
 Element type (vertex, facet, indexed, etc.).
 
AttributeUsage m_usage
 Attribute usage tag.
 
size_t m_num_channels = 0
 Number of channel for each value.
 

Attribute construction

 Attribute (AttributeElement element, AttributeUsage usage, size_t num_channels)
 Constructs a new instance. More...
 
 ~Attribute () override
 Destroys the object.
 
 Attribute (Attribute &&other) noexcept
 Move constructor. More...
 
Attributeoperator= (Attribute &&other) noexcept
 Assignment move operator. More...
 
 Attribute (const Attribute &other)
 Copy constructor. More...
 
Attributeoperator= (const Attribute &other)
 Assignment copy operator. More...
 
template<typename OtherValue >
Attributecast_assign (const Attribute< OtherValue > &other)
 Cast assignment operator. More...
 
AttributeValueType get_value_type () const override
 Gets the attribute value type. More...
 
void wrap (span< ValueType > buffer, size_t num_elements)
 Wraps an external buffer into the attribute. More...
 
void wrap (SharedSpan< ValueType > buffer_ptr, size_t num_elements)
 Wraps an external buffer into the attribute. More...
 
void wrap_const (span< const ValueType > buffer, size_t num_elements)
 Wraps a const external buffer into the attribute. More...
 
void wrap_const (SharedSpan< const ValueType > shared_buffer, size_t num_elements)
 Wraps a const external buffer into the attribute. More...
 
template<typename OtherValue >
static Attribute cast_copy (const Attribute< OtherValue > &other)
 Cast copy operator. More...
 

Detailed Description

template<typename ValueType_>
class lagrange::Attribute< ValueType_ >

Derived attribute class that stores the actual information.

Template Parameters
ValueType_Attribute value type.

Constructor & Destructor Documentation

◆ Attribute() [1/3]

Attribute ( AttributeElement  element,
AttributeUsage  usage,
size_t  num_channels 
)

Constructs a new instance.

Parameters
[in]elementElement type (vertex, facet, etc.).
[in]usageUsage tag.
[in]num_channelsNumber of channels.

◆ Attribute() [2/3]

Attribute ( Attribute< ValueType_ > &&  other)
noexcept

Move constructor.

Parameters
otherInstance to move from.

◆ Attribute() [3/3]

Attribute ( const Attribute< ValueType_ > &  other)
explicit

Copy constructor.

Parameters
[in]otherInstance to copy from.

Member Function Documentation

◆ operator=() [1/2]

Attribute & operator= ( Attribute< ValueType_ > &&  other)
noexcept

Assignment move operator.

Parameters
otherInstance to move from.
Returns
The result of the assignment.

◆ operator=() [2/2]

Attribute & operator= ( const Attribute< ValueType_ > &  other)

Assignment copy operator.

Parameters
[in]otherInstance to copy from.
Returns
The result of the assignment.

◆ cast_copy()

static Attribute cast_copy ( const Attribute< OtherValue > &  other)
static

Cast copy operator.

Creates an attribute by copying and casting values from another attribute with a different ValueType. Will print a warning if the source and target value types are identical.

Parameters
[in]otherThe other attribute to copy & cast from.
Template Parameters
OtherValueValue type for the other attribute.
Returns
A new attribute object whose values have been cast to the desired type.

◆ cast_assign()

Attribute & cast_assign ( const Attribute< OtherValue > &  other)

Cast assignment operator.

Replace the current attribute by copying and casting values from another attribute with a different ValueType. Will print a warning if the source and target value types are identical.

Parameters
[in]otherThe other attribute to copy & cast from.
Template Parameters
OtherValueValue type for the other attribute.
Returns
A reference to the assignment result.

◆ get_value_type()

AttributeValueType get_value_type ( ) const
overridevirtual

Gets the attribute value type.

Returns
An enum describing the value type.

Implements AttributeBase.

◆ wrap() [1/2]

void wrap ( lagrange::span< ValueType buffer,
size_t  num_elements 
)

Wraps an external buffer into the attribute.

The pointer must remain valid for the lifetime of the attribute. Note that only the number of element is allowed to change when wrapping an external buffer (the number of channels is fixed during the attribute construction).

Parameters
[in]bufferPointer to an external buffer to be used as storage. The pointed buffer must have a capacity (determined by span::size()) that is large enough to store num elements x num channels entries. The pointed buffer can be larger than required, in which case the padding capacity can be used to grow the attribute, as determined by the AttributeGrowthPolicy.
[in]num_elementsNew number of elements associated with the attribute.

◆ wrap() [2/2]

void wrap ( SharedSpan< ValueType buffer_ptr,
size_t  num_elements 
)

Wraps an external buffer into the attribute.

The buffer ownership is shared with the attribute. Note that only the number of element is allowed to change when wrapping an external buffer (the number of channels is fixed during the attribute construction).

Parameters
[in]shared_bufferPointer to an external buffer managed by a SharedSpan to be used as storage. This pointer exposes a view of the buffer managed by the owner. The buffer must have a capacity (determined by buffer_ptr.size()) that is large enough to store num elements x num channels entries. The buffer can be larger than required, in which case the padding capacity can be used to grow the attribute, as determined by the AttributeGrowthPolicy.
[in]num_elementsNew number of elements associated with the attribute.

◆ wrap_const() [1/2]

void wrap_const ( lagrange::span< const ValueType buffer,
size_t  num_elements 
)

Wraps a const external buffer into the attribute.

The pointer must remain valid for the lifetime of the attribute. Note that only the number of element is allowed to change when wrapping an external buffer (the number of channels is fixed during the attribute construction). Following this, any write operation to the attribute will throw an exception.

Parameters
[in]bufferPointer to an external buffer to be used as storage. The pointed buffer must have a capacity (determined by span::size()) that is large enough to store num elements x num channels entries. The pointed buffer can be larger than required, in which case the padding capacity can be used to grow the attribute, as determined by the AttributeGrowthPolicy.
[in]num_elementsNew number of elements associated with the attribute.

◆ wrap_const() [2/2]

void wrap_const ( SharedSpan< const ValueType shared_buffer,
size_t  num_elements 
)

Wraps a const external buffer into the attribute.

The buffer ownership is shared with the attribute. Note that only the number of element is allowed to change when wrapping an external buffer (the number of channels is fixed during the attribute construction).

Parameters
[in]shared_bufferPointer to an external buffer managed by a SharedSpan to be used as storage. This pointer exposes a view of the buffer managed by the owner. The buffer must have a capacity (determined by shared_buffer.size()) that is large enough to store num elements x num channels entries. The buffer can be larger than required, in which case the padding capacity can be used to grow the attribute, as determined by the AttributeGrowthPolicy.
[in]num_elementsNew number of elements associated with the attribute.

◆ set_default_value()

void set_default_value ( ValueType  value)
inline

Sets the default value to use when growing the attribute.

Parameters
[in]valueNew default value.

◆ set_growth_policy()

void set_growth_policy ( AttributeGrowthPolicy  policy)
inline

Sets the growth policy for external buffers.

Parameters
[in]policyNew policy.

◆ get_growth_policy()

AttributeGrowthPolicy get_growth_policy ( ) const
inline

Gets the growth policy for external buffers.

Returns
The growth policy.

◆ set_shrink_policy()

void set_shrink_policy ( AttributeShrinkPolicy  policy)
inline

Sets the shrink policy for external buffers.

Parameters
[in]policyNew policy.

◆ get_shrink_policy()

AttributeShrinkPolicy get_shrink_policy ( ) const
inline

Gets the shrink policy for external buffers.

Returns
The shrink policy.

◆ set_write_policy()

void set_write_policy ( AttributeWritePolicy  policy)
inline

Sets the write policy for read-only external buffers.

Parameters
[in]policyNew policy.

◆ get_write_policy()

AttributeWritePolicy get_write_policy ( ) const
inline

Gets the write policy for read-only external buffers.

Returns
The attribute write policy.

◆ set_copy_policy()

void set_copy_policy ( AttributeCopyPolicy  policy)
inline

Set copy policy for external buffers.

Parameters
[in]policyNew policy

◆ get_copy_policy()

AttributeCopyPolicy get_copy_policy ( ) const
inline

Get the copy policy for external buffers.

Returns
The attribute copy policy.

◆ create_internal_copy()

void create_internal_copy

Creates an internal copy of the attribute data.

The attribute buffer must be external before calling this function. An internal copy of the buffer is created (including padding size). This can be used prior to attribute export to ensure lifetime of the underlying object.

◆ shrink_to_fit()

void shrink_to_fit

Shrink attribute buffer to fit the current number of entries.

If the attribute points to an external buffer, an internal copy will be created if the external buffer capacity exceeds the number of entries in the attribute.

◆ reserve_entries()

void reserve_entries ( size_t  new_cap)

Reserve enough memory for new_cap entries.

The new capacity does not need to be a multiple of the number of channels (e.g. one may want to pad the last vertex coordinate with a single value).

Parameters
[in]new_capThe new buffer capacity.

◆ resize_elements()

void resize_elements ( size_t  num_elements)

Resize the buffer to contain num_elements elements.

New attribute entries will be zero-initialized.

Parameters
[in]num_elementsNew number of elements.

◆ insert_elements() [1/3]

void insert_elements ( lagrange::span< const ValueType values)

Inserts values for new elements.

Parameters
[in]valuesValues to be inserted. The span size must be a multiple of number of channels for the attribute.

◆ insert_elements() [2/3]

void insert_elements ( std::initializer_list< const ValueType values)

Inserts values for new elements.

Parameters
[in]valuesValues to be inserted. The span size must be a multiple of number of channels for the attribute.

◆ insert_elements() [3/3]

void insert_elements ( size_t  count)

Inserts new elements.

Use set_default_value to use a non-zero default value to initialize the new elements.

See also
set_default_value
Parameters
[in]countThe number elements to insert.

◆ empty()

bool empty ( ) const
inline

Test whether the attribute is empty (its size is 0).

Returns
Whether the attribute is empty.

◆ get_num_elements()

size_t get_num_elements ( ) const
inline

Gets the number of elements.

Returns
The number of elements.

◆ is_external()

bool is_external ( ) const
inline

Checks whether an attribute buffer is external or internally managed.

External buffers must remain valid for the lifetime of the attribute object.

Returns
True if the buffer is external, False otherwise.

◆ is_managed()

bool is_managed ( ) const
inline

Checks whether the attribute is managing the lifetime of the underlying buffer.

This is true for internal attributes (the buffer is allocated by the attribute itself), or for external attributes created by wrapping a SharedSpan object.

Returns
True if the buffer is managed, False otherwise.

◆ is_read_only()

bool is_read_only ( ) const
inline

Checks whether the attribute is external and pointing to a const buffer.

If the attribute data is internally managed, this always returns false.

Returns
True if the data is read only, False otherwise.

◆ get() [1/2]

ValueType get ( size_t  i,
size_t  c 
) const

Gets an entry for element i, at channel p.

Note that due to internal checks, it is more efficient to first retrieve a span() of the whole buffer, then do access operations on this span inside a for loop.

Parameters
[in]iElement to access.
[in]cChannel to access.
Returns
Value of the element attribute for the given channel.

◆ ref() [1/2]

ValueType & ref ( size_t  i,
size_t  c 
)

Gets a writable reference to the entry for element i, at channel p.

Note that due to internal checks, it is more efficient to first retrieve a span() of the whole buffer, then do access operations on this span inside a for loop.

Parameters
[in]iElement to access.
[in]cChannel to access.
Returns
Reference to the element attribute for the given channel.

◆ get() [2/2]

ValueType get ( size_t  i) const

Gets an entry for a scalar element i.

Note that due to internal checks, it is more efficient to first retrieve a span() of the whole buffer, then do access operations on this span inside a for loop.

Parameters
[in]iElement to access.
Returns
Value of the element attribute.

◆ ref() [2/2]

ValueType & ref ( size_t  i)

Gets a writable reference to a scalar element i, at channel p.

Note that due to internal checks, it is more efficient to first retrieve a span() of the whole buffer, then do access operations on this span inside a for loop.

Parameters
[in]iElement to access.
Returns
Reference to the element attribute.

◆ get_all()

lagrange::span< const ValueType > get_all

Returns a read-only view of the buffer spanning num elements x num channels.

The actual buffer may have a larger capacity (e.g. used for padding).

Returns
A read-only view of the attribute buffer.

◆ ref_all()

Returns a writable view of the buffer spanning num elements x num channels.

The actual buffer may have a larger capacity (e.g. used for padding).

Returns
A writable view of the attribute buffer.

◆ get_first()

lagrange::span< const ValueType > get_first ( size_t  num_elements) const

Returns a read-only view of the attribute values for the first elements in the buffer.

Parameters
[in]num_elementsNumber of elements to retrieve.
Returns
A read-only view of the attribute values.

◆ ref_first()

lagrange::span< ValueType > ref_first ( size_t  num_elements)

Returns a writable view of the attribute values for the first elements in the buffer.

Parameters
[in]num_elementsNumber of elements to retrieve.
Returns
A writable view of the attribute values.

◆ get_last()

lagrange::span< const ValueType > get_last ( size_t  num_elements) const

Returns a read-only view of the attribute values for the last elements in the buffer.

Parameters
[in]num_elementsNumber of elements to retrieve.
Returns
A read-only view of the attribute values.

◆ ref_last()

lagrange::span< ValueType > ref_last ( size_t  num_elements)

Returns a writable view of the attribute values for the last elements in the buffer.

Parameters
[in]num_elementsNumber of elements to retrieve.
Returns
A writable view of the attribute values.

◆ get_middle()

lagrange::span< const ValueType > get_middle ( size_t  first_element,
size_t  num_elements 
) const

Returns a read-only view of the attribute values for the middle elements in the buffer.

Parameters
[in]first_elementIndex of the first element to retrieve.
[in]num_elementsNumber of elements to retrieve.
Returns
A read-only view of the attribute values.

◆ ref_middle()

lagrange::span< ValueType > ref_middle ( size_t  first_element,
size_t  num_elements 
)

Returns a writable view of the attribute values for the middle elements in the buffer.

Parameters
[in]first_elementIndex of the first element to retrieve.
[in]num_elementsNumber of elements to retrieve.
Returns
A writable view of the attribute values.

◆ get_row()

lagrange::span< const ValueType > get_row ( size_t  element) const

Returns a read-only view of the attribute values for the element in the buffer.

Parameters
[in]elementIndex of the element to retrieve.
Returns
A read-only view of the attribute values.

◆ ref_row()

lagrange::span< ValueType > ref_row ( size_t  element)

Returns a writable view of the attribute values for the element in the buffer.

Parameters
[in]elementIndex of the first element to retrieve.
Returns
A writable view of the attribute values.

Member Data Documentation

◆ m_view

lagrange::span<ValueType> m_view
protected

Writable pointer to the buffer storing the attribute data.

For internal buffers, this will point to m_data.data(). For external buffers, it will be empty() if we are wrapping a read-only buffer, and point to the external data otherwise.

◆ m_const_view

lagrange::span<const ValueType> m_const_view
protected

Read-only pointer to the buffer storing the attribute data.

For internal buffers, this will point to m_data.data(). For external buffers, it will point to the external data.

◆ m_is_external

bool m_is_external = false
protected

Flag to determine whether an attribute is using an external or internal buffer.

We need this flag to distinguish if an empty buffer is internally managed or external.

◆ m_is_read_only

bool m_is_read_only = false
protected

Flag to determine whether an external attribute is read-only or writable.

We need this to distinguish whether a pointer to an empty buffer is read-only or "writable" (in the sense that a call to ref_all() should not produce an error).


The documentation for this class was generated from the following files: