Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Infinity::Util Namespace Reference

Classes

class  RuntimeUtils
 Utility for detecting available scripting runtime bridges. More...
 
class  StringUtils
 Utility functions for string manipulation and parsing. More...
 

Typedefs

template<class T , class E >
using Expected = tl::expected< T, E >
 Type-safe error handling using the expected/unexpected pattern.
 
template<class E >
using Unexpected = tl::unexpected< E >
 Represents an error value for Expected<T, E>.
 

Functions

template<class E >
auto make_unexpected (E &&e) -> Unexpected< typename std::decay< E >::type >
 Constructs an Unexpected error value.
 

Typedef Documentation

◆ Expected

template<class T , class E >
Infinity::Util::Expected

Type-safe error handling using the expected/unexpected pattern.

Expected<T, E> represents a value that may either contain a successful result of type T or an error of type E. This provides a type-safe alternative to exceptions for error handling, making error paths explicit in the type system.

This is a thin wrapper around tl::expected, providing the standard expected/unexpected pattern for the Infinity Engine. It enables:

  • Explicit error handling in function signatures
  • Composable error propagation
  • Zero-overhead error handling (no exceptions)
  • Clear distinction between success and failure paths
  • Type-safe error information

Common patterns:

  • Return Expected<T, ErrorType> from fallible operations
  • Use .has_value() or operator bool() to check for success
  • Access value with .value() or operator*
  • Access error with .error()
  • Chain operations with .and_then() or .or_else()
Template Parameters
TType of the successful value.
EType of the error value.
Note
This wraps tl::expected - see tl::expected documentation for full API.
Accessing .value() on an error state is undefined behavior.
Always check has_value() before accessing the value.

Example usage:

// Function returning expected result
Expected<Mesh, std::string> loadMesh(const std::string& path) {
if (!fileExists(path)) {
return make_unexpected("File not found: " + path);
}
Mesh mesh;
if (!parseMeshFile(path, mesh)) {
return make_unexpected("Failed to parse mesh: " + path);
}
return mesh; // Success
}
// Using the result
auto result = loadMesh("models/tree.mesh");
if (result.has_value()) {
Mesh& mesh = result.value();
// Use mesh...
} else {
std::cerr << "Error: " << result.error() << std::endl;
}
// Shorter form with operator bool and operator*
if (result) {
Mesh& mesh = *result;
// Use mesh...
}
// Chaining operations
auto texturedMesh = loadMesh("model.mesh")
.and_then([](Mesh& mesh) -> Expected<Mesh, std::string> {
return applyTextures(mesh);
})
.or_else([](const std::string& error) -> Expected<Mesh, std::string> {
return loadFallbackMesh();
});
// Error types can be custom structs
struct LoadError {
enum class Type { FileNotFound, ParseFailed, InvalidFormat };
Type type;
std::string details;
};
Expected<Material, LoadError> loadMaterial(const std::string& path) {
if (!fileExists(path)) {
return make_unexpected(LoadError{
LoadError::Type::FileNotFound,
"Material file not found: " + path
});
}
// ...
}
tl::expected< T, E > Expected
Type-safe error handling using the expected/unexpected pattern.
Definition Expected.hpp:99
auto make_unexpected(E &&e) -> Unexpected< typename std::decay< E >::type >
Constructs an Unexpected error value.
Definition Expected.hpp:133

◆ Unexpected

template<class E >
Infinity::Util::Unexpected

Represents an error value for Expected<T, E>.

Unexpected<E> wraps an error value to be returned from functions that return Expected<T, E>. Use make_unexpected() to construct.

Template Parameters
EType of the error value.

Function Documentation

◆ make_unexpected()

template<class E >
auto Infinity::Util::make_unexpected ( E &&  e) -> Unexpected<typename std::decay<E>::type>

Constructs an Unexpected error value.

Helper function to create an Unexpected<E> for returning errors from functions that return Expected<T, E>.

Template Parameters
EType of the error value (automatically deduced).
Parameters
eError value to wrap.
Returns
Unexpected<E> containing the error.
Expected<int, std::string> divide(int a, int b) {
if (b == 0) {
return make_unexpected("Division by zero");
}
return a / b;
}