optionalData.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::optionalData
28 
29 Description
30  A simplified version of std::optional (c++17), with much simpler
31  construction semantics.
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef optionalData_H
36 #define optionalData_H
37 
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 
40 namespace Foam
41 {
42 
43 /*---------------------------------------------------------------------------*\
44  Class optionalData Declaration
45 \*---------------------------------------------------------------------------*/
46 
47 template<class T>
48 class optionalData
49 {
50  // Private Data
51 
52  //- Validity of the value
53  bool good_;
54 
55  //- The value
56  T value_;
57 
58 
59 public:
60 
61  // Generated Methods
62 
63  //- Copy construct
64  optionalData(const optionalData<T>&) = default;
65 
66  //- Move construct
67  optionalData(optionalData<T>&&) = default;
68 
69  //- Copy assignment
70  optionalData<T>& operator=(const optionalData<T>&) = default;
71 
72  //- Move assignment
74 
75 
76  // Constructors
77 
78  //- Default construct
79  optionalData()
80  :
81  good_(false),
82  value_()
83  {}
84 
85  //- Copy construct from value
86  optionalData(const T& val)
87  :
88  good_(true),
89  value_(val)
90  {}
91 
92  //- Move construct from value
93  optionalData(T&& val)
94  :
95  good_(true),
96  value_(std::move(val))
97  {}
98 
99 
100  // Member Functions
101 
102  //- True if it has a value
103  bool has_value() const noexcept
104  {
105  return good_;
106  }
107 
108  //- Access to the value
109  T& value() noexcept
110  {
111  return value_;
112  }
113 
114  //- Access to the value
115  const T& value() const noexcept
116  {
117  return value_;
118  }
119 
120  //- Return value or default
121  const T& value_or(const T& deflt) const
122  {
123  return good_ ? value_ : deflt;
124  }
125 
126 
127  // Member Operators
128 
129  //- True if it has a value
130  explicit operator bool() const noexcept
131  {
132  return good_;
133  }
134 
135  //- Access the value
136  const T& operator*() const noexcept
137  {
138  return value_;
139  }
140 
141  //- Access the value
142  T& operator*() noexcept
143  {
144  return value_;
145  }
146 
147  //- Copy assignment from value
148  void operator=(const T& val)
149  {
150  good_ = true;
151  value_ = val;
152  }
153 
154  //- Move assignment from value
155  void operator=(T&& val)
156  {
157  good_ = true;
158  value_ = std::move(val);
159  }
160 };
161 
162 
163 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
164 
165 } // End namespace Foam
166 
167 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
168 
169 #endif
170 
171 // ************************************************************************* //
Foam::optionalData::operator=
void operator=(const T &val)
Copy assignment from value.
Definition: optionalData.H:147
Foam::optionalData
A simplified version of std::optional (c++17), with much simpler construction semantics.
Definition: optionalData.H:47
Foam::optionalData::optionalData
optionalData(const T &val)
Copy construct from value.
Definition: optionalData.H:85
Foam::optionalData::operator=
optionalData< T > & operator=(const optionalData< T > &)=default
Copy assignment.
Foam::optionalData::operator*
const T & operator*() const noexcept
Access the value.
Definition: optionalData.H:135
Foam::optionalData::optionalData
optionalData(T &&val)
Move construct from value.
Definition: optionalData.H:92
Foam::optionalData::value
const T & value() const noexcept
Access to the value.
Definition: optionalData.H:114
Foam::optionalData::operator=
void operator=(T &&val)
Move assignment from value.
Definition: optionalData.H:154
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::optionalData::optionalData
optionalData()
Default construct.
Definition: optionalData.H:78
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
bool
bool
Definition: EEqn.H:20
Foam::optionalData::operator*
T & operator*() noexcept
Access the value.
Definition: optionalData.H:141
Foam::optionalData::value_or
const T & value_or(const T &deflt) const
Return value or default.
Definition: optionalData.H:120
Foam::optionalData::value
T & value() noexcept
Access to the value.
Definition: optionalData.H:108
Foam::optionalData::has_value
bool has_value() const noexcept
True if it has a value.
Definition: optionalData.H:102