open-cpp-utils 0.0.1
Loading...
Searching...
No Matches
optional.h
1// =====================================================================================================================
2// Copyright 2024 Medusa Slockbower
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// =====================================================================================================================
15
16#ifndef OPTIONAL_H
17#define OPTIONAL_H
18
19namespace open_cpp_utils
20{
21 template<typename T>
23 {
24 public:
25 using value_type = T;
26
27 optional() : data_(), valid_(false) { }
28 optional(const value_type& data) : data_(data), valid_(true) { }
29 optional(value_type&& data) : data_(data), valid_(true) { }
30 optional(const optional& other) = default;
31 optional(optional&& other) = default;
32
33 optional& operator=(const optional& other) = default;
34 optional& operator=(optional&& other) = default;
35
36 optional& operator=(const value_type& data) { data_ = data; valid_ = true; return *this; }
37 optional& operator=(value_type&& data) { data_ = data; valid_ = true; return *this; }
38
39 value_type& operator+=(const value_type& data) { assert(valid_); data_ += data; return data_; }
40 value_type& operator-=(const value_type& data) { assert(valid_); data_ += data; return data_; }
41 value_type& operator*=(const value_type& data) { assert(valid_); data_ += data; return data_; }
42 value_type& operator/=(const value_type& data) { assert(valid_); data_ += data; return data_; }
43 value_type& operator%=(const value_type& data) { assert(valid_); data_ += data; return data_; }
44
45 value_type& operator<<=(const value_type& data) { assert(valid_); data_ <<= data; return data_; }
46 value_type& operator>>=(const value_type& data) { assert(valid_); data_ >>= data; return data_; }
47 value_type& operator|=(const value_type& data) { assert(valid_); data_ |= data; return data_; }
48 value_type& operator&=(const value_type& data) { assert(valid_); data_ &= data; return data_; }
49 value_type& operator^=(const value_type& data) { assert(valid_); data_ ^= data; return data_; }
50
51 [[nodiscard]] bool operator()() const { return valid_; }
52
53 operator value_type&() { assert(valid_); return data_; }
54 operator const value_type&() const { assert(valid_); return data_; }
55
56 value_type* operator->() { assert(valid_); return &data_; }
57 const value_type* operator->() const { assert(valid_); return &data_; }
58
59 value_type& operator*() { assert(valid_); return data_; }
60 const value_type& operator*() const { assert(valid_); return data_; }
61
62 void reset() { valid_ = false; }
63
64 private:
65 value_type data_;
66 bool valid_;
67 };
68}
69
70#endif //OPTIONAL_H
Definition optional.h:23