Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
kcenon::common::Optional< T > Class Template Referenceexport

Optional type similar to std::optional with Rust-like API. More...

#include <core.h>

Collaboration diagram for kcenon::common::Optional< T >:
Collaboration graph

Public Types

using value_type = T
 Type alias for the contained value type (for concept compatibility)
 

Public Member Functions

 Optional ()
 
 Optional (const T &value)
 
 Optional (T &&value)
 
 Optional (std::nullopt_t)
 
bool has_value () const
 
bool is_some () const
 
bool is_none () const
 
const T & value () const
 
T & value ()
 
const T & unwrap () const
 Get value from optional (throws if None)
 
unwrap_or (T default_value) const
 
template<typename F >
auto map (F &&func) const -> Optional< decltype(func(std::declval< T >()))>
 
 Optional ()
 
 Optional (const T &value)
 
 Optional (T &&value)
 
 Optional (std::nullopt_t)
 
bool has_value () const
 
bool is_some () const
 
bool is_none () const
 
const T & value () const
 
T & value ()
 
const T & unwrap () const
 
unwrap_or (T default_value) const
 
template<typename F >
auto map (F &&func) const -> Optional< decltype(func(std::declval< T >()))>
 

Private Attributes

std::optional< T > value_
 

Detailed Description

template<typename T>
class kcenon::common::Optional< T >

Optional type similar to std::optional with Rust-like API.

Template Parameters
TThe type of the contained value
Examples
unwrap_demo.cpp.

Definition at line 341 of file core.cppm.

Member Typedef Documentation

◆ value_type

template<typename T >
typedef T kcenon::common::Optional< T >::value_type = T
export

Type alias for the contained value type (for concept compatibility)

Definition at line 471 of file core.h.

Constructor & Destructor Documentation

◆ Optional() [1/8]

template<typename T >
kcenon::common::Optional< T >::Optional ( )
inline

Definition at line 477 of file core.h.

477: value_(std::nullopt) {}
std::optional< T > value_
Definition core.h:474

Referenced by kcenon::common::Optional< T >::map().

Here is the caller graph for this function:

◆ Optional() [2/8]

template<typename T >
kcenon::common::Optional< T >::Optional ( const T & value)
inline

Definition at line 478 of file core.h.

478: value_(value) {}
const T & value() const
Definition core.h:486

◆ Optional() [3/8]

template<typename T >
kcenon::common::Optional< T >::Optional ( T && value)
inline

Definition at line 479 of file core.h.

479: value_(std::move(value)) {}

◆ Optional() [4/8]

template<typename T >
kcenon::common::Optional< T >::Optional ( std::nullopt_t )
inline

Definition at line 480 of file core.h.

480: value_(std::nullopt) {}

◆ Optional() [5/8]

template<typename T >
kcenon::common::Optional< T >::Optional ( )
inlineexport

Definition at line 349 of file core.cppm.

349: value_(std::nullopt) {}

◆ Optional() [6/8]

template<typename T >
kcenon::common::Optional< T >::Optional ( const T & value)
inlineexport

Definition at line 350 of file core.cppm.

350: value_(value) {}

◆ Optional() [7/8]

template<typename T >
kcenon::common::Optional< T >::Optional ( T && value)
inlineexport

Definition at line 351 of file core.cppm.

351: value_(std::move(value)) {}

◆ Optional() [8/8]

template<typename T >
kcenon::common::Optional< T >::Optional ( std::nullopt_t )
inlineexport

Definition at line 352 of file core.cppm.

352: value_(std::nullopt) {}

Member Function Documentation

◆ has_value() [1/2]

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

Definition at line 482 of file core.h.

482{ return value_.has_value(); }

References kcenon::common::Optional< T >::value_.

Referenced by kcenon::common::Optional< T >::map(), and kcenon::common::Optional< T >::unwrap().

Here is the caller graph for this function:

◆ has_value() [2/2]

template<typename T >
bool kcenon::common::Optional< T >::has_value ( ) const
inlineexport

Definition at line 354 of file core.cppm.

354{ return value_.has_value(); }

References kcenon::common::Optional< T >::value_.

◆ is_none() [1/2]

template<typename T >
bool kcenon::common::Optional< T >::is_none ( ) const
inline

Definition at line 484 of file core.h.

484{ return !value_.has_value(); }

References kcenon::common::Optional< T >::value_.

◆ is_none() [2/2]

template<typename T >
bool kcenon::common::Optional< T >::is_none ( ) const
inlineexport

Definition at line 356 of file core.cppm.

