open-cpp-utils 0.0.1
Loading...
Searching...
No Matches
optional.h
1// =====================================================================================================================
2// open-cpp-utils, an open-source cpp library with data structures that extend the STL.
3// Copyright (C) 2024 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
19#ifndef OPEN_CPP_UTILS_OPTIONAL_H
20#define OPEN_CPP_UTILS_OPTIONAL_H
21
22#include <assert.h>
23
24namespace open_cpp_utils
25{
26 template<typename T>
28 {
29 public:
30 using value_type = T;
31
32 optional() : valid_(false) { }
33 optional(const value_type& data) : data_(data), valid_(true) { }
34 optional(value_type&& data) : data_(data), valid_(true) { }
35 optional(const optional& other) = default;
36 optional(optional&& other) = default;
37
38 optional& operator=(const optional& other) = default;
39 optional& operator=(optional&& other) = default;
40
41 optional& operator=(const value_type& data) { data_ = data; valid_ = true; return *this; }
42 optional& operator=(value_type&& data) { data_ = data; valid_ = true; return *this; }
43
44 bool operator==(const optional& b) const
45 {
46 if(not(valid_ || b.valid_)) return valid_ == b.valid_;
47 return data_ == b.data_;
48 }
49
50 value_type& operator+=(const value_type& data) { assert(valid_); data_ += data; return data_; }
51 value_type& operator-=(const value_type& data) { assert(valid_); data_ += data; return data_; }
52 value_type& operator*=(const value_type& data) { assert(valid_); data_ += data; return data_; }
53 value_type& operator/=(const value_type& data) { assert(valid_); data_ += data; return data_; }
54 value_type& operator%=(const value_type& data) { assert(valid_); data_ += data; return data_; }
55
56 value_type& operator<<=(const value_type& data) { assert(valid_); data_ <<= data; return data_; }
57 value_type& operator>>=(const value_type& data) { assert(valid_); data_ >>= data; return data_; }
58 value_type& operator|=(const value_type& data) { assert(valid_); data_ |= data; return data_; }
59 value_type& operator&=(const value_type& data) { assert(valid_); data_ &= data; return data_; }
60 value_type& operator^=(const value_type& data) { assert(valid_); data_ ^= data; return data_; }
61
62 [[nodiscard]] bool operator()() const { return valid_; }
63
64 operator value_type() const { assert(valid_); return data_; }
65
66 operator value_type&() { assert(valid_); return data_; }
67 operator const value_type&() const { assert(valid_); return data_; }
68
69 value_type* operator->() { assert(valid_); return &data_; }
70 const value_type* operator->() const { assert(valid_); return &data_; }
71
72 value_type& operator*() { assert(valid_); return data_; }
73 const value_type& operator*() const { assert(valid_); return data_; }
74
75 void reset() { valid_ = false; }
76
77 private:
78 value_type data_;
79 bool valid_;
80 };
81}
82
83#endif // OPEN_CPP_UTILS_OPTIONAL_H
Definition optional.h:28