Lagrange
Loading...
Searching...
No Matches
Miscellaneous

Useful functions that don't have their place anywhere else. More...

Namespaces

namespace  lagrange
 Main namespace for Lagrange.
 

Classes

class  copy_on_write_ptr< T >
 A handle type with copy-on-write semantics. More...
 
class  function_ref< F >
 A lightweight non-owning reference to a callable. More...
 
class  function_ref< R(Args...)>
 Specialization for function types. More...
 
struct  OrderedPairHash< T, Enable >
 Compute an order-dependent hash of a pair of values. More...
 
struct  OrderedPairHash< std::pair< U, V > >
 
class  SharedSpan< T >
 Shared span with ownership tracking. More...
 
class  value_ptr< T, Cloner, Deleter >
 Smart pointer with value semantics. More...
 

Typedefs

template<class T, span_CONFIG_EXTENT_TYPE Extent = ::nonstd::dynamic_extent>
using span = ::nonstd::span<T, Extent>
 A bounds-safe view for sequences of objects.
 
using extent_t = span_CONFIG_EXTENT_TYPE
 Span extent type.
 
template<class T, typename Extent, typename Layout>
using mdarray = MDSPAN_IMPL_STANDARD_NAMESPACE::Experimental::mdarray<T, Extent, Layout>
 
template<typename T>
using Array3D
 
template<class T, typename Extent, typename Layout>
using mdspan = MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<T, Extent, Layout>
 
template<class IndexType, size_t... Extents>
using extents = MDSPAN_IMPL_STANDARD_NAMESPACE::extents<IndexType, Extents...>
 
template<typename IndexType, size_t Rank>
using dextents = MDSPAN_IMPL_STANDARD_NAMESPACE::dextents<IndexType, Rank>
 
using layout_left = MDSPAN_IMPL_STANDARD_NAMESPACE::layout_left
 
using layout_right = MDSPAN_IMPL_STANDARD_NAMESPACE::layout_right
 
using layout_stride = MDSPAN_IMPL_STANDARD_NAMESPACE::layout_stride
 
using full_extent_t = MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent_t
 
template<typename T>
using View3D = mdspan<T, dextents<size_t, 3>, layout_stride>
 

Functions

LA_CORE_API void enable_fpe ()
 Enable floating-point exceptions (useful for debugging).
 
LA_CORE_API void disable_fpe ()
 Disable previously-enabled fpe.
 
template<typename R, typename... Args>
constexpr void swap (function_ref< R(Args...)> &lhs, function_ref< R(Args...)> &rhs) noexcept
 Swaps the referred callables of lhs and rhs.
 
template<typename R, typename... Args>
 function_ref (R(*)(Args...)) -> function_ref< R(Args...)>
 Deduce function_ref type from a function pointer.
 
template<class T>
void hash_combine (size_t &seed, const T &v)
 Hash an object v and combine it with an existing hash value seed.
 
template<typename T>
constexpr T invalid ()
 You can use invalid<T>() to get a value that can represent "invalid" values, such as invalid indices or invalid float data.
 
template<typename TargetType, typename SourceType>
constexpr auto safe_cast (SourceType value) -> std::enable_if_t<!std::is_same< SourceType, TargetType >::value, TargetType >
 Perform safe cast from SourceType to TargetType, where "safe" means:
 
template<typename T>
constexpr T safe_cast (T value)
 Safe cast specialization for TargetType == SourceType.
 
template<typename TargetType>
constexpr TargetType safe_cast (bool value)
 Safe cast specialization for bool.
 
template<typename T, typename U>
constexpr T safe_cast_enum (const U u)
 Casting an enum to scalar and vice versa.
 
template<typename Callback>
sg_detail::scope_guard< Callback > make_scope_guard (Callback &&callback) noexcept(std::is_nothrow_constructible< Callback, Callback && >::value)
 Creates a scope guard around a callable object.
 
template<typename T, typename Y>
SharedSpan< T > make_shared_span (const std::shared_ptr< Y > &r, T *element_ptr, size_t size)
 Created a SharedSpan object around an internal buffer of a parent object.
 
LA_CORE_API std::vector< std::string > string_split (const std::string &str, char delimiter)
 Split a std::string using a prescribed delimiter.
 
LA_CORE_API bool starts_with (std::string_view str, std::string_view prefix)
 Checks if the string begins with the given prefix.
 
