Lagrange
Miscellaneous

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

Modules

 function_ref
 A lightweight non-owning reference to a callable.
 

Namespaces

namespace  lagrange
 Main namespace for Lagrange.
 

Classes

class  copy_on_write_ptr< T >
 A handle type with copy-on-write semantics. 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. More...
 
using extent_t = span_CONFIG_EXTENT_TYPE
 Span extent type.
 

Functions

LA_CORE_API void enable_fpe ()
 Enable floating-point exceptions (useful for debugging). More...
 
LA_CORE_API void disable_fpe ()
 Disable previously-enabled fpe. More...
 
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. More...
 
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. More...
 
template<typename Index >
internal::Range< Index > range (Index end)
 Returns an iterable object representing the range [0, end). More...
 
template<typename Index >
internal::Range< Index > range (Index begin, Index end)
 Returns an iterable object representing the range [begin, end). More...
 
template<typename Index >
internal::SparseRange< Index > range_sparse (Index max, const std::vector< Index > &active)
 Returns an iterable object representing a subset of the range [0, max). More...
 
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: More...
 
template<typename T >
constexpr T safe_cast (T value)
 Safe cast specialization for TargetType == SourceType. More...
 
template<typename TargetType >
constexpr TargetType safe_cast (bool value)
 Safe cast specialization for bool. More...
 
template<typename T , typename U >
constexpr T safe_cast_enum (const U u)
 Casting an enum to scalar and vice versa. More...
 
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. More...
 
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. More...
 
LA_CORE_API std::vector< std::string > string_split (const std::string &str, char delimiter)
 Split a std::string using a prescribed delimiter. More...
 
LA_CORE_API bool starts_with (std::string_view str, std::string_view prefix)
 Checks if the string begins with the given prefix. More...
 
LA_CORE_API bool ends_with (std::string_view str, std::string_view suffix)
 Checks if the string ends with the given suffix. More...
 
LA_CORE_API std::string to_lower (std::string str)
 Convert a string to lowercase. More...
 
LA_CORE_API std::string to_upper (std::string str)
 Convert a string to uppercase. More...
 
template<typename... Args>
std::string string_format (fmt::format_string< Args... > format, Args &&... args)
 Format args according to the format string fmt, and return the result as a string. More...
 
template<class T , class... Args>
value_ptr< T > make_value_ptr (Args &&... args)
 Helper function to create a value_ptr for a given type. More...
 

Detailed Description

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

Typedef Documentation

◆ span

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<>.

Function Documentation

◆ enable_fpe()

void enable_fpe ( )

#include <lagrange/utils/fpe.h>

Enable floating-point exceptions (useful for debugging).

◆ disable_fpe()

void disable_fpe ( )

#include <lagrange/utils/fpe.h>

Disable previously-enabled fpe.

◆ hash_combine()

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()

constexpr 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.

◆ range() [1/2]

internal::Range< Index > range ( Index  end)

#include <lagrange/utils/range.h>

Returns an iterable object representing the range [0, end).

Parameters
[in]endEnd of the range. This bound is exclusive.
Template Parameters
IndexIndex type.
Returns
Iterator for the range [0, end).

◆ range() [2/2]

internal::Range< Index > range ( Index  begin,
Index  end 
)

#include <lagrange/utils/range.h>

Returns an iterable object representing the range [begin, end).

Parameters
[in]beginStart of the range. This bound is inclusive.
[in]endEnd of the range. This bound is exclusive.
Template Parameters
IndexIndex type.
Returns
Iterator for the range [start, end).

◆ range_sparse()

internal::SparseRange< Index > range_sparse ( Index  max,
const std::vector< Index > &  active 
)

#include <lagrange/utils/range.h>

Returns an iterable object representing a subset of the range [0, max).

If active is non-empty, it will iterate through the elements of active. Otherwise, it will iterate from 0 to max.

Warning
The active vector MUST exist while the range is being used. Do NOT write code such as:
for (int i : range_sparse(n, { 0 })) { ... } // Bad
internal::SparseRange< Index > range_sparse(Index max, const std::vector< Index > &active)
Returns an iterable object representing a subset of the range [0, max).
Definition: range.h:217
as the vector will be destructed while the iterator is still running.
Parameters
[in]maxEnd of the range. This bound is exclusive.
[in]activeActive indices in the range. If empty, it will iterate through the whole range instead.
Template Parameters
IndexIndex type.
Returns
Sparse iterator for a subset of the range [0, max).

◆ safe_cast() [1/3]

constexpr 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.
Parameters
[in]valueValue to cast.
Template Parameters
TargetTypeTarget scalar type.
SourceTypeSource scalar type.
Returns
Casted value.

◆ safe_cast() [2/3]

constexpr T safe_cast ( 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]

constexpr 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()

constexpr 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()

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()

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()

std::string string_format ( fmt::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()

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.