MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::Optional< T > Struct Template Reference

Represents a value that may or may not exist (an "Optional" value) Similar concept to std::optional, but different implementation and additional nicety methods which makes this a better alternative for simple loop-based iteration (via copy_if_present and move_if_present) More...

#include <optional.hpp>

Inheritance diagram for mtcore::Optional< T >:

Public Member Functions

 Optional ()
 Creates an empty Optional.
 
 Optional (const T &value)
 Creates an Optional from a value.
 
 Optional (T &&value)
 Creates an Optional from a value.
 
bool operator== (const Optional &other) const noexcept
 Checks if value is equal to another Optional.
 
bool operator== (const T &other) const noexcept
 Checks if value is equal to a non-Optional value.
 
value_or (const T &def) const noexcept
 Returns the value in the optional, or a default value if the optional is empty.
 
bool has_value () const noexcept
 
bool empty () const noexcept
 
T & value () noexcept
 
const T & value () const noexcept
 
bool copy_if_present (std::remove_const_t< T > &out) const noexcept
 Copies a value to a reference output destination if a value is present Designed to be used by simple iterators as part of a while loop on a next() method Example:
 
bool move_if_present (T &out) noexcept
 Moves a value to a reference output destination if a value is present.
 
const T & operator* () const
 Dereferences the internal value.
 
const T * operator-> () const
 Dereferences the internal value.
 
T & operator* ()
 Dereferences the internal value.
 
T * operator-> ()
 Dereferences the internal value.
 

Detailed Description

template<typename T>
struct mtcore::Optional< T >

Represents a value that may or may not exist (an "Optional" value) Similar concept to std::optional, but different implementation and additional nicety methods which makes this a better alternative for simple loop-based iteration (via copy_if_present and move_if_present)

Definition at line 235 of file optional.hpp.

Constructor & Destructor Documentation

◆ Optional() [1/3]

template<typename T>
mtcore::Optional< T >::Optional ( )
inline

Creates an empty Optional.

Definition at line 243 of file optional.hpp.

243: v(std::monostate{}) { ensure(!has_value()); }
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
Represents a value that may or may not exist (an "Optional" value) Similar concept to std::optional,...
Definition optional.hpp:235
bool has_value() const noexcept
Definition optional.hpp:294
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Optional() [2/3]

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

Creates an Optional from a value.

Definition at line 248 of file optional.hpp.

248: v(value) {}
T & value() noexcept
Definition optional.hpp:301
Here is the call graph for this function:

◆ Optional() [3/3]

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

Creates an Optional from a value.

Definition at line 253 of file optional.hpp.

253: v(std::forward<T>(value)) {}
Here is the call graph for this function:

Member Function Documentation

◆ copy_if_present()

template<typename T>
bool mtcore::Optional< T >::copy_if_present ( std::remove_const_t< T > & out) const
inlinenodiscardnoexcept

Copies a value to a reference output destination if a value is present Designed to be used by simple iterators as part of a while loop on a next() method Example:

auto iter = myCollection.iter();
T myVal;
while (iter.next().copy_if_present(myVal)) {
// loop contents
}
Generic iterator defaults built on common contracts Does not guarantee performance of iterators Actua...
Definition iter.hpp:91
Parameters
outWhere to put the destination
Returns
true if a value was copied, false otherwise

Definition at line 327 of file optional.hpp.

327 {
328 if (has_value()) {
329 out = std::get<T>(v);
330 return true;
331 }
332 return false;
333 }
Here is the call graph for this function:

◆ empty()

template<typename T>
bool mtcore::Optional< T >::empty ( ) const
inlinenodiscardnoexcept
Returns
if is empty

Definition at line 299 of file optional.hpp.

◆ has_value()

template<typename T>
bool mtcore::Optional< T >::has_value ( ) const
inlinenodiscardnoexcept
Returns
if has a value

Definition at line 294 of file optional.hpp.

294{ return std::holds_alternative<T>(v); }
Here is the caller graph for this function:

◆ move_if_present()

template<typename T>
bool mtcore::Optional< T >::move_if_present ( T & out)
inlinenodiscardnoexcept

