vecs
Fast, flexible ecs in C++ with ergonomic API
Loading...
Searching...
No Matches
vecs::Optional< T > Class Template Reference

Provides an interface for interacting with optional components. More...

#include <vecs.h>

Public Types

using value_type = T
 

Public Member Functions

 Optional ()
 Constructs an empty Optional with no value.
 
 Optional (T *ptr)
 Constructs an Optional that holds a pointer to a value.
 
 operator bool () const
 Checks if the Optional contains a value.
 
T & operator* () const
 Dereferences the contained value.
 
T & get () const
 Returns a reference to the contained value.
 
T * operator-> ()
 
T * operator-> () const
 
bool has_value () const
 Checks if a value is present.
 

Detailed Description

template<typename T>
class vecs::Optional< T >

Provides an interface for interacting with optional components.

Template Parameters
TThe type of the wrapped value.

Example usage:

world.spawn().insert(123, 321.f);
world.add_system([](Query<Entity, int, Optional<float>>& query) {
for (auto [e, i, maybe_f] : query) {
printf("Entity '%zu' has int component = %i\n", e, i);
if (maybe_f) {
printf("Entity '%zu' has float component = %f\n", e, *maybe_f);
}
}
});
Provides an interface for interacting with optional components.
Definition vecs.h:1088
Represents a query for components within the ECS world.
Definition vecs.h:1270

Constructor & Destructor Documentation

◆ Optional()

template<typename T >
vecs::Optional< T >::Optional ( T * ptr)
inlineexplicit

Constructs an Optional that holds a pointer to a value.

Parameters
ptrA pointer to the value to be wrapped.

Member Function Documentation

◆ get()

template<typename T >
T & vecs::Optional< T >::get ( ) const
inline

Returns a reference to the contained value.

Returns
A reference to the contained value.

◆ has_value()

template<typename T >
bool vecs::Optional< T >::has_value ( ) const
inline

Checks if a value is present.

Returns
True if the Optional contains a value, false otherwise.

◆ operator bool()

template<typename T >
vecs::Optional< T >::operator bool ( ) const
inline

Checks if the Optional contains a value.

Returns
True if a value is present, false otherwise.

◆ operator*()

template<typename T >
T & vecs::Optional< T >::operator* ( ) const
inline

Dereferences the contained value.

Returns
A reference to the contained value.

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