RStudio Launcher Plugin SDK  1.1.3
A software development kit for creating plugins that work the the RStudio Launcher.
Optional.hpp
1 /*
2  * Optional.hpp
3  *
4  * Copyright (C) 2020 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant to the terms of a commercial license agreement
7  * with RStudio, then this program is licensed to you under the following terms:
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
10  * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
11  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #ifndef LAUNCHER_PLUGINS_OPTIONAL_HPP
25 #define LAUNCHER_PLUGINS_OPTIONAL_HPP
26 
27 #include <memory>
28 
29 namespace rstudio {
30 namespace launcher_plugins {
31 
37 template <typename T>
38 class Optional
39 {
40 public:
44  Optional() = default;
45 
51  explicit Optional(T* in_value) :
52  m_value(in_value)
53  {
54  }
55 
61  explicit Optional(const T& in_value) :
62  m_value(new T(in_value))
63  {
64  }
65 
71  Optional(const Optional& in_other)
72  {
73  *this = in_other;
74  }
75 
81  Optional(Optional&& in_other) noexcept
82  {
83  m_value.swap(in_other.m_value);
84  }
85 
91  explicit operator bool() const
92  {
93  return hasValue();
94  }
95 
101  bool operator!() const
102  {
103  return !hasValue();
104  }
105 
113  Optional& operator=(const Optional& in_other)
114  {
115  if (this == &in_other)
116  return *this;
117 
118  if (in_other.m_value == nullptr)
119  m_value.reset();
120  else
121  m_value.reset(new T(*in_other.m_value));
122 
123  return *this;
124  }
125 
133  Optional& operator=(Optional&& in_other) noexcept
134  {
135  if (this == &in_other)
136  return *this;
137 
138  m_value = std::move(in_other.m_value);
139  return *this;
140  }
141 
149  Optional& operator=(T* in_value)
150  {
151  m_value.reset(in_value);
152  return *this;
153  }
154 
162  Optional& operator=(const T& in_value)
163  {
164  m_value.reset(new T(in_value));
165  return *this;
166  }
167 
175  const T& getValueOr(const T& in_default) const
176  {
177  if (hasValue())
178  return *m_value;
179 
180  return in_default;
181  }
182 
190  T& getValueOr(T& in_default)
191  {
192  return const_cast<T&>(const_cast<const Optional*>(this)->getValueOr(in_default));
193  }
194 
200  bool hasValue() const
201  {
202  return m_value != nullptr;
203  }
204 
205 private:
207  std::unique_ptr<T> m_value;
208 };
209 
210 } // namespace launcher_plugins
211 } // namespace rstudio
212 
213 #endif
rstudio::launcher_plugins::Optional::operator=
Optional & operator=(const T &in_value)
Assginment operator.
Definition: Optional.hpp:162
rstudio::launcher_plugins::Optional::Optional
Optional(const T &in_value)
Constructor.
Definition: Optional.hpp:61
rstudio::launcher_plugins::Optional::Optional
Optional()=default
Default constructor.
rstudio::launcher_plugins::Optional::operator!
bool operator!() const
Not boolean operator.
Definition: Optional.hpp:101
rstudio::launcher_plugins::Optional::operator=
Optional & operator=(T *in_value)
Assignment operator.
Definition: Optional.hpp:149
rstudio::launcher_plugins::Optional::getValueOr
const T & getValueOr(const T &in_default) const
Gets the value of this optional, or the provided default value if this optional has no value.
Definition: Optional.hpp:175
rstudio::launcher_plugins::Optional::Optional
Optional(T *in_value)
Constructor.
Definition: Optional.hpp:51
rstudio::launcher_plugins::Optional::getValueOr
T & getValueOr(T &in_default)
Gets the value of this optional, or the provided default value if this optional has no value.
Definition: Optional.hpp:190
rstudio::launcher_plugins::Optional::hasValue
bool hasValue() const
Checks whether this optional has a value.
Definition: Optional.hpp:200
rstudio::launcher_plugins::Optional::operator=
Optional & operator=(const Optional &in_other)
Assignment operator.
Definition: Optional.hpp:113
rstudio::launcher_plugins::Optional
Container class which represents a value that may or may not be set.
Definition: Optional.hpp:38
rstudio::launcher_plugins::Optional::Optional
Optional(Optional &&in_other) noexcept
Move constructor.
Definition: Optional.hpp:81
rstudio::launcher_plugins::Optional::Optional
Optional(const Optional &in_other)
Copy constructor.
Definition: Optional.hpp:71
rstudio::launcher_plugins::Optional::operator=
Optional & operator=(Optional &&in_other) noexcept
Assignment operator.
Definition: Optional.hpp:133