Reaktoro 
A unified framework for modeling chemically reactive systems
Optional.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright (C) 2014-2015 Allan Leal
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 #pragma once
19 
20 // Reaktoro includes
21 #include <Reaktoro/Common/Exception.hpp>
22 
23 // Boost includes
24 #include <boost/optional.hpp>
25 
26 namespace Reaktoro {
27 
29 template<typename T>
30 class Optional
31 {
32 public:
34  Optional() {}
35 
37  Optional(const T& value) : data(value) {}
38 
40  auto operator()() const -> const T& { return get(); }
41 
43  auto set(const T& value) -> void { data.reset(value); }
44 
46  auto get() const -> const T&
47  {
48  if(!empty()) return data.get();
49  else RuntimeError("Cannot get the value of the Optional instance.",
50  "Its value has not been initialized.");
51  }
52 
54  auto get() -> T&
55  {
56  if(!empty()) return data.get();
57  else RuntimeError("Cannot get the value of the Optional instance.",
58  "Its value has not been initialized.");
59  }
60 
62  auto empty() const -> bool { return !data; }
63 
64 private:
65  boost::optional<T> data;
66 };
67 
68 } // namespace Reaktoro
auto empty() const -> bool
Check if the Optional instance is initialized.
Definition: Optional.hpp:62
Optional()
Construct a default, uninitialized, Optional instance.
Definition: Optional.hpp:34
Optional(const T &value)
Construct a default, initialized, Optional instance.
Definition: Optional.hpp:37
auto operator()() const -> const T &
Retrieve the value of the Optional instance.
Definition: Optional.hpp:40
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
A type for defining an optional instance.
Definition: Optional.hpp:30