Moves a value to a reference output destination if a value is present.

Will mark the Optional as empty Designed to be used by simple iterators as part of a while loop on a next() method Example:

auto iter = myCollection.iter();
T myVal;
while (iter.next().move_if_present(myVal)) {
// loop contents
}
Parameters
outWhere to put the destination
Returns
true if a value was copied, false otherwise

Definition at line 352 of file optional.hpp.

352 {
353 if (has_value()) {
355 v = std::monostate{};
356 return true;
357 }
358 return false;
359 }
Here is the call graph for this function:

◆ operator*() [1/2]

template<typename T>
T & mtcore::Optional< T >::operator* ( )
inline

Dereferences the internal value.

Definition at line 380 of file optional.hpp.

380 {
381 ensure(has_value(), "CANNNOT DERERFERENCE NULL OPTIONAL");
382 return std::get<T>(v);
383 }
Here is the call graph for this function:

◆ operator*() [2/2]

template<typename T>
const T & mtcore::Optional< T >::operator* ( ) const
inline

Dereferences the internal value.

Definition at line 364 of file optional.hpp.

364 {
365 ensure(has_value(), "CANNNOT DERERFERENCE NULL OPTIONAL");
366 return std::get<T>(v);
367 }
Here is the call graph for this function:

◆ operator->() [1/2]

template<typename T>
T * mtcore::Optional< T >::operator-> ( )
inline

Dereferences the internal value.

Definition at line 388 of file optional.hpp.

388 {
389 ensure(has_value(), "CANNNOT DERERFERENCE NULL OPTIONAL");
390 return &std::get<T>(v);
391 }
Here is the call graph for this function:

◆ operator->() [2/2]

template<typename T>
const T * mtcore::Optional< T >::operator-> ( ) const
inline

Dereferences the internal value.

Definition at line 372 of file optional.hpp.

372 {
373 ensure(has_value(), "CANNNOT DERERFERENCE NULL OPTIONAL");
374 return &std::get<T>(v);
375 }
Here is the call graph for this function:

◆ operator==() [1/2]

template<typename T>
bool mtcore::Optional< T >::operator== ( const Optional< T > & other) const
inlinenoexcept

Checks if value is equal to another Optional.

Definition at line 258 of file optional.hpp.

258 {
259 if (has_value() != other.has_value()) {
260 return false;
261 }
262 if (!has_value()) {
263 return true;
264 }
265
266 ensure(has_value() && other.has_value(), "EXISTS CHECK FAILED");
267 return std::get<T>(v) == std::get<T>(other.v);
268 }
Here is the call graph for this function:

◆ operator==() [2/2]

template<typename T>
bool mtcore::Optional< T >::operator== ( const T & other) const
inlinenoexcept

Checks if value is equal to a non-Optional value.

Definition at line 273 of file optional.hpp.

273 {
274 if (!has_value()) {
275 return false;
276 }
277 return std::get<T>(v) == other;
278 }
Here is the call graph for this function:

◆ value() [1/2]

template<typename T>
const T & mtcore::Optional< T >::value ( ) const
inlinenodiscardnoexcept

Definition at line 306 of file optional.hpp.

306 {
307 ensure(has_value(), "CANNOT GET VALUE WHEN OPTIONAL IS EMPTY");
308 return std::get<T>(v);
309 }
Here is the call graph for this function:

◆ value() [2/2]

template<typename T>
T & mtcore::Optional< T >::value ( )
inlinenodiscardnoexcept

Definition at line 301 of file optional.hpp.

301 {
302 ensure(has_value(), "CANNOT GET VALUE WHEN OPTIONAL IS EMPTY");
303 return std::get<T>(v);
304 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ value_or()

template<typename T>
T mtcore::Optional< T >::value_or ( const T & def) const
inlinenodiscardnoexcept

Returns the value in the optional, or a default value if the optional is empty.

Parameters
defDefault value

Definition at line 284 of file optional.hpp.

284 {
285 if (has_value()) {
286 return value();
287 }
288 return def;
289 }
Here is the call graph for this function:

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