aboutsummaryrefslogtreecommitdiff
path: root/gdb/common/gdb_optional.h
blob: fef7a73e02a6ecfb1f37d0e71a43944cbb29c7b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* An optional object.

   Copyright (C) 2017 Free Software Foundation, Inc.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#ifndef GDB_OPTIONAL_H
#define GDB_OPTIONAL_H

namespace gdb
{

/* This class attempts to be a compatible subset of std::optional,
   which is slated to be available in C++17.  This class optionally
   holds an object of some type -- by default it is constructed not
   holding an object, but later the object can be "emplaced".  This is
   similar to using std::unique_ptr, but in-object allocation is
   guaranteed.  */
template<typename T>
class optional
{
public:

  optional ()
    : m_instantiated (false)
  {
  }

  ~optional ()
  {
    if (m_instantiated)
      destroy ();
  }

  /* These aren't deleted in std::optional, but it was simpler to
     delete them here, because currently the users of this class don't
     need them, and making them depend on the definition of T is
     somewhat complicated.  */
  optional (const optional &other) = delete;
  optional<T> &operator= (const optional &other) = delete;

  template<typename... Args>
  void emplace (Args &&... args)
  {
    if (m_instantiated)
      destroy ();
    new (&m_item) T (std::forward<Args>(args)...);
    m_instantiated = true;
  }

  /* Observers.  */
  constexpr const T *operator-> () const
  { return std::addressof (this->get ()); }

  T *operator-> ()
  { return std::addressof (this->get ()); }

  constexpr const T &operator* () const &
  { return this->get (); }

  T &operator* () &
  { return this->get (); }

  T &&operator* () &&
  { return std::move (this->get ()); }

  constexpr const T &&operator* () const &&
  { return std::move (this->get ()); }

  constexpr explicit operator bool () const noexcept
  { return m_instantiated; }

  constexpr bool has_value () const noexcept
  { return m_instantiated; }

private:

  /* Destroy the object.  */
  void destroy ()
  {
    gdb_assert (m_instantiated);
    m_instantiated = false;
    m_item.~T ();
  }

  /* The get operations have m_instantiated as a precondition.  */
  T &get () noexcept { return m_item; }
  constexpr const T &get () const noexcept { return m_item; }

  /* The object.  */
  union
  {
    struct { } m_dummy;
    T m_item;
  };

  /* True if the object was ever emplaced.  */
  bool m_instantiated;
};

}

#endif /* GDB_OPTIONAL_H */