LA_CORE_API bool ends_with (std::string_view str, std::string_view suffix)
 Checks if the string ends with the given suffix.
 
LA_CORE_API std::string to_lower (std::string str)
 Convert a string to lowercase.
 
LA_CORE_API std::string to_upper (std::string str)
 Convert a string to uppercase.
 
template<typename... Args>
std::string string_format (lagrange::format_string< Args... > format, Args &&... args)
 Format args according to the format string fmt, and return the result as a string.
 
template<class T, class... Args>
value_ptr< T > make_value_ptr (Args &&... args)
 Helper function to create a value_ptr for a given type.
 
template<typename T>
Array3D< T > create_image (size_t width, size_t height, size_t num_channels)
 Create an image with the given dimensions and number of channels.
 

Variables

constexpr auto dynamic_extent = MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent
 

Detailed Description

Useful functions that don't have their place anywhere else.

Typedef Documentation

◆ span

template<class T, span_CONFIG_EXTENT_TYPE Extent = ::nonstd::dynamic_extent>
using span = ::nonstd::span<T, Extent>

#include <lagrange/utils/span.h>

A bounds-safe view for sequences of objects.

Note
The current implementation is intended to be compatible with C++20's std::span<>.

◆ Array3D

template<typename T>
using Array3D

#include <lagrange/image/Array3D.h>

Initial value:
mdarray<
T,
MDSPAN_IMPL_STANDARD_NAMESPACE::dextents<size_t, 3>,
MDSPAN_IMPL_STANDARD_NAMESPACE::layout_stride>

Function Documentation

◆ hash_combine()

template<class T>
void hash_combine ( size_t & seed,
const T & v )

#include <lagrange/utils/hash.h>

Hash an object v and combine it with an existing hash value seed.

NOT commutative.

Copied from https://www.boost.org/doc/libs/1_64_0/boost/functional/hash/hash.hpp. SPDX-License-Identifier: BSL-1.0

◆ invalid()

template<typename T>
T invalid ( )
constexpr

#include <lagrange/utils/invalid.h>

You can use invalid<T>() to get a value that can represent "invalid" values, such as invalid indices or invalid float data.

invalid<T>() is guaranteed to always be the same value for a given type T.

This is supported for arithmetic types, and returns:

  • std::numeric_limits<T>::max() for integral types,
  • std::numeric_limits<T>::infinity() for floating point types.
Template Parameters
TType.
Returns
A value considered invalid for the given type.

◆ safe_cast() [1/3]

template<typename TargetType, typename SourceType>
auto safe_cast ( SourceType value) -> std::enable_if_t<!std::is_same<SourceType, TargetType>::value, TargetType>
constexpr

#include <lagrange/utils/safe_cast.h>

Perform safe cast from SourceType to TargetType, where "safe" means:

  • Type compatibility.
  • No over/under flow for numerical types.
  • No sign change caused by casting.
  • No large numerical error for floating point casts.

Example usage:

int x = lagrange::safe_cast<int>(-1.0); // good.
int x = lagrange::safe_cast<int>("-1.0"); // fail because incompatible types.
size_t x = lagrange::safe_cast<size_t>(-1.0); // fail because sign change.
int x = lagrange::safe_cast<int>(-1.5); // fail because truncation error.
constexpr auto safe_cast(SourceType value) -> std::enable_if_t<!std::is_same< SourceType, TargetType >::value, TargetType >
Perform safe cast from SourceType to TargetType, where "safe" means:
Definition safe_cast.h:51
Parameters
[in]valueValue to cast.
Template Parameters
TargetTypeTarget scalar type.
SourceTypeSource scalar type.
Returns
Casted value.

◆ safe_cast() [2/3]

template<typename T>
T safe_cast ( T value)
constexpr

#include <lagrange/utils/safe_cast.h>

Safe cast specialization for TargetType == SourceType.

Parameters
[in]valueValue to cast.
Template Parameters
TScalar type.
Returns
Casted value.

◆ safe_cast() [3/3]

template<typename TargetType>
TargetType safe_cast ( bool value)
constexpr

#include <lagrange/utils/safe_cast.h>

Safe cast specialization for bool.

Parameters
[in]valueValue to cast.
Template Parameters
TargetTypeTarget scalar type.
Returns
Casted value.

◆ safe_cast_enum()

template<typename T, typename U>
T safe_cast_enum ( const U u)
constexpr

#include <lagrange/utils/safe_cast.h>

Casting an enum to scalar and vice versa.

