CBuild
C++ build system with scripts written in c++
Loading...
Searching...
No Matches
optional.hpp
Go to the documentation of this file.
1
21#ifndef __OPTIONAL_HPP__
22#define __OPTIONAL_HPP__
23// C++ libraries
24#include "stdexcept"
25// Code
26namespace lib {
32template <class T> class optional {
33 private:
37 T var;
41 bool null;
42
43 public:
48 this->null = true;
49 }
55 optional(T val) {
56 this->var = val;
57 this->null = false;
58 }
64 void set(T val) {
65 this->var = val;
66 this->null = false;
67 }
71 void clear() {
72 this->null = true;
73 }
79 T get() {
80 if (this->null == false) {
81 return this->var;
82 } else {
83 throw new std::runtime_error("Datatype is null");
84 }
85 }
91 bool is() {
92 return !this->null;
93 }
94};
95} // namespace lib
96#endif // __OPTIONAL_HPP__
Optional data type.
Definition optional.hpp:32
bool null
Does values is saved.
Definition optional.hpp:41
T var
Intrnal variable.
Definition optional.hpp:37
optional()
Create new optional datatype.
Definition optional.hpp:47
void clear()
Clear value.
Definition optional.hpp:71
T get()
Get value.
Definition optional.hpp:79
void set(T val)
Set value.
Definition optional.hpp:64
optional(T val)
Create new optional datatype.
Definition optional.hpp:55
bool is()
If value is not null.
Definition optional.hpp:91
Optional datatype.
Definition map.hpp:29