356{ return !value_.has_value(); }

References kcenon::common::Optional< T >::value_.

◆ is_some() [1/2]

template<typename T >
bool kcenon::common::Optional< T >::is_some ( ) const
inline

Definition at line 483 of file core.h.

483{ return value_.has_value(); }

References kcenon::common::Optional< T >::value_.

◆ is_some() [2/2]

template<typename T >
bool kcenon::common::Optional< T >::is_some ( ) const
inlineexport

Definition at line 355 of file core.cppm.

355{ return value_.has_value(); }

References kcenon::common::Optional< T >::value_.

◆ map() [1/2]

template<typename T >
template<typename F >
auto kcenon::common::Optional< T >::map ( F && func) const -> Optional<decltype(func(std::declval<T>()))>
inline

Definition at line 521 of file core.h.

521 {
522 using ReturnType = decltype(func(std::declval<T>()));
523
524 if (has_value()) {
525 return Optional<ReturnType>(func(value_.value()));
526 } else {
527 return Optional<ReturnType>(std::nullopt);
528 }
529 }
bool has_value() const
Definition core.h:482

References kcenon::common::Optional< T >::has_value(), kcenon::common::Optional< T >::Optional(), and kcenon::common::Optional< T >::value_.

Here is the call graph for this function:

◆ map() [2/2]

template<typename T >
template<typename F >
auto kcenon::common::Optional< T >::map ( F && func) const -> Optional<decltype(func(std::declval<T>()))>
inlineexport

Definition at line 386 of file core.cppm.

386 {
387 using ReturnType = decltype(func(std::declval<T>()));
388
389 if (has_value()) {
390 return Optional<ReturnType>(func(value_.value()));
391 } else {
392 return Optional<ReturnType>(std::nullopt);
393 }
394 }

References kcenon::common::Optional< T >::has_value(), kcenon::common::Optional< T >::Optional(), and kcenon::common::Optional< T >::value_.

Here is the call graph for this function:

◆ unwrap() [1/2]

template<typename T >
const T & kcenon::common::Optional< T >::unwrap ( ) const
inline

Get value from optional (throws if None)

Exceptions
std::runtime_errorif optional is None with detailed location info
Note
When source_location is available, error messages include file/line info

Definition at line 508 of file core.h.

508 {
509 if (!has_value()) {
510 throw std::runtime_error("Called unwrap on None");
511 }
512 return value_.value();
513 }

References kcenon::common::Optional< T >::has_value(), and kcenon::common::Optional< T >::value_.

Here is the call graph for this function:

◆ unwrap() [2/2]

template<typename T >
const T & kcenon::common::Optional< T >::unwrap ( ) const
inlineexport

Definition at line 373 of file core.cppm.

373 {
374 if (!has_value()) {
375 throw std::runtime_error("Called unwrap on None");
376 }
377 return value_.value();
378 }

References kcenon::common::Optional< T >::has_value(), and kcenon::common::Optional< T >::value_.

Here is the call graph for this function:

◆ unwrap_or() [1/2]

template<typename T >
T kcenon::common::Optional< T >::unwrap_or ( T default_value) const
inline

Definition at line 516 of file core.h.

516 {
517 return value_.value_or(default_value);
518 }

References kcenon::common::Optional< T >::value_.

◆ unwrap_or() [2/2]

template<typename T >
T kcenon::common::Optional< T >::unwrap_or ( T default_value) const
inlineexport

Definition at line 381 of file core.cppm.

381 {
382 return value_.value_or(default_value);
383 }

References kcenon::common::Optional< T >::value_.

◆ value() [1/4]

template<typename T >
T & kcenon::common::Optional< T >::value ( )
inline

Definition at line 487 of file core.h.

487{ return value_.value(); }

References kcenon::common::Optional< T >::value_.

◆ value() [2/4]

template<typename T >
T & kcenon::common::Optional< T >::value ( )
inlineexport

Definition at line 359 of file core.cppm.

359{ return value_.value(); }

References kcenon::common::Optional< T >::value_.

◆ value() [3/4]

template<typename T >
const T & kcenon::common::Optional< T >::value ( ) const
inline

Definition at line 486 of file core.h.

486{ return value_.value(); }

References kcenon::common::Optional< T >::value_.

◆ value() [4/4]

template<typename T >
const T & kcenon::common::Optional< T >::value ( ) const
inlineexport

Definition at line 358 of file core.cppm.

358{ return value_.value(); }

References kcenon::common::Optional< T >::value_.

Member Data Documentation

◆ value_


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