These are only to be used for assigning enums as (mesh) attributes, or to be used for reading back enums that were saved as mesh attributes.

Parameters
[in]uValue to cast.
Template Parameters
TTarget scalar/enum type.
USource scalar/enum type.
Returns
Casted value.

◆ make_scope_guard()

template<typename Callback>
sg_detail::scope_guard< Callback > make_scope_guard ( Callback && callback) const &&
noexcept

#include <lagrange/utils/scope_guard.h>

Creates a scope guard around a callable object.

A scope guard is an object that employs RAII to execute a provided callback when leaving scope, be it through a fall-through, a return, or an exception. That callback can be a function, a function pointer, a functor, a lambda, a bind result, a std::function, a reference to any of these, or any other callable, as long as it respects a few preconditions – most of which are enforced during compilation, the rest being hopefully intuitive.

Parameters
callbackCallable object to execute when leaving scope.
Template Parameters
CallbackCallable object type.
Returns
A scope guard object protecting the execution of the callable via RAII.

◆ make_shared_span()

template<typename T, typename Y>
SharedSpan< T > make_shared_span ( const std::shared_ptr< Y > & r,
T * element_ptr,
size_t size )

#include <lagrange/utils/SharedSpan.h>

Created a SharedSpan object around an internal buffer of a parent object.

Template Parameters
TThe element_type of internal buffer.
YThe type of the parent object containing the buffer.
Parameters
[in]rA shared pointer of the parent object owning the buffer.
[in]element_ptrA raw pointer to the internal buffer contained in the parent object.
[in]sizeThe size of the internal buffer.
Returns
A SharedSpan that shares the memory ownership of the internal buffer.

◆ string_split()

std::vector< std::string > string_split ( const std::string & str,
char delimiter )

#include <lagrange/utils/strings.h>

Split a std::string using a prescribed delimiter.

Parameters
[in]strString to split.
[in]delimiterDelimiter.
Returns
An array of strings obtained after splitting.

◆ starts_with()

bool starts_with ( std::string_view str,
std::string_view prefix )

#include <lagrange/utils/strings.h>

Checks if the string begins with the given prefix.

Parameters
[in]strThe string.
[in]prefixThe prefix.
Note
Can be replaced with the standard library in C++20
Returns
true if the string begins with the provided prefix, false otherwise.

◆ ends_with()

bool ends_with ( std::string_view str,
std::string_view suffix )

#include <lagrange/utils/strings.h>

Checks if the string ends with the given suffix.

Parameters
[in]strThe string.
[in]suffixThe suffix.
Note
Can be replaced with the standard library in C++20
Returns
true if the string end with the provided suffix, false otherwise.

◆ to_lower()

std::string to_lower ( std::string str)

#include <lagrange/utils/strings.h>

Convert a string to lowercase.

Parameters
[in]strThe input string.
Returns
The same string converted to lowercase.
Note
This method assumes the input string is ASCII.

◆ to_upper()

std::string to_upper ( std::string str)

#include <lagrange/utils/strings.h>

Convert a string to uppercase.

Parameters
[in]strThe input string.
Returns
The same string converted to uppercase.
Note
This method assumes the input string is ASCII.

◆ string_format()

template<typename... Args>
std::string string_format ( lagrange::format_string< Args... > format,
Args &&... args )

#include <lagrange/utils/strings.h>

Format args according to the format string fmt, and return the result as a string.

Note
Can be replaced with std::format in C++20
Parameters
[in]formatAn object that represents the format string.
[in]argsArguments to be formatted.
Template Parameters
ArgsTypes of the arguments to be formatted.
Returns
A string object holding the formatted result.

◆ make_value_ptr()

template<class T, class... Args>
value_ptr< T > make_value_ptr ( Args &&... args)

#include <lagrange/utils/value_ptr.h>

Helper function to create a value_ptr for a given type.

Parameters
argsArguments to forward to the object's constructor.
Template Parameters
TType of the object to create.
ArgsArgument types for the object's constructor.
Returns
A value_ptr of the desired type.

◆ create_image()

template<typename T>
Array3D< T > create_image ( size_t width,
size_t height,
size_t num_channels )

#include <lagrange/image/Array3D.h>

Create an image with the given dimensions and number of channels.

Parameters
[in]widthImage width.
[in]heightImage height.
[in]num_channelsNumber of channels.
Template Parameters
TImage value type.
Returns
3D array matching the prescribed shape.