Loading...
Searching...
No Matches
by::tmay< T > Class Template Reference

Optional value wrapper for error indication without exceptions. More...

#include <tmay.hpp>

Inheritance diagram for by::tmay< T >:
by::tres< T, R >

Public Member Functions

 tmay (const T &value)
 
 tmay (const tmedium< T > &value)
 
Toperator-> ()
 
const Toperator-> () const BY_CONST_FUNC(operator->())
 
Toperator* ()
 
const Toperator* () const BY_CONST_FUNC(operator*())
 
 operator T& ()
 
 operator const T & () const
 
 operator nbool () const
 
nbool has () const
 
Tget ()
 Gets the contained value.
 
const Tget () const BY_CONST_FUNC(get()) virtual void rel()
 Releases/clears the contained value, making the instance empty.
 
virtual void set (const T &arg)
 

Detailed Description

template<typename T>
class by::tmay< T >

Optional value wrapper for error indication without exceptions.

Provides a way to indicate errors in return-by-value functions without using slow exceptions. Functionally almost identical to std::optional<T>, but with API naming adapted to byeol project conventions.

For normal operation, pass the value as T& to the constructor. For error situations, pass a tmedium class created as nullptr.

Remarks
Why use tmedium? See tmedium for details on why this intermediate type is used.
Common naming conventions The functions has(), get(), rel(), and set() follow naming conventions used throughout the entire project.

Usage

Example of using tmay for error handling:

tmay<std::string> loadConfig(const std::string& path) {
if(!fileExists(path))
return tmay<std::string>(); // Return empty for error
std::string content = readFile(path);
return tmay<std::string>(content); // Return value for success
}
auto config = loadConfig("config.txt");
if(config.has()) {
std::string& value = config.get();
}
Rich logging support with polymorphic type conversion.
Definition richLog.hpp:34
See also
Use tstr<T> or tweak<T> for return-by-reference cases

Member Function Documentation

◆ get()

template<typename T >
T * by::tmay< T >::get ( )

Gets the contained value.

Warning
Calling get() on an empty tmay instance results in undefined behavior. It is recommended to check with has() before calling this.
Returns
Pointer to the contained value.

◆ set()

template<typename T >
virtual void by::tmay< T >::set ( const T & arg)
virtual

Reimplemented in by::tres< T, R >.


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