Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
GEO::AttributeStore Class Referenceabstract

Notifies a set of AttributeStoreObservers each time the stored array changes size and/or base address and/or dimension. More...

#include <geogram/basic/attributes.h>

Inheritance diagram for GEO::AttributeStore:
GEO::TypedAttributeStore< T >

Public Member Functions

 AttributeStore (index_t elemsize, index_t dim=1)
 AttributeStore constructor. More...
 
virtual ~AttributeStore ()
 AttributeStore destructor.
 
virtual bool elements_type_matches (const std::string &type_name) const =0
 Tests whether this AttributeStore stores elements of a given type. More...
 
virtual std::string element_typeid_name () const =0
 Gets the typeid name of the element type stored in this AttributeStore. More...
 
index_t size () const
 Gets the size. More...
 
index_t capacity () const
 Gets the capacity. More...
 
virtual void resize (index_t new_size)=0
 Resizes this AttributeStore. More...
 
virtual void reserve (index_t new_capacity)=0
 Reserves memory. More...
 
virtual void clear (bool keep_memory=false)=0
 Resizes this AttributeStore to 0. More...
 
bool has_observers () const
 Tests whether observers listen to this AttributeStore. More...
 
index_t dimension () const
 Gets the dimension. More...
 
virtual void redim (index_t dim)=0
 Sets the dimension. More...
 
virtual void apply_permutation (const vector< index_t > &permutation)
 Applies a permutation to the stored attributes. More...
 
virtual void compress (const vector< index_t > &old2new)
 Compresses the stored attributes, by applying an index mapping that fills-in the gaps. More...
 
virtual void zero ()
 Zeroes all the memory associated with this AttributeStore. More...
 
virtual AttributeStoreclone () const =0
 Creates a new AttributeStore that is a carbon copy of this AttributeStore. More...
 
void copy_item (index_t to, index_t from)
 Copies an item. More...
 
void zero_item (index_t to)
 Sets an item to zero. More...
 
virtual void scale_item (index_t to, double s)
 Scales an item. More...
 
virtual void madd_item (index_t to, double s, index_t from)
 Adds a scaled item to another item \detais item[to] += s * item[from]. More...
 
void swap_items (index_t i, index_t j)
 Swaps two items. More...
 
void * data ()
 Gets a pointer to the stored data. More...
 
const void * data () const
 Gets a pointer to the stored data. More...
 
size_t element_size () const
 Gets the element size. More...
 

Static Public Member Functions

static bool element_type_name_is_known (const std::string &element_type_name)
 Tests whether a given element type is registered in the system. More...
 
static bool element_typeid_name_is_known (const std::string &element_typeid_name)
 Tests whether a given element type is registered in the system. More...
 
static AttributeStorecreate_attribute_store_by_element_type_name (const std::string &element_type_name, index_t dimension)
 Creates an attribute store of a given type. More...
 
static std::string element_type_name_by_element_typeid_name (const std::string &element_typeid_name)
 Gets an element type name from its mangled name. More...
 
static std::string element_typeid_name_by_element_type_name (const std::string &element_type_name)
 Gets an element mangled type name from its C++ name. More...
 
static void register_attribute_creator (AttributeStoreCreator *creator, const std::string &element_type_name, const std::string &element_typeid_name)
 Registers a new element type. More...
 

Protected Member Functions

virtual void notify (Memory::pointer base_addr, index_t size, index_t dim)
 If size or base address differ from the cached values, notify all the observers, and update the cached base address and size. More...
 
void register_observer (AttributeStoreObserver *observer)
 Registers an observer. More...
 
void unregister_observer (AttributeStoreObserver *observer)
 Unregisters an observer. More...
 

Protected Attributes

index_t element_size_
 
index_t dimension_
 
Memory::pointer cached_base_addr_
 
index_t cached_size_
 
index_t cached_capacity_
 
std::set< AttributeStoreObserver * > observers_
 
Process::spinlock lock_
 

Static Protected Attributes

static std::map< std::string, AttributeStoreCreator_vartype_name_to_creator_
 
static std::map< std::string, std::string > typeid_name_to_type_name_
 
static std::map< std::string, std::string > type_name_to_typeid_name_
 

Friends

class AttributeStoreObserver
 

Detailed Description

Notifies a set of AttributeStoreObservers each time the stored array changes size and/or base address and/or dimension.

Definition at line 195 of file attributes.h.

Constructor & Destructor Documentation

◆ AttributeStore()

GEO::AttributeStore::AttributeStore ( index_t  elemsize,
index_t  dim = 1 
)

AttributeStore constructor.

Parameters
[in]elemsizesize of one element, in bytes.
[in]dimnumber of elements in each item. Default is 1 for standard attributes and can be greater for vector attributes.

Member Function Documentation

◆ apply_permutation()

virtual void GEO::AttributeStore::apply_permutation ( const vector< index_t > &  permutation)
virtual

Applies a permutation to the stored attributes.

Applying a permutation to the data is equivalent to:

