libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNOption.h
Go to the documentation of this file.
1 /* Copyright 2011-2015 CoReNum, INSA-Lyon, Universite Paris Descartes
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser 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  * libcrn 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 Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNOption.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNOption_HEADER
23 #define CRNOption_HEADER
24 
25 namespace crn
26 {
33  template<typename T> class Option
34  {
35  public:
37  constexpr Option() noexcept : var(nullptr) {}
39  inline Option(const T &value):var(new T(value)) {}
41  inline Option(const Option &o)
42  {
43  if (o.var) var = new T(*(o.var));
44  else var = nullptr;
45  }
46  inline Option(Option &&o) noexcept : var(o.var) { o.var = nullptr; }
48  inline Option& operator=(const Option &o)
49  {
50  if (&o == this) return *this;
51  if (o.var)
52  {
53  if (var) *var = *(o.var);
54  else var = new T(*(o.var));
55  }
56  else if (var) { delete var; var = nullptr; }
57  return *this;
58  }
59  inline Option& operator=(Option &&o) noexcept { if (&o == this) return *this; delete var; var = o.var; o.var = nullptr; return *this; }
61  inline ~Option() { delete var; }
63  inline operator bool() const noexcept { return var != nullptr; }
65  inline T& Get() noexcept { return *var; }
67  inline const T& Get() const noexcept { return *var; }
69  inline T& operator*() noexcept { return *var; }
71  inline const T& operator*() const noexcept { return *var; }
73  inline T* operator->() noexcept { return var; }
75  inline const T* operator->() const noexcept { return var; }
76  private:
77  T *var;
78  };
79 }
80 
81 #endif
82 
83 
T & operator*() noexcept
Value access (undefined behaviour if the value is not set)
Definition: CRNOption.h:69
Option & operator=(Option &&o) noexcept
Definition: CRNOption.h:59
constexpr Option() noexcept
Default constructor.
Definition: CRNOption.h:37
const T * operator->() const noexcept
Method access (undefined behaviour if the value is not set)
Definition: CRNOption.h:75
Option(const T &value)
Constructor from value.
Definition: CRNOption.h:39
T & Get() noexcept
Value access (undefined behaviour if the value is not set)
Definition: CRNOption.h:65
Option & operator=(const Option &o)
Copy operator.
Definition: CRNOption.h:48
Option(Option &&o) noexcept
Definition: CRNOption.h:46
Option(const Option &o)
Copy constructor.
Definition: CRNOption.h:41
const T & Get() const noexcept
Value access (undefined behaviour if the value is not set)
Definition: CRNOption.h:67
T * operator->() noexcept
Method access (undefined behaviour if the value is not set)
Definition: CRNOption.h:73
A class to store an optional value.
Definition: CRNOption.h:33
~Option()
Destructor.
Definition: CRNOption.h:61
const T & operator*() const noexcept
Value access (undefined behaviour if the value is not set)
Definition: CRNOption.h:71