OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NullableValue.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
23 #ifndef NULLABLEVALUE_H
24 #define NULLABLEVALUE_H
25 
26 template <typename T>
28  public:
31 
32  bool isValid() const { return state_ == Valid; }
33  bool isInvalid() const { return state_ == Invalid; }
34  T getValue() const { return value_; }
35 
37  if (isInvalid()) {
38  return NullableValue<T>();
39  }
40  return NullableValue<T>(value_ + v);
41  }
43  if (isInvalid()) {
44  return NullableValue<T>();
45  }
46  return NullableValue<T>(value_ - v);
47  }
49  if (isInvalid()) {
50  return NullableValue<T>();
51  }
52  return NullableValue<T>(value_ * v);
53  }
55  if (isInvalid()) {
56  return NullableValue<T>();
57  }
58  return NullableValue<T>(value_ / v);
59  }
61  if (isInvalid() && other.isInvalid()) {
62  return NullableValue<T>();
63  }
64  return NullableValue<T>(value_ + other.getValue());
65  }
67  if (isInvalid() && other.isInvalid()) {
68  return NullableValue<T>();
69  }
70  return NullableValue<T>(value_ - other.getValue());
71  }
73  if (isInvalid() && other.isInvalid()) {
74  return NullableValue<T>();
75  }
76  return NullableValue<T>(value_ * other.getValue());
77  }
79  if (isInvalid() && other.isInvalid()) {
80  return NullableValue<T>();
81  }
82  return NullableValue<T>(value_ / other.getValue());
83  }
84  bool operator==(const T v) const { return isValid() ? (value_ == v) : false; }
85  bool operator!=(const T v) const { return isValid() ? (value_ != v) : false; }
86  bool operator>(const T v) const { return isValid() ? (value_ > v) : false; }
87  bool operator>=(const T v) const { return isValid() ? (value_ >= v) : false; }
88  bool operator<(const T v) const { return isValid() ? (value_ < v) : false; }
89  bool operator<=(const T v) const { return isValid() ? (value_ <= v) : false; }
90 
91  static T getDefaultValue();
92 
93  private:
94  enum State { Invalid, Valid };
97 };
98 
100 template <>
102  return 0.5;
103 };
104 
106 template <>
107 uint64_t Weight::getDefaultValue() {
108  return 1;
109 }
110 
111 #endif // NULLABLEVALUE_H
bool operator!=(const T v) const
Definition: NullableValue.h:85
static T getDefaultValue()
NullableValue< T > operator-(const NullableValue< T > &other) const
Definition: NullableValue.h:66
NullableValue< T > operator+(const NullableValue< T > &other) const
Definition: NullableValue.h:60
NullableValue< T > operator-(T v) const
Definition: NullableValue.h:42
bool isValid() const
Definition: NullableValue.h:32
bool operator>=(const T v) const
Definition: NullableValue.h:87
bool operator<(const T v) const
Definition: NullableValue.h:88
bool isInvalid() const
Definition: NullableValue.h:33
bool operator>(const T v) const
Definition: NullableValue.h:86
bool operator<=(const T v) const
Definition: NullableValue.h:89
T getValue() const
Definition: NullableValue.h:34
NullableValue< T > operator+(T v) const
Definition: NullableValue.h:36
NullableValue< T > operator*(const NullableValue< T > &other) const
Definition: NullableValue.h:72
NullableValue< T > operator/(T v) const
Definition: NullableValue.h:54
bool operator==(const T v) const
Definition: NullableValue.h:84
NullableValue< T > operator/(const NullableValue< T > &other) const
Definition: NullableValue.h:78
NullableValue< T > operator*(T v) const
Definition: NullableValue.h:48