Skip to content

Commit

Permalink
Fixes #691: Added a simple (and simplistic) optional_ref<T> class t…
Browse files Browse the repository at this point in the history
…emplate
  • Loading branch information
eyalroz committed Oct 26, 2024
1 parent 8131ccd commit 2703807
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/cuda/api/detail/optional_ref.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @file
*
* @brief An implementation of a simplistic optional-reference class
* (as optional<T&> is problematic semantically, and also probably not
* supported by this library's simplistic @ref optional implementation)
*
*/
#ifndef CUDA_API_WRAPPERS_OPTIONAL_REF_HPP_
#define CUDA_API_WRAPPERS_OPTIONAL_REF_HPP_

#include "optional.hpp"

namespace cuda {

namespace detail_ {

template<typename T>
struct optional_ref {
optional_ref &operator=(const optional_ref &other) = default;

optional_ref &operator=(optional_ref &&other) = default;

optional_ref &operator=(const T &value) = delete;

optional_ref &operator=(const T &&value) = delete;

optional_ref() noexcept: ptr_(nullptr)
{ }

optional_ref(T& v) noexcept : ptr_(&v)
{ }

optional_ref(const optional_ref &other) = default;

optional_ref(nullopt_t) noexcept : ptr_(nullptr) { }

~optional_ref() noexcept = default;

T& value() const
{ return *ptr_; }

T& value_or(T& fallback_ref) const
{
return has_value() ? value() : fallback_ref;
}

T& operator*() noexcept { return *ptr_; }
const T& operator*() const noexcept { return *ptr_; }
T* operator->() noexcept { return ptr_; }
const T* operator->() const noexcept { return ptr_; }

bool has_value() const noexcept
{ return ptr_ != nullptr; }

operator bool() const noexcept
{ return has_value(); }

void reset() noexcept
{ ptr_ = nullptr; }

protected:
T* ptr_;
};

} // namespace detail_

template<typename T>
using optional_ref = cuda::detail_::optional_ref<T>;

} // namespace cuda

//#endif // __cplusplus >= 201402L

#endif //CUDA_API_WRAPPERS_OPTIONAL_REF_HPP_

0 comments on commit 2703807

Please sign in to comment.