for(i=0; i<permutation.size(); i++) {
data2[i] = data[permutation[i]]
}
data = data2 ;
void * data()
Gets a pointer to the stored data.
Definition: attributes.h:416

But it is done in-place.

Parameters
[in]permutationthe permutation. It is temporarily changed during execution of the function, but identical to the input on exit.
Note
This function uses memcpy(). If required, it can be overloaded in derived classes.

◆ capacity()

index_t GEO::AttributeStore::capacity ( ) const
inline

Gets the capacity.

Returns
the number of items that can be stored without a reallocation.

Definition at line 247 of file attributes.h.

◆ clear()

virtual void GEO::AttributeStore::clear ( bool  keep_memory = false)
pure virtual

Resizes this AttributeStore to 0.

Parameters
[in]keep_memoryif true, then memory is kept reserved for future use.

Implemented in GEO::TypedAttributeStore< T >.

◆ clone()

virtual AttributeStore* GEO::AttributeStore::clone ( ) const
pure virtual

Creates a new AttributeStore that is a carbon copy of this AttributeStore.

Only the data is copied, observers are not copied.

Implemented in GEO::TypedAttributeStore< T >.

◆ compress()

virtual void GEO::AttributeStore::compress ( const vector< index_t > &  old2new)
virtual

Compresses the stored attributes, by applying an index mapping that fills-in the gaps.

This is equivalent to:

for(i=0; i<size(); i++) {
if(old2new[i] != index_t(-1)) {
data2[old2new[i]] = data[i];
}
}
data = data2 ;
index_t size() const
Gets the size.
Definition: attributes.h:238
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
Parameters
[in]old2newthe index mapping to be applied.
Precondition
old2new[i] <= i || old2new[i] == index_t(-1)
Note
This function uses memcpy(). If required, it can be overloaded in derived classes.

◆ copy_item()

void GEO::AttributeStore::copy_item ( index_t  to,
index_t  from 
)
inline

Copies an item.

Parameters
[in]toindex of the destination item
[in]fromindex of the source item

Definition at line 363 of file attributes.h.

◆ create_attribute_store_by_element_type_name()

static AttributeStore* GEO::AttributeStore::create_attribute_store_by_element_type_name ( const std::string &  element_type_name,
index_t  dimension 
)
inlinestatic

Creates an attribute store of a given type.

Parameters
[in]element_type_namea const reference to a string with the C++ type of the elements to be stored in the attribute
[in]dimensionnumber of elements in each item

Definition at line 476 of file attributes.h.

◆ data() [1/2]

void* GEO::AttributeStore::data ( )
inline

Gets a pointer to the stored data.

Returns
A pointer to the memory block

Definition at line 416 of file attributes.h.

◆ data() [2/2]

const void* GEO::AttributeStore::data ( ) const
inline

Gets a pointer to the stored data.

Returns
A const pointer to the memory block

Definition at line 424 of file attributes.h.

◆ dimension()

index_t GEO::AttributeStore::dimension ( ) const
inline

Gets the dimension.

The dimension is 1 for standard attributes and can be greater for vector attributes.

Definition at line 285 of file attributes.h.

◆ element_size()

size_t GEO::AttributeStore::element_size ( ) const
inline

Gets the element size.

Returns
the size of an element, in bytes

Definition at line 432 of file attributes.h.

◆ element_type_name_by_element_typeid_name()

static std::string GEO::AttributeStore::element_type_name_by_element_typeid_name ( const std::string &  element_typeid_name)
inlinestatic

Gets an element type name from its mangled name.

Parameters
[in]element_typeid_namea const reference to a string with the mangled type name, as given by typeid(T).name()
Returns
a string with the C++ type name
Precondition
element_typeid_name_is_known(element_typeid_name)

Definition at line 492 of file attributes.h.

◆ element_type_name_is_known()

static bool GEO::AttributeStore::element_type_name_is_known ( const std::string &  element_type_name)
inlinestatic

Tests whether a given element type is registered in the system.

Parameters
[in]element_type_namea const reference to a string with the C++ type name
Return values
trueif the element type was registered
falseotherwise

Definition at line 444 of file attributes.h.

◆ element_typeid_name()

virtual std::string GEO::AttributeStore::element_typeid_name ( ) const
pure virtual

Gets the typeid name of the element type stored in this AttributeStore.

Returns
the typeid name, as a string.

Implemented in GEO::TypedAttributeStore< T >.

◆ element_typeid_name_by_element_type_name()

static std::string GEO::AttributeStore::element_typeid_name_by_element_type_name ( const std::string &  element_type_name)
inlinestatic

Gets an element mangled type name from its C++ name.

Parameters
[in]element_type_namea reference to a string with the C++ type name
Returns
a string with the mangled type name, as given by typeid(T).name()
Precondition
element_type_name_is_known(element_type_name)

Definition at line 507 of file attributes.h.

◆ element_typeid_name_is_known()

