OpenShaderDesigner 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
19template<typename T>
21{
22public:
23 using Type = T;
24
25 Optional() : Data(), Valid(false) { }
26 Optional(const Type& data) : Data(data), Valid(true) { }
27 Optional(Type&& data) : Data(data), Valid(true) { }
28 Optional(const Optional& other) = default;
29 Optional(Optional&& other) = default;
30
31 Optional& operator=(const Optional& other) = default;
32 Optional& operator=(Optional&& other) = default;
33
34 Type& operator=(const Type& data) { Data = data; Valid = true; return Data; }
35 Type& operator=(Type&& data) { Data = data; Valid = true; return Data; }
36
37 Type& operator+=(const Type& data) { assert(Valid); Data += data; return Data; }
38 Type& operator-=(const Type& data) { assert(Valid); Data += data; return Data; }
39 Type& operator*=(const Type& data) { assert(Valid); Data += data; return Data; }
40 Type& operator/=(const Type& data) { assert(Valid); Data += data; return Data; }
41 Type& operator%=(const Type& data) { assert(Valid); Data += data; return Data; }
42
43 Type& operator<<=(const Type& data) { assert(Valid); Data <<= data; return Data; }
44 Type& operator>>=(const Type& data) { assert(Valid); Data >>= data; return Data; }
45 Type& operator|=(const Type& data) { assert(Valid); Data |= data; return Data; }
46 Type& operator&=(const Type& data) { assert(Valid); Data &= data; return Data; }
47 Type& operator^=(const Type& data) { assert(Valid); Data ^= data; return Data; }
48
49 [[nodiscard]] bool operator()() const { return Valid; }
50
51 operator Type&() { assert(Valid); return Data; }
52 operator const Type&() const { assert(Valid); return Data; }
53
54 Type* operator->() { assert(Valid); return &Data; }
55 const Type* operator->() const { assert(Valid); return &Data; }
56
57 Type& operator*() { assert(Valid); return Data; }
58 const Type& operator*() const { assert(Valid); return Data; }
59
60 void Reset() { Valid = false; }
61
62private:
63 Type Data;
64 bool Valid;
65};
66
67#endif //OPTIONAL_H
Definition Optional.h:21