arg_router  1.4.0
C++ command line argument parsing and routing
compile_time_optional.hpp
1 // Copyright (C) 2022 by Camden Mannett.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
4 
5 #pragma once
6 
7 #include <utility>
8 
10 {
28 template <typename T>
30 {
31 public:
33  using value_type = T;
34 
36  constexpr static bool empty = false;
37 
42  explicit constexpr compile_time_optional(value_type val) noexcept : val_(std::move(val)) {}
43 
48  constexpr explicit operator bool() const { return !empty; }
49 
55  constexpr const value_type* operator->() const noexcept { return &val_; }
56 
62  constexpr const value_type& operator*() const noexcept { return val_; }
63 
69  value_type* operator->() noexcept { return &val_; }
70 
76  value_type& operator*() noexcept { return val_; }
77 
78 private:
79  value_type val_;
80 };
81 
82 template <typename T>
83 class compile_time_optional<std::reference_wrapper<T>>
84 {
85 public:
86  using value_type = T;
87 
88  constexpr static bool empty = false;
89 
90  constexpr explicit operator bool() const { return !empty; }
91 
92  explicit compile_time_optional(std::reference_wrapper<T> val) noexcept : ref_(val) {}
93 
94  const value_type* operator->() const noexcept { return &(ref_.get()); }
95 
96  const value_type& operator*() const noexcept { return ref_; }
97 
98  value_type* operator->() noexcept { return &(ref_.get()); }
99 
100  value_type& operator*() noexcept { return ref_; }
101 
102 private:
103  std::reference_wrapper<value_type> ref_;
104 };
105 
106 template <>
107 class compile_time_optional<void>
108 {
109 public:
110  constexpr static bool empty = true;
111 
112  explicit constexpr operator bool() const { return !empty; }
113 };
114 
115 // Deduction guide
116 template <typename T = void>
117 compile_time_optional() -> compile_time_optional<void>;
118 } // namespace arg_router::utility
constexpr const value_type & operator*() const noexcept
constexpr compile_time_optional(value_type val) noexcept
constexpr const value_type * operator->() const noexcept