static bool GEO::AttributeStore::element_typeid_name_is_known ( const std::string &  element_typeid_name)
inlinestatic

Tests whether a given element type is registered in the system.

Parameters
[in]element_typeid_namea const reference to a string with the mangled type, as given by typeid(T).name()
Return values
trueif the element type was registered
falseotherwise

Definition at line 461 of file attributes.h.

◆ elements_type_matches()

virtual bool GEO::AttributeStore::elements_type_matches ( const std::string &  type_name) const
pure virtual

Tests whether this AttributeStore stores elements of a given type.

Parameters
[in]type_namethe name of the type, as given by typeid(T).name()
Return values
trueif this AttributeStore stores elements of type type_name
falseotherwise

Implemented in GEO::TypedAttributeStore< T >.

◆ has_observers()

bool GEO::AttributeStore::has_observers ( ) const
inline

Tests whether observers listen to this AttributeStore.

Return values
trueif at least one observer is bound to this AttributeStore
falseotherwise

Definition at line 276 of file attributes.h.

◆ madd_item()

virtual void GEO::AttributeStore::madd_item ( index_t  to,
double  s,
index_t  from 
)
virtual

Adds a scaled item to another item \detais item[to] += s * item[from].

Parameters
[in]tothe item
[in]sthe scaling factor
[in]fromthe item to be scaled and added

default implementation does nothing

Reimplemented in GEO::TypedAttributeStore< T >.

◆ notify()

virtual void GEO::AttributeStore::notify ( Memory::pointer  base_addr,
index_t  size,
index_t  dim 
)
protectedvirtual

If size or base address differ from the cached values, notify all the observers, and update the cached base address and size.

Parameters
[in]base_addrthe new base address
[in]sizethe new size
[in]dimthe new dimension

Reimplemented in GEO::TypedAttributeStore< T >.

◆ redim()

virtual void GEO::AttributeStore::redim ( index_t  dim)
pure virtual

Sets the dimension.

The dimension is 1 for standard attributes and can be greater for vector attributes. The existing fields are kept. If the new dimension is greater than the old one, then new fields are initialized to the default value for the attribute type.

Parameters
[in]dimthe new dimension

Implemented in GEO::TypedAttributeStore< T >.

◆ register_attribute_creator()

static void GEO::AttributeStore::register_attribute_creator ( AttributeStoreCreator creator,
const std::string &  element_type_name,
const std::string &  element_typeid_name 
)
inlinestatic

Registers a new element type.

Note
Internal use function, one should use geo_register_attribute_type instead
Parameters
[in]creatora pointer to the AttributeStoreCreator
[in]element_type_namea const reference to a string with the C++ type name of the elements
[in]element_typeid_namea const reference to a string with the mangled type name of the elements, as given by typeid(T).name()

Definition at line 524 of file attributes.h.

◆ register_observer()

void GEO::AttributeStore::register_observer ( AttributeStoreObserver observer)
protected

Registers an observer.

All the registered observers are notified whenever the size or base pointer in this AttributeStore change. The function is thread-safe.

Parameters
[in]observerthe AttributeStoreObserver to be registered.

◆ reserve()

virtual void GEO::AttributeStore::reserve ( index_t  new_capacity)
pure virtual

Reserves memory.

Parameters
[in]new_capacitytotal number of items to be stored.

Implemented in GEO::TypedAttributeStore< T >.

◆ resize()

virtual void GEO::AttributeStore::resize ( index_t  new_size)
pure virtual

Resizes this AttributeStore.

Parameters
[in]new_sizenew number of items

Implemented in GEO::TypedAttributeStore< T >.

◆ scale_item()

virtual void GEO::AttributeStore::scale_item ( index_t  to,
double  s 
)
virtual

Scales an item.

item[to] *= s

Parameters
[in]tothe index of the item
[in]sthe scaling factor

default implementation does nothing

Reimplemented in GEO::TypedAttributeStore< T >.

◆ size()

index_t GEO::AttributeStore::size ( ) const
inline

Gets the size.

Returns
the number of items

Definition at line 238 of file attributes.h.

◆ swap_items()

void GEO::AttributeStore::swap_items ( index_t  i,
index_t  j 
)

Swaps two items.

Parameters
[in]i,jthe indices of the items to be swapped

◆ unregister_observer()

void GEO::AttributeStore::unregister_observer ( AttributeStoreObserver observer)
protected

Unregisters an observer.

Parameters
[in]observerthe AttributeStoreObserver to be unregistered. The function is thread-safe.
Precondition
observer is registered.

◆ zero()

virtual void GEO::AttributeStore::zero ( )
virtual

Zeroes all the memory associated with this AttributeStore.

Subclasses may overload this function for attributes that have non "plain ordinary datatypes" and that need a more elaborate initialization mechanism.

◆ zero_item()

void GEO::AttributeStore::zero_item ( index_t  to)
inline

Sets an item to zero.

Parameters
[in]toindex of the item

Definition at line 378 of file attributes.h.


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